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.
- package/CHANGELOG.md +14 -0
- package/POSIX-CHECKLIST.md +16 -15
- package/dist/shell.es.js +943 -652
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +15 -6
- 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/lib/local/read.js +63 -16
- package/package.json +1 -1
- package/test/builtins.test.js +217 -0
- package/test/local-commands.test.js +86 -0
- package/test/scripts.test.js +97 -0
- package/test/shell.test.js +159 -0
- package/test/wbt.test.js +22 -0
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({
|