summaryrefslogtreecommitdiff
path: root/tools/test-helper/extension.js
blob: ad157bcb6f4b508f7c10aaa1255be5339e6410c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const vscode = require('vscode')
const cp = require('child_process')

function activate(context) {
    let panel = null

    function refreshPanel(stdout, stderr) {
        const uri = vscode.window.activeTextEditor.document.uri
        const { pngPath, refPath } = getPaths(uri)

        if (panel && panel.visible) {
            console.log('Refreshing WebView')
            const pngSrc = panel.webview.asWebviewUri(pngPath)
            const refSrc = panel.webview.asWebviewUri(refPath)
            panel.webview.html = ''

            // Make refresh notable.
            setTimeout(() => {
                panel.webview.html = getWebviewContent(pngSrc, refSrc, stdout, stderr)
            }, 50)
        }
    }

    const openCmd = vscode.commands.registerCommand("ShortcutMenuBar.testOpen", () => {
        panel = vscode.window.createWebviewPanel(
            'testOutput',
            'Test output',
            vscode.ViewColumn.Beside,
            {}
        )

        refreshPanel("", "")
    })

    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]
        const subPath = components[1]

        cp.exec(
            `cargo test --manifest-path ${dir}/Cargo.toml --test typeset ${subPath}`,
            (err, stdout, stderr) => {
                console.log('Ran tests')
                refreshPanel(stdout, stderr)
            }
        )
    })

    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) => {
                refreshPanel(stdout, stderr)
            })
        })
    })

    context.subscriptions.push(openCmd)
    context.subscriptions.push(refreshCmd)
    context.subscriptions.push(rerunCmd)
    context.subscriptions.push(approveCmd)
}

function getPaths(uri) {
    const pngPath = vscode.Uri.file(uri.path
        .replace("tests/typ", "tests/png")
        .replace(".typ", ".png"))

    const refPath = vscode.Uri.file(uri.path
        .replace("tests/typ", "tests/ref")
        .replace(".typ", ".png"))

    return { pngPath, refPath }
}

function getWebviewContent(pngSrc, refSrc, stdout, stderr) {
    return `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Test output</title>
        <style>
        body, html {
            width: 100%;
            margin: 0;
            padding: 0;
            text-align: center;
        }
        img {
            width: 80%;
            max-height: 40vh;
            object-fit: contain;
        }
        pre {
            display: inline-block;
            font-family: var(--vscode-editor-font-family);
            text-align: left;
            width: 80%;
        }
        </style>
    </head>
    <body>
        <h1>Output image</h1>
        <img src="${pngSrc}"/>

        <h1>Reference image</h1>
        <img src="${refSrc}"/>

        <h1>Standard output</h1>
        <pre>${stdout}</pre>

        <h1>Standard error</h1>
        <pre>${stderr}</pre>
    </body>
    </html>
    `
}

function deactivate() {}

module.exports = { activate, deactivate }