Skip to content

Commit 8a80ca7

Browse files
Fix output comparison for new notebook tests
1 parent 2b52ff8 commit 8a80ca7

8 files changed

Lines changed: 67 additions & 198 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {
2+
IDLRawNotebook,
3+
IDLRawNotebookVersion_2_0_0,
4+
} from '@idl/types/notebooks';
5+
import expect from 'expect';
6+
import { readFileSync } from 'fs';
7+
8+
/**
9+
* gets outputs that we can compare
10+
*/
11+
function GetOutputs(data: IDLRawNotebook<IDLRawNotebookVersion_2_0_0>) {
12+
return data.cells
13+
.map((cell) =>
14+
(cell.outputs || [])
15+
.map((output) =>
16+
output.items
17+
.map((item) =>
18+
Array.isArray(item.content)
19+
? item.content.join('\n').trimEnd().split(/\n/gim)
20+
: [item.content.trimEnd()]
21+
)
22+
.flat()
23+
)
24+
.flat()
25+
)
26+
.flat();
27+
}
28+
29+
/**
30+
* Compares the JSON text content of two notebook files on disk
31+
*/
32+
export async function CompareNotebookJSONOutputs(
33+
referenceUri: string,
34+
actualUri: string
35+
) {
36+
/**
37+
* Get reference notebook
38+
*/
39+
const refData: IDLRawNotebook<IDLRawNotebookVersion_2_0_0> = JSON.parse(
40+
readFileSync(referenceUri, 'utf-8')
41+
);
42+
43+
/**
44+
* Get actual notebook
45+
*/
46+
const actualData: IDLRawNotebook<IDLRawNotebookVersion_2_0_0> = JSON.parse(
47+
readFileSync(actualUri, 'utf-8')
48+
);
49+
50+
// compare
51+
expect(GetOutputs(refData)).toEqual(GetOutputs(actualData));
52+
}

apps/client-e2e/src/tests/notebooks/notebooks-replace-cell-paths-for-error.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { GetExtensionPath, Sleep } from '@idl/shared';
22
import { OpenNotebookInVSCode, VSCODE_COMMANDS } from '@idl/vscode/shared';
33
import expect from 'expect';
4-
import { readFileSync } from 'fs';
54
import * as vscode from 'vscode';
65

76
import { RunnerFunction } from '../runner.interface';
7+
import { CompareNotebookJSONOutputs } from './helpers/compare-notebook-json-outputs';
88

