Skip to content

Commit 95a09a9

Browse files
Factor out internal.scripts.testing_tools.run_adapter().
1 parent b8d9b71 commit 95a09a9

File tree

4 files changed

+79
-50
lines changed

4 files changed

+79
-50
lines changed

src/client/common/process/internal.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,61 @@ export namespace scripts {
306306
// There is no output to parse, so we do not return a function.
307307
return [script, ...testArgs];
308308
}
309+
310+
//============================
311+
// testing_tools/
312+
313+
export namespace testing_tools {
314+
const _SCRIPTS_DIR = path.join(SCRIPTS_DIR, 'testing_tools');
315+
316+
type TestNode = {
317+
id: string;
318+
name: string;
319+
parentid: string;
320+
};
321+
type TestParent = TestNode & {
322+
kind: 'folder' | 'file' | 'suite' | 'function';
323+
};
324+
type TestFSNode = TestParent & {
325+
kind: 'folder' | 'file';
326+
relpath: string;
327+
};
328+
329+
export type TestFolder = TestFSNode & {
330+
kind: 'folder';
331+
};
332+
export type TestFile = TestFSNode & {
333+
kind: 'file';
334+
};
335+
export type TestSuite = TestParent & {
336+
kind: 'suite';
337+
};
338+
// function-as-a-container is for parameterized ("sub") tests.
339+
export type TestFunction = TestParent & {
340+
kind: 'function';
341+
};
342+
export type Test = TestNode & {
343+
source: string;
344+
};
345+
export type DiscoveredTests = {
346+
rootid: string;
347+
root: string;
348+
parents: TestParent[];
349+
tests: Test[];
350+
};
351+
352+
//============================
353+
// run_adapter.py
354+
355+
export function run_adapter(adapterArgs: string[]): [string[], (out: string) => DiscoveredTests[]] {
356+
const script = path.join(_SCRIPTS_DIR, 'run_adapter.py');
357+
const args = [script, ...adapterArgs];
358+
359+
function parse(out: string): DiscoveredTests[] {
360+
return JSON.parse(out);
361+
}
362+
363+
return [args, parse];
364+
}
365+
}
309366
}

src/client/testing/common/services/discovery.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@
44
'use strict';
55

66
import { inject, injectable, named } from 'inversify';
7-
import * as path from 'path';
87
import { OutputChannel } from 'vscode';
98
import { traceError } from '../../../common/logger';
9+
import { scripts as internalScripts } from '../../../common/process/internal';
1010
import {
1111
ExecutionFactoryCreateWithEnvironmentOptions,
12-
ExecutionResult,
1312
IPythonExecutionFactory,
1413
SpawnOptions
1514
} from '../../../common/process/types';
1615
import { IOutputChannel } from '../../../common/types';
17-
import { EXTENSION_ROOT_DIR } from '../../../constants';
1816
import { captureTelemetry } from '../../../telemetry';
1917
import { EventName } from '../../../telemetry/constants';
2018
import { TEST_OUTPUT_CHANNEL } from '../constants';
2119
import { ITestDiscoveryService, TestDiscoveryOptions, Tests } from '../types';
2220
import { DiscoveredTests, ITestDiscoveredTestParser } from './types';
2321

