Skip to content

add tests for unittest execution adapter #21017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as assert from 'assert';
import * as path from 'path';
import { Uri } from 'vscode';
import { IConfigurationService } from '../../../../client/common/types';
import { EXTENSION_ROOT_DIR } from '../../../../client/constants';
import { ITestServer, TestCommandOptions } from '../../../../client/testing/testController/common/types';
import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter';

suite('Unittest test execution adapter', () => {
let stubConfigSettings: IConfigurationService;

setup(() => {
stubConfigSettings = ({
getSettings: () => ({
testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] },
}),
} as unknown) as IConfigurationService;
});

test('runTests should send the run command to the test server', async () => {
let options: TestCommandOptions | undefined;

const stubTestServer = ({
sendCommand(opt: TestCommandOptions): Promise<void> {
options = opt;
return Promise.resolve();
},
onDataReceived: () => {
// no body
},
createUUID: () => '123456789',
} as unknown) as ITestServer;

const uri = Uri.file('/foo/bar');
const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py');

const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings);
adapter.runTests(uri, [], false);

const expectedOptions: TestCommandOptions = {
workspaceFolder: uri,
command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] },
cwd: uri.fsPath,
uuid: '123456789',
debugBool: false,
testIds: [],
};

assert.deepStrictEqual(options, expectedOptions);
});
test("onDataReceivedHandler should parse the data if the cwd from the payload matches the test adapter's cwd", async () => {
const stubTestServer = ({
sendCommand(): Promise<void> {
return Promise.resolve();
},
onDataReceived: () => {
// no body
},
createUUID: () => '123456789',
} as unknown) as ITestServer;

const uri = Uri.file('/foo/bar');
const data = { status: 'success' };
const uuid = '123456789';

const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings);

// triggers runTests flow which will run onDataReceivedHandler and the
// promise resolves into the parsed data.
const promise = adapter.runTests(uri, [], false);

adapter.onDataReceivedHandler({ uuid, data: JSON.stringify(data) });

const result = await promise;

assert.deepStrictEqual(result, data);
});
test("onDataReceivedHandler should ignore the data if the cwd from the payload does not match the test adapter's cwd", async () => {
const correctUuid = '123456789';
const incorrectUuid = '987654321';
const stubTestServer = ({
sendCommand(): Promise<void> {
return Promise.resolve();
},
onDataReceived: () => {
// no body
},
createUUID: () => correctUuid,
} as unknown) as ITestServer;

const uri = Uri.file('/foo/bar');

const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings);

// triggers runTests flow which will run onDataReceivedHandler and the
// promise resolves into the parsed data.
const promise = adapter.runTests(uri, [], false);

const data = { status: 'success' };
// will not resolve due to incorrect UUID
adapter.onDataReceivedHandler({ uuid: incorrectUuid, data: JSON.stringify(data) });

const nextData = { status: 'error' };
// will resolve and nextData will be returned as result
adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) });

const result = await promise;

assert.deepStrictEqual(result, nextData);
});
});