From e7cc35073f95800a3dc53cfa6b7f924f47ac618c Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 24 Feb 2021 18:34:14 +0100 Subject: [PATCH] =?UTF-8?q?Tune=20test=20extension=20=F0=9F=9B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Separate refresh button which does not rerun the test - Shows stdout and stderr of commands in the preview --- tools/test-helper/README.md | 7 ++-- tools/test-helper/extension.js | 41 ++++++++++++++++-------- tools/test-helper/images/rerun-dark.svg | 11 +++++++ tools/test-helper/images/rerun-light.svg | 11 +++++++ tools/test-helper/package.json | 37 ++++++++++++++------- 5 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 tools/test-helper/images/rerun-dark.svg create mode 100644 tools/test-helper/images/rerun-light.svg diff --git a/tools/test-helper/README.md b/tools/test-helper/README.md index d99aa4962..d3092d0d7 100644 --- a/tools/test-helper/README.md +++ b/tools/test-helper/README.md @@ -4,7 +4,8 @@ This is a small VS Code extension that helps with managing Typst's test suite. When installed, three new buttons appear in the menubar for all `.typ` files in the `tests` folder. -- Open test output: Opens the output and reference images of a test to the side. -- Refresh test output: Re-runs the test and reloads the preview. -- Approve test output: Copies the output into the reference folder and optimizes +- Open: Opens the output and reference images of a test to the side. +- Refresh: Refreshes the preview. +- Rerun: Re-runs the test. +- Approve: Copies the output into the reference folder and optimizes it with `oxipng`. diff --git a/tools/test-helper/extension.js b/tools/test-helper/extension.js index 775b2894b..e5325bed8 100644 --- a/tools/test-helper/extension.js +++ b/tools/test-helper/extension.js @@ -4,7 +4,7 @@ const cp = require('child_process') function activate(context) { let panel = null - function refreshPanel() { + function refreshPanel(stdout, stderr) { const uri = vscode.window.activeTextEditor.document.uri const { pngPath, refPath } = getPaths(uri) @@ -13,11 +13,15 @@ function activate(context) { const pngSrc = panel.webview.asWebviewUri(pngPath) const refSrc = panel.webview.asWebviewUri(refPath) panel.webview.html = '' - panel.webview.html = getWebviewContent(pngSrc, refSrc) + + // Make refresh notable. + setTimeout(() => { + panel.webview.html = getWebviewContent(pngSrc, refSrc, stdout, stderr) + }, 50) } } - const openCmd = vscode.commands.registerCommand("ShortcutMenuBar.openTestOutput", () => { + const openCmd = vscode.commands.registerCommand("ShortcutMenuBar.testOpen", () => { panel = vscode.window.createWebviewPanel( 'testOutput', 'Test output', @@ -25,10 +29,14 @@ function activate(context) { {} ) - refreshPanel() + refreshPanel("", "") }) - const refreshCmd = vscode.commands.registerCommand("ShortcutMenuBar.refreshTestOutput", () => { + const refreshCmd = vscode.commands.registerCommand("ShortcutMenuBar.testRefresh", () => { + refreshPanel("", "") + }) + + const rerunCmd = vscode.commands.registerCommand("ShortcutMenuBar.testRerun", () => { const uri = vscode.window.activeTextEditor.document.uri const components = uri.fsPath.split('tests') const dir = components[0] @@ -37,29 +45,27 @@ function activate(context) { cp.exec( `cargo test --manifest-path ${dir}/Cargo.toml --test typeset ${subPath}`, (err, stdout, stderr) => { - console.log(stdout) - console.log(stderr) - refreshPanel() + console.log('Ran tests') + refreshPanel(stdout, stderr) } ) }) - const approveCmd = vscode.commands.registerCommand("ShortcutMenuBar.approveTestOutput", () => { + const approveCmd = vscode.commands.registerCommand("ShortcutMenuBar.testApprove", () => { const uri = vscode.window.activeTextEditor.document.uri const { pngPath, refPath } = getPaths(uri) vscode.workspace.fs.copy(pngPath, refPath, { overwrite: true }).then(() => { console.log('Copied to reference file') cp.exec(`oxipng -o max -a ${refPath.fsPath}`, (err, stdout, stderr) => { - console.log(stdout) - console.log(stderr) - refreshPanel() + refreshPanel(stdout, stderr) }) }) }) context.subscriptions.push(openCmd) context.subscriptions.push(refreshCmd) + context.subscriptions.push(rerunCmd) context.subscriptions.push(approveCmd) } @@ -75,7 +81,7 @@ function getPaths(uri) { return { pngPath, refPath } } -function getWebviewContent(pngSrc, refSrc) { +function getWebviewContent(pngSrc, refSrc, stdout, stderr) { return ` @@ -95,6 +101,9 @@ function getWebviewContent(pngSrc, refSrc) { max-height: 40vh; object-fit: contain; } + pre { + font-family: var(--vscode-editor-font-family); + } @@ -103,6 +112,12 @@ function getWebviewContent(pngSrc, refSrc) {

