|
1 | 1 | import * as vscode from "vscode"; |
2 | 2 | import * as assert from "assert"; |
3 | | -import { exampleWorkspacePath, exampleWorkspaceOutPath, copyFile, wait } from "./test-utils"; |
| 3 | +import { WORKSPACE_PATH, examplesOutUri, wait } from "./test-utils"; |
4 | 4 | import { isQuartoDoc } from "../core/doc"; |
5 | 5 | import { extension } from "./extension"; |
6 | 6 |
|
7 | 7 | const APPROX_TIME_TO_OPEN_VISUAL_EDITOR = 1700; |
8 | 8 |
|
9 | 9 | 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. |
11 | 12 | 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()); |
14 | 15 | }); |
15 | 16 |
|
16 | 17 | 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")); |
18 | 19 | const editor = await vscode.window.showTextDocument(doc); |
19 | 20 |
|
20 | 21 | assert.strictEqual(editor?.document.languageId, "quarto"); |
21 | 22 | assert.strictEqual(isQuartoDoc(editor?.document), true); |
22 | 23 | }); |
23 | 24 |
|
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 |
41 | 26 | // test. That's okay for this test, but could cause issues if you expect a qmd to look how it |
42 | 27 | // does in `/examples`. |
43 | 28 | 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")); |
45 | 30 | const editor = await vscode.window.showTextDocument(doc); |
46 | 31 |
|
47 | | - await extension().activate(); |
| 32 | + const { before, after } = await roundtrip(doc); |
48 | 33 |
|
49 | | - const docTextBefore = doc.getText(); |
| 34 | + assert.equal(before, after); |
| 35 | + }); |
50 | 36 |
|
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(); |
58 | 41 |
|
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); |
61 | 48 | }); |
62 | 49 | }); |
| 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 | +} |
0 commit comments