Skip to content

Commit 4074633

Browse files
fix failing path tests for notebook (#9195)
1 parent 1b43636 commit 4074633

3 files changed

Lines changed: 43 additions & 9 deletions

File tree

news/3 Code Health/9191.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix working directory path verification for notebook tests.

src/test/datascience/mockJupyterManager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export class MockJupyterManager implements IJupyterSessionManager {
149149
this.addCell(`__file__ = '${Uri.file('bar.py').fsPath.replace(/\\/g, '\\\\')}'`);
150150
this.addCell(`__file__ = '${Uri.file('foo').fsPath.replace(/\\/g, '\\\\')}'`);
151151
this.addCell(`__file__ = '${Uri.file('test.py').fsPath.replace(/\\/g, '\\\\')}'`);
152-
this.addCell('import os\nos.getcwd()', path.join(EXTENSION_ROOT_DIR));
152+
this.addCell('import os\nos.getcwd()', `'${path.join(EXTENSION_ROOT_DIR)}'`);
153153
}
154154

155155
public getConnInfo(): IConnection {
@@ -326,6 +326,10 @@ export class MockJupyterManager implements IJupyterSessionManager {
326326
return Promise.resolve([]);
327327
}
328328

329+
public changeWorkingDirectory(workingDir: string) {
330+
this.addCell('import os\nos.getcwd()', path.join(workingDir));
331+
}
332+
329333
private addCellOutput(cell: ICell, result?: undefined | string | number | nbformat.IUnrecognizedOutput | nbformat.IExecuteResult | nbformat.IDisplayData | nbformat.IStream | nbformat.IError, mimeType?: string) {
330334
const massagedResult = this.massageCellResult(result, mimeType);
331335
const data: nbformat.ICodeCell = cell.data as nbformat.ICodeCell;

src/test/datascience/notebook.functional.test.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,18 @@ suite('DataScience notebook tests', () => {
128128
}
129129
}
130130

131-
async function verifySimple(notebook: INotebook | undefined, code: string, expectedValue: any): Promise<void> {
131+
async function verifySimple(notebook: INotebook | undefined, code: string, expectedValue: any, pathVerify = false): Promise<void> {
132132
const cells = await notebook!.execute(code, path.join(srcDirectory(), 'foo.py'), 2, uuid());
133133
assert.equal(cells.length, 1, `Wrong number of cells returned`);
134134
const data = extractDataOutput(cells[0]);
135-
assert.equal(data, expectedValue, 'Cell value does not match');
135+
if (pathVerify) {
136+
// For a path comparison normalize output and add single quotes on expected value
137+
const normalizedOutput = path.normalize(data).toUpperCase();
138+
const normalizedTarget = `'${path.normalize(expectedValue).toUpperCase()}'`;
139+
assert.equal(normalizedOutput, normalizedTarget, 'Cell path values does not match');
140+
} else {
141+
assert.equal(data, expectedValue, 'Cell value does not match');
142+
}
136143
}
137144

138145
async function verifyError(notebook: INotebook | undefined, code: string, errorString: string): Promise<void> {
@@ -224,14 +231,21 @@ suite('DataScience notebook tests', () => {
224231
});
225232
}
226233

227-
async function createNotebook(useDefaultConfig: boolean, expectFailure?: boolean, usingDarkTheme?: boolean, purpose?: string): Promise<INotebook | undefined> {
234+
async function createNotebook(useDefaultConfig: boolean, expectFailure?: boolean, usingDarkTheme?: boolean, purpose?: string, workingDir?: string, launchingFile?: string): Promise<INotebook | undefined> {
228235
// Catch exceptions. Throw a specific assertion if the promise fails
229236
try {
230-
const server = await jupyterExecution.connectToNotebookServer({ usingDarkTheme, useDefaultConfig, workingDir: ioc.getSettings().datascience.notebookFileRoot, purpose: purpose ? purpose : '1' });
237+
const server = await jupyterExecution.connectToNotebookServer({ usingDarkTheme, useDefaultConfig, workingDir: workingDir ? workingDir : ioc.getSettings().datascience.notebookFileRoot, purpose: purpose ? purpose : '1' });
231238
if (expectFailure) {
232239
assert.ok(false, `Expected server to not be created`);
233240
}
234-
return server ? await server.createNotebook(Uri.parse(Identifiers.InteractiveWindowIdentity)) : undefined;
241+
if (server) {
242+
const notebook = await server.createNotebook(Uri.parse(Identifiers.InteractiveWindowIdentity));
243+
// If specified set our launch file
244+
if (launchingFile) {
245+
await notebook.setLaunchingFile(launchingFile);
246+
}
247+
return notebook;
248+
}
235249
} catch (exc) {
236250
if (!expectFailure) {
237251
assert.ok(false, `Expected server to be created, but got ${exc}`);
@@ -249,6 +263,12 @@ suite('DataScience notebook tests', () => {
249263
}
250264
}
251265

266+
function changeMockWorkingDirectory(workingDir: string) {
267+
if (ioc.mockJupyter) {
268+
ioc.mockJupyter.changeWorkingDirectory(workingDir);
269+
}
270+
}
271+
252272
function addInterruptableMockData(code: string, resultGenerator: (c: CancellationToken) => Promise<{ result: string; haveMore: boolean }>) {
253273
if (ioc.mockJupyter) {
254274
ioc.mockJupyter.addContinuousOutputCell(code, resultGenerator);
@@ -531,9 +551,18 @@ suite('DataScience notebook tests', () => {
531551
}
532552
});
533553

534-
runTest('Verify path', async () => {
535-
const notebook = await createNotebook(true);
536-
await verifySimple(notebook, 'import os\nos.getcwd()', EXTENSION_ROOT_DIR);
554+
runTest('Verify manual working directory', async () => {
555+
// Instead of default, manually set a working directory
556+
const notebook = await createNotebook(true, undefined, undefined, undefined, EXTENSION_ROOT_DIR);
557+
await verifySimple(notebook, 'import os\nos.getcwd()', EXTENSION_ROOT_DIR, true);
558+
});
559+
560+
// tslint:disable-next-line:no-invalid-template-strings
561+
runTest('Verify ${fileDirname} working directory', async () => {
562+
// Verify that the default ${fileDirname} setting sets the working directory to the file path
563+
changeMockWorkingDirectory(`'${srcDirectory()}'`);
564+
const notebook = await createNotebook(true, undefined, undefined, undefined, undefined, path.join(srcDirectory(), 'foo.py'));
565+
await verifySimple(notebook, 'import os\nos.getcwd()', srcDirectory(), true);
537566
});
538567

539568
runTest('Change Interpreter', async () => {

0 commit comments

Comments
 (0)