From 1d052eb46178d409aaa16a515b3f125c5fa9a06e Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Wed, 6 Dec 2023 09:11:04 -0800 Subject: [PATCH] Move test fixtures to independent workspace & add more tests --- .../test/fixtures/6 - workspace/flow.json | 21 ++++++ .../test/bar/test2.cdc | 0 .../test/bar/test3.cdc | 0 .../test/test1.cdc | 0 .../integration/6 - test-provider.test.ts | 66 ++++++++++++++++++- extension/test/unit/test-trie.test.ts | 0 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 extension/test/fixtures/6 - workspace/flow.json rename extension/test/fixtures/{workspace => 6 - workspace}/test/bar/test2.cdc (100%) rename extension/test/fixtures/{workspace => 6 - workspace}/test/bar/test3.cdc (100%) rename extension/test/fixtures/{workspace => 6 - workspace}/test/test1.cdc (100%) delete mode 100644 extension/test/unit/test-trie.test.ts diff --git a/extension/test/fixtures/6 - workspace/flow.json b/extension/test/fixtures/6 - workspace/flow.json new file mode 100644 index 00000000..1b28d61d --- /dev/null +++ b/extension/test/fixtures/6 - workspace/flow.json @@ -0,0 +1,21 @@ +{ + "emulators": { + "default": { + "port": 3569, + "serviceAccount": "emulator-account" + } + }, + "contracts": {}, + "networks": { + "emulator": "127.0.0.1:3569", + "mainnet": "access.mainnet.nodes.onflow.org:9000", + "testnet": "access.devnet.nodes.onflow.org:9000" + }, + "accounts": { + "emulator-account": { + "address": "f8d6e0586b0a20c7", + "key": "d2f9b3e122aa5289fb38edab611c8d1c2aa88d6fd8e3943a306a493361639812" + } + }, + "deployments": {} +} \ No newline at end of file diff --git a/extension/test/fixtures/workspace/test/bar/test2.cdc b/extension/test/fixtures/6 - workspace/test/bar/test2.cdc similarity index 100% rename from extension/test/fixtures/workspace/test/bar/test2.cdc rename to extension/test/fixtures/6 - workspace/test/bar/test2.cdc diff --git a/extension/test/fixtures/workspace/test/bar/test3.cdc b/extension/test/fixtures/6 - workspace/test/bar/test3.cdc similarity index 100% rename from extension/test/fixtures/workspace/test/bar/test3.cdc rename to extension/test/fixtures/6 - workspace/test/bar/test3.cdc diff --git a/extension/test/fixtures/workspace/test/test1.cdc b/extension/test/fixtures/6 - workspace/test/test1.cdc similarity index 100% rename from extension/test/fixtures/workspace/test/test1.cdc rename to extension/test/fixtures/6 - workspace/test/test1.cdc diff --git a/extension/test/integration/6 - test-provider.test.ts b/extension/test/integration/6 - test-provider.test.ts index b1cfc5b6..f25b505f 100644 --- a/extension/test/integration/6 - test-provider.test.ts +++ b/extension/test/integration/6 - test-provider.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, afterEach } from 'mocha' +import { beforeEach, afterEach, before } from 'mocha' import { TestProvider } from '../../src/test-provider/test-provider' import { Settings } from '../../src/settings/settings' import { FlowConfig } from '../../src/server/flow-config' @@ -8,12 +8,19 @@ import * as vscode from 'vscode' import * as sinon from 'sinon' import * as assert from 'assert' -const workspacePath = path.resolve(__dirname, './fixtures/workspace') +const workspacePath = path.resolve(__dirname, './fixtures/6 - workspace') suite('test provider tests', () => { let mockSettings: Settings let mockConfig: FlowConfig let testProvider: TestProvider + let cleanupFunctions: (() => void | Promise)[] = [] + + before(async function () { + this.timeout(5000) + vscode.workspace.updateWorkspaceFolders(0, vscode.workspace.workspaceFolders?.length ?? 0, { uri: vscode.Uri.file(workspacePath) }) + await new Promise((resolve) => setTimeout(resolve, 2500)) + }) beforeEach(async function () { this.timeout(5000) @@ -42,6 +49,9 @@ suite('test provider tests', () => { this.timeout(5000) testProvider.dispose() + for (const cleanupFunction of cleanupFunctions) { + await cleanupFunction() + } }) test('runs all tests in workspace and reports results', async function () { @@ -110,4 +120,56 @@ suite('test provider tests', () => { ]) assert.deepStrictEqual(failedTests, []) }).timeout(20000) + + test('runs tests including newly created file', async function () { + // Create new file + const testFilePath = path.join(workspacePath, 'test/bar/test4.cdc') + const testFileContents = ` + import Test + pub fun testPassing(): Bool { + return assert(true) + } + ` + await vscode.workspace.fs.writeFile(vscode.Uri.file(testFilePath), Buffer.from(testFileContents)) + cleanupFunctions.push(async () => { + await vscode.workspace.fs.delete(vscode.Uri.file(testFilePath)) + }) + await new Promise(resolve => setTimeout(resolve, 1000)) + + // Run tests + let runSpy: sinon.SinonSpiedInstance | undefined + await new Promise(resolve => { + void testProvider.runAllTests(undefined, (testRun) => { + const originalEnd = testRun.end + testRun.end = () => { + originalEnd.call(testRun) + resolve() + } + + runSpy = sinon.spy(testRun) + return runSpy + }) + }) + if (runSpy == null) throw new Error('runSpy is null') + + const passedTests = runSpy.passed.getCalls().map(call => ({ filepath: (call.args[0].uri as vscode.Uri).fsPath, id: call.args[0].id })) + const failedTests = runSpy.failed.getCalls().map(call => ({ filepath: (call.args[0].uri as vscode.Uri).fsPath, id: call.args[0].id, message: (call.args[1] as any).message })) + + passedTests.sort((a, b) => a.filepath.localeCompare(b.filepath)) + failedTests.sort((a, b) => a.filepath.localeCompare(b.filepath)) + + assert.strictEqual(passedTests.length + failedTests.length, 6) + assert.deepStrictEqual(passedTests, [ + { filepath: path.join(workspacePath, 'test/bar/test2.cdc'), id: ':testPassing' }, + { filepath: path.join(workspacePath, 'test/bar/test3.cdc'), id: ':testPassing' }, + { filepath: path.join(workspacePath, 'test/bar/test4.cdc'), id: ':testPassing' }, + { filepath: path.join(workspacePath, 'test/test1.cdc'), id: ':testPassing' } + ]) + assert.deepStrictEqual(failedTests, [ + { filepath: path.join(workspacePath, 'test/bar/test2.cdc'), id: ':testFailing', message: 'FAIL: Execution failed:\nerror: assertion failed\n --> 7465737400000000000000000000000000000000000000000000000000000000:8:2\n' }, + { filepath: path.join(workspacePath, 'test/bar/test3.cdc'), id: ':testFailing', message: 'FAIL: Execution failed:\nerror: assertion failed\n --> 7465737400000000000000000000000000000000000000000000000000000000:4:2\n' } + ]) + + vscode.workspace.updateWorkspaceFolders(0, 1) + }).timeout(20000) }) diff --git a/extension/test/unit/test-trie.test.ts b/extension/test/unit/test-trie.test.ts deleted file mode 100644 index e69de29b..00000000