Skip to content

Commit

Permalink
Refactor: Mutualize assertWillComeTrue function
Browse files Browse the repository at this point in the history
The same pattern is used in all integration tests to await an event.
  • Loading branch information
deribaucourt committed Dec 14, 2023
1 parent 2d53d34 commit ed64788
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 50 deletions.
26 changes: 7 additions & 19 deletions integration-tests/src/tests/bitbake-parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import { afterEach } from 'mocha'

import path from 'path'
import { addLayer, resetLayer } from '../utils/bitbake'
import { delay } from '../utils/async'
import { assertWillComeTrue, assertWorkspaceWillBeOpen, delay } from '../utils/async'

suite('Bitbake Commands Test Suite', () => {
suite('Bitbake Parsing Test Suite', () => {
let disposables: vscode.Disposable[] = []
let workspaceURI: vscode.Uri
let buildFolder: vscode.Uri

suiteSetup(async function (this: Mocha.Context) {
this.timeout(100000)
while (vscode.workspace.workspaceFolders === undefined || vscode.workspace.workspaceFolders?.length === 0) {
await delay(100)
}
workspaceURI = vscode.workspace.workspaceFolders[0].uri
await assertWorkspaceWillBeOpen()
workspaceURI = (vscode.workspace.workspaceFolders as vscode.WorkspaceFolder[])[0].uri
buildFolder = vscode.Uri.joinPath(workspaceURI, 'build')

// Generate the build directory for the addLayer functions to work
Expand All @@ -32,10 +30,7 @@ suite('Bitbake Commands Test Suite', () => {
}
})
await vscode.commands.executeCommand('bitbake.parse-recipes')
// eslint-disable-next-line no-unmodified-loop-condition
while (!taskExecuted) {
await delay(100)
}
await assertWillComeTrue(async () => taskExecuted)
disposable.dispose()
})

Expand Down Expand Up @@ -63,10 +58,7 @@ suite('Bitbake Commands Test Suite', () => {
await delay(500)

await vscode.commands.executeCommand('bitbake.parse-recipes')
// eslint-disable-next-line no-unmodified-loop-condition
while (!taskExecuted) {
await delay(100)
}
await assertWillComeTrue(async () => taskExecuted)

const diagnostics = vscode.languages.getDiagnostics()
assert.strictEqual(diagnostics.length, 0)
Expand All @@ -84,11 +76,7 @@ suite('Bitbake Commands Test Suite', () => {
await addLayer(path.resolve(__dirname, '../../project-folder/sources/meta-error'), workspacePath)

await vscode.commands.executeCommand('bitbake.parse-recipes')

// eslint-disable-next-line no-unmodified-loop-condition
while (!taskExecuted) {
await delay(100)
}
await assertWillComeTrue(async () => taskExecuted)

await resetLayer(path.resolve(__dirname, '../../project-folder/sources/meta-error'), workspacePath)

Expand Down
16 changes: 6 additions & 10 deletions integration-tests/src/tests/command-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as assert from 'assert'
import * as vscode from 'vscode'

import { delay } from '../utils/async'
import { assertWillComeTrue, assertWorkspaceWillBeOpen } from '../utils/async'
import path from 'path'

suite('Bitbake Command Wrapper', () => {
Expand All @@ -20,10 +20,8 @@ suite('Bitbake Command Wrapper', () => {
suiteSetup(async function (this: Mocha.Context) {
/* eslint-disable no-template-curly-in-string */
this.timeout(100000)
while (vscode.workspace.workspaceFolders === undefined || vscode.workspace.workspaceFolders?.length === 0) {
await delay(100)
}
workspaceURI = vscode.workspace.workspaceFolders[0].uri
await assertWorkspaceWillBeOpen()
workspaceURI = (vscode.workspace.workspaceFolders as vscode.WorkspaceFolder[])[0].uri
buildFolder = vscode.Uri.joinPath(workspaceURI, 'build-crops')

const vscodeBitbake = vscode.extensions.getExtension('yocto-project.yocto-bitbake')
Expand Down Expand Up @@ -61,15 +59,13 @@ suite('Bitbake Command Wrapper', () => {

await vscode.workspace.openTextDocument(docUri)

while (definitions.length !== 1) {
await assertWillComeTrue(async () => {
definitions = await vscode.commands.executeCommand(
'vscode.executeDefinitionProvider',
docUri,
new vscode.Position(0, 10)
)

// We cannot get an event when the BitBake scan is complete, so we have to wait for it to finish and hope for the best
await delay(100)
}
return definitions.length === 1
})
}).timeout(300000)
})
13 changes: 4 additions & 9 deletions integration-tests/src/tests/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as assert from 'assert'
import * as vscode from 'vscode'
import path from 'path'
import { delay } from '../utils/async'
import { assertWillComeTrue } from '../utils/async'

suite('Bitbake Completion Test Suite', () => {
const filePath = path.resolve(__dirname, '../../project-folder/sources/meta-fixtures/completion.bb')
Expand All @@ -33,19 +33,14 @@ suite('Bitbake Completion Test Suite', () => {

const testCompletion = async (position: vscode.Position, expected: string): Promise<void> => {
let completionList: vscode.CompletionList = { items: [] }
while (!checkHasItemWithLabel(completionList, expected)) {
await assertWillComeTrue(async () => {
completionList = await vscode.commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
docUri,
position
)
// For completion to work, an "embedded language document" needs to be generated.
// We have no practical way to know when it will be done.
// Attempts to wait for the "embedded language document" to be created in its folder still produced incorrect results.
// So here we are, just hoping everything is fine so our test won't take forever to fail.
await delay(100)
}
assert.strictEqual(checkHasItemWithLabel(completionList, expected), true)
return checkHasItemWithLabel(completionList, expected)
})
}

test('Completion appears properly on bitbake variable', async () => {
Expand Down
13 changes: 5 additions & 8 deletions integration-tests/src/tests/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as assert from 'assert'
import * as vscode from 'vscode'
import path from 'path'
import { delay } from '../utils/async'
import { assertWillComeTrue } from '../utils/async'

suite('Bitbake Hover Test Suite', () => {
const filePath = path.resolve(__dirname, '../../project-folder/sources/meta-fixtures/hover.bb')
Expand All @@ -24,18 +24,15 @@ suite('Bitbake Hover Test Suite', () => {

const testHover = async (position: vscode.Position, expected: string): Promise<void> => {
let hoverResult: vscode.Hover[] = []
while (hoverResult.length === 0) {

await assertWillComeTrue(async () => {
hoverResult = await vscode.commands.executeCommand<vscode.Hover[]>(
'vscode.executeHoverProvider',
docUri,
position
)
// For hover to work, an "embedded language document" needs to be generated.
// We have no practical way to know when it will be done.
// Attempts to wait for the "embedded language document" to be created in its folder still produced incorrect results.
// So here we are, just hoping everything is fine so our test won't take forever to fail.
await delay(100)
}
return hoverResult.length > 0
})

assert.strictEqual(hoverResult.length, 1)
const content = hoverResult[0]?.contents[0]
Expand Down
6 changes: 2 additions & 4 deletions integration-tests/src/tests/vscode-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import * as assert from 'assert'
import { after } from 'mocha'
import * as vscode from 'vscode'
import { delay } from '../utils/async'
import { assertWorkspaceWillBeOpen } from '../utils/async'

suite('VSCode integration Test Suite', () => {
suiteSetup(async function (this: Mocha.Context) {
this.timeout(10000)
while (vscode.workspace.workspaceFolders === undefined || vscode.workspace.workspaceFolders?.length === 0) {
await delay(100)
}
await assertWorkspaceWillBeOpen()
})

after(() => {
Expand Down
18 changes: 18 additions & 0 deletions integration-tests/src/utils/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import assert from 'assert'
import * as vscode from 'vscode'

export async function delay (ms: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, ms))
}

/// Asserts that the given predicate will come true within the given timeout.
/// Since we often want to test events have happened but they depend on asynchronous
/// external VSCode and extension processes, we can't listen for them directly.
export async function assertWillComeTrue (predicate: () => Promise<boolean>, timeout: number = 300000): Promise<void> {
const startTime = Date.now()
while (!(await predicate()) && (Date.now() - startTime < timeout)) {
await delay(100)
}
assert.ok(predicate())
}

export async function assertWorkspaceWillBeOpen (timeout: number = 10000): Promise<void> {
await assertWillComeTrue(async () => (vscode.workspace.workspaceFolders !== undefined && vscode.workspace.workspaceFolders?.length !== 0), timeout)
}

0 comments on commit ed64788

Please sign in to comment.