Skip to content

Commit 8fd4f08

Browse files
committed
test: test runner cli processing in electron
1 parent d897409 commit 8fd4f08

2 files changed

Lines changed: 60 additions & 16 deletions

File tree

test/UnitTestReporter.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,34 @@ define(function (require, exports, module) {
6565
return '';
6666
}
6767

68+
function hasCliFlag(args, flagName) {
69+
return args.some(arg => arg === `--${flagName}` || arg.startsWith(`--${flagName}=`));
70+
}
71+
6872
function quitIfNeeded(exitStatus) {
69-
if(!window.__TAURI__){
73+
const isTauri = !!window.__TAURI__;
74+
const isElectron = !!window.electronAppAPI?.isElectron;
75+
76+
if (!isTauri && !isElectron) {
7077
return;
7178
}
79+
7280
const WAIT_TIME_TO_COMPLETE_TEST_LOGGING_SEC = 10;
7381
console.log("Scheduled Quit in Seconds: ", WAIT_TIME_TO_COMPLETE_TEST_LOGGING_SEC);
74-
setTimeout(()=>{
75-
window.__TAURI__.cli.getMatches().then(matches=>{
76-
if(matches && matches.args["quit-when-done"] && matches.args["quit-when-done"].occurrences) {
77-
window.__TAURI__.process.exit(exitStatus);
78-
}
79-
});
82+
setTimeout(() => {
83+
if (isTauri) {
84+
window.__TAURI__.cli.getMatches().then(matches => {
85+
if (matches && matches.args["quit-when-done"] && matches.args["quit-when-done"].occurrences) {
86+
window.__TAURI__.process.exit(exitStatus);
87+
}
88+
});
89+
} else if (isElectron) {
90+
window.electronAppAPI.getCliArgs().then(args => {
91+
if (hasCliFlag(args, 'quit-when-done')) {
92+
window.electronAppAPI.quitApp(exitStatus);
93+
}
94+
});
95+
}
8096
}, WAIT_TIME_TO_COMPLETE_TEST_LOGGING_SEC * 1000);
8197
}
8298

test/index-dist-test.html

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,48 @@
44
<meta charset="UTF-8">
55
<title>Starting tests...</title>
66
<script type="text/javascript">
7+
function navigateToTests(testSuiteToExec) {
8+
if (testSuiteToExec) {
9+
location.href = `test/SpecRunner.html?spec=All&category=${testSuiteToExec}`;
10+
} else {
11+
location.href = `test/SpecRunner.html`;
12+
}
13+
}
14+
15+
function parseCliArg(args, argName) {
16+
// Parse --argName=value or --argName value from array
17+
for (let i = 0; i < args.length; i++) {
18+
const arg = args[i];
19+
if (arg.startsWith(`--${argName}=`)) {
20+
return arg.split('=')[1];
21+
}
22+
if (arg === `--${argName}` && args[i + 1] && !args[i + 1].startsWith('--')) {
23+
return args[i + 1];
24+
}
25+
}
26+
return null;
27+
}
28+
729
function redirectToTestPage() {
8-
if(window.__TAURI__) {
9-
__TAURI__.window.appWindow.setFocus().catch(console.error).finally(()=>{
10-
__TAURI__.cli.getMatches().then(matches=>{
30+
if (window.__TAURI__) {
31+
// Tauri path
32+
__TAURI__.window.appWindow.setFocus().catch(console.error).finally(() => {
33+
__TAURI__.cli.getMatches().then(matches => {
1134
const testSuiteToExec = matches.args["run-tests"].value;
12-
if(testSuiteToExec){
13-
location.href = `test/SpecRunner.html?spec=All&category=${testSuiteToExec}`;
14-
} else {
15-
location.href = `test/SpecRunner.html`;
16-
}
35+
navigateToTests(testSuiteToExec);
36+
}).catch(console.error);
37+
});
38+
} else if (window.electronAppAPI?.isElectron) {
39+
// Electron path
40+
window.electronAPI.focusWindow().catch(console.error).finally(() => {
41+
window.electronAppAPI.getCliArgs().then(args => {
42+
const testSuiteToExec = parseCliArg(args, 'run-tests');
43+
navigateToTests(testSuiteToExec);
1744
}).catch(console.error);
1845
});
1946
} else {
20-
location.href = "test/SpecRunner.html";
47+
// Browser fallback
48+
navigateToTests(null);
2149
}
2250
}
2351
</script>

0 commit comments

Comments
 (0)