Skip to content

Commit 7445100

Browse files
authored
add snapshot test warn, throw in CI, helper function, more tests. Remove roundtrip-changes test. (#790)
1 parent 9c8dee2 commit 7445100

File tree

8 files changed

+255
-42
lines changed

8 files changed

+255
-42
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
bibliography: references.bib
3+
---
4+
5+
Welcome to valid basics 2.
6+
7+
```{=html}
8+
<div>hi</div>
9+
```
10+
11+
`hi`{=html}
12+
13+
```{{r}}
14+
1 + 1
15+
```
16+
17+
> This is a block quote this is the same block quote
18+
19+
| this is a line block
20+
| this is another line block
21+
22+
This is *emphasized* text
23+
24+
This is also *emphasized* text
25+
26+
hello [underline]{.underline} ~~strikethrough~~ ^super^ ~sub~ [smallcaps]{.smallcaps}
27+
28+
<!--# this is an html comment -->
29+
30+
definition term
31+
32+
: definition content
33+
34+
definition term 2
35+
36+
: definition content 2
37+
38+
------------------------------------------------------------------------
39+
40+
::: {#thm-pythagoras}
41+
#### Pythagoras's Theorem
42+
43+
In a right triangle, the lengths of the two shorter sides $a$, $b$ and the longer side $c$ stand in the relation $$
44+
a^2 + b^2 = c^2.
45+
$$
46+
:::
47+
48+
See @thm-pythagoras for details.
49+
50+
{{< meta title >}}
51+
52+
| Col1 | Col2 | Col3 |
53+
|------|------|-----:|
54+
| 11 | 21 | 31 |
55+
| 12 | 22 | 32 |
56+
| 13 | 23 | 33 |
57+
58+
: table caption
59+
60+
@riehl2023
61+
62+
[@riehl2023]
63+
64+
hey footnote[^1]
65+
66+
[^1]: hello I'm a foot node
67+
68+
be careful of newline at end of file, roundtripping removes it:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
````{=markdown}
2+
```{=markdown}
3+
hi
4+
```
5+
````
6+
7+
```{{python}}
8+
a = 3
9+
```
10+
11+
> ```
12+
> 1+1
13+
> ```
14+
>
15+
> $$
16+
> 3 = 1 + 1 + 1 = 2 + 1 = 1 + 2
17+
> $$
18+
>
19+
> > block quote in block quote
20+
>
21+
> +-----+-------+
22+
> | $$ | ``` r |
23+
> | 1+0 | 1+1 |
24+
> | $$ | ``` |
25+
> +-----+-------+
26+
> | 3 | 4 |
27+
> +-----+-------+
28+
>
29+
> : my table caption
30+
>
31+
> ::: hello
32+
> fenced div in block quote
33+
> :::
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@article{riehl2023,
2+
title = {Could ∞-Category Theory Be Taught to Undergraduates?},
3+
author = {Riehl, Emily},
4+
year = {2023},
5+
month = {05},
6+
date = {2023-05-01},
7+
journal = {Notices of the American Mathematical Society},
8+
pages = {1},
9+
volume = {70},
10+
number = {05},
11+
doi = {10.1090/noti2692},
12+
url = {http://dx.doi.org/10.1090/noti2692},
13+
langid = {en}
14+
}

apps/vscode/src/test/examples/roundtrip-changes.qmd

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
bibliography: references.bib
3+
---
4+
5+
Welcome to valid basics 2.
6+
7+
```{=html}
8+
<div>hi</div>
9+
```
10+
11+
`hi`{=html}
12+
13+
```{{r}}
14+
1 + 1
15+
```
16+
17+
> This is a block quote
18+
> this is the same block quote
19+
20+
| this is a line block
21+
| this is another line block
22+
23+
This is *emphasized* text
24+
25+
This is also _emphasized_ text
26+
27+
hello [underline]{.underline} ~~strikethrough~~ ^super^ ~sub~ [smallcaps]{.smallcaps}
28+
29+
<!--# this is an html comment -->
30+
31+
definition term
32+
33+
: definition content
34+
35+
definition term 2
36+
37+
: definition content 2
38+
39+
------------------------------------------------------------------------
40+
41+
::: {#thm-pythagoras}
42+
#### Pythagoras's Theorem
43+
In a right triangle, the lengths of the two shorter sides $a$, $b$ and the longer side $c$ stand in the relation
44+
$$
45+
a^2 + b^2 = c^2.
46+
$$
47+
:::
48+
49+
See @thm-pythagoras for details.
50+
51+
{{< meta title >}}
52+
53+
| Col1 | Col2 | Col3 |
54+
|------|------|-----:|
55+
| 11 | 21 | 31 |
56+
| 12 | 22 | 32 |
57+
| 13 | 23 | 33 |
58+
59+
: table caption
60+
61+
@riehl2023
62+
63+
[@riehl2023]
64+
65+
hey footnote[^1]
66+
67+
[^1]: hello I'm a foot node
68+
69+
be careful of newline at end of file, roundtripping removes it:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
``````{=markdown}
2+
```{=markdown}
3+
hi
4+
```
5+
``````
6+
7+
````
8+
```{python}
9+
a = 3
10+
```
11+
````
12+
13+
> ```
14+
> 1+1
15+
> ```
16+
>
17+
> $$
18+
> 3 = 1 + 1 + 1 = 2 + 1 = 1 + 2
19+
> $$
20+
>
21+
> > block quote in block quote
22+
>
23+
> +-----+-------+
24+
> | $$ | ``` r |
25+
> | 1+0 | 1+1 |
26+
> | $$ | ``` |
27+
> +-----+-------+
28+
> | 3 | 4 |
29+
> +-----+-------+
30+
>
31+
> : my table caption
32+
>
33+
> ::: hello
34+
> fenced div in block quote
35+
> :::

apps/vscode/src/test/quartoDoc.test.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,34 @@ suite("Quarto basics", function () {
3030
assert.equal(before, after);
3131
});
3232

33-
test("Roundtrip changes roundtrip-changes.qmd", async function () {
34-
// We want this test to fail locally so that we can reference the
35-
// before/affter diff that Mocha logs, but we dont wan't CI to fail.
36-
if (process.env['CI']) this.skip();
33+
roundtripSnapshotTest('valid-basics.qmd');
3734

38-
const { doc } = await openAndShowTextDocument("roundtrip-changes.qmd");
35+
roundtripSnapshotTest('valid-basics-2.qmd');
3936

40-
const { before, after } = await roundtrip(doc);
41-
42-
assert.equal(before, after);
43-
});
37+
roundtripSnapshotTest('valid-nesting.qmd');
4438

45-
test("Roundtripped valid-basics.qmd matches snapshot", async function () {
46-
const { doc } = await openAndShowTextDocument("valid-basics.qmd");
39+
roundtripSnapshotTest('invalid.qmd');
4740

48-
const { after } = await roundtrip(doc);
49-
50-
assert.equal(after, await readOrCreateSnapshot("roundtripped-valid-basics.qmd", after));
51-
});
52-
53-
test("Roundtripped invalid.qmd matches snapshot", async function () {
54-
const { doc } = await openAndShowTextDocument("invalid.qmd");
55-
56-
const { after } = await roundtrip(doc);
41+
roundtripSnapshotTest('capsule-leak.qmd');
42+
});
5743

58-
assert.equal(after, await readOrCreateSnapshot("roundtripped-invalid.qmd", after));
59-
});
60-
test("Roundtripped capsule-leak.qmd matches snapshot", async function () {
61-
const { doc } = await openAndShowTextDocument("capsule-leak.qmd");
44+
/**
45+
*
46+
* When the test is run on the dev's machine for the first time, saves the roundtripped file as a snapshot.
47+
* All subsequent runs of the test compare the roundtripped file to that snapshot.
48+
*
49+
* Useful for capturing the behaviour of roundtripping at a point in time and testing against that.
50+
*
51+
* @param filename A .qmd file in the examples folder to snapshot test
52+
*/
53+
function roundtripSnapshotTest(filename: string) {
54+
const snapshotFileName = `roundtripped-${filename}`;
55+
56+
test(`Roundtripped ${filename} matches snapshot`, async function () {
57+
const { doc } = await openAndShowTextDocument(filename);
6258

6359
const { after } = await roundtrip(doc);
6460

65-
assert.equal(after, await readOrCreateSnapshot("roundtripped-capsule-leak.qmd", after));
61+
assert.equal(after, await readOrCreateSnapshot(snapshotFileName, after));
6662
});
67-
});
63+
}

apps/vscode/src/test/test-utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,28 @@ export async function roundtrip(doc: vscode.TextDocument) {
4949
return { before, after };
5050
}
5151

52+
const YELLOW_COLOR_ESCAPE_CODE = '\x1b[33m';
53+
const RESET_COLOR_ESCAPE_CODE = '\x1b[0m';
54+
5255
export async function readOrCreateSnapshot(fileName: string, content: string) {
5356
const snapshotUri = examplesUri(path.join('generated_snapshots', fileName));
5457
try {
5558
const doc = await vscode.workspace.openTextDocument(snapshotUri);
5659
return doc.getText();
5760
} catch {
61+
if (process.env['CI']) throw 'Attempted to create snapshot in CI!';
62+
63+
console.warn(`${YELLOW_COLOR_ESCAPE_CODE}
64+
⚠︎ Created snapshot in file:
65+
${snapshotUri}
66+
Please take a look at the snapshot file and ensure it is what you expect
67+
If it looks good to you, please commit the generated snapshot along with your test code
68+
If you did not intend to create a snapshot, please carefully check your test code and delete the snapshot file
69+
${RESET_COLOR_ESCAPE_CODE}`);
70+
5871
await vscode.workspace.fs.writeFile(
5972
snapshotUri,
60-
Buffer.from(content, 'utf8')
73+
Buffer.from(content, 'utf8') as Uint8Array
6174
);
6275
return content;
6376
}

0 commit comments

Comments
 (0)