|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License. |
| 4 | +import { TestController, TestRun, TestRunProfileKind, Uri } from 'vscode'; |
| 5 | +import * as typeMoq from 'typemoq'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as assert from 'assert'; |
| 8 | +import * as fs from 'fs'; |
| 9 | +import * as sinon from 'sinon'; |
| 10 | +import { ITestController, ITestResultResolver } from '../../../client/testing/testController/common/types'; |
| 11 | +import { IPythonExecutionFactory } from '../../../client/common/process/types'; |
| 12 | +import { IConfigurationService, ITestOutputChannel } from '../../../client/common/types'; |
| 13 | +import { IServiceContainer } from '../../../client/ioc/types'; |
| 14 | +import { EXTENSION_ROOT_DIR_FOR_TESTS, initialize } from '../../initialize'; |
| 15 | +import { traceError, traceLog } from '../../../client/logging'; |
| 16 | +import { UnittestTestExecutionAdapter } from '../../../client/testing/testController/unittest/testExecutionAdapter'; |
| 17 | +import { PythonResultResolver } from '../../../client/testing/testController/common/resultResolver'; |
| 18 | +import { TestProvider } from '../../../client/testing/types'; |
| 19 | +import { UNITTEST_PROVIDER } from '../../../client/testing/common/constants'; |
| 20 | +import { IEnvironmentVariablesProvider } from '../../../client/common/variables/types'; |
| 21 | +import { createTypeMoq } from '../../mocks/helper'; |
| 22 | +import * as pixi from '../../../client/pythonEnvironments/common/environmentManagers/pixi'; |
| 23 | + |
| 24 | +suite('End to End Tests: unittest adapters', () => { |
| 25 | + let resultResolver: ITestResultResolver; |
| 26 | + let pythonExecFactory: IPythonExecutionFactory; |
| 27 | + let configService: IConfigurationService; |
| 28 | + let serviceContainer: IServiceContainer; |
| 29 | + let envVarsService: IEnvironmentVariablesProvider; |
| 30 | + let workspaceUri: Uri; |
| 31 | + let testOutputChannel: typeMoq.IMock<ITestOutputChannel>; |
| 32 | + let testController: TestController; |
| 33 | + let getPixiStub: sinon.SinonStub; |
| 34 | + const unittestProvider: TestProvider = UNITTEST_PROVIDER; |
| 35 | + const rootPathSmallWorkspace = path.join( |
| 36 | + EXTENSION_ROOT_DIR_FOR_TESTS, |
| 37 | + 'src', |
| 38 | + 'testTestingRootWkspc', |
| 39 | + 'smallWorkspace', |
| 40 | + ); |
| 41 | + // const rootPathLargeWorkspace = path.join( |
| 42 | + // EXTENSION_ROOT_DIR_FOR_TESTS, |
| 43 | + // 'src', |
| 44 | + // 'testTestingRootWkspc', |
| 45 | + // 'largeWorkspace', |
| 46 | + // ); |
| 47 | + const rootPathErrorWorkspace = path.join( |
| 48 | + EXTENSION_ROOT_DIR_FOR_TESTS, |
| 49 | + 'src', |
| 50 | + 'testTestingRootWkspc', |
| 51 | + 'errorWorkspace', |
| 52 | + ); |
| 53 | + const rootPathDiscoverySymlink = path.join( |
| 54 | + EXTENSION_ROOT_DIR_FOR_TESTS, |
| 55 | + 'src', |
| 56 | + 'testTestingRootWkspc', |
| 57 | + 'symlinkWorkspace', |
| 58 | + ); |
| 59 | + const nestedTarget = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testTestingRootWkspc', 'target workspace'); |
| 60 | + const nestedSymlink = path.join( |
| 61 | + EXTENSION_ROOT_DIR_FOR_TESTS, |
| 62 | + 'src', |
| 63 | + 'testTestingRootWkspc', |
| 64 | + 'symlink_parent-folder', |
| 65 | + ); |
| 66 | + suiteSetup(async () => { |
| 67 | + // create symlink for specific symlink test |
| 68 | + const target = rootPathSmallWorkspace; |
| 69 | + const dest = rootPathDiscoverySymlink; |
| 70 | + serviceContainer = (await initialize()).serviceContainer; |
| 71 | + try { |
| 72 | + fs.symlink(target, dest, 'dir', (err) => { |
| 73 | + if (err) { |
| 74 | + traceError(err); |
| 75 | + } else { |
| 76 | + traceLog('Symlink created successfully for regular symlink end to end tests.'); |
| 77 | + } |
| 78 | + }); |
| 79 | + fs.symlink(nestedTarget, nestedSymlink, 'dir', (err) => { |
| 80 | + if (err) { |
| 81 | + traceError(err); |
| 82 | + } else { |
| 83 | + traceLog('Symlink created successfully for nested symlink end to end tests.'); |
| 84 | + } |
| 85 | + }); |
| 86 | + } catch (err) { |
| 87 | + traceError(err); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + setup(async () => { |
| 92 | + getPixiStub = sinon.stub(pixi, 'getPixi'); |
| 93 | + getPixiStub.resolves(undefined); |
| 94 | + |
| 95 | + // create objects that were injected |
| 96 | + configService = serviceContainer.get<IConfigurationService>(IConfigurationService); |
| 97 | + pythonExecFactory = serviceContainer.get<IPythonExecutionFactory>(IPythonExecutionFactory); |
| 98 | + pythonExecFactory.createCondaExecutionService = async () => undefined; |
| 99 | + testController = serviceContainer.get<TestController>(ITestController); |
| 100 | + envVarsService = serviceContainer.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider); |
| 101 | + |
| 102 | + // create objects that were not injected |
| 103 | + |
| 104 | + testOutputChannel = createTypeMoq<ITestOutputChannel>(); |
| 105 | + testOutputChannel |
| 106 | + .setup((x) => x.append(typeMoq.It.isAny())) |
| 107 | + .callback((appendVal: any) => { |
| 108 | + traceLog('output channel - ', appendVal.toString()); |
| 109 | + }) |
| 110 | + .returns(() => { |
| 111 | + // Whatever you need to return |
| 112 | + }); |
| 113 | + testOutputChannel |
| 114 | + .setup((x) => x.appendLine(typeMoq.It.isAny())) |
| 115 | + .callback((appendVal: any) => { |
| 116 | + traceLog('output channel ', appendVal.toString()); |
| 117 | + }) |
| 118 | + .returns(() => { |
| 119 | + // Whatever you need to return |
| 120 | + }); |
| 121 | + }); |
| 122 | + teardown(() => { |
| 123 | + sinon.restore(); |
| 124 | + }); |
| 125 | + suiteTeardown(async () => { |
| 126 | + // remove symlink |
| 127 | + const dest = rootPathDiscoverySymlink; |
| 128 | + if (fs.existsSync(dest)) { |
| 129 | + fs.unlink(dest, (err) => { |
| 130 | + if (err) { |
| 131 | + traceError(err); |
| 132 | + } else { |
| 133 | + traceLog('Symlink removed successfully after tests, rootPathDiscoverySymlink.'); |
| 134 | + } |
| 135 | + }); |
| 136 | + } else { |
| 137 | + traceLog('Symlink was not found to remove after tests, exiting successfully, rootPathDiscoverySymlink.'); |
| 138 | + } |
| 139 | + |
| 140 | + if (fs.existsSync(nestedSymlink)) { |
| 141 | + fs.unlink(nestedSymlink, (err) => { |
| 142 | + if (err) { |
| 143 | + traceError(err); |
| 144 | + } else { |
| 145 | + traceLog('Symlink removed successfully after tests, nestedSymlink.'); |
| 146 | + } |
| 147 | + }); |
| 148 | + } else { |
| 149 | + traceLog('Symlink was not found to remove after tests, exiting successfully, nestedSymlink.'); |
| 150 | + } |
| 151 | + }); |
| 152 | + test('unittest execution adapter seg fault error handling', async () => { |
| 153 | + resultResolver = new PythonResultResolver(testController, unittestProvider, workspaceUri); |
| 154 | + let callCount = 0; |
| 155 | + let failureOccurred = false; |
| 156 | + let failureMsg = ''; |
| 157 | + resultResolver._resolveExecution = async (data, _token?) => { |
| 158 | + // do the following asserts for each time resolveExecution is called, should be called once per test. |
| 159 | + callCount = callCount + 1; |
| 160 | + traceLog(`unittest execution adapter seg fault error handling \n ${JSON.stringify(data)}`); |
| 161 | + try { |
| 162 | + if (data.status === 'error') { |
| 163 | + if (data.error === undefined) { |
| 164 | + // Dereference a NULL pointer |
| 165 | + const indexOfTest = JSON.stringify(data).search('Dereference a NULL pointer'); |
| 166 | + if (indexOfTest === -1) { |
| 167 | + failureOccurred = true; |
| 168 | + failureMsg = 'Expected test to have a null pointer'; |
| 169 | + } |
| 170 | + } else if (data.error.length === 0) { |
| 171 | + failureOccurred = true; |
| 172 | + failureMsg = "Expected errors in 'error' field"; |
| 173 | + } |
| 174 | + } else { |
| 175 | + const indexOfTest = JSON.stringify(data.result).search('error'); |
| 176 | + if (indexOfTest === -1) { |
| 177 | + failureOccurred = true; |
| 178 | + failureMsg = |
| 179 | + 'If payload status is not error then the individual tests should be marked as errors. This should occur on windows machines.'; |
| 180 | + } |
| 181 | + } |
| 182 | + if (data.result === undefined) { |
| 183 | + failureOccurred = true; |
| 184 | + failureMsg = 'Expected results to be present'; |
| 185 | + } |
| 186 | + // make sure the testID is found in the results |
| 187 | + const indexOfTest = JSON.stringify(data).search('test_seg_fault.TestSegmentationFault.test_segfault'); |
| 188 | + if (indexOfTest === -1) { |
| 189 | + failureOccurred = true; |
| 190 | + failureMsg = 'Expected testId to be present'; |
| 191 | + } |
| 192 | + } catch (err) { |
| 193 | + failureMsg = err ? (err as Error).toString() : ''; |
| 194 | + failureOccurred = true; |
| 195 | + } |
| 196 | + return Promise.resolve(); |
| 197 | + }; |
| 198 | + |
| 199 | + const testId = `test_seg_fault.TestSegmentationFault.test_segfault`; |
| 200 | + const testIds: string[] = [testId]; |
| 201 | + |
| 202 | + // set workspace to test workspace folder |
| 203 | + workspaceUri = Uri.parse(rootPathErrorWorkspace); |
| 204 | + configService.getSettings(workspaceUri).testing.unittestArgs = ['-s', '.', '-p', '*test*.py']; |
| 205 | + |
| 206 | + // run pytest execution |
| 207 | + const executionAdapter = new UnittestTestExecutionAdapter( |
| 208 | + configService, |
| 209 | + testOutputChannel.object, |
| 210 | + resultResolver, |
| 211 | + envVarsService, |
| 212 | + ); |
| 213 | + const testRun = typeMoq.Mock.ofType<TestRun>(); |
| 214 | + testRun |
| 215 | + .setup((t) => t.token) |
| 216 | + .returns( |
| 217 | + () => |
| 218 | + ({ |
| 219 | + onCancellationRequested: () => undefined, |
| 220 | + } as any), |
| 221 | + ); |
| 222 | + await executionAdapter |
| 223 | + .runTests(workspaceUri, testIds, TestRunProfileKind.Run, testRun.object, pythonExecFactory) |
| 224 | + .finally(() => { |
| 225 | + assert.strictEqual(callCount, 1, 'Expected _resolveExecution to be called once'); |
| 226 | + assert.strictEqual(failureOccurred, false, failureMsg); |
| 227 | + }); |
| 228 | + }); |
| 229 | +}); |
0 commit comments