99
/**
1010
* Verifies we replace paths when we have syntax errors
@@ -32,16 +32,8 @@ export const NotebooksReplaceCellPathsForError: RunnerFunction = async (
3232
// save to disk
3333
await nb.save();
3434

35-
// get the code we should expect, independent of line ending
36-
const actual = readFileSync(nbUri, { encoding: 'utf-8' }).split(/\r?\n/gim);
37-
38-
// get the code we should expect, independent of line ending
39-
const expected = readFileSync(expectedUri, { encoding: 'utf-8' }).split(
40-
/\r?\n/gim
41-
);
42-
43-
// verify the content matches what we should have
44-
expect(expected).toEqual(actual);
35+
// compare outputs
36+
await CompareNotebookJSONOutputs(expectedUri, nbUri);
4537

4638
// run all cells
4739
await vscode.commands.executeCommand(VSCODE_COMMANDS.NOTEBOOK_CLEAR_OUTPUTS);

apps/client-e2e/src/tests/notebooks/notebooks-replace-cell-paths-on-stop.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { GetExtensionPath, Sleep } from '@idl/shared';
22
import { OpenNotebookInVSCode, VSCODE_COMMANDS } from '@idl/vscode/shared';
33
import expect from 'expect';
4-
import { readFileSync } from 'fs';
54
import * as vscode from 'vscode';
65

76
import { RunnerFunction } from '../runner.interface';
7+
import { CompareNotebookJSONOutputs } from './helpers/compare-notebook-json-outputs';
88

99
/**
1010
* Verifies we replace paths when we stop
@@ -30,16 +30,8 @@ export const NotebooksReplaceCellPathsOnStop: RunnerFunction = async (init) => {
3030
// save to disk
3131
await nb.save();
3232

33-
// get the code we should expect, independent of line ending
34-
const actual = readFileSync(nbUri, { encoding: 'utf-8' }).split(/\r?\n/gim);
35-
36-
// get the code we should expect, independent of line ending
37-
const expected = readFileSync(expectedUri, { encoding: 'utf-8' }).split(
38-
/\r?\n/gim
39-
);
40-
41-
// verify the content matches what we should have
42-
expect(expected).toEqual(actual);
33+
// compare outputs
34+
await CompareNotebookJSONOutputs(expectedUri, nbUri);
4335

4436
// run all cells
4537
await vscode.commands.executeCommand(VSCODE_COMMANDS.NOTEBOOK_CLEAR_OUTPUTS);

apps/client-e2e/src/tests/notebooks/notebooks-verify-implied-print.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { GetExtensionPath, Sleep } from '@idl/shared';
22
import { OpenNotebookInVSCode, VSCODE_COMMANDS } from '@idl/vscode/shared';
33
import expect from 'expect';
4-
import { readFileSync } from 'fs';
5-
import { platform } from 'os';
64
import * as vscode from 'vscode';
75

86
import { RunnerFunction } from '../runner.interface';
7+
import { CompareNotebookJSONOutputs } from './helpers/compare-notebook-json-outputs';
98

109
/**
1110
* Verifies implied print behaves as expected
@@ -16,9 +15,7 @@ export const NotebooksVerifyImpliedPrint: RunnerFunction = async (init) => {
1615
);
1716

1817
const expectedUri = GetExtensionPath(
19-
platform() === 'win32'
20-
? 'idl/test/client-e2e/notebooks/implied-print/implied-print-results.idlnb'
21-
: 'idl/test/client-e2e/notebooks/implied-print/implied-print-results-mac.idlnb'
18+
'idl/test/client-e2e/notebooks/implied-print/implied-print-results.idlnb'
2219
);
2320

2421
// open notebook
@@ -33,16 +30,8 @@ export const NotebooksVerifyImpliedPrint: RunnerFunction = async (init) => {
3330
// save to disk
3431
await nb.save();
3532

36-
// get the code we should expect, independent of line ending
37-
const actual = readFileSync(nbUri, { encoding: 'utf-8' }).split(/\r?\n/gim);
38-
39-
// get the code we should expect, independent of line ending
40-
const expected = readFileSync(expectedUri, { encoding: 'utf-8' }).split(
41-
/\r?\n/gim
42-
);
43-
44-
// verify the content matches what we should have
45-
expect(expected).toEqual(actual);
33+
// compare outputs
34+
await CompareNotebookJSONOutputs(expectedUri, nbUri);
4635

4736
// run all cells
4837
await vscode.commands.executeCommand(VSCODE_COMMANDS.NOTEBOOK_CLEAR_OUTPUTS);

idl/test/client-e2e/notebooks/implied-print/implied-print-results-mac.idlnb

Lines changed: 0 additions & 150 deletions
This file was deleted.

idl/test/client-e2e/notebooks/implied-print/implied-print-results.idlnb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@
8787
{
8888
"mime": "text/plain",
8989
"content": [
90-
" 1 2 3 4",
91-
""
90+
" 1 2 3 4"
9291
]
9392
}
9493
],
@@ -110,8 +109,7 @@
110109
{
111110
"mime": "text/plain",
112111
"content": [
113-
" 1 2 3 4",
114-
""
112+
" 1 2 3 4"
115113
]
116114
}
117115
],
@@ -131,7 +129,7 @@
131129
"",
132130
"awesomeFooBar",
133131
"",
134-
"arr"
132+
"arr5"
135133
],
136134
"metadata": {},
137135
"outputs": [
@@ -141,8 +139,7 @@
141139
"mime": "text/plain",
142140
"content": [
143141
"Hello from awesomeFooBar!",
144-
" 1 2 3 4",
145-
""
142+
" 1 2 3 4"
146143
]
147144
}
148145
],

idl/test/client-e2e/notebooks/replace-paths/on-stop-results.idlnb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{
55
"type": "code",
66
"content": [
7-
"print, 5",
87
"stop"
98
],
109
"metadata": {},
@@ -14,8 +13,7 @@
1413
{
1514
"mime": "text/plain",
1615
"content": [
17-
" 5",
18-
"% Stop encountered: $MAIN$, Notebook: on-stop.idlnb, Cell 1, Line 2",
16+
"% Stop encountered: $MAIN$, Notebook: on-stop.idlnb, Cell 1, Line 1",
1917
""
2018
]
2119
}

idl/test/client-e2e/notebooks/replace-paths/on-stop.idlnb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{
55
"type": "code",
66
"content": [
7-
"print, 5",
87
"stop"
98
],
109
"metadata": {},

0 commit comments

Comments
 (0)