Skip to content

Commit

Permalink
Fix opening local schema on windows (redhat-developer#479)
Browse files Browse the repository at this point in the history
* redhat-developer#513 fix opening local schema on windows

Signed-off-by: Yevhen Vydolob <[email protected]>

* fix test

Signed-off-by: Yevhen Vydolob <[email protected]>

* Fix opening local schema file on windows

Signed-off-by: Yevhen Vydolob <[email protected]>

* fix test

Signed-off-by: Yevhen Vydolob <[email protected]>

* fix test

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored Jun 16, 2021
1 parent b7d71e0 commit 17f98cf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/languageservice/services/schemaRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const schemaRequestHandler = (
let scheme = URI.parse(uri).scheme.toLowerCase();

// test if uri is windows path, ie starts with 'c:\'
if (/^[a-z]:\\/i.test(uri)) {
if (/^[a-z]:[\\/]/i.test(uri)) {
const winUri = URI.file(uri);
scheme = winUri.scheme.toLowerCase();
uri = winUri.toString();
Expand Down
9 changes: 8 additions & 1 deletion src/languageservice/services/yamlCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function registerCommands(commandExecutor: CommandExecutor, connection: C
if (!uri) {
return;
}
if (!uri.startsWith('file')) {
// if uri points to local file of its a windows path
if (!uri.startsWith('file') && !/^[a-z]:[\\/]/i.test(uri)) {
const origUri = URI.parse(uri);
const customUri = URI.from({
scheme: 'json-schema',
Expand All @@ -24,6 +25,12 @@ export function registerCommands(commandExecutor: CommandExecutor, connection: C
uri = customUri.toString();
}

// test if uri is windows path, ie starts with 'c:\' and convert to URI
if (/^[a-z]:[\\/]/i.test(uri)) {
const winUri = URI.file(uri);
uri = winUri.toString();
}

const result = await connection.window.showDocument({ uri: uri, external: false, takeFocus: true });
if (!result) {
connection.window.showErrorMessage(`Cannot open ${uri}`);
Expand Down
21 changes: 17 additions & 4 deletions test/schemaRequestHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import { schemaRequestHandler } from '../src/languageservice/services/schemaRequ
import * as sinon from 'sinon';
import * as fs from 'fs';
import { Connection } from 'vscode-languageserver';
import * as assert from 'assert';
import { URI } from 'vscode-uri';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';

const expect = chai.expect;
chai.use(sinonChai);

describe('Schema Request Handler Tests', () => {
describe('schemaRequestHandler', () => {
Expand All @@ -25,18 +29,27 @@ describe('Schema Request Handler Tests', () => {
it('Should care Win URI', async () => {
const connection = <Connection>{};
const resultPromise = schemaRequestHandler(connection, 'c:\\some\\window\\path\\scheme.json', [], URI.parse(''), false);
assert.ok(readFileStub.calledOnceWith('c:\\some\\window\\path\\scheme.json'));
expect(readFileStub).calledOnceWith('c:\\some\\window\\path\\scheme.json');
readFileStub.callArgWith(2, undefined, '{some: "json"}');
const result = await resultPromise;
assert.equal(result, '{some: "json"}');
expect(result).to.be.equal('{some: "json"}');
});

it('UNIX URI should works', async () => {
const connection = <Connection>{};
const resultPromise = schemaRequestHandler(connection, '/some/unix/path/', [], URI.parse(''), false);
readFileStub.callArgWith(2, undefined, '{some: "json"}');
const result = await resultPromise;
assert.equal(result, '{some: "json"}');
expect(result).to.be.equal('{some: "json"}');
});

it('should handle not valid Windows path', async () => {
const connection = <Connection>{};
const resultPromise = schemaRequestHandler(connection, 'A:/some/window/path/scheme.json', [], URI.parse(''), false);
expect(readFileStub).calledOnceWith(URI.file('a:/some/window/path/scheme.json').fsPath);
readFileStub.callArgWith(2, undefined, '{some: "json"}');
const result = await resultPromise;
expect(result).to.be.equal('{some: "json"}');
});
});
});
19 changes: 19 additions & 0 deletions test/yamlCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as chai from 'chai';
import { registerCommands } from '../src/languageservice/services/yamlCommands';
import { commandExecutor } from '../src/languageserver/commandExecutor';
import { Connection } from 'vscode-languageserver/node';
import { URI } from 'vscode-uri';

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -45,4 +46,22 @@ describe('Yaml Commands', () => {
await arg[1](JSON_SCHEMA_LOCAL);
expect(showDocumentStub).to.have.been.calledWith({ uri: JSON_SCHEMA_LOCAL, external: false, takeFocus: true });
});

it('JumpToSchema handler should call "showDocument" with plain win path', async () => {
const showDocumentStub = sandbox.stub();
const connection = ({
window: {
showDocument: showDocumentStub,
},
} as unknown) as Connection;
showDocumentStub.resolves(true);
registerCommands(commandExecutor, connection);
const arg = commandExecutorStub.args[0];
await arg[1]('a:\\some\\path\\to\\schema.json');
expect(showDocumentStub).to.have.been.calledWith({
uri: URI.file('a:\\some\\path\\to\\schema.json').toString(),
external: false,
takeFocus: true,
});
});
});

0 comments on commit 17f98cf

Please sign in to comment.