brsh 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -332,3 +332,94 @@ describe('for loop', () => {
332
332
  expect(output).toContain('2y');
333
333
  });
334
334
  });
335
+
336
+ describe('break and continue', () => {
337
+ let shell;
338
+
339
+ beforeEach(async () => {
340
+ shell = await createShell({
341
+ filesystem: {
342
+ scripts: {
343
+ 'while-break.sh': '#!/sh.js\nI=0\nwhile true; do\nI=$((I+1))\nif [ $I -eq 3 ]; then\nbreak\nfi\necho $I\ndone\necho done',
344
+ 'for-break.sh': '#!/sh.js\nfor x in a b c d; do\nif [ $x = c ]; then\nbreak\nfi\necho $x\ndone\necho end',
345
+ 'for-continue.sh': '#!/sh.js\nfor x in 1 2 3 4 5; do\nif [ $x = 3 ]; then\ncontinue\nfi\necho $x\ndone',
346
+ 'nested-break.sh': '#!/sh.js\nfor a in 1 2; do\nfor b in x y z; do\nif [ $b = y ]; then\nbreak\nfi\necho $a$b\ndone\ndone',
347
+ }
348
+ },
349
+ permissions: execPerms(
350
+ '/scripts/while-break.sh',
351
+ '/scripts/for-break.sh',
352
+ '/scripts/for-continue.sh',
353
+ '/scripts/nested-break.sh'
354
+ )
355
+ });
356
+ });
357
+
358
+ it('break exits a while loop early', async () => {
359
+ const { output } = await run(shell, '/scripts/while-break.sh');
360
+ expect(output).toContain('1');
361
+ expect(output).toContain('2');
362
+ expect(output).not.toContain('3');
363
+ expect(output).toContain('done');
364
+ });
365
+
366
+ it('break exits a for loop early', async () => {
367
+ const { output } = await run(shell, '/scripts/for-break.sh');
368
+ expect(output).toContain('a');
369
+ expect(output).toContain('b');
370
+ expect(output).not.toContain('c');
371
+ expect(output).not.toContain('d');
372
+ expect(output).toContain('end');
373
+ });
374
+
375
+ it('continue skips the current iteration', async () => {
376
+ const { output } = await run(shell, '/scripts/for-continue.sh');
377
+ expect(output).toContain('1');
378
+ expect(output).toContain('2');
379
+ expect(output).not.toContain('3');
380
+ expect(output).toContain('4');
381
+ expect(output).toContain('5');
382
+ });
383
+
384
+ it('break only exits the innermost loop', async () => {
385
+ const { output } = await run(shell, '/scripts/nested-break.sh');
386
+ expect(output).toContain('1x');
387
+ expect(output).toContain('2x');
388
+ expect(output).not.toContain('1y');
389
+ expect(output).not.toContain('2y');
390
+ });
391
+ });
392
+
393
+ describe('sequential script execution', () => {
394
+ it('pipe output appears before the next script line runs', async () => {
395
+ const shell = await createShell({
396
+ filesystem: {
397
+ scripts: {
398
+ 'pipe-seq.sh': '#!/sh.js\necho first | cat\necho second'
399
+ }
400
+ },
401
+ permissions: execPerms('/scripts/pipe-seq.sh')
402
+ });
403
+ const output = [];
404
+ shell.on('stdOut', l => output.push(l));
405
+ await shell.onCommand('/scripts/pipe-seq.sh', false);
406
+ expect(output.indexOf('first')).toBeLessThan(output.indexOf('second'));
407
+ });
408
+
409
+ it('loop iterations run in order when body contains a pipe', async () => {
410
+ const shell = await createShell({
411
+ filesystem: {
412
+ scripts: {
413
+ 'loop-pipe.sh': '#!/sh.js\nfor x in 1 2 3; do\necho $x | cat\ndone'
414
+ }
415
+ },
416
+ permissions: execPerms('/scripts/loop-pipe.sh')
417
+ });
418
+ const output = [];
419
+ shell.on('stdOut', l => output.push(l));
420
+ await shell.onCommand('/scripts/loop-pipe.sh', false);
421
+ expect(output[0]).toBe('1');
422
+ expect(output[1]).toBe('2');
423
+ expect(output[2]).toBe('3');
424
+ });
425
+ });