11import { url } from "inspector" ;
2- import { cpus } from "os" ;
32import { isMainThread , Worker } from "worker_threads" ;
4- import { isNil , refreshEnvironmentCache , spawn } from "@compas/stdlib" ;
5- import { loadTestConfig } from "../../testing/config.js" ;
3+ import { isNil , spawn } from "@compas/stdlib" ;
4+ import { testingLoadConfig } from "../../testing/config.js" ;
65import { printTestResultsFromWorkers } from "../../testing/printer.js" ;
6+ import { testLogger } from "../../testing/state.js" ;
77import {
8- setAreTestRunning ,
9- setTestLogger ,
10- testLogger ,
11- } from "../../testing/state.js" ;
12- import {
13- listTestFiles ,
148 runTestsInProcess ,
9+ testingListFiles ,
1510 workerFile ,
1611} from "../../testing/worker-internal.js" ;
1712
@@ -96,13 +91,6 @@ export async function cliExecutor(logger, state) {
9691 } ;
9792 }
9893
99- if ( state . flags . coverage && ! isNil ( state . flags . randomizeRounds ) ) {
100- logger . error ( "Can't run '--coverage' with '--randomize-rounds'." ) ;
101- return {
102- exitStatus : "failed" ,
103- } ;
104- }
105-
10694 if ( state . flags . serial && ! isNil ( state . flags . parallelCount ) ) {
10795 logger . error ( "Can't specify both '--serial' and '--parallel-count'." ) ;
10896 return {
@@ -117,61 +105,46 @@ export async function cliExecutor(logger, state) {
117105 } ;
118106 }
119107
120- /** @type {number } */
121- // @ts -ignore
122- const parallelCount = state . flags . serial
123- ? 1
124- : state . flags . parallelCount ?? Math . min ( 4 , cpus ( ) . length - 1 ) ;
108+ // @ts -expect-error
109+ const testConfig = await testingLoadConfig ( logger , state . flags ) ;
125110
126- if ( state . flags . coverage ) {
111+ if ( testConfig . coverage ) {
127112 const { exitCode } = await spawn ( `npx` , [
128113 "c8" ,
129114 "node" ,
130115 process . argv [ 1 ] ,
131116 "test" ,
132117 "--parallel-count" ,
133- `${ parallelCount } ` ,
118+ String ( testConfig . parallelCount ) ,
119+ "--randomize-rounds" ,
120+ String ( testConfig . randomizeRounds ) ,
121+ "--with-logs" ,
122+ String ( testConfig . withLogs ) ,
123+ "--bail" ,
124+ String ( testConfig . bail ) ,
134125 ] ) ;
135126
136127 return {
137128 exitStatus : exitCode === 0 ? "passed" : "failed" ,
138129 } ;
139130 }
140131
141- state . flags . randomizeRounds = state . flags . randomizeRounds ?? 1 ;
142-
143- state . flags . withLogs = state . flags . withLogs ?? false ;
144-
145- process . env . _COMPAS_TEST_WITH_LOGS = String ( state . flags . withLogs ) ;
146- process . env . __COMPAS_TEST_PARALLEL_COUNT = String ( parallelCount ) ;
147- process . env . __COMPAS_TEST_RANDOMIZE_ROUNDS = String (
148- state . flags . randomizeRounds ,
149- ) ;
150- refreshEnvironmentCache ( ) ;
151-
152- // Make sure to set tests running, so `mainTestFn` is 'disabled'.
153- setAreTestRunning ( true ) ;
154- setTestLogger ( logger ) ;
155-
156- if ( parallelCount === 1 && state . flags . randomizeRounds === 1 ) {
132+ if ( testConfig . parallelCount === 1 && testConfig . randomizeRounds === 1 ) {
157133 // Run serial tests in the same process
158- const exitCode = await runTestsInProcess ( {
159- bail : ! ! state . flags . bail ,
160- } ) ;
134+ const exitCode = await runTestsInProcess ( testConfig ) ;
161135
162136 return {
163137 exitStatus : exitCode === 0 ? "passed" : "failed" ,
164138 } ;
165139 }
166140
167- await loadTestConfig ( ) ;
168- const files = await listTestFiles ( ) ;
141+ const files = await testingListFiles ( testConfig ) ;
169142
170143 // Almost does the same things as `mainTestFn`, however since tests are run by workers
171144 // instead of directly. We dispatch them, and then print the results.
172145 const results = [ ] ;
173146
174- for ( let i = 0 ; i < Number ( state . flags . randomizeRounds ) ; ++ i ) {
147+ for ( let i = 0 ; i < Number ( testConfig . randomizeRounds ) ; ++ i ) {
175148 if ( i !== 0 ) {
176149 // Shuffle files in place
177150 // From: https://stackoverflow.com/a/6274381
@@ -181,8 +154,8 @@ export async function cliExecutor(logger, state) {
181154 }
182155 }
183156
184- const workers = initializeWorkers ( parallelCount ) ;
185- const testResult = await runTests ( workers , files ) ;
157+ const workers = initializeWorkers ( testConfig . parallelCount ) ;
158+ const testResult = await runTests ( testConfig , workers , files ) ;
186159
187160 // Early exit on test failure
188161 const hasFailure = testResult . find ( ( it ) => it . isFailed ) ;
@@ -213,11 +186,11 @@ export async function cliExecutor(logger, state) {
213186 * Run tests on a worker pull-basis.
214187 * Once all files are done or in process, we request results.
215188 *
189+ * @param {import("../../testing/config.js").TestConfig } testConfig
216190 * @param {Worker[] } workers
217191 * @param {string[] } files
218192 */
219- async function runTests ( workers , files ) {
220- const isDebugging = ! ! url ( ) ;
193+ async function runTests ( testConfig , workers , files ) {
221194 let idx = 0 ;
222195 const results = [ ] ;
223196
@@ -256,9 +229,6 @@ async function runTests(workers, files) {
256229 worker . postMessage ( {
257230 type : "provide_file" ,
258231 file,
259- options : {
260- isDebugging,
261- } ,
262232 } ) ;
263233 }
264234 } else if ( message . type === "provide_result" ) {
0 commit comments