Repository navigation
Conversation
lbesecker195
left a comment
There was a problem hiding this comment.
Compared shell.grep against the system grep (ugrep 7.8.4, GNU-compatible output) on 8 inputs. On main, -A/-C drop the last context line whenever the file doesn't end in a newline (e.g. -A 1 on before\\nmatch\\nafter gives match\\n instead of match\\nafter\\n, and -C 1 on match\\nb\\nmatch\\nc loses c). With this branch all 8 match grep, including the trailing-newline and trailing blank line cases, and npx ava test/grep.js passes 40 tests. LGTM.
| }); | ||
|
|
||
| ['-A', '-C'].forEach(option => { | ||
| ['', '\n'].forEach(ending => { |
There was a problem hiding this comment.
Instead of for-loops, could we modularize this with a different coding pattern? What I'd like better is something like:
const checkBehaviorForLineEndings(t, option, lineEnding) {
// Write the test logic here.
}
test('-A option, final context line ending with newline', t => {
checkBehaviorForLineEndings(t, '-A', '\n');
});
test('-C option, final context line ending with newline', t => {
checkBehaviorForLineEndings(t, '-C', '\n');
});
// So on and so forth...| }); | ||
| }); | ||
|
|
||
| test('-A option, final blank context line', t => { |
There was a problem hiding this comment.
Why is there not a corresponding test for -C?
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1268 +/- ##
=======================================
Coverage 97.10% 97.10%
=======================================
Files 36 36
Lines 1518 1519 +1
=======================================
+ Hits 1474 1475 +1
Misses 44 44 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
grep -Aandgrep -Comit the final context line when input ends without a newline. For example,ShellString('match\nafter').grep('-A', 1, 'match')returns only"match\n"with exit code 0.Calculate the actual line count when bounding the after-context slice. This includes the final text line for inputs without a terminating newline and continues to exclude the empty split element created by a terminating newline. Regression tests cover
-A,-C, line numbering, both input endings, and a real final blank line.Validation on Node.js v24.16.0 / Windows:
npx ava test/grep.js test/pipe.js --tap: 50 passing.npm run lintandgit diff --check: passing.npm test -- --tap: 619 passing, 10 failing, 6 skipped. Original HEAD has the exact same 10 failing test names (614 passing, 10 failing, 6 skipped): twocmdtests expect Unix-style command errors, and eight concern symlinks incp,find, andlnon this Windows setup.Investigated and implemented with OpenAI Codex assistance; reproduction and tests were executed locally.