Skip to content

Commit dd3098b

Browse files
authored
Add some roundtripping vscode tests (#773)
* Simplify suite setup, clean up test code * Add start of roundtrip-failures
1 parent 196c3a5 commit dd3098b

File tree

4 files changed

+64
-61
lines changed

4 files changed

+64
-61
lines changed

apps/vscode/src/test/examples/hello.qmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ format: html
1010
```
1111

1212
*YO!*
13+
14+
note the lack of newline at the end of this file.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
``````{=markdown}
2+
```{=markdown}
3+
hi
4+
```
5+
``````
6+
7+
kBlockCapsuleSentinel uuid sentinel leak during SE→VE
8+
``````{{python}}
9+
```
10+
dog
11+
```
12+
``````
13+
14+
`````
15+
```{python}
16+
a = 3
17+
````
18+
`````
19+
20+
_YO!_
21+
22+
be careful of newline at end of file, roundtripping removes it:
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
11
import * as vscode from "vscode";
22
import * as assert from "assert";
3-
import { exampleWorkspacePath, exampleWorkspaceOutPath, copyFile, wait } from "./test-utils";
3+
import { WORKSPACE_PATH, examplesOutUri, wait } from "./test-utils";
44
import { isQuartoDoc } from "../core/doc";
55
import { extension } from "./extension";
66

77
const APPROX_TIME_TO_OPEN_VISUAL_EDITOR = 1700;
88

99
suite("Quarto basics", function () {
10-
// Before we run any tests, we should copy any files that get edited in the tests to file under `exampleWorkspaceOutPath`
10+
// Before running tests, copy `./examples` to a new folder `./examples-out`.
11+
// We copy to examples-out because the tests modify the files.
1112
suiteSetup(async function () {
12-
const didCopyFile = await copyFile(exampleWorkspacePath('hello.qmd'), exampleWorkspaceOutPath('hello.qmd'));
13-
assert.ok(didCopyFile);
13+
await vscode.workspace.fs.delete(examplesOutUri(), { recursive: true });
14+
await vscode.workspace.fs.copy(vscode.Uri.file(WORKSPACE_PATH), examplesOutUri());
1415
});
1516

1617
test("Can open a Quarto document", async function () {
17-
const doc = await vscode.workspace.openTextDocument(exampleWorkspaceOutPath("hello.qmd"));
18+
const doc = await vscode.workspace.openTextDocument(examplesOutUri("hello.qmd"));
1819
const editor = await vscode.window.showTextDocument(doc);
1920

2021
assert.strictEqual(editor?.document.languageId, "quarto");
2122
assert.strictEqual(isQuartoDoc(editor?.document), true);
2223
});
2324

24-
// Note: the following tests may be flaky. They rely on waiting estimated amounts of time for commands to complete.
25-
test("Can edit in visual mode", async function () {
26-
const doc = await vscode.workspace.openTextDocument(exampleWorkspaceOutPath("hello.qmd"));
27-
const editor = await vscode.window.showTextDocument(doc);
28-
29-
await extension().activate();
30-
31-
// manually confirm visual mode so dialogue pop-up doesn't show because dialogues cause test errors
32-
// and switch to visual editor
33-
await vscode.commands.executeCommand("quarto.test_setkVisualModeConfirmedTrue");
34-
await wait(300); // It seems necessary to wait around 300ms for this command to be done.
35-
await vscode.commands.executeCommand("quarto.editInVisualMode");
36-
await wait(APPROX_TIME_TO_OPEN_VISUAL_EDITOR);
37-
38-
assert.ok(await vscode.commands.executeCommand("quarto.test_isInVisualEditor"));
39-
});
40-
// Note: this test runs after the previous test, so `hello.qmd` has already been touched by the previous
25+
// Note: this test runs after the previous test, so `hello.qmd` can already be touched by the previous
4126
// test. That's okay for this test, but could cause issues if you expect a qmd to look how it
4227
// does in `/examples`.
4328
test("Roundtrip doesn't change hello.qmd", async function () {
44-
const doc = await vscode.workspace.openTextDocument(exampleWorkspaceOutPath("hello.qmd"));
29+
const doc = await vscode.workspace.openTextDocument(examplesOutUri("hello.qmd"));
4530
const editor = await vscode.window.showTextDocument(doc);
4631

47-
await extension().activate();
32+
const { before, after } = await roundtrip(doc);
4833

49-
const docTextBefore = doc.getText();
34+
assert.equal(before, after);
35+
});
5036

51-
// switch to visual editor and back
52-
await vscode.commands.executeCommand("quarto.test_setkVisualModeConfirmedTrue");
53-
await wait(300);
54-
await vscode.commands.executeCommand("quarto.editInVisualMode");
55-
await wait(APPROX_TIME_TO_OPEN_VISUAL_EDITOR);
56-
await vscode.commands.executeCommand("quarto.editInSourceMode");
57-
await wait(300);
37+
test("Roundtrip does change roundtrip-failures.qmd", async function () {
38+
// We want this test to fail locally so that we can reference the
39+
// before/affter diff that Mocha logs, but we dont wan't CI to fail.
40+
if (process.env['CI']) this.skip();
5841

59-
const docTextAfter = doc.getText();
60-
assert.ok(docTextBefore === docTextAfter, docTextAfter);
42+
const doc = await vscode.workspace.openTextDocument(examplesOutUri("roundtrip-failures.qmd"));
43+
const editor = await vscode.window.showTextDocument(doc);
44+
45+
const { before, after } = await roundtrip(doc);
46+
47+
assert.equal(before, after);
6148
});
6249
});
50+
51+
async function roundtrip(doc: vscode.TextDocument) {
52+
const before = doc.getText();
53+
54+
// switch to visual editor and back
55+
await vscode.commands.executeCommand("quarto.test_setkVisualModeConfirmedTrue");
56+
await wait(300);
57+
await vscode.commands.executeCommand("quarto.editInVisualMode");
58+
await wait(APPROX_TIME_TO_OPEN_VISUAL_EDITOR);
59+
await vscode.commands.executeCommand("quarto.editInSourceMode");
60+
await wait(300);
61+
62+
const after = doc.getText();
63+
64+
return { before, after };
65+
}

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

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,12 @@ export const EXTENSION_ROOT_DIR =
1313

1414
export const TEST_PATH = path.join(EXTENSION_ROOT_DIR, "src", "test");
1515
export const WORKSPACE_PATH = path.join(TEST_PATH, "examples");
16+
export const WORKSPACE_OUT_PATH = path.join(TEST_PATH, "examples-out");
1617

17-
export function exampleWorkspacePath(file: string): string {
18-
return path.join(WORKSPACE_PATH, file);
19-
}
20-
export function exampleWorkspaceOutPath(file: string): string {
21-
return path.join(WORKSPACE_PATH, 'examples-out', file);
18+
export function examplesOutUri(fileName: string = ''): vscode.Uri {
19+
return vscode.Uri.file(path.join(WORKSPACE_OUT_PATH, fileName));
2220
}
2321

2422
export function wait(ms: number) {
2523
return new Promise(resolve => setTimeout(resolve, ms));
2624
}
27-
28-
export async function copyFile(
29-
sourcePath: string,
30-
destPath: string,
31-
): Promise<boolean> {
32-
try {
33-
const wsedit = new vscode.WorkspaceEdit();
34-
const data = await vscode.workspace.fs.readFile(
35-
vscode.Uri.file(sourcePath)
36-
);
37-
const destFileUri = vscode.Uri.file(destPath);
38-
wsedit.createFile(destFileUri, { ignoreIfExists: true });
39-
40-
await vscode.workspace.fs.writeFile(destFileUri, data);
41-
42-
let isDone = await vscode.workspace.applyEdit(wsedit);
43-
if (isDone) return true;
44-
else return false;
45-
} catch (err) {
46-
return false;
47-
}
48-
}

0 commit comments

Comments
 (0)