summaryrefslogtreecommitdiffstats
path: root/util/wasm/batchedtestrunner/batchedtestrunner.js
blob: 9a7597b7b81fa4d1654225f89b43d0c3447a7b4f (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

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

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

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

class RunnerStatus {
    static Running = 'Running';
    static Completed = 'Completed';
    static Error = 'Error';
}

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

// Represents the public API of the runner.
class WebApi {
    #results = new Map();
    #status = RunnerStatus.Running;
    #statusChangedEventPrivate;
    #testStatusChangedEventPrivate;

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

    // The callback receives the private interface of this object, meant not to be used by the
    // end user on the web side.
    constructor(receivePrivateInterface) {
        receivePrivateInterface({
            registerTest: testName => this.#registerTest(testName),
            setTestStatus: (testName, status) => this.#setTestStatus(testName, status),
            setTestResultData: (testName, testStatus, exitCode, textOutput) =>
                this.#setTestResultData(testName, testStatus, exitCode, textOutput),
            setTestRunnerStatus: status => this.#setTestRunnerStatus(status),
        });
    }

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

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

    #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, textOutput) {
        const testData = this.#results.get(testName);
        const statusChanged = testStatus !== testData.status;
        testData.status = testStatus;
        testData.exitCode = exitCode;
        testData.textOutput = textOutput;
        if (statusChanged)
            this.#testStatusChangedEventPrivate.fireEvent(testName, testStatus);
    }

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

class BatchedTestRunner {
    static #TestBatchModuleName = 'test_batch';

    #loader;
    #privateWebApi;

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

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

        const testsToExecute = testName ? [testName] : await this.#getTestClassNames(module);
        testsToExecute.forEach(testClassName => this.#privateWebApi.registerTest(testClassName));
        for (const testClassName of testsToExecute) {
            let result = {};
            this.#privateWebApi.setTestStatus(testClassName, TestStatus.Running);

            try {
                const LogToStdoutSpecialFilename = '-';
                result = await module.exec({
                    args: [testClassName, '-o', `${LogToStdoutSpecialFilename},xml`],
                });

                if (result.exitCode < 0)
                    throw new ProgramError(result.exitCode);
                result.status = TestStatus.Completed;
            } catch (e) {
                result.status = e instanceof ProgramError ? TestStatus.Error : TestStatus.Crashed;
                result.stdout = e instanceof AbortedError ? e.stdout : result.stdout;
            }
            this.#privateWebApi.setTestResultData(
                testClassName, result.status, result.exitCode, result.stdout);
            }
        }

    async run(testName) {
        try {
            await this.#doRun(testName);
            this.#privateWebApi.setTestRunnerStatus(RunnerStatus.Completed);
        } catch (e) {
            this.#privateWebApi.setTestRunnerStatus(RunnerStatus.Error);
        }
    }

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

(() => {
    let privateWebApi;
    window.qtTestRunner = new WebApi(privateApi => privateWebApi = privateApi);

    const parsed = parseQuery(location.search);
    const testName = parsed['qtestname'];
    if (typeof testName !== 'undefined' && (typeof testName !== 'string' || testName === '')) {
        console.error('The testName parameter is incorrect');
        return;
    }

    const resourceLocator = new ResourceLocator('');
    const testRunner = new BatchedTestRunner(
        new ModuleLoader(new ResourceFetcher(resourceLocator), resourceLocator),
        privateWebApi
    );

    testRunner.run(testName);
})();