Skip to content
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

Fix unhandled rejected promises and fix tests #1921

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions news/2 Fixes/1919.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix unhandled rejected promises in unit tests.
1 change: 1 addition & 0 deletions news/3 Code Health/1918.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Log unhandled rejected promises when running unit tests.
13 changes: 9 additions & 4 deletions src/test/debugger/configProvider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,15 @@ import { IServiceContainer } from '../../../client/ioc/types';
setupIoc(pythonPath, isWindows, isMac, isLinux);
setupActiveEditor(pythonFile, PYTHON_LANGUAGE);

const execOutput = pyramidExists ? Promise.resolve({ stdout: pyramidFilePath }) : Promise.reject('No Module');
pythonExecutionService.setup(e => e.exec(TypeMoq.It.isValue(args), TypeMoq.It.isAny()))
.returns(() => execOutput)
.verifiable(TypeMoq.Times.exactly(addPyramidDebugOption ? 1 : 0));
if (pyramidExists) {
pythonExecutionService.setup(e => e.exec(TypeMoq.It.isValue(args), TypeMoq.It.isAny()))
.returns(() => Promise.resolve({ stdout: pyramidFilePath }))
.verifiable(TypeMoq.Times.exactly(addPyramidDebugOption ? 1 : 0));
} else {
pythonExecutionService.setup(e => e.exec(TypeMoq.It.isValue(args), TypeMoq.It.isAny()))
.returns(() => Promise.reject('No Module Available'))
.verifiable(TypeMoq.Times.exactly(addPyramidDebugOption ? 1 : 0));
}
fileSystem.setup(f => f.fileExists(TypeMoq.It.isValue(pserveFilePath)))
.returns(() => Promise.resolve(pyramidExists))
.verifiable(TypeMoq.Times.exactly(pyramidExists && addPyramidDebugOption ? 1 : 0));
Expand Down
12 changes: 12 additions & 0 deletions src/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,17 @@ if (MOCHA_REPORTER_JUNIT) {
};
}

process.on('unhandledRejection', (ex: string | Error, a) => {
const message = [`${ex}`];
if (typeof ex !== 'string' && ex && ex.message) {
message.push(ex.name);
message.push(ex.message);
if (ex.stack) {
message.push(ex.stack);
}
}
console.error(`Unhandled Promise Rejection with the message ${message.join(', ')}`);
});

testRunner.configure(options, { coverageConfig: '../coverconfig.json' });
module.exports = testRunner;
37 changes: 20 additions & 17 deletions src/test/install/channelManager.messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Container } from 'inversify';
import * as TypeMoq from 'typemoq';
import { IApplicationShell } from '../../client/common/application/types';
import { InstallationChannelManager } from '../../client/common/installer/channelManager';
import { IModuleInstaller } from '../../client/common/installer/types';
import { Architecture, IPlatformService } from '../../client/common/platform/types';
import { Product } from '../../client/common/types';
import { IInterpreterService, InterpreterType, PythonInterpreter } from '../../client/interpreter/contracts';
Expand Down Expand Up @@ -46,13 +47,15 @@ suite('Installation - channel messages', () => {

interpreters = TypeMoq.Mock.ofType<IInterpreterService>();
serviceManager.addSingletonInstance<IInterpreterService>(IInterpreterService, interpreters.object);

const moduleInstaller = TypeMoq.Mock.ofType<IModuleInstaller>();
serviceManager.addSingletonInstance<IModuleInstaller>(IModuleInstaller, moduleInstaller.object);
});

test('No installers message: Unknown/Windows', async () => {
platform.setup(x => x.isWindows).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Unknown,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.showNoInstallersMessage();
async (message: string, url: string) => {
verifyMessage(message, ['Pip'], ['Conda']);
verifyUrl(url, ['Windows', 'Pip']);
});
Expand All @@ -61,8 +64,7 @@ suite('Installation - channel messages', () => {
test('No installers message: Conda/Windows', async () => {
platform.setup(x => x.isWindows).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Conda,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.showNoInstallersMessage();
async (message: string, url: string) => {
verifyMessage(message, ['Pip', 'Conda'], []);
verifyUrl(url, ['Windows', 'Pip', 'Conda']);
});
Expand All @@ -72,8 +74,7 @@ suite('Installation - channel messages', () => {
platform.setup(x => x.isWindows).returns(() => false);
platform.setup(x => x.isMac).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Unknown,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.showNoInstallersMessage();
async (message: string, url: string) => {
verifyMessage(message, ['Pip'], ['Conda']);
verifyUrl(url, ['Mac', 'Pip']);
});
Expand All @@ -83,8 +84,7 @@ suite('Installation - channel messages', () => {
platform.setup(x => x.isWindows).returns(() => false);
platform.setup(x => x.isMac).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Conda,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.showNoInstallersMessage();
async (message: string, url: string) => {
verifyMessage(message, ['Pip', 'Conda'], []);
verifyUrl(url, ['Mac', 'Pip', 'Conda']);
});
Expand All @@ -95,8 +95,7 @@ suite('Installation - channel messages', () => {
platform.setup(x => x.isMac).returns(() => false);
platform.setup(x => x.isLinux).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Unknown,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.showNoInstallersMessage();
async (message: string, url: string) => {
verifyMessage(message, ['Pip'], ['Conda']);
verifyUrl(url, ['Linux', 'Pip']);
});
Expand All @@ -107,8 +106,7 @@ suite('Installation - channel messages', () => {
platform.setup(x => x.isMac).returns(() => false);
platform.setup(x => x.isLinux).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Conda,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.showNoInstallersMessage();
async (message: string, url: string) => {
verifyMessage(message, ['Pip', 'Conda'], []);
verifyUrl(url, ['Linux', 'Pip', 'Conda']);
});
Expand All @@ -117,11 +115,10 @@ suite('Installation - channel messages', () => {
test('No channels message', async () => {
platform.setup(x => x.isWindows).returns(() => true);
await testInstallerMissingMessage(InterpreterType.Unknown,
async (channels: InstallationChannelManager, message: string, url: string) => {
await channels.getInstallationChannel(Product.pylint);
async (message: string, url: string) => {
verifyMessage(message, ['Pip'], ['Conda']);
verifyUrl(url, ['Windows', 'Pip']);
});
}, 'getInstallationChannel');
});

function verifyMessage(message: string, present: string[], missing: string[]) {
Expand All @@ -142,7 +139,8 @@ suite('Installation - channel messages', () => {

async function testInstallerMissingMessage(
interpreterType: InterpreterType,
verify: (c: InstallationChannelManager, m: string, u: string) => void): Promise<void> {
verify: (m: string, u: string) => Promise<void>,
methodType: 'showNoInstallersMessage' | 'getInstallationChannel' = 'showNoInstallersMessage'): Promise<void> {

const activeInterpreter: PythonInterpreter = {
...info,
Expand All @@ -167,6 +165,11 @@ suite('Installation - channel messages', () => {
appShell.setup(x => x.openUrl(TypeMoq.It.isAnyString())).callback((s: string) => {
url = s;
});
verify(channels, message, url);
if (methodType === 'showNoInstallersMessage') {
await channels.showNoInstallersMessage();
} else {
await channels.getInstallationChannel(Product.pylint);
}
await verify(message, url);
}
});
6 changes: 3 additions & 3 deletions src/test/unittests/common/debugLauncher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ suite('Unit Tests - Debug Launcher', () => {
const testProviders: TestProvider[] = ['nosetest', 'pytest', 'unittest'];
testProviders.forEach(testProvider => {
[true, false].forEach(useExperimentalDebugger => {
const testTitleSuffix = `(Test Framework '${testProvider}', and use experimental debugger = '${useExperimentalDebugger}'`;
const testTitleSuffix = `(Test Framework '${testProvider}', and use experimental debugger = '${useExperimentalDebugger}')`;
const testLaunchScript = getTestLauncherScript(testProvider, useExperimentalDebugger);
const debuggerType = useExperimentalDebugger ? 'pythonExperimental' : 'python';

Expand Down Expand Up @@ -131,7 +131,7 @@ suite('Unit Tests - Debug Launcher', () => {
const cancellationToken = new CancellationTokenSource();
cancellationToken.cancel();
const token = cancellationToken.token;
expect(debugLauncher.launchDebugger({ cwd: '', args: [], token, testProvider })).to.be.eventually.equal(undefined, 'not undefined');
await expect(debugLauncher.launchDebugger({ cwd: '', args: [], token, testProvider })).to.be.eventually.equal(undefined, 'not undefined');
debugService.verifyAll();
});
test(`Must throw an exception if there are no workspaces ${testTitleSuffix}`, async () => {
Expand All @@ -142,7 +142,7 @@ suite('Unit Tests - Debug Launcher', () => {
.returns(() => Promise.resolve(undefined as any))
.verifiable(TypeMoq.Times.never());

expect(debugLauncher.launchDebugger({ cwd: '', args: [], testProvider })).to.eventually.throw('Please open a workspace');
await expect(debugLauncher.launchDebugger({ cwd: '', args: [], testProvider })).to.eventually.rejectedWith('Please open a workspace');
debugService.verifyAll();
});
});
Expand Down