Visitar URL original
fix(grep): include final context line without trailing newline by emme1t · Pull Request #1268 · shelljs/shelljs · GitHub
Skip to content

fix(grep): include final context line without trailing newline - #1268

Open
emme1t wants to merge 2 commits into
shelljs:mainfrom
emme1t:fix/grep-final-context-line
Open

emme1t wants to merge 2 commits into
shelljs:mainfrom
emme1t:fix/grep-final-context-line

Conversation

@emme1t

@emme1t emme1t commented Sep 12, 2026

Copy link
Copy Markdown

grep -A and grep -C omit 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:

  • Before the fix: 2 of the 5 new regressions fail, both for input without a trailing newline.
  • npx ava test/grep.js test/pipe.js --tap: 50 passing.
  • npm run lint and git diff --check: passing.
  • Five input/option combinations match GNU grep's stdout and exit code.
  • 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): two cmd tests expect Unix-style command errors, and eight concern symlinks in cp, find, and ln on this Windows setup.

Investigated and implemented with OpenAI Codex assistance; reproduction and tests were executed locally.

@lbesecker195 lbesecker195 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test/grep.js Outdated
});

['-A', '-C'].forEach(option => {
['', '\n'].forEach(ending => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

Comment thread test/grep.js
});
});

test('-A option, final blank context line', t => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there not a corresponding test for -C?

@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.10%. Comparing base (f364da6) to head (286ffa7).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants