Skip to content

Commit 7d706b4

Browse files
committed
fix: serial log can be truncated at end of wokwi-cli run #38
Don't use `process.exit(0)` in case of success - instead, let the code finish executing and terminate properly.
1 parent 8108a85 commit 7d706b4

File tree

3 files changed

+44
-47
lines changed

3 files changed

+44
-47
lines changed

packages/cli/src/ExpectEngine.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ import { EventEmitter } from 'events';
22

33
export class ExpectEngine extends EventEmitter {
44
readonly expectTexts: string[] = [];
5-
readonly failTexts: string[] = [];
6-
75
private currentLine = '';
86

9-
onMatch(type: 'match' | 'fail', text: string) {
10-
this.emit(type, text);
11-
}
12-
137
feed(bytes: number[]) {
148
for (const byte of bytes) {
159
const char = String.fromCharCode(byte);
@@ -25,15 +19,28 @@ export class ExpectEngine extends EventEmitter {
2519
}
2620
}
2721

22+
/**
23+
* Waits for a specific text to appear in the input stream.
24+
*
25+
* @param text The text to wait for.
26+
* @returns A promise that resolves when the text is matched.
27+
*/
28+
async waitForMatch(text: string): Promise<void> {
29+
this.expectTexts.push(text);
30+
return new Promise<void>((resolve) => {
31+
this.on('match', (matchedText) => {
32+
if (matchedText === text) {
33+
this.expectTexts.splice(this.expectTexts.indexOf(text), 1);
34+
resolve();
35+
}
36+
});
37+
});
38+
}
39+
2840
private testMatches() {
2941
for (const candidate of this.expectTexts) {
3042
if (this.currentLine.includes(candidate)) {
31-
this.onMatch('match', candidate);
32-
}
33-
}
34-
for (const candidate of this.failTexts) {
35-
if (this.currentLine.includes(candidate)) {
36-
this.onMatch('fail', candidate);
43+
this.emit('match', candidate);
3744
}
3845
}
3946
}

packages/cli/src/TestScenario.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class TestScenario {
8282
await this.executeStep(client, step);
8383
}
8484
this.log(chalkTemplate`{green Scenario completed successfully}`);
85-
process.exit(0);
8685
}
8786

8887
async executeStep(client: APIClient, step: IStepDefinition) {

packages/cli/src/main.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async function main() {
8484

8585
if (args['--version'] === true || args['--short-version'] === true) {
8686
printVersion(args['--short-version']);
87-
process.exit(0);
87+
return;
8888
}
8989

9090
if (!quiet) {
@@ -93,12 +93,12 @@ async function main() {
9393

9494
if (args['--help']) {
9595
cliHelp();
96-
process.exit(0);
96+
return;
9797
}
9898

9999
if (args._[0] === 'init') {
100100
await initProjectWizard(args._[1] ?? '.', { diagramFile });
101-
process.exit(0);
101+
return;
102102
}
103103

104104
const token = process.env.WOKWI_CLI_TOKEN;
@@ -250,36 +250,6 @@ async function main() {
250250

251251
const serialLogStream = serialLogFile ? createWriteStream(serialLogFile) : null;
252252

253-
if (expectText) {
254-
expectEngine.expectTexts.push(expectText);
255-
expectEngine.on('match', (text) => {
256-
if (text !== expectText) {
257-
return;
258-
}
259-
260-
if (!quiet) {
261-
console.log(chalkTemplate`\n\nExpected text found: {green "${expectText}"}`);
262-
console.log('TEST PASSED.');
263-
}
264-
client.close();
265-
process.exit(0);
266-
});
267-
}
268-
269-
if (failText) {
270-
expectEngine.failTexts.push(failText);
271-
expectEngine.on('fail', (text) => {
272-
if (text !== failText) {
273-
return;
274-
}
275-
276-
console.error(chalkTemplate`\n\n{red Error:} Unexpected text found: {yellow "${text}"}`);
277-
console.error('TEST FAILED.');
278-
client.close();
279-
process.exit(1);
280-
});
281-
}
282-
283253
const transport = new WebSocketTransport(token, DEFAULT_SERVER, version, sha);
284254
const client = new APIClient(transport);
285255
client.onConnected = (hello) => {
@@ -309,8 +279,29 @@ async function main() {
309279
}
310280

311281
const promises = [];
312-
let screenshotPromise = Promise.resolve();
313282

283+
if (expectText) {
284+
promises.push(
285+
expectEngine.waitForMatch(expectText).then(() => {
286+
if (!quiet) {
287+
console.log(chalkTemplate`\n\nExpected text found: {green "${expectText}"}`);
288+
console.log('TEST PASSED.');
289+
}
290+
}),
291+
);
292+
}
293+
if (failText) {
294+
void expectEngine.waitForMatch(failText).then(() => {
295+
console.error(
296+
chalkTemplate`\n\n{red Error:} Unexpected text found: {yellow "${failText}"}`,
297+
);
298+
console.error('TEST FAILED.');
299+
client.close();
300+
process.exit(1);
301+
});
302+
}
303+
304+
let screenshotPromise = Promise.resolve();
314305
if (screenshotPart != null && screenshotTime != null) {
315306
if (timeout && screenshotTime > timeout) {
316307
console.error(

0 commit comments

Comments
 (0)