From c12be8daf60cd3d5554e9f5465aa5e8a5187d288 Mon Sep 17 00:00:00 2001 From: Kirill Dubovitskiy Date: Mon, 7 Aug 2023 14:54:37 -0700 Subject: Refactor test scripts and update build configurations - Added a new esbuild for: - bundling test runners (run tests on vscode + mocha runner) - bundling all tests in the project - Some opinionated renamings / moving test related things around - Changed output directory in tsconfig to avoid overriding the entire out directory - bundles tests also go there Some poking around trying to deprecate jest as it is not used and mocha is more often used for vscode extension testing. Though I assume jest was added to test GUI within extension. This work was started because there are compilation issues with the current setup and neither of the tests are actually working so figured getting one to a working spot is a good start --- extension/src/__mocks__/vscode.ts | 7 --- .../src/activation/test/environmentSetup.test.ts | 70 ---------------------- extension/src/continueIdeClient.ts | 4 +- extension/src/terminal/terminalEmulator.ts | 2 +- extension/src/test-runner/mochaRunner.ts | 35 +++++++++++ extension/src/test-runner/runTestOnVSCodeHost.ts | 24 ++++++++ extension/src/test-suite/environmentSetup.test.ts | 19 ++++++ extension/src/test-suite/extension.test.ts | 16 +++++ extension/src/test-suite/util.test.ts | 18 ++++++ extension/src/test/runTest.ts | 23 ------- extension/src/test/suite/extension.test.ts | 16 ----- extension/src/test/suite/index.ts | 38 ------------ extension/src/test/suite/util.test.ts | 18 ------ 13 files changed, 115 insertions(+), 175 deletions(-) delete mode 100644 extension/src/__mocks__/vscode.ts delete mode 100644 extension/src/activation/test/environmentSetup.test.ts create mode 100644 extension/src/test-runner/mochaRunner.ts create mode 100644 extension/src/test-runner/runTestOnVSCodeHost.ts create mode 100644 extension/src/test-suite/environmentSetup.test.ts create mode 100644 extension/src/test-suite/extension.test.ts create mode 100644 extension/src/test-suite/util.test.ts delete mode 100644 extension/src/test/runTest.ts delete mode 100644 extension/src/test/suite/extension.test.ts delete mode 100644 extension/src/test/suite/index.ts delete mode 100644 extension/src/test/suite/util.test.ts (limited to 'extension/src') diff --git a/extension/src/__mocks__/vscode.ts b/extension/src/__mocks__/vscode.ts deleted file mode 100644 index d415b5a0..00000000 --- a/extension/src/__mocks__/vscode.ts +++ /dev/null @@ -1,7 +0,0 @@ -const vscode = { - window: { - onDidChangeVisibleTextEditors: jest.fn(), - }, -}; - -module.exports = vscode; diff --git a/extension/src/activation/test/environmentSetup.test.ts b/extension/src/activation/test/environmentSetup.test.ts deleted file mode 100644 index ca487416..00000000 --- a/extension/src/activation/test/environmentSetup.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -const child_process = require("child_process"); -import { platform } from "os"; -import { getPythonPipCommands } from "../environmentSetup"; - -jest.mock("os"); -jest.mock("child_process"); - -function mockPythonVersionMappings(mappings: { [pythonCmd: string]: string }) { - (child_process.exec as jest.Mock).mockImplementation( - (command: string, options: any) => { - const pythonCmd = command.split(" ")[0]; - if (pythonCmd in mappings) { - return Promise.resolve([mappings[pythonCmd], ""]); - } else { - return Promise.resolve(["", stubStderr]); - } - } - ); -} - -const stubStderr = - "This is a stub stderr, but will be checked only for existence."; -describe("getPythonPipCommands", () => { - describe("on Windows", () => { - it("should return the correct Python and Pip commands", async () => { - (platform as jest.Mock).mockReturnValue("win32"); - mockPythonVersionMappings({ - python: "Python 3.8.0", - }); - - const [pythonCmd, pipCmd] = await getPythonPipCommands(); - - expect(pythonCmd).toBe("python"); - expect(pipCmd).toBe("pip"); - - jest.restoreAllMocks(); - }); - describe("on MacOS", () => { - (platform as jest.Mock).mockReturnValue("darwin"); - it("should check through all python versions after finding 3.7", async () => { - mockPythonVersionMappings({ - python: "", - python3: "Python 3.7.0", - "python3.11": "Python 3.11.0", - }); - - const [pythonCmd, pipCmd] = await getPythonPipCommands(); - - expect(pythonCmd).toBe("python3.11"); - expect(pipCmd).toBe("pip3.11"); - - jest.restoreAllMocks(); - }); - - it("should use python3 if that maps to valid version", async () => { - mockPythonVersionMappings({ - python: "", - python3: "Python 3.8.0", - }); - - const [pythonCmd, pipCmd] = await getPythonPipCommands(); - - expect(pythonCmd).toBe("python3"); - expect(pipCmd).toBe("pip3"); - - jest.restoreAllMocks(); - }); - }); - }); -}); diff --git a/extension/src/continueIdeClient.ts b/extension/src/continueIdeClient.ts index cb7baaa6..d89093ca 100644 --- a/extension/src/continueIdeClient.ts +++ b/extension/src/continueIdeClient.ts @@ -12,7 +12,7 @@ import { rejectSuggestionCommand, } from "./suggestions"; import { FileEditWithFullContents } from "../schema/FileEditWithFullContents"; -import fs = require("fs"); +import * as fs from 'fs'; import { WebsocketMessenger } from "./util/messenger"; import { diffManager } from "./diffs"; const os = require("os"); @@ -383,7 +383,7 @@ class IdeProtocolClient { async getUserSecret(key: string) { // Check if secret already exists in VS Code settings (global) let secret = vscode.workspace.getConfiguration("continue").get(key); - if (typeof secret !== "undefined" && secret !== null) return secret; + if (typeof secret !== "undefined" && secret !== null) {return secret;} // If not, ask user for secret secret = await vscode.window.showInputBox({ diff --git a/extension/src/terminal/terminalEmulator.ts b/extension/src/terminal/terminalEmulator.ts index bab59c78..3b90f9f8 100644 --- a/extension/src/terminal/terminalEmulator.ts +++ b/extension/src/terminal/terminalEmulator.ts @@ -1,7 +1,7 @@ /* Terminal emulator - commented because node-pty is causing problems. */ import * as vscode from "vscode"; -import os = require("os"); +import * as os from 'os'; import stripAnsi from "strip-ansi"; import { longestCommonSubsequence } from "../util/lcs"; diff --git a/extension/src/test-runner/mochaRunner.ts b/extension/src/test-runner/mochaRunner.ts new file mode 100644 index 00000000..b964fe5f --- /dev/null +++ b/extension/src/test-runner/mochaRunner.ts @@ -0,0 +1,35 @@ +import * as path from "path"; +import Mocha from "mocha"; +import * as glob from "glob"; + +export function run() { + // Create the mocha test + const mocha = new Mocha({ + ui: "tdd", + color: true, + }); + + // See esbuild.test.mjs for more details + // Assumes this file is in out/test-runner/mochaRunner.js + const compiledTestSuitesDirectory = path.resolve(__dirname, "../test-suites"); + + glob.sync("**/**.test.js", { cwd: compiledTestSuitesDirectory }).forEach((file) => { + mocha.addFile(path.resolve(compiledTestSuitesDirectory, file)); + }); + + return new Promise((c, e) => { + try { + // Run the mocha test + mocha.run((failures) => { + if (failures > 0) { + e(new Error(`${failures} tests failed.`)); + } else { + c(); + } + }); + } catch (err) { + console.error(err); + e(err); + } + }); +} diff --git a/extension/src/test-runner/runTestOnVSCodeHost.ts b/extension/src/test-runner/runTestOnVSCodeHost.ts new file mode 100644 index 00000000..2a542ffc --- /dev/null +++ b/extension/src/test-runner/runTestOnVSCodeHost.ts @@ -0,0 +1,24 @@ +import { runTests } from "@vscode/test-electron"; +import * as path from "path"; + +async function main() { + try { + // The folder containing the Extension Manifest package.json + // Passed to `--extensionDevelopmentPath` + + // Assumes this file is in out/test-runner/runTestOnVSCodeHost.js + const extensionDevelopmentPath = path.resolve(__dirname, "../../"); + + // The path to test runner + // Passed to --extensionTestsPath + const extensionTestsPath = path.resolve(extensionDevelopmentPath, "out/test-runner/mochaRunner"); + + // Download VS Code, unzip it and run the integration test + await runTests({ extensionDevelopmentPath, extensionTestsPath }); + } catch (err) { + console.error("Failed to run tests", err); + process.exit(1); + } +} + +main(); diff --git a/extension/src/test-suite/environmentSetup.test.ts b/extension/src/test-suite/environmentSetup.test.ts new file mode 100644 index 00000000..7610e70d --- /dev/null +++ b/extension/src/test-suite/environmentSetup.test.ts @@ -0,0 +1,19 @@ +import { test, describe } from "mocha"; +import * as assert from "assert"; + +import { getContinueServerUrl } from "../bridge"; +import { startContinuePythonServer } from "../activation/environmentSetup"; +import fetch from "node-fetch"; + +describe("Can start python server", () => { + test("Can start python server", async () => { + await startContinuePythonServer(); + + await new Promise((resolve) => setTimeout(resolve, 50)); + + // Check if server is running + const serverUrl = getContinueServerUrl(); + const response = await fetch(`${serverUrl}/health`); + assert.equal(response.status, 200); + }); +}); diff --git a/extension/src/test-suite/extension.test.ts b/extension/src/test-suite/extension.test.ts new file mode 100644 index 00000000..890820b2 --- /dev/null +++ b/extension/src/test-suite/extension.test.ts @@ -0,0 +1,16 @@ +import { test, describe } from "mocha"; +import * as assert from "assert"; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from "vscode"; +// import * as myExtension from '../../extension'; + +describe("Extension Test Suite", () => { + vscode.window.showInformationMessage("Start all tests."); + + test("Sample test", () => { + assert.strictEqual(-1, [1, 2, 3].indexOf(5)); + assert.strictEqual(-1, [1, 2, 3].indexOf(0)); + }); +}); diff --git a/extension/src/test-suite/util.test.ts b/extension/src/test-suite/util.test.ts new file mode 100644 index 00000000..2b301b0c --- /dev/null +++ b/extension/src/test-suite/util.test.ts @@ -0,0 +1,18 @@ +import { test, describe } from "mocha"; +import assert from "assert"; +import { convertSingleToDoubleQuoteJSON } from "../util/util"; + +describe("utils.ts", () => { + test("convertSingleToDoubleQuoteJson", () => { + let pairs = [ + [`{'a': 'b'}`, `{"a": "b"}`], + [`{'a': "b", "c": 'd'}`, `{"a": "b", "c": "d"}`], + [`{'a': '\\'"'}`, `{"a": "'\\""}`], + ]; + for (let pair of pairs) { + let result = convertSingleToDoubleQuoteJSON(pair[0]); + assert(result === pair[1]); + JSON.parse(result); + } + }); +}); diff --git a/extension/src/test/runTest.ts b/extension/src/test/runTest.ts deleted file mode 100644 index e810ed5b..00000000 --- a/extension/src/test/runTest.ts +++ /dev/null @@ -1,23 +0,0 @@ -import * as path from "path"; - -import { runTests } from "@vscode/test-electron"; - -async function main() { - try { - // The folder containing the Extension Manifest package.json - // Passed to `--extensionDevelopmentPath` - const extensionDevelopmentPath = path.resolve(__dirname, "../../"); - - // The path to test runner - // Passed to --extensionTestsPath - const extensionTestsPath = path.resolve(__dirname, "./suite/index"); - - // Download VS Code, unzip it and run the integration test - await runTests({ extensionDevelopmentPath, extensionTestsPath }); - } catch (err) { - console.error("Failed to run tests"); - process.exit(1); - } -} - -main(); diff --git a/extension/src/test/suite/extension.test.ts b/extension/src/test/suite/extension.test.ts deleted file mode 100644 index 890820b2..00000000 --- a/extension/src/test/suite/extension.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { test, describe } from "mocha"; -import * as assert from "assert"; - -// You can import and use all API from the 'vscode' module -// as well as import your extension to test it -import * as vscode from "vscode"; -// import * as myExtension from '../../extension'; - -describe("Extension Test Suite", () => { - vscode.window.showInformationMessage("Start all tests."); - - test("Sample test", () => { - assert.strictEqual(-1, [1, 2, 3].indexOf(5)); - assert.strictEqual(-1, [1, 2, 3].indexOf(0)); - }); -}); diff --git a/extension/src/test/suite/index.ts b/extension/src/test/suite/index.ts deleted file mode 100644 index 772a0152..00000000 --- a/extension/src/test/suite/index.ts +++ /dev/null @@ -1,38 +0,0 @@ -import * as path from "path"; -import * as Mocha from "mocha"; -import * as glob from "glob"; - -export function run(): Promise { - // Create the mocha test - const mocha = new Mocha({ - ui: "tdd", - color: true, - }); - - const testsRoot = path.resolve(__dirname, ".."); - - return new Promise((c, e) => { - glob("**/**.test.js", { cwd: testsRoot }, (err, files) => { - if (err) { - return e(err); - } - - // Add files to the test suite - files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f))); - - try { - // Run the mocha test - mocha.run((failures: any) => { - if (failures > 0) { - e(new Error(`${failures} tests failed.`)); - } else { - c(); - } - }); - } catch (err) { - console.error(err); - e(err); - } - }); - }); -} diff --git a/extension/src/test/suite/util.test.ts b/extension/src/test/suite/util.test.ts deleted file mode 100644 index 0ba1473b..00000000 --- a/extension/src/test/suite/util.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { test, describe } from "mocha"; -import * as assert from "assert"; -import { convertSingleToDoubleQuoteJSON } from "../../util/util"; - -describe("utils.ts", () => { - test("convertSingleToDoubleQuoteJson", () => { - let pairs = [ - [`{'a': 'b'}`, `{"a": "b"}`], - [`{'a': "b", "c": 'd'}`, `{"a": "b", "c": "d"}`], - [`{'a': '\\'"'}`, `{"a": "'\\""}`], - ]; - for (let pair of pairs) { - let result = convertSingleToDoubleQuoteJSON(pair[0]); - assert(result === pair[1]); - JSON.parse(result); - } - }); -}); -- cgit v1.2.3-70-g09d2