24-
const DISCOVERY_FILE = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'testing_tools', 'run_adapter.py');
25-
2622
@injectable()
2723
export class TestsDiscoveryService implements ITestDiscoveryService {
2824
constructor(
@@ -32,20 +28,19 @@ export class TestsDiscoveryService implements ITestDiscoveryService {
3228
) {}
3329
@captureTelemetry(EventName.UNITTEST_DISCOVER_WITH_PYCODE, undefined, true)
3430
public async discoverTests(options: TestDiscoveryOptions): Promise<Tests> {
35-
let output: ExecutionResult<string> | undefined;
3631
try {
37-
output = await this.exec(options);
38-
const discoveredTests = JSON.parse(output.stdout) as DiscoveredTests[];
32+
const discoveredTests = await this.exec(options);
3933
return this.parser.parse(options.workspaceFolder, discoveredTests);
4034
} catch (ex) {
41-
if (output) {
42-
traceError('Failed to parse discovered Test', new Error(output.stdout));
35+
if (ex.stdout) {
36+
traceError('Failed to parse discovered Test', new Error(ex.stdout));
4337
}
4438
traceError('Failed to parse discovered Test', ex);
4539
throw ex;
4640
}
4741
}
48-
public async exec(options: TestDiscoveryOptions): Promise<ExecutionResult<string>> {
42+
public async exec(options: TestDiscoveryOptions): Promise<DiscoveredTests[]> {
43+
const [args, parse] = internalScripts.testing_tools.run_adapter(options.args);
4944
const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = {
5045
allowEnvironmentFetchExceptions: false,
5146
resource: options.workspaceFolder
@@ -56,8 +51,13 @@ export class TestsDiscoveryService implements ITestDiscoveryService {
5651
cwd: options.cwd,
5752
throwOnStdErr: true
5853
};
59-
const argv = [DISCOVERY_FILE, ...options.args];
60-
this.outChannel.appendLine(`python ${argv.join(' ')}`);
61-
return execService.exec(argv, spawnOptions);
54+
this.outChannel.appendLine(`python ${args.join(' ')}`);
55+
const proc = await execService.exec(args, spawnOptions);
56+
try {
57+
return parse(proc.stdout);
58+
} catch (ex) {
59+
ex.stdout = proc.stdout;
60+
throw ex; // re-throw
61+
}
6262
}
6363
}

src/client/testing/common/services/types.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,15 @@
44
'use strict';
55

66
import { Uri } from 'vscode';
7+
import { scripts as internalScripts } from '../../../common/process/internal';
78
import { Tests } from '../types';
89

9-
export type TestNode = {
10-
id: string;
11-
name: string;
12-
parentid: string;
13-
};
14-
export type TestParent = TestNode & {
15-
kind: 'folder' | 'file' | 'suite' | 'function';
16-
};
17-
export type TestFSNode = TestParent & {
18-
kind: 'folder' | 'file';
19-
relpath: string;
20-
};
21-
export type TestFolder = TestFSNode & {
22-
kind: 'folder';
23-
};
24-
export type TestFile = TestFSNode & {
25-
kind: 'file';
26-
};
27-
export type TestSuite = TestParent & {
28-
kind: 'suite';
29-
};
30-
// function-as-a-container is for parameterized ("sub") tests.
31-
export type TestFunction = TestParent & {
32-
kind: 'function';
33-
};
34-
export type Test = TestNode & {
35-
source: string;
36-
};
37-
export type DiscoveredTests = {
38-
rootid: string;
39-
root: string;
40-
parents: TestParent[];
41-
tests: Test[];
42-
};
10+
export type DiscoveredTests = internalScripts.testing_tools.DiscoveredTests;
11+
export type Test = internalScripts.testing_tools.Test;
12+
export type TestFolder = internalScripts.testing_tools.TestFolder;
13+
export type TestFile = internalScripts.testing_tools.TestFile;
14+
export type TestSuite = internalScripts.testing_tools.TestSuite;
15+
export type TestFunction = internalScripts.testing_tools.TestFunction;
4316

4417
export const ITestDiscoveredTestParser = Symbol('ITestDiscoveredTestParser');
4518
export interface ITestDiscoveredTestParser {

src/test/testing/common/services/discovery.unit.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ suite('Unit Tests - Common Discovery', () => {
4646
};
4747
const discoveredTests: DiscoveredTests[] = [{ hello: 1 } as any];
4848
const parsedResult = ({ done: true } as any) as Tests;
49-
const json = JSON.stringify(discoveredTests);
50-
discovery.exec = () => Promise.resolve({ stdout: json });
49+
discovery.exec = () => Promise.resolve(discoveredTests);
5150
when(parser.parse(options.workspaceFolder, deepEqual(discoveredTests))).thenResolve(parsedResult as any);
5251

5352
const tests = await discovery.discoverTests(options);

0 commit comments

Comments
 (0)