Skip to content

Commit 7823cd3

Browse files
committed
feat(tests): enhance test runner with grep and specific file filtering
1 parent 50fcef4 commit 7823cd3

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

apps/vscode-e2e/src/runTest.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as path from "path"
2+
import * as os from "os"
3+
import * as fs from "fs/promises"
24

35
import { runTests } from "@vscode/test-electron"
46

@@ -12,10 +14,36 @@ async function main() {
1214
// Passed to --extensionTestsPath
1315
const extensionTestsPath = path.resolve(__dirname, "./suite/index")
1416

17+
// Create a temporary workspace folder for tests
18+
const testWorkspace = await fs.mkdtemp(path.join(os.tmpdir(), "roo-test-workspace-"))
19+
20+
// Get test filter from command line arguments or environment variable
21+
// Usage examples:
22+
// - npm run test:e2e -- --grep "write-to-file"
23+
// - TEST_GREP="apply-diff" npm run test:e2e
24+
// - TEST_FILE="task.test.js" npm run test:e2e
25+
const testGrep = process.argv.find((arg, i) => process.argv[i - 1] === "--grep") || process.env.TEST_GREP
26+
const testFile = process.argv.find((arg, i) => process.argv[i - 1] === "--file") || process.env.TEST_FILE
27+
28+
// Pass test filters as environment variables to the test runner
29+
const extensionTestsEnv = {
30+
...process.env,
31+
...(testGrep && { TEST_GREP: testGrep }),
32+
...(testFile && { TEST_FILE: testFile }),
33+
}
34+
1535
// Download VS Code, unzip it and run the integration test
16-
await runTests({ extensionDevelopmentPath, extensionTestsPath })
17-
} catch {
18-
console.error("Failed to run tests")
36+
await runTests({
37+
extensionDevelopmentPath,
38+
extensionTestsPath,
39+
launchArgs: [testWorkspace],
40+
extensionTestsEnv,
41+
})
42+
43+
// Clean up the temporary workspace
44+
await fs.rm(testWorkspace, { recursive: true, force: true })
45+
} catch (error) {
46+
console.error("Failed to run tests", error)
1947
process.exit(1)
2048
}
2149
}

apps/vscode-e2e/src/suite/index.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,40 @@ export async function run() {
2727

2828
globalThis.api = api
2929

30-
// Add all the tests to the runner.
31-
const mocha = new Mocha({ ui: "tdd", timeout: 300_000 })
30+
// Configure Mocha with grep pattern if provided
31+
const mochaOptions: Mocha.MochaOptions = {
32+
ui: "tdd",
33+
timeout: 300_000,
34+
}
35+
36+
// Apply grep filter if TEST_GREP is set
37+
if (process.env.TEST_GREP) {
38+
mochaOptions.grep = process.env.TEST_GREP
39+
console.log(`Running tests matching pattern: ${process.env.TEST_GREP}`)
40+
}
41+
42+
const mocha = new Mocha(mochaOptions)
3243
const cwd = path.resolve(__dirname, "..")
33-
;(await glob("**/**.test.js", { cwd })).forEach((testFile) => mocha.addFile(path.resolve(cwd, testFile)))
44+
45+
// Get test files based on filter
46+
let testFiles: string[]
47+
if (process.env.TEST_FILE) {
48+
// Run specific test file
49+
const specificFile = process.env.TEST_FILE.endsWith(".js")
50+
? process.env.TEST_FILE
51+
: `${process.env.TEST_FILE}.js`
52+
testFiles = await glob(`**/${specificFile}`, { cwd })
53+
console.log(`Running specific test file: ${specificFile}`)
54+
} else {
55+
// Run all test files
56+
testFiles = await glob("**/**.test.js", { cwd })
57+
}
58+
59+
if (testFiles.length === 0) {
60+
throw new Error(`No test files found matching criteria: ${process.env.TEST_FILE || "all tests"}`)
61+
}
62+
63+
testFiles.forEach((testFile) => mocha.addFile(path.resolve(cwd, testFile)))
3464

3565
// Let's go!
3666
return new Promise<void>((resolve, reject) =>

0 commit comments

Comments
 (0)