diff options
| author | Agando <roduccomm@gmail.com> | 2026-10-27 16:26:30 +0100 |
|---|---|---|
| committer | Agando <roduccomm@gmail.com> | 2026-10-27 16:26:30 +0100 |
| commit | fd24b52c87d53bad5bd9c81d8e008511f160eda5 (patch) | |
| tree | a083f1971d68378b469529268ba32a6049d31734 /modules/test/test-shell-stdin.js | |
| parent | f325b6d15179bb02147039c89e9fcfac6d8e067b (diff) | |
fix(newos4): piped grep searched the working directory instead of its input
The stdin work in 5cf5010 fixed wc/head/tail but left grep alone, and grep's no-operand branch
sweeps the cwd. Measured on v4.6.115: `cat /gdir/in.txt | grep zzz` answered
`other.txt:1: zzz in a cwd file` — a file that was never piped, and `cat in.txt | grep bbb`
reported the match twice (`in.txt` from the cwd sweep plus `__pipebuf__` from the injected file).
- module-fs.js: when terminal._pipeInput is set and no file operand was given, grep searches the
staged text and prints `<line>: <text>` (stdin has no filename, as in GNU). The cwd sweep stays
for a bare interactive `grep x`.
- shell.js: _isPipeAware now covers only sort/cut/uniq/expand. grep/head/tail/wc read _pipeInput
themselves, so injecting the __pipebuf__ file produced duplicate hits and named stdin results.
Verification:
- test-shell-stdin.js 20 asserts (+4 for grep: no cwd match, non-match reports none, match prints
no filename, -c counts stdin). RED on HEAD: G1/G2/G3 fail with `other.txt:1: zzz in a cwd file`.
- Suite 18/18 files; maintain --fix "node tests 18/18 pass".
- Live v4.6.116 after `cd /gdir` (where other.txt contains zzz):
`cat /gdir/in.txt | grep zzz` -> (no matches) · `| grep bbb` -> `2: bbb` · `| grep -c b` -> 1 ·
`| head -n 2` -> aaa,bbb · `| tail -n 1` -> ccc · `| wc -l` -> 3.
Credit: the false-positive grep was reported by the stdin subagent as an aside; re-derived here
against the live terminal before fixing.
Diffstat (limited to 'modules/test/test-shell-stdin.js')
| -rw-r--r-- | modules/test/test-shell-stdin.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/modules/test/test-shell-stdin.js b/modules/test/test-shell-stdin.js index 10ba190..1b0fbca 100644 --- a/modules/test/test-shell-stdin.js +++ b/modules/test/test-shell-stdin.js @@ -29,6 +29,33 @@ const mk = (files, pipeInput) => { }; const last = (out) => out.length ? out[out.length - 1] : '<nothing printed>'; +// (g) grep over a pipe must search the piped text, never the working directory. +// Measured 2026-10-27 on v4.6.115: `cat /gdir/in.txt | grep zzz` answered +// "other.txt:1: zzz in a cwd file" — a file that was never piped. +{ + const decoy = { + readFile: () => null, + getCurrentDir: () => ({ name: 'gdir', content: { + 'other.txt': { type: 'file', content: 'zzz in a cwd file' }, + 'in.txt': { type: 'file', content: 'aaa\nbbb\nccc' }, + } }), + }; + const out = []; + const term = { fs: decoy, _pipeInput: 'aaa\nbbb\nccc', print: (m) => out.push(String(m)) }; + const m = new FileSystemModule(); + m.terminal = term; m.registerCommands(); + m.commands.grep(['zzz']); + ok('G1 grep over a pipe does not search the cwd', !out.join('|').includes('other.txt'), JSON.stringify(out)); + ok('G2 a non-match over a pipe reports no matches', out.join('|').includes('(no matches)'), JSON.stringify(out)); + + out.length = 0; + m.commands.grep(['bbb']); + ok('G3 a match prints the line number without a filename', out.length === 1 && out[0] === '2: bbb', JSON.stringify(out)); + + out.length = 0; + m.commands.grep(['-c', 'b']); + ok('G4 grep -c over a pipe counts stdin lines', out.length === 1 && out[0] === '1', JSON.stringify(out)); +} // (a) wc -l over 3 lines of stdin { const { m, out } = mk({}, 'alpha\nbeta\ngamma'); |