Skip to content

Commit b771c0e

Browse files
authored
add tests for unittest execution adapter (#21017)
1 parent 6fbfde9 commit b771c0e

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as assert from 'assert';
5+
import * as path from 'path';
6+
import { Uri } from 'vscode';
7+
import { IConfigurationService } from '../../../../client/common/types';
8+
import { EXTENSION_ROOT_DIR } from '../../../../client/constants';
9+
import { ITestServer, TestCommandOptions } from '../../../../client/testing/testController/common/types';
10+
import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter';
11+
12+
suite('Unittest test execution adapter', () => {
13+
let stubConfigSettings: IConfigurationService;
14+
15+
setup(() => {
16+
stubConfigSettings = ({
17+
getSettings: () => ({
18+
testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] },
19+
}),
20+
} as unknown) as IConfigurationService;
21+
});
22+
23+
test('runTests should send the run command to the test server', async () => {
24+
let options: TestCommandOptions | undefined;
25+
26+
const stubTestServer = ({
27+
sendCommand(opt: TestCommandOptions): Promise<void> {
28+
options = opt;
29+
return Promise.resolve();
30+
},
31+
onDataReceived: () => {
32+
// no body
33+
},
34+
createUUID: () => '123456789',
35+
} as unknown) as ITestServer;
36+
37+
const uri = Uri.file('/foo/bar');
38+
const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py');
39+
40+
const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings);
41+
adapter.runTests(uri, [], false);
42+
43+
const expectedOptions: TestCommandOptions = {
44+
workspaceFolder: uri,
45+
command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] },
46+
cwd: uri.fsPath,
47+
uuid: '123456789',
48+
debugBool: false,
49+
testIds: [],
50+
};
51+
52+
assert.deepStrictEqual(options, expectedOptions);
53+
});
54+
test("onDataReceivedHandler should parse the data if the cwd from the payload matches the test adapter's cwd", async () => {
55+
const stubTestServer = ({
56+
sendCommand(): Promise<void> {
57+
return Promise.resolve();
58+
},
59+
onDataReceived: () => {
60+
// no body
61+
},
62+
createUUID: () => '123456789',
63+
} as unknown) as ITestServer;
64+
65+
const uri = Uri.file('/foo/bar');
66+
const data = { status: 'success' };
67+
const uuid = '123456789';
68+
69+
const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings);
70+
71+
// triggers runTests flow which will run onDataReceivedHandler and the
72+
// promise resolves into the parsed data.
73+
const promise = adapter.runTests(uri, [], false);
74+
75+
adapter.onDataReceivedHandler({ uuid, data: JSON.stringify(data) });
76+
77+
const result = await promise;
78+
79+
assert.deepStrictEqual(result, data);
80+
});
81+
test("onDataReceivedHandler should ignore the data if the cwd from the payload does not match the test adapter's cwd", async () => {
82+
const correctUuid = '123456789';
83+
const incorrectUuid = '987654321';
84+
const stubTestServer = ({
85+
sendCommand(): Promise<void> {
86+
return Promise.resolve();
87+
},
88+
onDataReceived: () => {
89+
// no body
90+
},
91+
createUUID: () => correctUuid,
92+
} as unknown) as ITestServer;
93+
94+
const uri = Uri.file('/foo/bar');
95+
96+
const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings);
97+
98+
// triggers runTests flow which will run onDataReceivedHandler and the
99+
// promise resolves into the parsed data.
100+
const promise = adapter.runTests(uri, [], false);
101+
102+
const data = { status: 'success' };
103+
// will not resolve due to incorrect UUID
104+
adapter.onDataReceivedHandler({ uuid: incorrectUuid, data: JSON.stringify(data) });
105+
106+
const nextData = { status: 'error' };
107+
// will resolve and nextData will be returned as result
108+
adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) });
109+
110+
const result = await promise;
111+
112+
assert.deepStrictEqual(result, nextData);
113+
});
114+
});

0 commit comments

Comments
 (0)