diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f3da14dd..7cc418b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v4 with: - node-version: 18 + node-version-file: '.nvmrc' - name: Install sg run: npm install -g @ast-grep/cli - name: Install deps diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index f0816b0a..cd3f2668 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -17,7 +17,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v4 with: - node-version: 16 + node-version-file: '.nvmrc' - name: Install deps run: npm install - name: Install vsce diff --git a/scripts/test/integration-test.cjs b/scripts/test/integration-test.cjs index d26a6aa0..d7581494 100644 --- a/scripts/test/integration-test.cjs +++ b/scripts/test/integration-test.cjs @@ -2,6 +2,8 @@ const path = require('node:path') const Mocha = require('mocha') const glob = require('glob') +const TESTS_ROOT = path.join(__dirname, '../../tests') + async function run() { await import('tsx') // Create the mocha test @@ -13,7 +15,7 @@ async function run() { }) /** - Before any other tests, run the testSetup.ts file + Before any other tests, run the testSetup.ts file This will set up the test environment necessary for the other tests to run */ mocha.addFile(path.join(__dirname, 'testSetup.ts')) @@ -25,14 +27,13 @@ async function run() { * So you could play with sort order on the tests to investigate */ try { - const testsRoot = path.join(__dirname, '../../src/test') - let files = await glob('**.test.ts', { cwd: testsRoot }) + let files = await glob('**.test.ts', { cwd: TESTS_ROOT }) // Add files to the test suite files.sort((a, b) => { return a.localeCompare(b) }) files.forEach(f => { - mocha.addFile(path.resolve(testsRoot, f)) + mocha.addFile(path.resolve(TESTS_ROOT, f)) }) return new Promise((resolve, reject) => { diff --git a/scripts/test/testSetup.ts b/scripts/test/testSetup.ts index 6d78065b..f3fb915a 100644 --- a/scripts/test/testSetup.ts +++ b/scripts/test/testSetup.ts @@ -1,4 +1,4 @@ -import { activate } from '../../src/test/utils' +import { activate } from '../../tests/utils' /** * This initialization runs once, before the rest of the tests. * It is used to open the test fixture folder and activate the extension. diff --git a/src/extension.ts b/src/extension.ts index 3c9bc985..25fbbf93 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -264,7 +264,7 @@ async function watchEditsToRestartLanguageClient(context: ExtensionContext) { try { // get the absolute URI to this relative path let configUri = Uri.file( - workspace.workspaceFolders![0].uri.fsPath + '/' + configPath + path.join(workspace.workspaceFolders![0].uri.fsPath, configPath) ) // read the config file let configText = await workspace.fs.readFile(configUri) diff --git a/src/test/codefix.test.ts b/tests/codefix.test.ts similarity index 100% rename from src/test/codefix.test.ts rename to tests/codefix.test.ts diff --git a/src/test/diagnostics.test.ts b/tests/diagnostics.test.ts similarity index 100% rename from src/test/diagnostics.test.ts rename to tests/diagnostics.test.ts diff --git a/src/test/fileModifications.test.ts b/tests/fileModifications.test.ts similarity index 86% rename from src/test/fileModifications.test.ts rename to tests/fileModifications.test.ts index 7f7039ae..ab2f7efc 100644 --- a/src/test/fileModifications.test.ts +++ b/tests/fileModifications.test.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode' import * as yaml from 'js-yaml' +import path from 'path' import { getDocUri, @@ -8,16 +9,19 @@ import { assertDiagnosticsEqual } from './utils' -const MAX_WAIT_TIME_FOR_UPDATE = 5000 // ms -const MAX_WAIT_TIME_FOR_INITIAL_DIAGNOSTICS = 10000 // ms +const MAX_WAIT_TIME_FOR_UPDATE = 10000 // ms +const MAX_WAIT_TIME_FOR_INITIAL_DIAGNOSTICS = 15000 // ms const docUri = getDocUri('test.ts') -const diagnosticss = getExpectedDiagnosticss() +const diagnostics = getExpectedDiagnostics() async function writeNewRule() { let vscodeuri = vscode.Uri.file( - vscode.workspace.workspaceFolders![0].uri.fsPath + - '/rules/no-math-random.yml' + path.join( + vscode.workspace.workspaceFolders![0].uri.fsPath, + 'rules', + 'no-math-random.yml' + ) ) await vscode.workspace.fs.writeFile( vscodeuri, @@ -34,14 +38,17 @@ note: no Math.random()`) } async function deleteNewRule() { let vscodeuri = vscode.Uri.file( - vscode.workspace.workspaceFolders![0].uri.fsPath + - '/rules/no-math-random.yml' + path.join( + vscode.workspace.workspaceFolders![0].uri.fsPath, + 'rules', + 'no-math-random.yml' + ) ) await vscode.workspace.fs.delete(vscodeuri) } async function setRuleDirs(newRuleDirs: string[]) { let vscodeuri = vscode.Uri.file( - vscode.workspace.workspaceFolders![0].uri.fsPath + '/sgconfig.yml' + path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, 'sgconfig.yml') ) let content = await vscode.workspace.fs.readFile(vscodeuri) let configText = new TextDecoder().decode(content) @@ -62,7 +69,7 @@ suite('Should update when files change', () => { try { assertDiagnosticsEqual( vscode.languages.getDiagnostics(docUri), - diagnosticss[0] + diagnostics[0] ) } catch (e) { console.warn( @@ -79,7 +86,7 @@ suite('Should update when files change', () => { } assertDiagnosticsEqual( vscode.languages.getDiagnostics(docUri), - diagnosticss[0] + diagnostics[0] ) } }) @@ -88,7 +95,7 @@ suite('Should update when files change', () => { await waitForDiagnosticChange(MAX_WAIT_TIME_FOR_UPDATE) assertDiagnosticsEqual( vscode.languages.getDiagnostics(docUri), - diagnosticss[1] + diagnostics[1] ) }) test('Update on rule deletion', async () => { @@ -96,7 +103,7 @@ suite('Should update when files change', () => { await waitForDiagnosticChange(MAX_WAIT_TIME_FOR_UPDATE) assertDiagnosticsEqual( vscode.languages.getDiagnostics(docUri), - diagnosticss[2] + diagnostics[2] ) }) test('Update on ruleDirs change to nonexistent path', async () => { @@ -109,7 +116,7 @@ suite('Should update when files change', () => { await waitForDiagnosticChange(MAX_WAIT_TIME_FOR_UPDATE) assertDiagnosticsEqual( vscode.languages.getDiagnostics(docUri), - diagnosticss[0] + diagnostics[0] ) }) }) @@ -120,7 +127,7 @@ function toRange(sLine: number, sChar: number, eLine: number, eChar: number) { return new vscode.Range(start, end) } -function getExpectedDiagnosticss() { +function getExpectedDiagnostics() { const full = [ { message: 'No Math.random', diff --git a/src/test/utils.ts b/tests/utils.ts similarity index 96% rename from src/test/utils.ts rename to tests/utils.ts index 29a5e0cb..843a92e7 100644 --- a/src/test/utils.ts +++ b/tests/utils.ts @@ -5,6 +5,8 @@ import * as assert from 'assert' export let doc: vscode.TextDocument export let editor: vscode.TextEditor +const FIXTURE_PATH = path.resolve(__dirname, '../fixture') + /** * Prepare the vscode environment for testing by opening the fixture folder * and activating the extension. @@ -15,9 +17,7 @@ export let editor: vscode.TextEditor export async function activate() { // The extensionId is `publisher.name` from package.json const ext = vscode.extensions.getExtension('ast-grep.ast-grep-vscode')! - const fixtureFolderUri = vscode.Uri.file( - path.resolve(__dirname, '../../fixture') - ) + const fixtureFolderUri = vscode.Uri.file(FIXTURE_PATH) await ext.activate() try { // open ast-grep project to locate sgconfig.yml @@ -118,7 +118,7 @@ async function sleep(ms: number) { } export const getDocPath = (p: string) => { - return path.resolve(__dirname, '../../fixture', p) + return path.resolve(FIXTURE_PATH, p) } export const getDocUri = (p: string) => { return vscode.Uri.file(getDocPath(p)) diff --git a/tsconfig.json b/tsconfig.json index a5bf525e..05890041 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "target": "es2020", "lib": ["es2020", "DOM"], "outDir": "out", - "rootDir": "src", + "rootDir": ".", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, @@ -12,6 +12,6 @@ "skipLibCheck": true, "jsx": "react-jsx" }, - "include": ["src"], + "include": ["src", "tests"], "exclude": ["node_modules", ".vscode-test"] }