Skip to content

Commit 02dc3ed

Browse files
DonJayamanneMikhail Arkhipov
authored and
Mikhail Arkhipov
committed
🐛 handle failure to detect conda environment (#829)
* 🐛 handle failure to detect conda environment * 🔨 revert threadid test * 🔨 use sleep function and wait after tests
1 parent cd67eb0 commit 02dc3ed

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/client/common/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const PREFIX = 'Python Extension: ';
77
export class Logger implements ILogger {
88
public logError(message: string, ex?: Error) {
99
if (ex) {
10-
console.error(`${PREFIX}${message}`, error);
10+
console.error(`${PREFIX}${message}`, ex);
1111
} else {
1212
console.error(`${PREFIX}${message}`);
1313
}

src/client/interpreter/locators/services/condaService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class CondaService implements ICondaService {
2828
const homeDir = this.platform.isWindows ? process.env.USERPROFILE : (process.env.HOME || process.env.HOMEPATH);
2929
return homeDir ? path.join(homeDir, '.conda', 'environments.txt') : undefined;
3030
}
31-
constructor( @inject(IServiceContainer) private serviceContainer: IServiceContainer,
31+
constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer,
3232
@inject(IInterpreterLocatorService) @named(WINDOWS_REGISTRY_SERVICE) @optional() private registryLookupForConda?: IInterpreterLocatorService) {
3333
this.processService = this.serviceContainer.get<IProcessService>(IProcessService);
3434
this.platform = this.serviceContainer.get<IPlatformService>(IPlatformService);
@@ -106,10 +106,10 @@ export class CondaService implements ICondaService {
106106
const fs = this.serviceContainer.get<IFileSystem>(IFileSystem);
107107

108108
// From the list of conda environments find this dir.
109-
let matchingEnvs = environments!.filter(item => fs.arePathsSame(item.path, interpreterPathToMatch));
109+
let matchingEnvs = Array.isArray(environments) ? environments.filter(item => fs.arePathsSame(item.path, interpreterPathToMatch)) : [];
110110
if (matchingEnvs.length === 0) {
111111
environments = await this.getCondaEnvironments(true);
112-
matchingEnvs = environments!.filter(item => fs.arePathsSame(item.path, interpreterPathToMatch));
112+
matchingEnvs = Array.isArray(environments) ? environments.filter(item => fs.arePathsSame(item.path, interpreterPathToMatch)) : [];
113113
}
114114

115115
if (matchingEnvs.length > 0) {

src/test/debugger/misc.test.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ const EXPERIMENTAL_DEBUG_ADAPTER = path.join(__dirname, '..', '..', 'client', 'd
4343
});
4444
teardown(async () => {
4545
// Wait for a second before starting another test (sometimes, sockets take a while to get closed).
46-
await new Promise(resolve => setTimeout(resolve, 1000));
46+
await sleep(1000);
4747
try {
4848
// tslint:disable-next-line:no-empty
4949
await debugClient.stop().catch(() => { });
5050
// tslint:disable-next-line:no-empty
5151
} catch (ex) { }
52+
await sleep(1000);
5253
});
5354

5455
function buildLauncArgs(pythonFile: string, stopOnEntry: boolean = false): LaunchRequestArguments {
@@ -134,15 +135,19 @@ const EXPERIMENTAL_DEBUG_ADAPTER = path.join(__dirname, '..', '..', 'client', 'd
134135
]);
135136
});
136137
test('Ensure threadid is int32', async () => {
137-
const launchArgs = buildLauncArgs('sample2.py', false);
138-
const breakpointLocation = { path: path.join(debugFilesPath, 'sample2.py'), column: 0, line: 5 };
139-
await debugClient.hitBreakpoint(launchArgs, breakpointLocation);
138+
if (debuggerType !== 'python') {
139+
return this.skip();
140+
}
141+
const threadIdPromise = debugClient.waitForEvent('thread');
140142

141-
const threads = await debugClient.threadsRequest();
142-
expect(threads).to.be.not.equal(undefined, 'no threads response');
143-
expect(threads.body.threads).to.be.lengthOf(1);
143+
await Promise.all([
144+
debugClient.configurationSequence(),
145+
debugClient.launch(buildLauncArgs('simplePrint.py', true)),
146+
debugClient.waitForEvent('initialized'),
147+
debugClient.waitForEvent('stopped')
148+
]);
144149

145-
const threadId = threads.body.threads[0].id;
150+
const threadId = ((await threadIdPromise) as ThreadEvent).body.threadId;
146151
expect(threadId).to.be.lessThan(MAX_SIGNED_INT32 + 1, 'ThreadId is not an integer');
147152
await Promise.all([
148153
debugClient.continueRequest({ threadId }),

src/test/interpreters/condaService.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,33 @@ suite('Interpreters Conda Service', () => {
514514
assert.equal(isAvailable, false);
515515
});
516516

517+
async function testFailureOfGettingCondaEnvironments(isWindows: boolean, isOsx: boolean, isLinux: boolean, pythonPath: string) {
518+
platformService.setup(p => p.isLinux).returns(() => isLinux);
519+
platformService.setup(p => p.isWindows).returns(() => isWindows);
520+
platformService.setup(p => p.isMac).returns(() => isOsx);
521+
522+
const stateFactory = TypeMoq.Mock.ofType<IPersistentStateFactory>();
523+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IPersistentStateFactory))).returns(() => stateFactory.object);
524+
const state = new MockState({ data: undefined });
525+
stateFactory.setup(s => s.createGlobalPersistentState(TypeMoq.It.isValue('CONDA_ENVIRONMENTS'), TypeMoq.It.isValue(undefined))).returns(() => state);
526+
processService.setup(p => p.exec(TypeMoq.It.isValue('conda'), TypeMoq.It.isValue(['--version']), TypeMoq.It.isAny())).returns(() => Promise.resolve({ stdout: 'some value' }));
527+
processService.setup(p => p.exec(TypeMoq.It.isValue('conda'), TypeMoq.It.isValue(['env', 'list']), TypeMoq.It.isAny())).returns(() => Promise.reject(new Error('Failed')));
528+
const condaEnv = await condaService.getCondaEnvironment(pythonPath);
529+
expect(condaEnv).to.be.equal(undefined, 'Conda should be undefined');
530+
}
531+
test('Fails to identify an environment as a conda env (windows)', async () => {
532+
const pythonPath = path.join('c', 'users', 'xyz', '.conda', 'envs', 'one', 'python.exe');
533+
fileSystem.setup(f => f.directoryExistsAsync(TypeMoq.It.isValue(path.join(path.dirname(pythonPath), 'conda-meta')))).returns(() => Promise.resolve(true));
534+
await testFailureOfGettingCondaEnvironments(true, false, false, pythonPath);
535+
});
536+
test('Fails to identify an environment as a conda env (linux)', async () => {
537+
const pythonPath = path.join('c', 'users', 'xyz', '.conda', 'envs', 'one', 'python');
538+
fileSystem.setup(f => f.directoryExistsAsync(TypeMoq.It.isValue(path.join(path.dirname(pythonPath), 'conda-meta')))).returns(() => Promise.resolve(true));
539+
await testFailureOfGettingCondaEnvironments(false, false, true, pythonPath);
540+
});
541+
test('Fails to identify an environment as a conda env (osx)', async () => {
542+
const pythonPath = path.join('c', 'users', 'xyz', '.conda', 'envs', 'one', 'python');
543+
fileSystem.setup(f => f.directoryExistsAsync(TypeMoq.It.isValue(path.join(path.dirname(pythonPath), 'conda-meta')))).returns(() => Promise.resolve(true));
544+
await testFailureOfGettingCondaEnvironments(false, true, false, pythonPath);
545+
});
517546
});

0 commit comments

Comments
 (0)