brsh 3.10.5 → 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.
@@ -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/lib/local/read.js CHANGED
@@ -29,7 +29,8 @@ class Read extends LocalCommand {
29
29
  }
30
30
 
31
31
  this.nchars = options.nchars;
32
- this.var = options.name;
32
+ this.names = options.name || [];
33
+ this.raw = options.raw;
33
34
 
34
35
  return new Promise((resolve) => {
35
36
  const tid = setTimeout(() => {
@@ -54,35 +55,75 @@ class Read extends LocalCommand {
54
55
  this.inputBuffer = ""
55
56
  }
56
57
 
57
- if (key.length === 1) {
58
- this.inputBuffer += key;
58
+ //without -r a backslash escapes the next char, and backslash-newline continues the line
59
+ if(!this.raw && !this.escaped && key === '\\') {
60
+ this.escaped = true;
61
+ return;
59
62
  }
60
63
 
61
- if (key === 'Enter') {
62
- if (this.var) {
63
- this.context.setVar(this.var, this.inputBuffer || '');
64
- this.completionCallback();
65
- } else {
66
- this.completionCallback(this.inputBuffer || '');
64
+ if(this.escaped) {
65
+ delete this.escaped;
66
+ if(key === 'Enter') {
67
+ return;
67
68
  }
68
- return;
69
69
  }
70
70
 
71
- if(this.inputBuffer.length === this.nchars) {
72
- if(this.var) {
73
- this.context.setVar(this.var, this.inputBuffer)
74
- this.completionCallback();
71
+ if (key.length === 1) {
72
+ this.inputBuffer += key;
73
+ if(this.inputBuffer.length !== this.nchars) {
75
74
  return;
76
75
  }
77
- this.completionCallback(this.inputBuffer);
76
+ }
77
+
78
+ if(key === 'Enter' || this.inputBuffer.length === this.nchars) {
79
+ this._finish();
78
80
  }
79
81
  }
80
82
 
83
+ _finish() {
84
+ if(this.names.length < 1) {
85
+ return this.completionCallback(this.inputBuffer);
86
+ }
87
+
88
+ //getVar returns '' for unset, but an empty IFS is meaningful
89
+ const ifs = this.context.vars.IFS;
90
+ this._split(this.inputBuffer, ifs === undefined ? ' \t\n' : ifs)
91
+ .forEach((value, i) => this.context.setVar(this.names[i], value));
92
+ this.completionCallback();
93
+ }
94
+
95
+ //one value per name, the last one takes the remainder
96
+ _split(line, ifs) {
97
+ const {names} = this;
98
+ const esc = s => s.replace(/[\]\\^-]/g, '\\$&');
99
+ const ws = esc(ifs.replace(/[^ \t\n]/g, ''));
100
+ const other = esc(ifs.replace(/[ \t\n]/g, ''));
101
+ const trimStart = s => ws ? s.replace(new RegExp(`^[${ws}]+`), '') : s;
102
+ const trimEnd = s => ws ? s.replace(new RegExp(`[${ws}]+$`), '') : s;
103
+ const delim = ifs === '' ? null : new RegExp(other ? `[${ws}]*[${other}][${ws}]*|[${ws}]+` : `[${ws}]+`);
104
+
105
+ let rest = trimStart(line);
106
+ return names.map((_, i) => {
107
+ if(i === names.length -1) {
108
+ return trimEnd(rest);
109
+ }
110
+ const m = delim && delim.exec(rest);
111
+ if(!m) {
112
+ const field = rest;
113
+ rest = '';
114
+ return field;
115
+ }
116
+ const field = rest.slice(0, m.index);
117
+ rest = rest.slice(m.index + m[0].length);
118
+ return field;
119
+ });
120
+ }
121
+
81
122
  printHelp(e) {
82
123
  if(e) {
83
124
  this.stdOut = e.message;
84
125
  }
85
- this.stdOut = 'usage: read [-n nchars] [-p prompt] [-t timeout] [name ...]'
126
+ this.stdOut = 'usage: read [-r] [-n nchars] [-p prompt] [-t timeout] [name ...]'
86
127
  }
87
128
 
88
129
  parseArgs() {
@@ -102,6 +143,11 @@ class Read extends LocalCommand {
102
143
  alias: 't',
103
144
  type: Number
104
145
  },
146
+ {
147
+ name: 'raw',
148
+ alias: 'r',
149
+ type: Boolean
150
+ },
105
151
  {
106
152
  name: 'help',
107
153
  alias: 'h',
@@ -109,6 +155,7 @@ class Read extends LocalCommand {
109
155
  },
110
156
  {
111
157
  defaultOption: true,
158
+ multiple: true,
112
159
  name: 'name',
113
160
  type: String
114
161
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.10.5",
3
+ "version": "3.10.7",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
@@ -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
+ });
@@ -176,6 +176,92 @@ describe('read', () => {
176
176
  expect(shell.context.getVar('CHAR')).toBe('x');
177
177
  });
178
178
 
179
+ const feed = async (cmd, text) => {
180
+ const p = shell.onCommand(cmd);
181
+ await new Promise(resolve => setImmediate(resolve));
182
+ [...text].forEach(c => shell.onInput(c));
183
+ shell.onInput('Enter');
184
+ await p;
185
+ };
186
+
187
+ it('splits input across multiple names, last takes the remainder', async () => {
188
+ await feed('read -t 1 A B C', ' one two three four ');
189
+ expect(shell.context.getVar('A')).toBe('one');
190
+ expect(shell.context.getVar('B')).toBe('two');
191
+ expect(shell.context.getVar('C')).toBe('three four');
192
+ });
193
+
194
+ it('respects IFS', async () => {
195
+ await shell.onCommand('export IFS=:');
196
+ await feed('read -t 1 A B', 'x:y:z');
197
+ expect(shell.context.getVar('A')).toBe('x');
198
+ expect(shell.context.getVar('B')).toBe('y:z');
199
+ });
200
+
201
+ it('trims surrounding IFS whitespace with a single name', async () => {
202
+ await feed('read -t 1 V', ' padded value ');
203
+ expect(shell.context.getVar('V')).toBe('padded value');
204
+ });
205
+
206
+ it('leaves extra names empty when input has fewer fields', async () => {
207
+ await feed('read -t 1 A B C', 'only');
208
+ expect(shell.context.getVar('A')).toBe('only');
209
+ expect(shell.context.getVar('B')).toBe('');
210
+ expect(shell.context.getVar('C')).toBe('');
211
+ });
212
+
213
+ it('handles whitespace around non-whitespace IFS separators', async () => {
214
+ await shell.onCommand('export IFS=" :"');
215
+ await feed('read -t 1 A B C', 'x : y z');
216
+ expect(shell.context.getVar('A')).toBe('x');
217
+ expect(shell.context.getVar('B')).toBe('y');
218
+ expect(shell.context.getVar('C')).toBe('z');
219
+ });
220
+
221
+ it('treats an escaped backslash as a literal backslash without -r', async () => {
222
+ await feed('read -t 1 V', 'a\\\\b');
223
+ expect(shell.context.getVar('V')).toBe('a\\b');
224
+ });
225
+
226
+ it('continues the line on backslash-newline without -r', async () => {
227
+ const p = shell.onCommand('read -t 1 V');
228
+ await new Promise(resolve => setImmediate(resolve));
229
+ ['a', '\\', 'Enter', 'b', 'Enter'].forEach(k => shell.onInput(k));
230
+ await p;
231
+ expect(shell.context.getVar('V')).toBe('ab');
232
+ });
233
+
234
+ it('ends the line on backslash-newline with -r', async () => {
235
+ const p = shell.onCommand('read -r -t 1 V');
236
+ await new Promise(resolve => setImmediate(resolve));
237
+ ['a', '\\', 'Enter'].forEach(k => shell.onInput(k));
238
+ await p;
239
+ expect(shell.context.getVar('V')).toBe('a\\');
240
+ });
241
+
242
+ it('accepts -r combined with -n', async () => {
243
+ const p = shell.onCommand('read -r -n 3 -t 1 V');
244
+ await new Promise(resolve => setImmediate(resolve));
245
+ [...'a\\bcd'].forEach(c => shell.onInput(c));
246
+ await p;
247
+ expect(shell.context.getVar('V')).toBe('a\\b');
248
+ });
249
+
250
+ it('mentions -r in usage', async () => {
251
+ const { output } = await run(shell, 'read -h');
252
+ expect(output.join(' ')).toContain('-r');
253
+ });
254
+
255
+ it('strips backslashes without -r', async () => {
256
+ await feed('read -t 1 V', 'a\\bc');
257
+ expect(shell.context.getVar('V')).toBe('abc');
258
+ });
259
+
260
+ it('keeps backslashes with -r', async () => {
261
+ await feed('read -r -t 1 V', 'a\\bc');
262
+ expect(shell.context.getVar('V')).toBe('a\\bc');
263
+ });
264
+
179
265
  it('shows help with -h', async () => {
180
266
  const { output } = await run(shell, 'read -h');
181
267
  expect(output.join(' ')).toMatch(/usage/i);
@@ -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
+ });