Reference image

+ +

Standard output

+
${stdout}
+ +

Standard error

+
${stderr}
` diff --git a/tools/test-helper/images/rerun-dark.svg b/tools/test-helper/images/rerun-dark.svg new file mode 100644 index 000000000..7a916fb44 --- /dev/null +++ b/tools/test-helper/images/rerun-dark.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tools/test-helper/images/rerun-light.svg b/tools/test-helper/images/rerun-light.svg new file mode 100644 index 000000000..b4d5e092f --- /dev/null +++ b/tools/test-helper/images/rerun-light.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tools/test-helper/package.json b/tools/test-helper/package.json index 55065838c..157df0cb7 100644 --- a/tools/test-helper/package.json +++ b/tools/test-helper/package.json @@ -10,15 +10,16 @@ "Other" ], "activationEvents": [ - "onCommand:ShortcutMenuBar.openTestOutput", - "onCommand:ShortcutMenuBar.approveTestOutput", - "onCommand:ShortcutMenuBar.refreshTestOutput" + "onCommand:ShortcutMenuBar.testOpen", + "onCommand:ShortcutMenuBar.testRefresh", + "onCommand:ShortcutMenuBar.testRerun", + "onCommand:ShortcutMenuBar.testApprove" ], "main": "./extension.js", "contributes": { "commands": [ { - "command": "ShortcutMenuBar.openTestOutput", + "command": "ShortcutMenuBar.testOpen", "title": "Open test output", "category": "ShortcutMenuBar", "icon": { @@ -27,8 +28,8 @@ } }, { - "command": "ShortcutMenuBar.refreshTestOutput", - "title": "Refresh test output", + "command": "ShortcutMenuBar.testRefresh", + "title": "Refresh preview", "category": "ShortcutMenuBar", "icon": { "light": "images/refresh-light.svg", @@ -36,8 +37,17 @@ } }, { - "command": "ShortcutMenuBar.approveTestOutput", - "title": "Approve test output", + "command": "ShortcutMenuBar.testRerun", + "title": "Rerun test", + "category": "ShortcutMenuBar", + "icon": { + "light": "images/rerun-light.svg", + "dark": "images/rerun-dark.svg" + } + }, + { + "command": "ShortcutMenuBar.testApprove", + "title": "Approve output", "category": "ShortcutMenuBar", "icon": { "light": "images/approve-light.svg", @@ -49,17 +59,22 @@ "editor/title": [ { "when": "resourceExtname == .typ && resourcePath =~ /.*tests.*/", - "command": "ShortcutMenuBar.openTestOutput", + "command": "ShortcutMenuBar.testOpen", "group": "navigation@0" }, { "when": "resourceExtname == .typ && resourcePath =~ /.*tests.*/", - "command": "ShortcutMenuBar.refreshTestOutput", + "command": "ShortcutMenuBar.testRefresh", + "group": "navigation@1" + }, + { + "when": "resourceExtname == .typ && resourcePath =~ /.*tests.*/", + "command": "ShortcutMenuBar.testRerun", "group": "navigation@2" }, { "when": "resourceExtname == .typ && resourcePath =~ /.*tests.*/", - "command": "ShortcutMenuBar.approveTestOutput", + "command": "ShortcutMenuBar.testApprove", "group": "navigation@3" } ]