Skip to content

Commit

Permalink
remove filePathToUrl as well, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed Nov 30, 2021
1 parent bf16500 commit 4a3bf20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
3 changes: 1 addition & 2 deletions packages/graphql-language-service-server/src/GraphQLCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { parseDocument } from './parseDocument';
import stringToHash from './stringToHash';
import glob from 'glob';
import { LoadConfigOptions } from './types';
import { pathToFileURL } from 'url';
import { URI } from 'vscode-uri';

// Maximum files to read when processing GraphQL files.
Expand Down Expand Up @@ -366,7 +365,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
// the docs indicate that is what's there :shrug:
const cacheEntry = globResult.statCache[filePath] as fs.Stats;
return {
filePath: pathToFileURL(filePath).toString(),
filePath: URI.parse(filePath).toString(),
mtime: Math.trunc(cacheEntry.mtime.getTime() / 1000),
size: cacheEntry.size,
};
Expand Down
11 changes: 4 additions & 7 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import mkdirp from 'mkdirp';
import { readFileSync, existsSync, writeFileSync, writeFile } from 'fs';
import { pathToFileURL } from 'url';
import * as path from 'path';
import glob from 'fast-glob';
import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -132,7 +131,7 @@ export class MessageProcessor {
};
this._tmpDir = tmpDir || tmpdir();
this._tmpDirBase = path.join(this._tmpDir, 'graphql-language-service');
this._tmpUriBase = pathToFileURL(this._tmpDirBase).toString();
this._tmpUriBase = URI.parse(this._tmpDirBase).toString();
this._loadConfigOptions = loadConfigOptions;
if (
loadConfigOptions.extensions &&
Expand Down Expand Up @@ -832,9 +831,7 @@ export class MessageProcessor {
const isFileUri = existsSync(uri);
let version = 1;
if (isFileUri) {
const schemaUri = pathToFileURL(
path.join(project.dirpath, uri),
).toString();
const schemaUri = URI.parse(path.join(project.dirpath, uri)).toString();
const schemaDocument = this._getCachedDocument(schemaUri);

if (schemaDocument) {
Expand All @@ -860,7 +857,7 @@ export class MessageProcessor {
projectTmpPath = path.join(projectTmpPath, appendPath);
}
if (prependWithProtocol) {
return pathToFileURL(path.resolve(projectTmpPath)).toString();
return URI.parse(path.resolve(projectTmpPath)).toString();
} else {
return path.resolve(projectTmpPath);
}
Expand Down Expand Up @@ -1015,7 +1012,7 @@ export class MessageProcessor {
}

// build full system URI path with protocol
const uri = pathToFileURL(filePath).toString();
const uri = URI.parse(filePath).toString();

// i would use the already existing graphql-config AST, but there are a few reasons we can't yet
const contents = this._parser(document.rawSDL, uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,43 @@ describe('MessageProcessor', () => {
await expect(result[0].uri).toEqual(`${queryPathUri}/test3.graphql`);
});

describe('handleDidOpenOrSaveNotification', () => {
const mockReadFileSync: jest.Mock = jest.requireMock('fs').readFileSync;

beforeEach(() => {
mockReadFileSync.mockReturnValue('');
messageProcessor._updateGraphQLConfig = jest.fn();
});
it('updates config for standard config filename changes', async () => {
await messageProcessor.handleDidOpenOrSaveNotification({
textDocument: {
uri: `${pathToFileURL('.')}/.graphql.config.js`,
languageId: 'js',
version: 0,
text: '',
},
});

expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
});

it('updates config for custom config filename changes', async () => {
const customConfigName = 'custom-config-name.yml';
messageProcessor._settings = { load: { fileName: customConfigName } };

await messageProcessor.handleDidOpenOrSaveNotification({
textDocument: {
uri: `${pathToFileURL('.')}/${customConfigName}`,
languageId: 'js',
version: 0,
text: '',
},
});

expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
});
});

it('parseDocument finds queries in tagged templates', async () => {
const text = `
// @flow
Expand Down Expand Up @@ -506,34 +543,5 @@ export function Example(arg: string) {}`;

expect(messageProcessor._updateGraphQLConfig).not.toHaveBeenCalled();
});

it('updates config for standard config filename changes', async () => {
await messageProcessor.handleWatchedFilesChangedNotification({
changes: [
{
uri: `${pathToFileURL('.')}/.graphql.config`,
type: FileChangeType.Changed,
},
],
});

expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
});

it('updates config for custom config filename changes', async () => {
const customConfigName = 'custom-config-name.yml';
messageProcessor._settings = { load: { fileName: customConfigName } };

await messageProcessor.handleWatchedFilesChangedNotification({
changes: [
{
uri: `${pathToFileURL('.')}/${customConfigName}`,
type: FileChangeType.Changed,
},
],
});

expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
});
});
});

0 comments on commit 4a3bf20

Please sign in to comment.