-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Code] correctly handle url when workspace is a symbol link.
- Loading branch information
spacedragon
committed
Feb 22, 2019
1 parent
a4cc1d1
commit 1a1c4e4
Showing
2 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
x-pack/plugins/code/server/lsp/workspace_handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import mkdirp from 'mkdirp'; | ||
import * as os from 'os'; | ||
import rimraf from 'rimraf'; | ||
import { ResponseMessage } from 'vscode-jsonrpc/lib/messages'; | ||
import { LspRequest } from '../../model'; | ||
import { ConsoleLoggerFactory } from '../utils/console_logger_factory'; | ||
import { WorkspaceHandler } from './workspace_handler'; | ||
|
||
const baseDir = fs.mkdtempSync(path.join(os.tmpdir(), 'code_test')); | ||
const workspaceDir = path.join(baseDir, 'workspace'); | ||
const repoDir = path.join(baseDir, 'repo'); | ||
|
||
function handleResponseUri(wh: WorkspaceHandler, uri: string) { | ||
const dummyRequest: LspRequest = { | ||
method: 'textDocument/edefinition', | ||
params: [], | ||
}; | ||
const dummyResponse: ResponseMessage = { | ||
id: null, | ||
jsonrpc: '', | ||
result: [ | ||
{ | ||
location: { | ||
uri, | ||
}, | ||
}, | ||
], | ||
}; | ||
wh.handleResponse(dummyRequest, dummyResponse); | ||
return dummyResponse.result[0].location.uri; | ||
} | ||
|
||
function makeAFile( | ||
workspacePath: string = workspaceDir, | ||
repo = 'github.com/Microsoft/TypeScript-Node-Starter', | ||
revision = 'master', | ||
file = 'src/controllers/user.ts' | ||
) { | ||
const fullPath = path.join(workspacePath, repo, '__randomString', revision, file); | ||
mkdirp.sync(path.dirname(fullPath)); | ||
fs.writeFileSync(fullPath, ''); | ||
const strInUrl = fullPath | ||
.split(path.sep) | ||
.map(value => encodeURIComponent(value)) | ||
.join('/'); | ||
const uri = `file:///${strInUrl}`; | ||
return { repo, revision, file, uri }; | ||
} | ||
|
||
test('file system url should be converted', async () => { | ||
const workspaceHandler = new WorkspaceHandler( | ||
repoDir, | ||
workspaceDir, | ||
// @ts-ignore | ||
null, | ||
new ConsoleLoggerFactory() | ||
); | ||
const { repo, revision, file, uri } = makeAFile(workspaceDir); | ||
const converted = handleResponseUri(workspaceHandler, uri); | ||
expect(converted).toBe(`git://${repo}/blob/${revision}/${file}`); | ||
}); | ||
|
||
test('should support symbol link', async () => { | ||
const symlinkToWorkspace = path.join(baseDir, 'linkWorkspace'); | ||
fs.symlinkSync(workspaceDir, symlinkToWorkspace, 'dir'); | ||
// @ts-ignore | ||
const workspaceHandler = new WorkspaceHandler( | ||
repoDir, | ||
symlinkToWorkspace, | ||
// @ts-ignore | ||
null, | ||
new ConsoleLoggerFactory() | ||
); | ||
|
||
const { repo, revision, file, uri } = makeAFile(workspaceDir); | ||
const converted = handleResponseUri(workspaceHandler, uri); | ||
expect(converted).toBe(`git://${repo}/blob/${revision}/${file}`); | ||
}); | ||
|
||
test('should support spaces in workspace dir', async () => { | ||
const workspaceHasSpaces = path.join(baseDir, 'work space'); | ||
const workspaceHandler = new WorkspaceHandler( | ||
repoDir, | ||
workspaceHasSpaces, | ||
// @ts-ignore | ||
null, | ||
new ConsoleLoggerFactory() | ||
); | ||
const { repo, revision, file, uri } = makeAFile(workspaceHasSpaces); | ||
const converted = handleResponseUri(workspaceHandler, uri); | ||
expect(converted).toBe(`git://${repo}/blob/${revision}/${file}`); | ||
}); | ||
|
||
test('should throw a error if url is invalid', async () => { | ||
const workspaceHandler = new WorkspaceHandler( | ||
repoDir, | ||
workspaceDir, | ||
// @ts-ignore | ||
null, | ||
new ConsoleLoggerFactory() | ||
); | ||
const invalidDir = path.join(baseDir, 'invalid_dir'); | ||
const { uri } = makeAFile(invalidDir); | ||
expect(() => handleResponseUri(workspaceHandler, uri)).toThrow(); | ||
}); | ||
|
||
beforeAll(() => { | ||
mkdirp.sync(workspaceDir); | ||
mkdirp.sync(repoDir); | ||
}); | ||
|
||
afterAll(() => { | ||
rimraf.sync(baseDir); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters