Skip to content

Commit c645e38

Browse files
author
David Kutugata
authored
Added unit tests for datascience error handler. (#6684)
* Added unit tests for datascience error handler. * Fixed tests to compare resolved promise instead of promise. * changed handle error mock to always return string * corrected the '.find is not a function' error * used .find differently * used foreach instead of find to avoid '.find is not a function' * make sure installers has at least one element * fixed unit tests to use typemoq * changed a fucntion to be async to make the test work, also updated the package.json * returned package.json to what it was as it fails validation on linux
1 parent ea4f0c4 commit c645e38

7 files changed

Lines changed: 107 additions & 104 deletions

File tree

‎news/3 Code Health/6670.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Unit Tests for DataScience Error Handler.

‎package-lock.json‎

Lines changed: 10 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/client/datascience/editor-integration/codewatcher.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export class CodeWatcher implements ICodeWatcher {
328328
}
329329
this.sendPerceivedCellExecute(stopWatch);
330330
} catch (err) {
331-
this.dataScienceErrorHandler.handleError(err);
331+
this.dataScienceErrorHandler.handleError(err).ignoreErrors();
332332
}
333333

334334
return result;
@@ -342,7 +342,7 @@ export class CodeWatcher implements ICodeWatcher {
342342
const activeInteractiveWindow = await this.interactiveWindowProvider.getOrCreateActive();
343343
return activeInteractiveWindow.addMessage(message);
344344
} catch (err) {
345-
this.dataScienceErrorHandler.handleError(err);
345+
this.dataScienceErrorHandler.handleError(err).ignoreErrors();
346346
}
347347
}
348348
}

‎src/client/datascience/errorHandler/errorHandler.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class DataScienceErrorHandler implements IDataScienceErrorHandler {
1818
@inject(IInstallationChannelManager) protected channels: IInstallationChannelManager) {
1919
}
2020

21-
public handleError(err: Error) {
21+
public async handleError(err: Error): Promise<void> {
2222
if (err instanceof JupyterInstallError) {
2323
this.applicationShell.showInformationMessage(
2424
localize.DataScience.jupyterNotSupported(),

‎src/client/datascience/interactive-window/interactiveWindowCommandListener.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export class InteractiveWindowCommandListener implements IDataScienceCommandList
227227
}
228228
} else {
229229
this.dataScienceErrorHandler.handleError(
230-
new JupyterInstallError(localize.DataScience.jupyterNotSupported(), localize.DataScience.pythonInteractiveHelpLink()));
230+
new JupyterInstallError(localize.DataScience.jupyterNotSupported(), localize.DataScience.pythonInteractiveHelpLink())).ignoreErrors();
231231
}
232232
}
233233

‎src/client/datascience/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export interface IInteractiveWindowProvider {
170170

171171
export const IDataScienceErrorHandler = Symbol('IDataScienceErrorHandler');
172172
export interface IDataScienceErrorHandler {
173-
handleError(err: Error): void;
173+
handleError(err: Error): Promise<void>;
174174
}
175175

176176
export const IInteractiveWindow = Symbol('IInteractiveWindow');
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
'use strict';
4+
import * as typemoq from 'typemoq';
5+
import { IApplicationShell } from '../../client/common/application/types';
6+
import { IInstallationChannelManager, IModuleInstaller } from '../../client/common/installer/types';
7+
import { ILogger } from '../../client/common/types';
8+
import * as localize from '../../client/common/utils/localize';
9+
import { DataScienceErrorHandler } from '../../client/datascience/errorHandler/errorHandler';
10+
import { JupyterInstallError } from '../../client/datascience/jupyter/jupyterInstallError';
11+
import { JupyterSelfCertsError } from '../../client/datascience/jupyter/jupyterSelfCertsError';
12+
13+
suite('DataScience Error Handler Unit Tests', () => {
14+
let applicationShell: typemoq.IMock<IApplicationShell>;
15+
let logger: typemoq.IMock<ILogger>;
16+
let channels: typemoq.IMock<IInstallationChannelManager>;
17+
let dataScienceErrorHandler: DataScienceErrorHandler;
18+
19+
setup(() => {
20+
applicationShell = typemoq.Mock.ofType<IApplicationShell>();
21+
logger = typemoq.Mock.ofType<ILogger>();
22+
channels = typemoq.Mock.ofType<IInstallationChannelManager>();
23+
24+
dataScienceErrorHandler = new DataScienceErrorHandler(applicationShell.object, logger.object, channels.object);
25+
});
26+
const message = 'Test error message.';
27+
28+
test('Default error', async () => {
29+
applicationShell.setup(app => app.showErrorMessage(typemoq.It.isAny()))
30+
.returns(() => Promise.resolve(message))
31+
.verifiable(typemoq.Times.once());
32+
33+
logger.setup(log => log.logError(typemoq.It.isAny()))
34+
.verifiable(typemoq.Times.once());
35+
36+
const err = new Error(message);
37+
await dataScienceErrorHandler.handleError(err);
38+
39+
applicationShell.verifyAll();
40+
logger.verifyAll();
41+
});
42+
43+
test('Jupyter Self Certificates Error', async () => {
44+
logger.setup(log => log.logError(typemoq.It.isAny()))
45+
.verifiable(typemoq.Times.once());
46+
47+
const err = new JupyterSelfCertsError(message);
48+
await dataScienceErrorHandler.handleError(err);
49+
50+
logger.verifyAll();
51+
});
52+
53+
test('Jupyter Install Error', async () => {
54+
applicationShell.setup(app => app.showInformationMessage(typemoq.It.isAny(),
55+
typemoq.It.isValue(localize.DataScience.jupyterInstall()),
56+
typemoq.It.isValue(localize.DataScience.notebookCheckForImportNo())))
57+
.returns(() => Promise.resolve(localize.DataScience.jupyterInstall()))
58+
.verifiable(typemoq.Times.once());
59+
60+
logger.setup(log => log.logError(typemoq.It.isAny()))
61+
.verifiable(typemoq.Times.once());
62+
63+
const installers: IModuleInstaller[] = [
64+
{
65+
name: 'Pip',
66+
displayName: 'Pip',
67+
priority: 0,
68+
isSupported: () => Promise.resolve(true),
69+
installModule: () => Promise.resolve()
70+
},
71+
{
72+
name: 'Conda',
73+
displayName: 'Conda',
74+
priority: 0,
75+
isSupported: () => Promise.resolve(true),
76+
installModule: () => Promise.resolve()
77+
}
78+
];
79+
80+
channels.setup(ch => ch.getInstallationChannels())
81+
.returns(() => Promise.resolve(installers))
82+
.verifiable(typemoq.Times.once());
83+
84+
const err = new JupyterInstallError(message, 'test.com');
85+
await dataScienceErrorHandler.handleError(err);
86+
87+
applicationShell.verifyAll();
88+
logger.verifyAll();
89+
channels.verifyAll();
90+
});
91+
});

0 commit comments

Comments
 (0)