forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Code] try prune a worktree if it already exists (elastic#36338) (ela…
- Loading branch information
Yulong
authored
May 10, 2019
1 parent
15dc75c
commit 92e70ca
Showing
4 changed files
with
166 additions
and
68 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ import Git from '@elastic/nodegit'; | |
import fs from 'fs'; | ||
import mkdirp from 'mkdirp'; | ||
import path from 'path'; | ||
// import rimraf from 'rimraf'; | ||
import rimraf from 'rimraf'; | ||
import sinon from 'sinon'; | ||
|
||
import assert from 'assert'; | ||
|
@@ -39,15 +39,16 @@ const packagejson = ` | |
} | ||
`; | ||
describe('lsp_service tests', () => { | ||
let firstCommitSha = ''; | ||
let secondCommitSha = ''; | ||
async function prepareProject(repoPath: string) { | ||
mkdirp.sync(repoPath); | ||
const repo = await Git.Repository.init(repoPath, 0); | ||
const helloContent = "console.log('hello world');"; | ||
fs.writeFileSync(path.join(repo.workdir(), filename), helloContent, 'utf8'); | ||
fs.writeFileSync(path.join(repo.workdir(), 'package.json'), packagejson, 'utf8'); | ||
const index = await repo.refreshIndex(); | ||
|
||
let index = await repo.refreshIndex(); | ||
await index.addByPath(filename); | ||
await index.addByPath('package.json'); | ||
index.write(); | ||
const treeId = await index.writeTree(); | ||
const committer = Git.Signature.create('tester', '[email protected]', Date.now() / 1000, 60); | ||
|
@@ -59,8 +60,22 @@ describe('lsp_service tests', () => { | |
treeId, | ||
[] | ||
); | ||
// eslint-disable-next-line no-console | ||
console.log(`created commit ${commit.tostrS()}`); | ||
firstCommitSha = commit.tostrS(); | ||
fs.writeFileSync(path.join(repo.workdir(), 'package.json'), packagejson, 'utf8'); | ||
index = await repo.refreshIndex(); | ||
await index.addByPath('package.json'); | ||
index.write(); | ||
const treeId2 = await index.writeTree(); | ||
const commit2 = await repo.createCommit( | ||
'HEAD', | ||
committer, | ||
committer, | ||
'commit2 for test', | ||
treeId2, | ||
[commit] | ||
); | ||
secondCommitSha = commit2.tostrS(); | ||
|
||
return repo; | ||
} | ||
|
||
|
@@ -109,36 +124,44 @@ describe('lsp_service tests', () => { | |
return path.resolve(pa) === path.resolve(pb); | ||
} | ||
|
||
it('process a hover request', async () => { | ||
function mockLspService() { | ||
const esClient = mockEsClient(); | ||
const revision = 'master'; | ||
|
||
const lspservice = new LspService( | ||
return new LspService( | ||
'127.0.0.1', | ||
serverOptions, | ||
esClient, | ||
installManager, | ||
new ConsoleLoggerFactory(), | ||
new RepositoryConfigController(esClient) | ||
); | ||
} | ||
|
||
async function sendHoverRequest(lspservice: LspService, revision: string) { | ||
const method = 'textDocument/hover'; | ||
|
||
const params = { | ||
textDocument: { | ||
uri: `git://${repoUri}/blob/${revision}/${filename}`, | ||
}, | ||
position: { | ||
line: 0, | ||
character: 1, | ||
}, | ||
}; | ||
return await lspservice.sendRequest(method, params); | ||
} | ||
|
||
it('process a hover request', async () => { | ||
const lspservice = mockLspService(); | ||
try { | ||
const params = { | ||
textDocument: { | ||
uri: `git://${repoUri}/blob/${revision}/${filename}`, | ||
}, | ||
position: { | ||
line: 0, | ||
character: 1, | ||
}, | ||
}; | ||
const workspaceHandler = lspservice.workspaceHandler; | ||
const wsSpy = sinon.spy(workspaceHandler, 'handleRequest'); | ||
const controller = lspservice.controller; | ||
const ctrlSpy = sinon.spy(controller, 'handleRequest'); | ||
|
||
const method = 'textDocument/hover'; | ||
const revision = 'master'; | ||
|
||
const response = await lspservice.sendRequest(method, params); | ||
const response = await sendHoverRequest(lspservice, revision); | ||
assert.ok(response); | ||
assert.ok(response.result.contents); | ||
|
||
|
@@ -172,30 +195,11 @@ describe('lsp_service tests', () => { | |
}).timeout(10000); | ||
|
||
it('unload a workspace', async () => { | ||
const esClient = mockEsClient(); | ||
const revision = 'master'; | ||
const lspservice = new LspService( | ||
'127.0.0.1', | ||
serverOptions, | ||
esClient, | ||
installManager, | ||
new ConsoleLoggerFactory(), | ||
new RepositoryConfigController(esClient) | ||
); | ||
const lspservice = mockLspService(); | ||
try { | ||
const params = { | ||
textDocument: { | ||
uri: `git://${repoUri}/blob/${revision}/${filename}`, | ||
}, | ||
position: { | ||
line: 0, | ||
character: 1, | ||
}, | ||
}; | ||
|
||
const method = 'textDocument/hover'; | ||
const revision = 'master'; | ||
// send a dummy request to open a workspace; | ||
const response = await lspservice.sendRequest(method, params); | ||
const response = await sendHoverRequest(lspservice, revision); | ||
assert.ok(response); | ||
const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision); | ||
const workspaceFolderExists = fs.existsSync(workspacePath); | ||
|
@@ -228,4 +232,65 @@ describe('lsp_service tests', () => { | |
} | ||
// @ts-ignore | ||
}).timeout(10000); | ||
|
||
it('should work if a worktree exists', async () => { | ||
const lspservice = mockLspService(); | ||
try { | ||
const revision = 'master'; | ||
// send a dummy request to open a workspace; | ||
const response = await sendHoverRequest(lspservice, revision); | ||
assert.ok(response); | ||
const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision); | ||
const workspaceFolderExists = fs.existsSync(workspacePath); | ||
// workspace is opened | ||
assert.ok(workspaceFolderExists); | ||
const bareRepoWorktree = path.join( | ||
serverOptions.repoPath, | ||
repoUri, | ||
'worktrees', | ||
`workspace-${revision}` | ||
); | ||
// worktree is exists | ||
const bareRepoWorktreeExists = fs.existsSync(bareRepoWorktree); | ||
assert.ok(bareRepoWorktreeExists); | ||
// delete the workspace folder but leave worktree | ||
rimraf.sync(workspacePath); | ||
// send a dummy request to open it again | ||
await sendHoverRequest(lspservice, revision); | ||
assert.ok(fs.existsSync(workspacePath)); | ||
|
||
return; | ||
} finally { | ||
await lspservice.shutdown(); | ||
} | ||
// @ts-ignore | ||
}).timeout(10000); | ||
|
||
it('should update if a worktree is not the newest', async () => { | ||
const lspservice = mockLspService(); | ||
try { | ||
const revision = 'master'; | ||
// send a dummy request to open a workspace; | ||
const response = await sendHoverRequest(lspservice, revision); | ||
assert.ok(response); | ||
const workspacePath = path.resolve(serverOptions.workspacePath, repoUri, revision); | ||
const workspaceRepo = await Git.Repository.open(workspacePath); | ||
// workspace is newest now | ||
assert.strictEqual((await workspaceRepo.getHeadCommit()).sha(), secondCommitSha); | ||
const firstCommit = await workspaceRepo.getCommit(firstCommitSha); | ||
// reset workspace to an older one | ||
// @ts-ignore | ||
await Git.Reset.reset(workspaceRepo, firstCommit, Git.Reset.TYPE.HARD, {}); | ||
assert.strictEqual((await workspaceRepo.getHeadCommit()).sha(), firstCommitSha); | ||
|
||
// send a request again; | ||
await sendHoverRequest(lspservice, revision); | ||
// workspace_handler should update workspace to the newest one | ||
assert.strictEqual((await workspaceRepo.getHeadCommit()).sha(), secondCommitSha); | ||
return; | ||
} finally { | ||
await lspservice.shutdown(); | ||
} | ||
// @ts-ignore | ||
}).timeout(10000); | ||
}); |
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
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 |
---|---|---|
|
@@ -1478,10 +1478,10 @@ | |
resolved "https://registry.yarnpkg.com/@elastic/node-crypto/-/node-crypto-1.0.0.tgz#4d325df333fe1319556bb4d54214098ada1171d4" | ||
integrity sha512-bbjbEyILPRTRt0xnda18OttLtlkJBPuXx3CjISUSn9jhWqHoFMzfOaZ73D5jxZE2SaFZUrJYfPpqXP6qqPufAQ== | ||
|
||
"@elastic/[email protected].19": | ||
version "0.25.0-alpha.19" | ||
resolved "https://registry.yarnpkg.com/@elastic/nodegit/-/nodegit-0.25.0-alpha.19.tgz#a05a712dedbdbd7fe649cb970eb5677c5ec4ada8" | ||
integrity sha512-fOG7tXkf8wmZEVMld+tkZtS3BRsUlrh95sRNUglHDd0G3g+ow9YDjV3dFHlLST0pTWirKyJuxrow2KgpLoWplA== | ||
"@elastic/[email protected].20": | ||
version "0.25.0-alpha.20" | ||
resolved "https://registry.yarnpkg.com/@elastic/nodegit/-/nodegit-0.25.0-alpha.20.tgz#74f36cb8c137386aeebacded574d1d6a4e6d9a01" | ||
integrity sha512-wvBTfKVAhFkTMh/N6oO/5NOa+m+cRiZLrIPHpC3ITPBQiDAUP+g2/NzKVGh/5vx5D/Y9ucy7VsbP13zU9rSi0g== | ||
dependencies: | ||
fs-extra "^7.0.0" | ||
json5 "^2.1.0" | ||
|