|
1 | | -import * as fs from 'node:fs' |
2 | | -import * as os from 'node:os' |
3 | | -import * as path from 'node:path' |
4 | | - |
5 | 1 | import { TEST_ID_SEPARATOR } from '../constants.js' |
6 | | -import { |
7 | | - isConfig, |
8 | | - isSpec, |
9 | | - isTestcase, |
10 | | - isWdioTestItem, |
11 | | - type SpecFileTestItem, |
12 | | - type WdioConfigTestItem, |
13 | | - type TestcaseTestItem, |
14 | | -} from '../test/index.js' |
| 2 | +import { isConfig, isSpec, isTestcase, isWdioTestItem } from '../test/index.js' |
15 | 3 | import { log } from '../utils/logger.js' |
16 | 4 |
|
17 | 5 | import type * as vscode from 'vscode' |
18 | 6 | import type { RunTestOptions } from './types.js' |
| 7 | +import type { WdioExtensionWorkerInterface } from './types.js' |
19 | 8 | import type { ResultSet } from '../reporter/types.js' |
| 9 | +import type { SpecFileTestItem, WdioConfigTestItem, TestcaseTestItem } from '../test/index.js' |
20 | 10 |
|
21 | | -export async function runWdio(test: vscode.TestItem) { |
22 | | - if (!isWdioTestItem(test)) { |
23 | | - throw new Error("The metadata for TestItem is not set. This is extension's bug.") |
24 | | - } |
| 11 | +type Listeners = { |
| 12 | + stdout: (data: string) => void |
| 13 | + stderr: (data: string) => void |
| 14 | +} |
25 | 15 |
|
26 | | - if (!isConfig(test) && !isSpec(test) && !isTestcase(test)) { |
27 | | - throw new Error('Workspace TestItem is not valid.') |
28 | | - } |
29 | | - const isCucumberTestItems = checkCucumberTestItems(test) |
| 16 | +export class TestRunner { |
| 17 | + public stdout = '' |
| 18 | + private _stderr = '' |
| 19 | + private _listeners: Listeners | undefined |
30 | 20 |
|
31 | | - const cucumberSpecs = isCucumberTestItems && isTestcase(test) ? getCucumberSpec(test) : undefined |
| 21 | + constructor(private _worker: WdioExtensionWorkerInterface) {} |
| 22 | + public async run(test: vscode.TestItem) { |
| 23 | + if (!isWdioTestItem(test)) { |
| 24 | + throw new Error("The metadata for TestItem is not set. This is extension's bug.") |
| 25 | + } |
32 | 26 |
|
33 | | - const _specs = isSpec(test) || isTestcase(test) ? getSpec(test) : undefined |
| 27 | + if (!isConfig(test) && !isSpec(test) && !isTestcase(test)) { |
| 28 | + throw new Error('Workspace TestItem is not valid.') |
| 29 | + } |
| 30 | + const isCucumberTestItems = checkCucumberTestItems(test) |
34 | 31 |
|
35 | | - const specs = !cucumberSpecs ? _specs : cucumberSpecs |
| 32 | + const cucumberSpecs = isCucumberTestItems && isTestcase(test) ? getCucumberSpec(test) : undefined |
36 | 33 |
|
37 | | - const grep = !isCucumberTestItems && isTestcase(test) ? getGrep(test) : undefined |
38 | | - const range = !isCucumberTestItems && isTestcase(test) ? getRange(test) : undefined |
39 | | - const outputDir = getOutputDir() |
| 34 | + const _specs = isSpec(test) || isTestcase(test) ? getSpec(test) : undefined |
40 | 35 |
|
41 | | - try { |
42 | | - const testOptions: RunTestOptions = { |
43 | | - outputDir, |
44 | | - configPath: test.metadata.repository.wdioConfigPath, |
45 | | - specs, |
46 | | - grep, |
47 | | - range, |
48 | | - } |
| 36 | + const specs = !cucumberSpecs ? _specs : cucumberSpecs |
49 | 37 |
|
50 | | - log.trace(`REQUEST: ${JSON.stringify(testOptions, null, 2)}`) |
51 | | - await test.metadata.repository.worker.ensureConnected() |
52 | | - const result = await test.metadata.repository.worker.rpc.runTest(testOptions) |
| 38 | + const grep = !isCucumberTestItems && isTestcase(test) ? getGrep(test) : undefined |
| 39 | + const range = !isCucumberTestItems && isTestcase(test) ? getRange(test) : undefined |
53 | 40 |
|
54 | | - const resultData = parseJson<ResultSet[]>(result.stdout) |
55 | | - log.trace(`RESULT: ${JSON.stringify(resultData, null, 2)}`) |
| 41 | + try { |
| 42 | + const testOptions: RunTestOptions = { |
| 43 | + configPath: test.metadata.repository.wdioConfigPath, |
| 44 | + specs, |
| 45 | + grep, |
| 46 | + range, |
| 47 | + } |
56 | 48 |
|
57 | | - return { |
58 | | - success: result.success, |
59 | | - duration: 0, |
60 | | - detail: resultData, |
61 | | - errorMessage: result.error, |
| 49 | + log.trace(`REQUEST: ${JSON.stringify(testOptions, null, 2)}`) |
| 50 | + await this._worker.ensureConnected() |
| 51 | + this.setListener() |
| 52 | + const result = await this._worker.rpc.runTest(testOptions) |
| 53 | + this.removeListener() |
| 54 | + |
| 55 | + const resultData = parseJson<ResultSet[]>(result.json) |
| 56 | + log.trace(`RESULT: ${JSON.stringify(resultData, null, 2)}`) |
| 57 | + |
| 58 | + return { |
| 59 | + success: result.success, |
| 60 | + duration: 0, |
| 61 | + detail: resultData, |
| 62 | + log: result.stdout, |
| 63 | + errorMessage: result.error, |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + const _error = error as Error |
| 67 | + return { |
| 68 | + success: false, |
| 69 | + errorMessage: _error.message, |
| 70 | + detail: [], |
| 71 | + } |
62 | 72 | } |
63 | | - } catch (error) { |
64 | | - const _error = error as Error |
65 | | - return { |
66 | | - success: false, |
67 | | - errorMessage: _error.message, |
68 | | - detail: [], |
| 73 | + } |
| 74 | + |
| 75 | + private setListener() { |
| 76 | + this._listeners = { |
| 77 | + stdout: (data: string) => this.stdoutListener(data), |
| 78 | + stderr: (data: string) => this.stderrListener(data), |
69 | 79 | } |
| 80 | + this._worker.on('stdout', this._listeners.stdout) |
| 81 | + this._worker.on('stderr', this._listeners.stderr) |
70 | 82 | } |
71 | | -} |
72 | 83 |
|
73 | | -function getOutputDir() { |
74 | | - const resultRootDir = path.join(os.tmpdir(), 'vscode-webdriverio') |
75 | | - try { |
76 | | - fs.mkdirSync(resultRootDir, { recursive: true }) |
77 | | - const outputDir = fs.mkdtempSync(path.join(resultRootDir, 'result-')) |
78 | | - return outputDir |
79 | | - } catch (error) { |
80 | | - const errorMessage = error instanceof Error ? error.message : String(error) |
81 | | - log.debug(`Failed to create output directory: ${errorMessage}`) |
82 | | - log.debug('Fallback to extract data from stdout.') |
83 | | - return |
| 84 | + private removeListener() { |
| 85 | + if (this._listeners) { |
| 86 | + this._worker.removeListener('stdout', this._listeners.stdout) |
| 87 | + this._worker.removeListener('stderr', this._listeners.stderr) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private stdoutListener(data: string) { |
| 92 | + this.stdout += data + '\n' |
| 93 | + } |
| 94 | + |
| 95 | + private stderrListener(data: string) { |
| 96 | + this._stderr += data + '\n' |
84 | 97 | } |
85 | 98 | } |
86 | 99 |
|
|
0 commit comments