brsh 1.4.0 → 2.1.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.
Files changed (58) hide show
  1. package/.github/workflows/busybox-compat.yml +166 -0
  2. package/.github/workflows/pages.yml +33 -0
  3. package/.github/workflows/release.yml +40 -0
  4. package/.releaserc.json +18 -0
  5. package/CHANGELOG.md +362 -0
  6. package/bin/brsh-cmd.js +78 -0
  7. package/bin/exportFileSystem.js +81 -156
  8. package/busybox-testsuite/cat.tests +49 -0
  9. package/busybox-testsuite/cp.tests +206 -0
  10. package/busybox-testsuite/grep.tests +237 -0
  11. package/busybox-testsuite/testing.sh +180 -0
  12. package/dist/brsh.css +2 -0
  13. package/dist/shell.es.js +5667 -0
  14. package/dist/shell.es.js.map +1 -0
  15. package/dist/shell.min.js +14 -222
  16. package/dist/shell.min.js.map +1 -149
  17. package/dist/terminal.es.js +6228 -0
  18. package/dist/terminal.es.js.map +1 -0
  19. package/dist/terminal.min.js +113 -18
  20. package/dist/terminal.min.js.map +1 -21
  21. package/index.js +206 -49
  22. package/lib/Context.js +12 -4
  23. package/lib/FileSystem.js +99 -6
  24. package/lib/NodeFileSystem.js +238 -0
  25. package/lib/ScriptCommand.js +193 -15
  26. package/lib/Terminal.js +188 -12
  27. package/lib/WasmCommand.js +252 -0
  28. package/lib/buildFilesystemStructure.js +58 -0
  29. package/lib/builtins/cat.js +67 -9
  30. package/lib/builtins/echo.js +36 -5
  31. package/lib/builtins/false.js +13 -0
  32. package/lib/builtins/grep.js +267 -0
  33. package/lib/builtins/index.js +9 -2
  34. package/lib/builtins/ls.js +86 -18
  35. package/lib/builtins/test.js +105 -0
  36. package/lib/builtins/true.js +10 -0
  37. package/lib/local/chmod.js +72 -0
  38. package/lib/local/cp.js +156 -0
  39. package/lib/local/index.js +8 -1
  40. package/lib/local/jsfunction.js +58 -0
  41. package/lib/local/mkdir.js +44 -0
  42. package/lib/local/mv.js +96 -0
  43. package/lib/local/pwd.js +13 -0
  44. package/lib/local/read.js +6 -4
  45. package/lib/local/rm.js +71 -0
  46. package/lib/local/which.js +2 -2
  47. package/package.json +31 -34
  48. package/test/builtins.test.js +505 -0
  49. package/test/exportFileSystem.test.js +226 -0
  50. package/test/helpers.js +35 -0
  51. package/test/local-commands.test.js +593 -0
  52. package/test/node-filesystem.test.js +133 -0
  53. package/test/scripts.test.js +277 -0
  54. package/test/shell.test.js +245 -0
  55. package/test/streaming.test.js +113 -0
  56. package/vite.config.shell.js +21 -0
  57. package/vite.config.terminal.js +27 -0
  58. package/vitest.config.js +15 -0
