Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoah1 committed Oct 29, 2024
1 parent 022ccd4 commit 83629bb
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 54 deletions.
1 change: 1 addition & 0 deletions src/test-explorer/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ export class TestResolver implements OnModuleInit, vscode.Disposable {
}

if (this.openDocumentWatcherEnabled) return
this.openDocumentWatcherEnabled = true
this.ctx.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(async doc => {
// Discovery within newly opened documents.
Expand Down
183 changes: 129 additions & 54 deletions src/test/suite/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ suite('Test Resolver', () => {
return result
}

const documentStub = {
uri: vscode.Uri.parse('file:///sample/path/test_file.py'),
languageId: 'python',
}

let onDidOpenStub: sinon.SinonStub
let sendRequestStub: sinon.SinonStub

beforeEach(() => {
testCaseStore.onModuleInit()
testResolver.onModuleInit()
Expand All @@ -284,6 +292,12 @@ suite('Test Resolver', () => {
.returns('./projectview.bazelproject')
.onSecondCall()
.returns('./projectview2.bazelproject')

sandbox.stub(vscode.workspace, 'textDocuments').get(() => [documentStub])

onDidOpenStub = sandbox.stub(vscode.workspace, 'onDidOpenTextDocument')
onDidOpenStub.returns({dispose: () => {}})
sendRequestStub = sandbox.stub(sampleConn, 'sendRequest')
})

test('root', async () => {
Expand Down Expand Up @@ -312,9 +326,7 @@ suite('Test Resolver', () => {
})

test('targets below root', async () => {
const sendRequestStub = sandbox
.stub(sampleConn, 'sendRequest')
.returns(Promise.resolve(sampleBuildTargetsResult))
sendRequestStub.returns(Promise.resolve(sampleBuildTargetsResult))

const root = testCaseStore.testController.createTestItem(
'root',
Expand Down Expand Up @@ -389,9 +401,7 @@ suite('Test Resolver', () => {
assert.ok(testCaseStore.testController.resolveHandler)

// Simulate an error in requesting targets.
const sendRequestStub = sandbox
.stub(sampleConn, 'sendRequest')
.returns(Promise.reject(new Error('sample error')))
sendRequestStub.returns(Promise.reject(new Error('sample error')))
try {
await testCaseStore.testController.resolveHandler(root)
assert.fail('Expected error')
Expand All @@ -416,19 +426,15 @@ suite('Test Resolver', () => {
const emptyResult: bsp.WorkspaceBuildTargetsResult = {
targets: [],
}
const sendRequestStub = sandbox
.stub(sampleConn, 'sendRequest')
.resolves(emptyResult)
sendRequestStub.resolves(emptyResult)
await testCaseStore.testController.resolveHandler(root)
const message = root.error as vscode.MarkdownString
assert.ok(message.value.includes('No test targets found'))
})

test('source files within a target', async () => {
const buildTarget = sampleBuildTargetsResult.targets[0]
const sendRequestStub = sandbox
.stub(sampleConn, 'sendRequest')
.returns(Promise.resolve(sampleSourceItemsResult(buildTarget)))
sendRequestStub.resolves(sampleSourceItemsResult(buildTarget))

languageToolsStub.getDocumentTestCases.resolves({
isTestFile: true,
Expand Down Expand Up @@ -521,9 +527,6 @@ suite('Test Resolver', () => {
})

test('expand targets based on open files, success', async () => {
// Setup
const sendRequestStub = sandbox.stub(sampleConn, 'sendRequest')

// Expect a sequence of 3 BSP requests.
sendRequestStub
.onFirstCall()
Expand All @@ -540,11 +543,6 @@ suite('Test Resolver', () => {
.withArgs(settings.SettingName.AUTO_EXPAND_TARGET)
.returns(true)

const documentStub = {
uri: vscode.Uri.parse('file:///sample/path/test_file.py'),
languageId: 'python',
}

languageToolsStub.getDocumentTestCases.resolves({
isTestFile: true,
testCases: getSampleDocumentTestItems(),
Expand All @@ -559,10 +557,6 @@ suite('Test Resolver', () => {
},
})

const workspaceTextDocumentStub = sandbox
.stub(vscode.workspace, 'textDocuments')
.get(() => [documentStub])

const root = testCaseStore.testController.createTestItem(
'root',
'Bazel Test Targets'
Expand Down Expand Up @@ -630,12 +624,10 @@ suite('Test Resolver', () => {
assert.equal(child.children.size, 1)
})
}
assert.ok(onDidOpenStub.calledOnce)
})

test('expand targets based on open files, error', async () => {
// Setup
const sendRequestStub = sandbox.stub(sampleConn, 'sendRequest')

// Expect a sequence of 3 BSP requests.
sendRequestStub
.onFirstCall()
Expand All @@ -649,11 +641,6 @@ suite('Test Resolver', () => {
.withArgs(settings.SettingName.AUTO_EXPAND_TARGET)
.returns(true)

const documentStub = {
uri: vscode.Uri.parse('file:///sample/path/test_file.py'),
languageId: 'python',
}

languageToolsStub.getDocumentTestCases.resolves({
isTestFile: true,
testCases: getSampleDocumentTestItems(),
Expand All @@ -668,10 +655,6 @@ suite('Test Resolver', () => {
},
})

const workspaceTextDocumentStub = sandbox
.stub(vscode.workspace, 'textDocuments')
.get(() => [documentStub])

const root = testCaseStore.testController.createTestItem(
'root',
'Bazel Test Targets'
Expand Down Expand Up @@ -703,12 +686,10 @@ suite('Test Resolver', () => {
assert.ok(item)
assert.equal(item.children.size, 0)
}
assert.ok(onDidOpenStub.calledOnce)
})

test('expand targets based on open files, out of scope', async () => {
// Setup
const sendRequestStub = sandbox.stub(sampleConn, 'sendRequest')

// Expect a sequence of 3 BSP requests.
sendRequestStub
.onFirstCall()
Expand All @@ -722,11 +703,6 @@ suite('Test Resolver', () => {
.withArgs(settings.SettingName.AUTO_EXPAND_TARGET)
.returns(true)

const documentStub = {
uri: vscode.Uri.parse('file:///sample/path/test_file.py'),
languageId: 'python',
}

const docTestCases = {
isTestFile: true,
testCases: getSampleDocumentTestItems(),
Expand All @@ -742,10 +718,6 @@ suite('Test Resolver', () => {
}
languageToolsStub.getDocumentTestCases.resolves(docTestCases)

const workspaceTextDocumentStub = sandbox
.stub(vscode.workspace, 'textDocuments')
.get(() => [documentStub])

const root = testCaseStore.testController.createTestItem(
'root',
'Bazel Test Targets'
Expand Down Expand Up @@ -780,12 +752,11 @@ suite('Test Resolver', () => {
assert.ok(
syncHintStub.enable.calledOnceWith(documentStub.uri, '', docTestCases)
)
assert.ok(onDidOpenStub.calledOnce)
})

test('refresh success', async () => {
const sendRequestStub = sandbox
.stub(sampleConn, 'sendRequest')
.resolves(sampleBuildTargetsResult)
sendRequestStub.resolves(sampleBuildTargetsResult)

const root = testCaseStore.testController.createTestItem(
'root',
Expand All @@ -806,10 +777,114 @@ suite('Test Resolver', () => {
})
})

test('expand targets based on open files, multiple calls', async () => {
extensionSettingStub
.withArgs(settings.SettingName.AUTO_EXPAND_TARGET)
.returns(true)

languageToolsStub.getDocumentTestCases.resolves({
isTestFile: true,
testCases: getSampleDocumentTestItems(),
documentTest: {
name: 'My Document',
range: new vscode.Range(
new vscode.Position(0, 0),
new vscode.Position(0, 0)
),
uri: documentStub.uri,
testFilter: 'test_file.py',
},
})

const root = testCaseStore.testController.createTestItem(
'root',
'Bazel Test Targets'
)
testCaseStore.testController.items.add(root)
testCaseStore.testCaseMetadata.set(
root,
new TestCaseInfo(root, undefined, TestItemType.Root)
)
assert.ok(testCaseStore.testController.resolveHandler)

sendRequestStub
// First series of 3 requests.
.onCall(0)
.resolves(sampleBuildTargetsResult)
.onCall(1)
.resolves({targets: [sampleBuildTargetsResult.targets[0].id]})
.onCall(2)
.resolves(sampleSourceItemsResult(sampleBuildTargetsResult.targets[0]))
// Second series of 3 requests.
.onCall(3)
.resolves(sampleBuildTargetsResult)
.onCall(4)
.resolves({targets: [sampleBuildTargetsResult.targets[0].id]})
.onCall(5)
.resolves(sampleSourceItemsResult(sampleBuildTargetsResult.targets[0]))

await testCaseStore.testController.resolveHandler(root)
await testCaseStore.testController.resolveHandler(root)

// Validate expected document positions in the tree.
const docTestItem = [
root.children
.get('{targetdir}:/repo/root/base/directory')
?.children.get('{targetdir}:/repo/root/base/directory/a')
?.children.get('a')
?.children.get(
'{sourcefile}:a:/repo/root/base/directory/a/MyFile1.language'
),
root.children
.get('{targetdir}:/repo/root/base/directory')
?.children.get('{targetdir}:/repo/root/base/directory/a')
?.children.get('a')
?.children.get(
'{sourcefile}:a:/repo/root/base/directory/a/MyFile2.language'
),
root.children
.get('{targetdir}:/repo/root/base/directory')
?.children.get('{targetdir}:/repo/root/base/directory/a')
?.children.get('a')
?.children.get('{sourcedir}:a:/repo/root/base/directory/a/src/dir')
?.children.get('{sourcedir}:a:/repo/root/base/directory/a/src/dir/1')
?.children.get(
'{sourcefile}:a:/repo/root/base/directory/a/src/dir/1/MyFile4.language'
),
root.children
.get('{targetdir}:/repo/root/base/directory')
?.children.get('{targetdir}:/repo/root/base/directory/a')
?.children.get('a')
?.children.get('{sourcedir}:a:/repo/root/base/directory/a/src/dir')
?.children.get('{sourcedir}:a:/repo/root/base/directory/a/src/dir/2')
?.children.get(
'{sourcefile}:a:/repo/root/base/directory/a/src/dir/2/MyFile5.language'
),
root.children
.get('{targetdir}:/repo/root/base/directory')
?.children.get('{targetdir}:/repo/root/base/directory/a')
?.children.get('a')
?.children.get('{sourcedir}:a:/repo/root/base/directory/a/src/dir')
?.children.get('{sourcedir}:a:/repo/root/base/directory/a/src/dir/2')
?.children.get(
'{sourcefile}:a:/repo/root/base/directory/a/src/dir/2/MyFile6.language'
),
]

for (const item of docTestItem) {
assert.ok(item)
assert.equal(item.children.size, 2)
item.children.forEach(child => {
assert.ok(testCaseStore.testCaseMetadata.get(child))
assert.equal(child.children.size, 1)
})
}
assert.equal(sendRequestStub.callCount, 6)
assert.ok(onDidOpenStub.calledOnce)
})

test('cancelled refresh', async () => {
const sendRequestStub = sandbox
.stub(sampleConn, 'sendRequest')
.rejects({code: CANCEL_ERROR_CODE})
sendRequestStub.rejects({code: CANCEL_ERROR_CODE})

const root = testCaseStore.testController.createTestItem(
'root',
Expand Down

0 comments on commit 83629bb

Please sign in to comment.