brsh 3.10.6 → 3.10.7
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.
- package/CHANGELOG.md +7 -0
- package/POSIX-CHECKLIST.md +16 -15
- package/dist/shell.es.js +915 -642
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +15 -7
- package/dist/shell.min.js.map +1 -1
- package/index.js +118 -39
- package/lib/builtins/index.js +2 -0
- package/lib/builtins/readInput.js +37 -0
- package/lib/builtins/sed.js +361 -0
- package/lib/builtins/tail.js +76 -0
- package/package.json +1 -1
- package/test/builtins.test.js +217 -0
- package/test/scripts.test.js +97 -0
- package/test/shell.test.js +159 -0
- package/test/wbt.test.js +22 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Command = require('../Command');
|
|
4
|
+
const {readInput, toLines} = require('./readInput');
|
|
5
|
+
|
|
6
|
+
class Tail extends Command {
|
|
7
|
+
static get name() { return 'tail'; }
|
|
8
|
+
|
|
9
|
+
get requiresFilesystem() {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
run() {
|
|
14
|
+
let count = '10';
|
|
15
|
+
let bytes = false;
|
|
16
|
+
const paths = [];
|
|
17
|
+
|
|
18
|
+
const args = this.arguments;
|
|
19
|
+
for(let i = 0; i < args.length; i++) {
|
|
20
|
+
const arg = args[i];
|
|
21
|
+
if(arg === '-n' || arg === '-c') {
|
|
22
|
+
bytes = arg === '-c';
|
|
23
|
+
count = args[++i];
|
|
24
|
+
} else if(/^-[nc]./.test(arg)) {
|
|
25
|
+
bytes = arg[1] === 'c';
|
|
26
|
+
count = arg.slice(2);
|
|
27
|
+
} else if(/^-\d+$/.test(arg) || /^\+\d+$/.test(arg)) {
|
|
28
|
+
//legacy -N and +N
|
|
29
|
+
count = arg;
|
|
30
|
+
} else if(arg === '-f' || arg === '-q') {
|
|
31
|
+
continue;
|
|
32
|
+
} else if(arg.startsWith('-') && arg.length > 1) {
|
|
33
|
+
this.stdErr = `tail: invalid option -- ${arg.slice(1)}`;
|
|
34
|
+
this.exitCode = 1;
|
|
35
|
+
return Promise.resolve();
|
|
36
|
+
} else {
|
|
37
|
+
paths.push(arg);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if(!/^[+-]?\d+$/.test(count || '')) {
|
|
42
|
+
this.stdErr = `tail: invalid number: ${count}`;
|
|
43
|
+
this.exitCode = 1;
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
}
|
|
46
|
+
const fromStart = count.startsWith('+');
|
|
47
|
+
const n = Math.abs(parseInt(count, 10));
|
|
48
|
+
|
|
49
|
+
const sources = paths.length > 0 ? paths : [undefined];
|
|
50
|
+
sources.forEach((path, i) => {
|
|
51
|
+
const text = readInput(this, path, 'tail');
|
|
52
|
+
if(text === null) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if(sources.length > 1) {
|
|
57
|
+
i > 0 && (this.stdOut = '');
|
|
58
|
+
this.stdOut = `==> ${path === undefined ? 'standard input' : path} <==`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if(bytes) {
|
|
62
|
+
const out = fromStart ? text.slice(Math.max(n - 1, 0)) : (n < 1 ? '' : text.slice(-n));
|
|
63
|
+
toLines(out).forEach(line => this.stdOut = line);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const lines = toLines(text);
|
|
68
|
+
const out = fromStart ? lines.slice(Math.max(n - 1, 0)) : (n < 1 ? [] : lines.slice(-n));
|
|
69
|
+
out.forEach(line => this.stdOut = line);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return Promise.resolve();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = Tail;
|
package/package.json
CHANGED
package/test/builtins.test.js
CHANGED
|
@@ -514,3 +514,220 @@ describe('test new operators', () => {
|
|
|
514
514
|
expect(exitCode).toBe(0);
|
|
515
515
|
});
|
|
516
516
|
});
|
|
517
|
+
|
|
518
|
+
describe('tail', () => {
|
|
519
|
+
let shell;
|
|
520
|
+
beforeEach(async () => {
|
|
521
|
+
shell = await createShell({
|
|
522
|
+
filesystem: {
|
|
523
|
+
home: {
|
|
524
|
+
'nums.txt': '1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n',
|
|
525
|
+
'short.txt': 'a\nb\n',
|
|
526
|
+
'abc.txt': 'abcdef\n',
|
|
527
|
+
'empty.txt': ''
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
it('prints the last 10 lines by default', async () => {
|
|
534
|
+
const { output } = await run(shell, 'tail /home/nums.txt');
|
|
535
|
+
expect(output).toEqual(['3', '4', '5', '6', '7', '8', '9', '10', '11', '12']);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it('-n N prints the last N lines', async () => {
|
|
539
|
+
const { output } = await run(shell, 'tail -n 2 /home/nums.txt');
|
|
540
|
+
expect(output).toEqual(['11', '12']);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it('accepts -nN and legacy -N', async () => {
|
|
544
|
+
expect((await run(shell, 'tail -n3 /home/nums.txt')).output).toEqual(['10', '11', '12']);
|
|
545
|
+
expect((await run(shell, 'tail -1 /home/nums.txt')).output).toEqual(['12']);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it('-n +N starts at line N', async () => {
|
|
549
|
+
const { output } = await run(shell, 'tail -n +11 /home/nums.txt');
|
|
550
|
+
expect(output).toEqual(['11', '12']);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('-n 0 prints nothing', async () => {
|
|
554
|
+
const { output } = await run(shell, 'tail -n 0 /home/nums.txt');
|
|
555
|
+
expect(output).toEqual([]);
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it('prints everything when the file is shorter than N', async () => {
|
|
559
|
+
const { output } = await run(shell, 'tail -n 5 /home/short.txt');
|
|
560
|
+
expect(output).toEqual(['a', 'b']);
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
it('-c N prints the last N bytes', async () => {
|
|
564
|
+
const { output } = await run(shell, 'tail -c 4 /home/abc.txt');
|
|
565
|
+
expect(output).toEqual(['def']);
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
it('prints nothing for an empty file', async () => {
|
|
569
|
+
const { output } = await run(shell, 'tail /home/empty.txt');
|
|
570
|
+
expect(output).toEqual([]);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it('reads from a pipe', async () => {
|
|
574
|
+
const { output } = await run(shell, 'cat /home/nums.txt | tail -n 2');
|
|
575
|
+
expect(output).toEqual(['11', '12']);
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
it('prints headers for multiple files', async () => {
|
|
579
|
+
const { output } = await run(shell, 'tail -n 1 /home/short.txt /home/abc.txt');
|
|
580
|
+
expect(output).toEqual(['==> /home/short.txt <==', 'b', '', '==> /home/abc.txt <==', 'abcdef']);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it('errors on a missing file but still reads the others', async () => {
|
|
584
|
+
const { output, errors, exitCode } = await run(shell, 'tail -n 1 /home/nope /home/short.txt');
|
|
585
|
+
expect(errors.join(' ')).toMatch(/nope.*No such file/);
|
|
586
|
+
expect(output).toContain('b');
|
|
587
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
it('errors on an invalid count', async () => {
|
|
591
|
+
const { errors, exitCode } = await run(shell, 'tail -n abc /home/short.txt');
|
|
592
|
+
expect(errors.join(' ')).toMatch(/invalid number/);
|
|
593
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
describe('sed', () => {
|
|
598
|
+
let shell;
|
|
599
|
+
beforeEach(async () => {
|
|
600
|
+
shell = await createShell({
|
|
601
|
+
filesystem: {
|
|
602
|
+
home: {
|
|
603
|
+
'f.txt': 'one\ntwo\nthree\nfour\nfive\n',
|
|
604
|
+
'words.txt': 'foo bar foo baz foo\n'
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
const sed = async args => (await run(shell, `sed ${args}`)).output;
|
|
611
|
+
|
|
612
|
+
it('substitutes the first match per line', async () => {
|
|
613
|
+
expect(await sed(`'s/foo/X/' /home/words.txt`)).toEqual(['X bar foo baz foo']);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
it('g flag substitutes all matches', async () => {
|
|
617
|
+
expect(await sed(`'s/foo/X/g' /home/words.txt`)).toEqual(['X bar X baz X']);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
it('numeric flag substitutes the Nth match, with g from the Nth on', async () => {
|
|
621
|
+
expect(await sed(`'s/foo/X/2' /home/words.txt`)).toEqual(['foo bar X baz foo']);
|
|
622
|
+
expect(await sed(`'s/foo/X/2g' /home/words.txt`)).toEqual(['foo bar X baz X']);
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it('i flag ignores case', async () => {
|
|
626
|
+
expect(await sed(`'s/FOO/X/i' /home/words.txt`)).toEqual(['X bar foo baz foo']);
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
it('supports & and backreferences in the replacement', async () => {
|
|
630
|
+
expect(await sed(`'s/t\\(w\\)o/[&:\\1]/' /home/f.txt`)).toContain('[two:w]');
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
it('accepts other delimiters and escaped delimiters', async () => {
|
|
634
|
+
expect((await sed(`'s,o,0,g' /home/f.txt`))[0]).toBe('0ne');
|
|
635
|
+
expect(await sed(`'s/foo/a\\/b/' /home/words.txt`)).toEqual(['a/b bar foo baz foo']);
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it('BRE treats + and ( literally, escaped forms are operators', async () => {
|
|
639
|
+
await shell.onCommand('echo "a+b" > /home/p.txt');
|
|
640
|
+
expect(await sed(`'s/a+b/X/' /home/p.txt`)).toEqual(['X']);
|
|
641
|
+
expect((await sed(`'s/o\\+/0/' /home/f.txt`))[0]).toBe('0ne');
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it('-E uses extended regex', async () => {
|
|
645
|
+
expect((await sed(`-E 's/o+|w/0/' /home/f.txt`))[1]).toBe('t0o');
|
|
646
|
+
expect((await sed(`-E 's/(t)(w)/\\2\\1/' /home/f.txt`))[1]).toBe('wto');
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
it('supports POSIX character classes', async () => {
|
|
650
|
+
expect(await sed(`'s/[[:alpha:]]*/X/' /home/words.txt`)).toEqual(['X bar foo baz foo']);
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
it('-n with p prints only matching lines', async () => {
|
|
654
|
+
expect(await sed(`-n '/t/p' /home/f.txt`)).toEqual(['two', 'three']);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
it('s///p with -n prints only substituted lines', async () => {
|
|
658
|
+
expect(await sed(`-n 's/o/0/p' /home/f.txt`)).toEqual(['0ne', 'tw0', 'f0ur']);
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
it('d deletes lines by number, regex and range', async () => {
|
|
662
|
+
expect(await sed(`'2d' /home/f.txt`)).toEqual(['one', 'three', 'four', 'five']);
|
|
663
|
+
expect(await sed(`'/o/d' /home/f.txt`)).toEqual(['three', 'five']);
|
|
664
|
+
expect(await sed(`'2,4d' /home/f.txt`)).toEqual(['one', 'five']);
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it('$ addresses the last line', async () => {
|
|
668
|
+
expect(await sed(`'$d' /home/f.txt`)).toEqual(['one', 'two', 'three', 'four']);
|
|
669
|
+
expect(await sed(`-n '$p' /home/f.txt`)).toEqual(['five']);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it('regex ranges start at a match and end at a later match', async () => {
|
|
673
|
+
expect(await sed(`-n '/two/,/four/p' /home/f.txt`)).toEqual(['two', 'three', 'four']);
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it('a numeric end address behind the start is a single line', async () => {
|
|
677
|
+
expect(await sed(`-n '3,1p' /home/f.txt`)).toEqual(['three']);
|
|
678
|
+
});
|
|
679
|
+
|
|
680
|
+
it('! negates the address', async () => {
|
|
681
|
+
expect(await sed(`-n '2!p' /home/f.txt`)).toEqual(['one', 'three', 'four', 'five']);
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
it('q quits after printing the current line', async () => {
|
|
685
|
+
expect(await sed(`'2q' /home/f.txt`)).toEqual(['one', 'two']);
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
it('= prints line numbers', async () => {
|
|
689
|
+
expect(await sed(`-n '$=' /home/f.txt`)).toEqual(['5']);
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it('y transliterates characters', async () => {
|
|
693
|
+
expect((await sed(`'y/on/ON/' /home/f.txt`))[0]).toBe('ONe');
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
it('runs multiple -e scripts and ; separated commands in order', async () => {
|
|
697
|
+
expect((await sed(`-e '1d' -e 's/o/0/' /home/f.txt`))[0]).toBe('tw0');
|
|
698
|
+
expect((await sed(`'1d;s/o/0/' /home/f.txt`))[0]).toBe('tw0');
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it('reads from a pipe', async () => {
|
|
702
|
+
const { output } = await run(shell, 'cat /home/f.txt | sed -n 2p');
|
|
703
|
+
expect(output).toEqual(['two']);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it('an empty regex reuses the last one', async () => {
|
|
707
|
+
expect((await sed(`'/two/s//2/' /home/f.txt`))[1]).toBe('2');
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
it('errors on an unknown command', async () => {
|
|
711
|
+
const { errors, exitCode } = await run(shell, `sed 'k' /home/f.txt`);
|
|
712
|
+
expect(errors.join(' ')).toMatch(/unknown command/);
|
|
713
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
it('errors on an unterminated s command', async () => {
|
|
717
|
+
const { errors, exitCode } = await run(shell, `sed 's/a/b' /home/f.txt`);
|
|
718
|
+
expect(errors.join(' ')).toMatch(/unterminated/);
|
|
719
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it('errors without a script', async () => {
|
|
723
|
+
const { errors, exitCode } = await run(shell, 'sed');
|
|
724
|
+
expect(errors.join(' ')).toMatch(/usage/);
|
|
725
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
it('errors on a missing file', async () => {
|
|
729
|
+
const { errors, exitCode } = await run(shell, `sed p /home/nope`);
|
|
730
|
+
expect(errors.join(' ')).toMatch(/No such file/);
|
|
731
|
+
expect(exitCode).toBeGreaterThan(0);
|
|
732
|
+
});
|
|
733
|
+
});
|
package/test/scripts.test.js
CHANGED
|
@@ -610,3 +610,100 @@ describe('multiline quotes and line continuations in scripts', () => {
|
|
|
610
610
|
});
|
|
611
611
|
});
|
|
612
612
|
|
|
613
|
+
|
|
614
|
+
describe('parameter expansion and literal dollars inside scripts', () => {
|
|
615
|
+
let shell;
|
|
616
|
+
const scripts = {
|
|
617
|
+
'for-default.sh': '#!/sh.js\nfor i in ${LIST:-a b c}; do\necho $i\ndone',
|
|
618
|
+
'case-default.sh': '#!/sh.js\ncase ${MODE:-fast} in\nfast) echo quick;;\nslow) echo steady;;\nesac',
|
|
619
|
+
'assign.sh': '#!/sh.js\nNAME=${NAME:-guest}\necho hello $NAME',
|
|
620
|
+
'nested.sh': '#!/sh.js\nOUT="${NOPE:-$(echo "a b")}"\necho "$OUT"',
|
|
621
|
+
'sed-dollar.sh': '#!/sh.js\ncat /data/f.txt | sed -n \'$p\'',
|
|
622
|
+
'tail-pipe.sh': '#!/sh.js\ncat /data/f.txt | tail -n 2',
|
|
623
|
+
'length.sh': '#!/sh.js\nW=hello\necho ${#W}',
|
|
624
|
+
'single-quote.sh': '#!/sh.js\nA=1\necho \'$A\''
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
beforeEach(async () => {
|
|
628
|
+
shell = await createShell({
|
|
629
|
+
filesystem: {
|
|
630
|
+
scripts,
|
|
631
|
+
data: {'f.txt': 'one\ntwo\nthree\n'}
|
|
632
|
+
},
|
|
633
|
+
permissions: execPerms(...Object.keys(scripts).map(n => `/scripts/${n}`))
|
|
634
|
+
});
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
const out = async path => (await run(shell, path)).output;
|
|
638
|
+
|
|
639
|
+
it('for list uses a default', async () => {
|
|
640
|
+
expect(await out('/scripts/for-default.sh')).toEqual(['a', 'b', 'c']);
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
it('for list uses the variable over the default when set', async () => {
|
|
644
|
+
await shell.onCommand('export LIST="x y"');
|
|
645
|
+
expect(await out('/scripts/for-default.sh')).toEqual(['x', 'y']);
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
it('case word uses a default', async () => {
|
|
649
|
+
expect(await out('/scripts/case-default.sh')).toEqual(['quick']);
|
|
650
|
+
await shell.onCommand('export MODE=slow');
|
|
651
|
+
expect(await out('/scripts/case-default.sh')).toEqual(['steady']);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
it('assigns from a default', async () => {
|
|
655
|
+
expect(await out('/scripts/assign.sh')).toEqual(['hello guest']);
|
|
656
|
+
await shell.onCommand('export NAME=eliot');
|
|
657
|
+
expect(await out('/scripts/assign.sh')).toEqual(['hello eliot']);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
it('handles a quoted command substitution default', async () => {
|
|
661
|
+
expect(await out('/scripts/nested.sh')).toEqual(['a b']);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
it('gives ${#var} length', async () => {
|
|
665
|
+
expect(await out('/scripts/length.sh')).toEqual(['5']);
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
it('keeps $ literal in single quotes for sed', async () => {
|
|
669
|
+
expect(await out('/scripts/sed-dollar.sh')).toEqual(['three']);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it('keeps $ literal in single quotes for echo', async () => {
|
|
673
|
+
expect(await out('/scripts/single-quote.sh')).toEqual(['$A']);
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
it('pipes into tail', async () => {
|
|
677
|
+
expect(await out('/scripts/tail-pipe.sh')).toEqual(['two', 'three']);
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
describe('$@ and $*', () => {
|
|
682
|
+
let shell;
|
|
683
|
+
const scripts = {
|
|
684
|
+
'args.sh': '#!/sh.js\necho "n=$#"\nfor a in "$@"; do\necho "[$a]"\ndone\necho "star=$*"\necho "at=$@"',
|
|
685
|
+
'fn.sh': '#!/sh.js\nf() {\nfor a in "$@"; do\necho "[$a]"\ndone\necho "star=$*"\n}\nf a "b c" d',
|
|
686
|
+
'none.sh': '#!/sh.js\nfor a in "$@"; do\necho "[$a]"\ndone\necho "n=$#"'
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
beforeEach(async () => {
|
|
690
|
+
shell = await createShell({
|
|
691
|
+
filesystem: {scripts},
|
|
692
|
+
permissions: execPerms(...Object.keys(scripts).map(n => `/scripts/${n}`))
|
|
693
|
+
});
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
const out = async cmd => (await run(shell, cmd)).output;
|
|
697
|
+
|
|
698
|
+
it('"$@" keeps each argument as a separate word in a script', async () => {
|
|
699
|
+
expect(await out('/scripts/args.sh p "q r" s')).toEqual(['n=3', '[p]', '[q r]', '[s]', 'star=p q r s', 'at=p q r s']);
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
it('"$@" keeps each argument as a separate word in a function', async () => {
|
|
703
|
+
expect(await out('/scripts/fn.sh')).toEqual(['[a]', '[b c]', '[d]', 'star=a b c d']);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it('"$@" expands to nothing with no arguments', async () => {
|
|
707
|
+
expect(await out('/scripts/none.sh')).toEqual(['n=0']);
|
|
708
|
+
});
|
|
709
|
+
});
|
package/test/shell.test.js
CHANGED
|
@@ -779,3 +779,162 @@ describe('unquoted expansion word splitting', () => {
|
|
|
779
779
|
});
|
|
780
780
|
|
|
781
781
|
|
|
782
|
+
describe('parameter expansion ${var:-default} family', () => {
|
|
783
|
+
let shell;
|
|
784
|
+
beforeEach(async () => { shell = await createShell(); });
|
|
785
|
+
|
|
786
|
+
const out = async cmd => (await run(shell, cmd)).output;
|
|
787
|
+
|
|
788
|
+
it(':- uses the default when unset or empty', async () => {
|
|
789
|
+
expect(await out('echo ${NOPE:-fallback}')).toEqual(['fallback']);
|
|
790
|
+
await shell.onCommand('export SET=value');
|
|
791
|
+
expect(await out('echo ${SET:-fallback}')).toEqual(['value']);
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
it('- without a colon behaves the same', async () => {
|
|
795
|
+
expect(await out('echo ${NOPE-fallback}')).toEqual(['fallback']);
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
it(':= assigns the default', async () => {
|
|
799
|
+
expect(await out('echo ${NEW:=assigned}')).toEqual(['assigned']);
|
|
800
|
+
expect(shell.context.getVar('NEW')).toBe('assigned');
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
it(':+ uses the alternative only when set', async () => {
|
|
804
|
+
expect(await out('echo ${NOPE:+alt}')).toEqual([]);
|
|
805
|
+
await shell.onCommand('export SET=1');
|
|
806
|
+
expect(await out('echo ${SET:+alt}')).toEqual(['alt']);
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
it(':? errors when unset and passes when set', async () => {
|
|
810
|
+
const res = await run(shell, 'echo ${NOPE:?must be set}');
|
|
811
|
+
expect(res.errors.join(' ')).toContain('NOPE: must be set');
|
|
812
|
+
expect(res.exitCode).toBeGreaterThan(0);
|
|
813
|
+
expect(res.output).toEqual([]);
|
|
814
|
+
await shell.onCommand('export SET=1');
|
|
815
|
+
expect(await out('echo ${SET:?must be set}')).toEqual(['1']);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
it('${#var} gives the string length', async () => {
|
|
819
|
+
await shell.onCommand('export WORD=hello');
|
|
820
|
+
expect(await out('echo ${#WORD}')).toEqual(['5']);
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
it('the default can contain spaces and quotes when quoted', async () => {
|
|
824
|
+
expect(await out('echo "${NOPE:-two words}"')).toEqual(['two words']);
|
|
825
|
+
expect(await out('echo ${NOPE:-"two words"}')).toEqual(['two words']);
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it('the default can reference another variable', async () => {
|
|
829
|
+
await shell.onCommand('export OTHER=from-other');
|
|
830
|
+
expect(await out('echo ${NOPE:-$OTHER}')).toEqual(['from-other']);
|
|
831
|
+
expect(await out('echo ${NOPE:-${OTHER}}')).toEqual(['from-other']);
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
it('the default can be a command substitution containing quotes and parens', async () => {
|
|
835
|
+
expect(await out('echo "${NOPE:-$(echo "a b")}"')).toEqual(['a b']);
|
|
836
|
+
expect(await out('echo "${NOPE:-$(echo $((1 + 2)))}"')).toEqual(['3']);
|
|
837
|
+
});
|
|
838
|
+
|
|
839
|
+
it('a } inside a quoted command substitution does not end the expansion early', async () => {
|
|
840
|
+
expect(await out('echo "${NOPE:-$(echo "x}y")}"')).toEqual(['x}y']);
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
it('is not re-expanded when the value contains a dollar', async () => {
|
|
844
|
+
await shell.onCommand('export DOLLAR=\'$HOME\'');
|
|
845
|
+
expect(await out('echo ${DOLLAR}')).toEqual(['$HOME']);
|
|
846
|
+
});
|
|
847
|
+
|
|
848
|
+
it('still expands plain $VAR and ${VAR}', async () => {
|
|
849
|
+
await shell.onCommand('export A=1');
|
|
850
|
+
expect(await out('echo $A${A}$A')).toEqual(['111']);
|
|
851
|
+
});
|
|
852
|
+
|
|
853
|
+
it('works inside assignments', async () => {
|
|
854
|
+
await shell.onCommand('FINAL=${FINAL:-default}');
|
|
855
|
+
expect(shell.context.getVar('FINAL')).toBe('default');
|
|
856
|
+
});
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
describe('literal dollar signs', () => {
|
|
860
|
+
let shell;
|
|
861
|
+
beforeEach(async () => { shell = await createShell(); });
|
|
862
|
+
|
|
863
|
+
it('single quotes block expansion', async () => {
|
|
864
|
+
await shell.onCommand('export A=1');
|
|
865
|
+
const { output } = await run(shell, `echo '$A \${A:-x} $(echo hi)'`);
|
|
866
|
+
expect(output).toEqual(['$A ${A:-x} $(echo hi)']);
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
it('a backslash blocks expansion outside and inside double quotes', async () => {
|
|
870
|
+
await shell.onCommand('export A=1');
|
|
871
|
+
expect((await run(shell, 'echo \\$A')).output).toEqual(['$A']);
|
|
872
|
+
expect((await run(shell, 'echo "\\$A"')).output).toEqual(['$A']);
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
|
|
876
|
+
describe('multi-line strings, arithmetic and substitution output', () => {
|
|
877
|
+
let shell;
|
|
878
|
+
beforeEach(async () => { shell = await createShell(); });
|
|
879
|
+
|
|
880
|
+
const out = async cmd => (await run(shell, cmd)).output;
|
|
881
|
+
|
|
882
|
+
it('keeps a multi-line double-quoted string as one argument', async () => {
|
|
883
|
+
expect(await out('echo "line one\nline two"')).toEqual(['line one', 'line two']);
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it('assigns a multi-line double-quoted string to a variable', async () => {
|
|
887
|
+
await shell.onCommand('MSG="line one\nline two"');
|
|
888
|
+
expect(shell.context.getVar('MSG')).toBe('line one\nline two');
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
it('expands a variable holding newlines inside quotes', async () => {
|
|
892
|
+
await shell.onCommand('MSG="a\nb"');
|
|
893
|
+
expect(await out('echo "$MSG"')).toEqual(['a', 'b']);
|
|
894
|
+
});
|
|
895
|
+
|
|
896
|
+
it('$((n + 1)) with spaces, assigned and quoted', async () => {
|
|
897
|
+
await shell.onCommand('n=1');
|
|
898
|
+
await shell.onCommand('n=$((n + 1))');
|
|
899
|
+
expect(shell.context.getVar('n')).toBe('2');
|
|
900
|
+
expect(await out('echo $((n + 1))')).toEqual(['3']);
|
|
901
|
+
expect(await out('echo "$((n * 3))"')).toEqual(['6']);
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
it('arithmetic works as a default and inside a default', async () => {
|
|
905
|
+
await shell.onCommand('n=4');
|
|
906
|
+
expect(await out('echo ${NOPE:-$((n + 1))}')).toEqual(['5']);
|
|
907
|
+
});
|
|
908
|
+
|
|
909
|
+
it('does not split on ; inside $( )', async () => {
|
|
910
|
+
await shell.onCommand('X=$(echo a; echo b)');
|
|
911
|
+
expect(shell.context.getVar('X')).toBe('a\nb');
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
it('does not split on && or || or | inside $( )', async () => {
|
|
915
|
+
expect(await out('echo "$(echo a && echo b)"')).toEqual(['a', 'b']);
|
|
916
|
+
expect(await out('echo "$(false || echo c)"')).toEqual(['c']);
|
|
917
|
+
expect(await out('echo "$(echo hello | cat)"')).toEqual(['hello']);
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
it('does not split on operators inside ${ } defaults', async () => {
|
|
921
|
+
expect(await out('echo "${NOPE:-a;b}"')).toEqual(['a;b']);
|
|
922
|
+
expect(await out('echo "${NOPE:-a|b}"')).toEqual(['a|b']);
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
it('still splits operators after a substitution', async () => {
|
|
926
|
+
expect(await out('echo $(echo a) ; echo b')).toEqual(['a', 'b']);
|
|
927
|
+
expect(await out('echo ${NOPE:-x} && echo y')).toEqual(['x', 'y']);
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
it('captured output containing ) } quotes and $ is not re-parsed', async () => {
|
|
931
|
+
await shell.onCommand(`X=$(echo 'a) b} "c" $HOME')`);
|
|
932
|
+
expect(shell.context.getVar('X')).toBe('a) b} "c" $HOME');
|
|
933
|
+
expect(await out('echo "$X"')).toEqual(['a) b} "c" $HOME']);
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
it('captured multi-line output keeps its lines inside quotes', async () => {
|
|
937
|
+
await shell.onCommand('X=$(echo a; echo b)');
|
|
938
|
+
expect(await out('echo "$X"')).toEqual(['a', 'b']);
|
|
939
|
+
});
|
|
940
|
+
});
|
package/test/wbt.test.js
CHANGED
|
@@ -1150,6 +1150,28 @@ describe('WBT Wllama v3 WASM & WebGPU package (`wllama`, `llama`, `llama-cli`)',
|
|
|
1150
1150
|
expect(runHelpRes.output.join('\n')).toContain('Usage: wllama');
|
|
1151
1151
|
});
|
|
1152
1152
|
|
|
1153
|
+
it('captures wllama output in $(...) and keeps it intact when expanded', async () => {
|
|
1154
|
+
await run(shell, 'REPLY_TEXT=$(wllama -m /home/tinyllama.gguf -p "Hello (world) {x}")');
|
|
1155
|
+
const captured = shell.context.getVar('REPLY_TEXT');
|
|
1156
|
+
expect(captured).toContain('Hello (world) {x}');
|
|
1157
|
+
|
|
1158
|
+
const res = await run(shell, 'echo "$REPLY_TEXT"');
|
|
1159
|
+
expect(res.output.join('\n')).toBe(captured);
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
it('uses wllama output in a ${var:-default} and inside a larger command', async () => {
|
|
1163
|
+
const res = await run(shell, 'echo "got: ${NOPE:-$(wllama -m /home/tinyllama.gguf -p "hi there")}"');
|
|
1164
|
+
expect(res.exitCode).toBe(0);
|
|
1165
|
+
expect(res.output.join('\n')).toContain('got: ');
|
|
1166
|
+
expect(res.output.join('\n')).toContain('hi there');
|
|
1167
|
+
});
|
|
1168
|
+
|
|
1169
|
+
it('pipes captured wllama output through other commands', async () => {
|
|
1170
|
+
const res = await run(shell, 'wllama -m /home/tinyllama.gguf -p "one two" | tail -n 1');
|
|
1171
|
+
expect(res.exitCode).toBe(0);
|
|
1172
|
+
expect(res.output.join('\n')).toContain('one two');
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1153
1175
|
it('installs wllama from the built ESM browser bundle without __dirname', async () => {
|
|
1154
1176
|
const { default: BundledWbt } = await import('../dist/wbt.es.js');
|
|
1155
1177
|
const ConfiguredBundledWbt = BundledWbt.configure({
|