summaryrefslogtreecommitdiffstats
path: root/util/wasm/batchedtestrunner/batchedtestrunner.js
blob: 7cd5f2d3506fcf1e356ef60e2d5fd9891bbfa120 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import {
    AbortedError,
} from './qwasmjsruntime.js';

import { EventSource } from './util.js';

class ProgramError extends Error {
    constructor(exitCode) {
        super(`The program reported an exit code of ${exitCode}`)
    }
}

export class RunnerStatus {
    static Running = 'Running';
    static Passed = 'Passed';
    static Error = 'Error';
    static TestCrashed = 'TestCrashed';
    static TestsFailed = 'TestsFailed';
}

export class TestStatus {
    static Pending = 'Pending';
    static Running = 'Running';
    static Completed = 'Completed';
    static Error = 'Error';
    static Failed = 'Failed';
    static Crashed = 'Crashed';
}

export class BatchedTestRunner {
    static #TestBatchModuleName = 'test_batch';

    #loader;

    #results = new Map();
    #status = RunnerStatus.Running;
    #numberOfFailed = 0;
    #statusChangedEventPrivate;
    #testStatusChangedEventPrivate;
    #testOutputChangedEventPrivate;
    #errorDetails;

    onStatusChanged =
        new EventSource((privateInterface) => this.#statusChangedEventPrivate = privateInterface);
    onTestStatusChanged =
        new EventSource((privateInterface) =>
            this.#testStatusChangedEventPrivate = privateInterface);
    onTestOutputChanged =
        new EventSource(
            (privateInterface) => this.#testOutputChangedEventPrivate = privateInterface);

    constructor(loader) {
        this.#loader = loader;
    }

    get results() { return this.#results; }

    get status() { return this.#status; }

    get numberOfFailed() {
        if (this.#status !== RunnerStatus.TestsFailed)
            throw new Error(`numberOfFailed called with status=${this.#status}`);
        return this.#numberOfFailed;
    }

    get errorDetails() { return this.#errorDetails; }

    async run(targetIsBatch, testName, functions, testOutputFormat) {
        try {
            await this.#doRun(targetIsBatch, testName, functions, testOutputFormat);
        } catch (e) {
            this.#setTestRunnerError(e.message);
            return;
        }

        const status = (() => {
            const hasAnyCrashedTest =
                !![...window.qtTestRunner.results.values()].find(
                    result => result.status === TestStatus.Crashed);
            if (hasAnyCrashedTest)
                return { code: RunnerStatus.TestCrashed };
            const numberOfFailed = [...window.qtTestRunner.results.values()].reduce(
                (previous, current) => previous + current.exitCode, 0);
            return {
                code: (numberOfFailed ? RunnerStatus.TestsFailed : RunnerStatus.Passed),
                numberOfFailed
            };
        })();

        this.#setTestRunnerStatus(status.code, status.numberOfFailed);
    }

    async #doRun(targetIsBatch, testName, functions, testOutputFormat) {
        const module = await this.#loader.loadEmscriptenModule(
            targetIsBatch ? BatchedTestRunner.#TestBatchModuleName : testName,
            () => { }
        );

        const testsToExecute = (testName || !targetIsBatch)
            ? [testName] : await this.#getTestClassNames(module);
        testsToExecute.forEach(testClassName => this.#registerTest(testClassName));

        for (const testClassName of testsToExecute) {
            let result = {};
            this.#setTestStatus(testClassName, TestStatus.Running);

            try {
                const LogToStdoutSpecialFilename = '-';
                result = await module.exec({
                    args: [...(targetIsBatch ? [testClassName] : []),
                           ...(functions ?? []),
                           '-o', `${LogToStdoutSpecialFilename},${testOutputFormat}`],
                    onStdout: (output) => {
                        this.#addTestOutput(testClassName, output);
                    }
                });

                if (result.exitCode < 0)
                    throw new ProgramError(result.exitCode);
                result.status = result.exitCode > 0 ? TestStatus.Failed : TestStatus.Completed;
                // Yield to other tasks on the main thread.
                await new Promise(resolve => window.setTimeout(resolve, 0));
            } catch (e) {
                result.status = e instanceof ProgramError ? TestStatus.Error : TestStatus.Crashed;
                result.stdout = e instanceof AbortedError ? e.stdout : result.stdout;
            }
            this.#setTestResultData(testClassName, result.status, result.exitCode);
        }
    }

    async #getTestClassNames(module) {
        return (await module.exec()).stdout.trim().split(' ');
    }

    #registerTest(testName) {
        this.#results.set(testName, { status: TestStatus.Pending, output: [] });
    }

    #setTestStatus(testName, status) {
        const testData = this.#results.get(testName);
        if (testData.status === status)
            return;
        this.#results.get(testName).status = status;
        this.#testStatusChangedEventPrivate.fireEvent(testName, status);
    }

    #setTestResultData(testName, testStatus, exitCode) {
        const testData = this.#results.get(testName);
        const statusChanged = testStatus !== testData.status;
        testData.status = testStatus;
        testData.exitCode = exitCode;
        if (statusChanged)
            this.#testStatusChangedEventPrivate.fireEvent(testName, testStatus);
    }

    #setTestRunnerStatus(status, numberOfFailed) {
        if (status === this.#status)
            return;
        this.#status = status;
        this.#numberOfFailed = numberOfFailed;
        this.#statusChangedEventPrivate.fireEvent(status);
    }

    #setTestRunnerError(details) {
        this.#status = RunnerStatus.Error;
        this.#errorDetails = details;
        this.#statusChangedEventPrivate.fireEvent(this.#status);
    }

    #addTestOutput(testName, output) {
        const testData = this.#results.get(testName);
        testData.output.push(output);
        this.#testOutputChangedEventPrivate.fireEvent(testName, testData.output);
    }
}