@@ -0,0 +1,505 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import { createShell, run } from './helpers.js';
3
+
4
+ describe('echo', () => {
5
+ let shell;
6
+ beforeEach(async () => { shell = await createShell(); });
7
+
8
+ it('outputs its argument', async () => {
9
+ const { output } = await run(shell, 'echo hello');
10
+ expect(output).toContain('hello');
11
+ });
12
+
13
+ it('outputs a multi-word quoted string', async () => {
14
+ const { output } = await run(shell, 'echo "hello world"');
15
+ expect(output).toContain('hello world');
16
+ });
17
+
18
+ it('outputs multiple unquoted arguments joined with spaces', async () => {
19
+ const { output } = await run(shell, 'echo hello world');
20
+ expect(output).toContain('hello world');
21
+ });
22
+
23
+ it('outputs an expanded variable', async () => {
24
+ await shell.onCommand('export FOO=bar');
25
+ const { output } = await run(shell, 'echo $FOO');
26
+ expect(output).toContain('bar');
27
+ });
28
+ });
29
+
30
+ describe('ls', () => {
31
+ let shell;
32
+ beforeEach(async () => {
33
+ shell = await createShell({
34
+ filesystem: {
35
+ home: {
36
+ 'file.txt': 'hello',
37
+ '.hidden': 'secret',
38
+ subdir: {}
39
+ }
40
+ },
41
+ cwd: '/'
42
+ });
43
+ });
44
+
45
+ it('lists files in the current directory', async () => {
46
+ await shell.onCommand('cd /home');
47
+ const { output } = await run(shell, 'ls');
48
+ expect(output.join(' ')).toMatch(/file\.txt/);
49
+ });
50
+
51
+ it('lists files at an explicit path', async () => {
52
+ const { output } = await run(shell, 'ls /home');
53
+ expect(output.join(' ')).toMatch(/file\.txt/);
54
+ });
55
+
56
+ it('hides dotfiles without -a', async () => {
57
+ await shell.onCommand('cd /home');
58
+ const { output } = await run(shell, 'ls');
59
+ expect(output.join(' ')).not.toMatch(/\.hidden/);
60
+ });
61
+
62
+ it('shows dotfiles with -a', async () => {
63
+ await shell.onCommand('cd /home');
64
+ const { output } = await run(shell, 'ls -a');
65
+ expect(output.join(' ')).toMatch(/\.hidden/);
66
+ });
67
+
68
+ it('shows long listing with -l', async () => {
69
+ await shell.onCommand('cd /home');
70
+ const { output } = await run(shell, 'ls -l');
71
+ expect(output.some((l) => l.includes('rw') || l.includes('drw'))).toBe(true);
72
+ });
73
+
74
+ it('shows correct permissions for a regular file', async () => {
75
+ const { output } = await run(shell, 'ls -l /home');
76
+ const fileLine = output.find(l => l.includes('file.txt'));
77
+ expect(fileLine).toBeDefined();
78
+ expect(fileLine).toMatch(/^-rw-r--r--/);
79
+ });
80
+
81
+ it('shows correct permissions for a directory', async () => {
82
+ const { output } = await run(shell, 'ls -l /home');
83
+ const dirLine = output.find(l => l.includes('subdir'));
84
+ expect(dirLine).toBeDefined();
85
+ expect(dirLine).toMatch(/^drwxr-xr-x/);
86
+ });
87
+
88
+ it('shows updated permissions after chmod', async () => {
89
+ await shell.onCommand('chmod 755 /home/file.txt');
90
+ const { output } = await run(shell, 'ls -l /home');
91
+ const fileLine = output.find(l => l.includes('file.txt'));
92
+ expect(fileLine).toMatch(/^-rwxr-xr-x/);
93
+ });
94
+
95
+ it('errors on a non-existent path', async () => {
96
+ const { errors, exitCode } = await run(shell, 'ls /does/not/exist');
97
+ expect(errors.length).toBeGreaterThan(0);
98
+ expect(exitCode).toBeGreaterThan(0);
99
+ });
100
+
101
+ it('shows help with -h', async () => {
102
+ const { output } = await run(shell, 'ls -h');
103
+ expect(output.join(' ')).toMatch(/usage/i);
104
+ });
105
+ });
106
+
107
+ describe('cat', () => {
108
+ let shell;
109
+ beforeEach(async () => {
110
+ shell = await createShell({
111
+ filesystem: {
112
+ home: { 'readme.txt': 'Hello from cat' }
113
+ }
114
+ });
115
+ });
116
+
117
+ it('outputs file contents', async () => {
118
+ const { output } = await run(shell, 'cat /home/readme.txt');
119
+ expect(output).toContain('Hello from cat');
120
+ });
121
+
122
+ it('errors on a missing file', async () => {
123
+ const { errors, exitCode } = await run(shell, 'cat /home/missing.txt');
124
+ expect(errors.length).toBeGreaterThan(0);
125
+ expect(exitCode).toBeGreaterThan(0);
126
+ });
127
+
128
+ it('errors when targeting a directory', async () => {
129
+ const { errors, exitCode } = await run(shell, 'cat /home');
130
+ expect(errors.length).toBeGreaterThan(0);
131
+ expect(exitCode).toBeGreaterThan(0);
132
+ });
133
+
134
+ it('errors when called with no arguments', async () => {
135
+ const { errors, exitCode } = await run(shell, 'cat');
136
+ expect(errors.length).toBeGreaterThan(0);
137
+ expect(exitCode).toBeGreaterThan(0);
138
+ });
139
+ });
140
+
141
+ describe('true / false', () => {
142
+ let shell;
143
+ beforeEach(async () => { shell = await createShell(); });
144
+
145
+ it('true exits with code 0', async () => {
146
+ const { exitCode } = await run(shell, 'true');
147
+ expect(exitCode).toBe(0);
148
+ });
149
+
150
+ it('false exits with code 1', async () => {
151
+ const { exitCode } = await run(shell, 'false');
152
+ expect(exitCode).toBe(1);
153
+ });
154
+ });
155
+
156
+ describe('test and [ ]', () => {
157
+ let shell;
158
+ beforeEach(async () => {
159
+ shell = await createShell({
160
+ filesystem: { home: { 'readme.txt': 'hi', subdir: {} } }
161
+ });
162
+ });
163
+
164
+ it('exits 0 when strings are equal (=)', async () => {
165
+ const { exitCode } = await run(shell, 'test hello = hello');
166
+ expect(exitCode).toBe(0);
167
+ });
168
+
169
+ it('exits 1 when strings differ (=)', async () => {
170
+ const { exitCode } = await run(shell, 'test hello = world');
171
+ expect(exitCode).toBe(1);
172
+ });
173
+
174
+ it('exits 0 for != when strings differ', async () => {
175
+ const { exitCode } = await run(shell, 'test a != b');
176
+ expect(exitCode).toBe(0);
177
+ });
178
+
179
+ it('exits 0 for -n with non-empty string', async () => {
180
+ const { exitCode } = await run(shell, 'test -n hello');
181
+ expect(exitCode).toBe(0);
182
+ });
183
+
184
+ it('exits 1 for -n with empty string', async () => {
185
+ const { exitCode } = await run(shell, 'test -n ""');
186
+ expect(exitCode).toBe(1);
187
+ });
188
+
189
+ it('exits 0 for -z with empty string', async () => {
190
+ const { exitCode } = await run(shell, 'test -z ""');
191
+ expect(exitCode).toBe(0);
192
+ });
193
+
194
+ it('exits 0 for numeric -eq', async () => {
195
+ const { exitCode } = await run(shell, 'test 5 -eq 5');
196
+ expect(exitCode).toBe(0);
197
+ });
198
+
199
+ it('exits 1 for numeric -eq when not equal', async () => {
200
+ const { exitCode } = await run(shell, 'test 5 -eq 6');
201
+ expect(exitCode).toBe(1);
202
+ });
203
+
204
+ it('exits 0 for -gt when greater', async () => {
205
+ const { exitCode } = await run(shell, 'test 10 -gt 3');
206
+ expect(exitCode).toBe(0);
207
+ });
208
+
209
+ it('exits 1 for -gt when not greater', async () => {
210
+ const { exitCode } = await run(shell, 'test 3 -gt 10');
211
+ expect(exitCode).toBe(1);
212
+ });
213
+
214
+ it('exits 0 for -f on an existing file', async () => {
215
+ const { exitCode } = await run(shell, 'test -f /home/readme.txt');
216
+ expect(exitCode).toBe(0);
217
+ });
218
+
219
+ it('exits 1 for -f on a directory', async () => {
220
+ const { exitCode } = await run(shell, 'test -f /home/subdir');
221
+ expect(exitCode).toBe(1);
222
+ });
223
+
224
+ it('exits 0 for -d on a directory', async () => {
225
+ const { exitCode } = await run(shell, 'test -d /home/subdir');
226
+ expect(exitCode).toBe(0);
227
+ });
228
+
229
+ it('exits 1 for -d on a file', async () => {
230
+ const { exitCode } = await run(shell, 'test -d /home/readme.txt');
231
+ expect(exitCode).toBe(1);
232
+ });
233
+
234
+ it('bracket syntax [ ] works the same as test', async () => {
235
+ const { exitCode } = await run(shell, '[ hello = hello ]');
236
+ expect(exitCode).toBe(0);
237
+ });
238
+
239
+ it('bracket syntax exits 1 for false condition', async () => {
240
+ const { exitCode } = await run(shell, '[ hello = world ]');
241
+ expect(exitCode).toBe(1);
242
+ });
243
+
244
+ it('! negates the result', async () => {
245
+ const { exitCode } = await run(shell, 'test ! hello = world');
246
+ expect(exitCode).toBe(0);
247
+ });
248
+ });
249
+
250
+ describe('grep', () => {
251
+ let shell;
252
+ beforeEach(async () => {
253
+ shell = await createShell({
254
+ filesystem: {
255
+ home: {
256
+ 'a.txt': 'hello world\nfoo bar\nHELLO again',
257
+ 'b.txt': 'second file\nfoo baz',
258
+ subdir: {
259
+ 'c.txt': 'nested foo line'
260
+ }
261
+ }
262
+ },
263
+ cwd: '/home'
264
+ });
265
+ });
266
+
267
+ it('matches a basic pattern', async () => {
268
+ const { output, exitCode } = await run(shell, 'grep foo /home/a.txt');
269
+ expect(output).toContain('foo bar');
270
+ expect(exitCode).toBe(0);
271
+ });
272
+
273
+ it('exits 1 when pattern not found', async () => {
274
+ const { exitCode } = await run(shell, 'grep nomatch /home/a.txt');
275
+ expect(exitCode).toBe(1);
276
+ });
277
+
278
+ it('-i matches case-insensitively', async () => {
279
+ const { output } = await run(shell, 'grep -i hello /home/a.txt');
280
+ expect(output).toContain('hello world');
281
+ expect(output).toContain('HELLO again');
282
+ });
283
+
284
+ it('-v inverts match', async () => {
285
+ const { output } = await run(shell, 'grep -v foo /home/a.txt');
286
+ expect(output).toContain('hello world');
287
+ expect(output).not.toContain('foo bar');
288
+ });
289
+
290
+ it('-n prefixes with line numbers', async () => {
291
+ const { output } = await run(shell, 'grep -n foo /home/a.txt');
292
+ expect(output.some(l => l.startsWith('2:'))).toBe(true);
293
+ });
294
+
295
+ it('-c prints match count', async () => {
296
+ const { output } = await run(shell, 'grep -c foo /home/a.txt');
297
+ expect(output).toContain('1');
298
+ });
299
+
300
+ it('-l lists filenames only for multi-file search', async () => {
301
+ const { output } = await run(shell, 'grep -l foo /home/a.txt /home/b.txt');
302
+ expect(output).toContain('/home/a.txt');
303
+ expect(output).toContain('/home/b.txt');
304
+ });
305
+
306
+ it('prefixes filename when multiple files given', async () => {
307
+ const { output } = await run(shell, 'grep foo /home/a.txt /home/b.txt');
308
+ expect(output.some(l => l.startsWith('/home/a.txt:'))).toBe(true);
309
+ expect(output.some(l => l.startsWith('/home/b.txt:'))).toBe(true);
310
+ });
311
+
312
+ it('-r searches recursively', async () => {
313
+ const { output } = await run(shell, 'grep -r foo /home');
314
+ expect(output.some(l => l.includes('foo'))).toBe(true);
315
+ expect(output.some(l => l.includes('nested foo line'))).toBe(true);
316
+ });
317
+
318
+ it('-F treats pattern as fixed string', async () => {
319
+ const { output } = await run(shell, 'grep -F foo /home/a.txt');
320
+ expect(output).toContain('foo bar');
321
+ });
322
+
323
+ it('-e accepts pattern as flag argument', async () => {
324
+ const { output } = await run(shell, 'grep -e foo /home/a.txt');
325
+ expect(output).toContain('foo bar');
326
+ });
327
+
328
+ it('errors on missing file with exit code 2', async () => {
329
+ const { errors, exitCode } = await run(shell, 'grep foo /home/missing.txt');
330
+ expect(exitCode).toBe(2);
331
+ expect(errors.length).toBeGreaterThan(0);
332
+ });
333
+
334
+ it('errors when no pattern given', async () => {
335
+ const { errors, exitCode } = await run(shell, 'grep');
336
+ expect(exitCode).toBe(2);
337
+ expect(errors.length).toBeGreaterThan(0);
338
+ });
339
+
340
+ it('-q suppresses output but still sets exit code', async () => {
341
+ const { output, exitCode } = await run(shell, 'grep -q foo /home/a.txt');
342
+ expect(output.length).toBe(0);
343
+ expect(exitCode).toBe(0);
344
+ });
345
+
346
+ it('-- ends option parsing so pattern can start with -', async () => {
347
+ const shell2 = await createShell({
348
+ filesystem: { home: { 'f.txt': '-dashed line\nnormal line' } }
349
+ });
350
+ const { output } = await run(shell2, 'grep -- -dashed /home/f.txt');
351
+ expect(output).toContain('-dashed line');
352
+ });
353
+ });
354
+
355
+ describe('echo flags', () => {
356
+ let shell;
357
+ beforeEach(async () => { shell = await createShell(); });
358
+
359
+ it('-n suppresses trailing newline (emits same lines)', async () => {
360
+ const { output } = await run(shell, 'echo -n hello');
361
+ expect(output).toContain('hello');
362
+ });
363
+
364
+ it('-e interprets \\n as a newline', async () => {
365
+ const { output } = await run(shell, 'echo -e "line1\\nline2"');
366
+ expect(output).toContain('line1');
367
+ expect(output).toContain('line2');
368
+ });
369
+
370
+ it('-e interprets \\t as a tab', async () => {
371
+ const { output } = await run(shell, 'echo -e "a\\tb"');
372
+ expect(output.join('')).toContain('a\tb');
373
+ });
374
+
375
+ it('-ne combines -n and -e', async () => {
376
+ const { output } = await run(shell, 'echo -ne "x\\ny"');
377
+ expect(output).toContain('x');
378
+ expect(output).toContain('y');
379
+ });
380
+ });
381
+
382
+ describe('cat flags', () => {
383
+ let shell;
384
+ beforeEach(async () => {
385
+ shell = await createShell({
386
+ filesystem: { home: { 'file.txt': 'line1\nline2\nline3\n' } }
387
+ });
388
+ });
389
+
390
+ it('-n numbers all lines', async () => {
391
+ const { output } = await run(shell, 'cat -n /home/file.txt');
392
+ expect(output.some(l => l.match(/^\s+1\t/))).toBe(true);
393
+ expect(output.some(l => l.match(/^\s+3\t/))).toBe(true);
394
+ });
395
+
396
+ it('concatenates multiple files', async () => {
397
+ await run(shell, 'mkdir /home/more');
398
+ await run(shell, 'mkdir -p /tmp');
399
+ // create second file via a different approach — use the filesystem directly
400
+ shell.context.fs.setFileByPath('/home/file2.txt', 'line4\n');
401
+ const { output } = await run(shell, 'cat /home/file.txt /home/file2.txt');
402
+ expect(output).toContain('line1');
403
+ expect(output).toContain('line4');
404
+ });
405
+
406
+ it('-s squeezes consecutive blank lines', async () => {
407
+ shell.context.fs.setFileByPath('/home/blank.txt', 'a\n\n\nb');
408
+ const { output } = await run(shell, 'cat -s /home/blank.txt');
409
+ const blanks = output.filter(l => l === '');
410
+ expect(blanks.length).toBeLessThanOrEqual(1);
411
+ });
412
+
413
+ it('errors on missing file', async () => {
414
+ const { errors, exitCode } = await run(shell, 'cat /nope.txt');
415
+ expect(exitCode).toBeGreaterThan(0);
416
+ expect(errors.length).toBeGreaterThan(0);
417
+ });
418
+ });
419
+
420
+ describe('ls new flags', () => {
421
+ let shell;
422
+ beforeEach(async () => {
423
+ shell = await createShell({
424
+ filesystem: {
425
+ home: {
426
+ 'file.txt': 'hello',
427
+ subdir: { 'inner.txt': 'inner' }
428
+ }
429
+ },
430
+ cwd: '/home'
431
+ });
432
+ });
433
+
434
+ it('-1 prints one entry per line', async () => {
435
+ const { output } = await run(shell, 'ls -1 /home');
436
+ expect(output.length).toBeGreaterThanOrEqual(2);
437
+ expect(output).toContain('file.txt');
438
+ expect(output).toContain('subdir');
439
+ });
440
+
441
+ it('-F appends / to directories', async () => {
442
+ const { output } = await run(shell, 'ls -F /home');
443
+ expect(output.join(' ')).toMatch(/subdir\//);
444
+ });
445
+
446
+ it('-R lists subdirectories recursively', async () => {
447
+ const { output } = await run(shell, 'ls -R /home');
448
+ expect(output.join('\n')).toMatch(/inner\.txt/);
449
+ });
450
+ });
451
+
452
+ describe('test new operators', () => {
453
+ let shell;
454
+ beforeEach(async () => {
455
+ shell = await createShell({
456
+ filesystem: { home: { 'file.txt': 'data', subdir: {} } }
457
+ });
458
+ });
459
+
460
+ it('-e returns true for existing file', async () => {
461
+ const { exitCode } = await run(shell, 'test -e /home/file.txt');
462
+ expect(exitCode).toBe(0);
463
+ });
464
+
465
+ it('-e returns true for existing directory', async () => {
466
+ const { exitCode } = await run(shell, 'test -e /home/subdir');
467
+ expect(exitCode).toBe(0);
468
+ });
469
+
470
+ it('-e returns false for missing path', async () => {
471
+ const { exitCode } = await run(shell, 'test -e /nope');
472
+ expect(exitCode).toBe(1);
473
+ });
474
+
475
+ it('-x returns true for executable file', async () => {
476
+ await shell.onCommand('chmod +x /home/file.txt');
477
+ const { exitCode } = await run(shell, 'test -x /home/file.txt');
478
+ expect(exitCode).toBe(0);
479
+ });
480
+
481
+ it('-x returns false for non-executable file', async () => {
482
+ const { exitCode } = await run(shell, 'test -x /home/file.txt');
483
+ expect(exitCode).toBe(1);
484
+ });
485
+
486
+ it('-a requires both conditions to be true', async () => {
487
+ const { exitCode } = await run(shell, 'test -e /home/file.txt -a -d /home/subdir');
488
+ expect(exitCode).toBe(0);
489
+ });
490
+
491
+ it('-a fails if one condition is false', async () => {
492
+ const { exitCode } = await run(shell, 'test -e /home/file.txt -a -d /home/file.txt');
493
+ expect(exitCode).toBe(1);
494
+ });
495
+
496
+ it('-o succeeds if either condition is true', async () => {
497
+ const { exitCode } = await run(shell, 'test -e /nope -o -e /home/file.txt');
498
+ expect(exitCode).toBe(0);
499
+ });
500
+
501
+ it('-s returns true for non-empty file', async () => {
502
+ const { exitCode } = await run(shell, 'test -s /home/file.txt');
503
+ expect(exitCode).toBe(0);
504
+ });
505
+ });