Skip to content

Fix heredoc with space not reported as covered (fixes #102)#103

Merged
infertux merged 1 commit intoinfertux:masterfrom
sonikaarora:fix-heredoc-with-spaces
Feb 17, 2026
Merged

Fix heredoc with space not reported as covered (fixes #102)#103
infertux merged 1 commit intoinfertux:masterfrom
sonikaarora:fix-heredoc-with-spaces

Conversation

@sonikaarora
Copy link
Copy Markdown
Contributor

@sonikaarora sonikaarora commented Feb 16, 2026

Fix heredoc coverage for heredocs with spaces

Fixes #102

Problem

When using heredocs with spaces between << and the delimiter (e.g., << EOF), the heredoc content lines were not being marked as covered, even though they were executed.

Example from issue #102:

cat << EOF > test.txt
This is some file content
EOF

In this case, only line 1 was marked as covered, while lines 2-3 (heredoc content and terminator) were reported as uncovered.

Root Cause

The regex pattern in lib/bashcov/lexer.rb (line 44) did not account for optional whitespace between the heredoc operator and delimiter:

/\A[^\n]+<<-?'?(\w+)'?.*$.*\1/m

This pattern matched heredocs without spaces (<<EOF) but failed to match heredocs with spaces (<< EOF).

Solution

Added \s* to allow optional whitespace between <<-? and the delimiter:

/\A[^\n]+<<-?\s*'?(\w+)'?.*$.*\1/m

Testing

Tested with the exact script from issue #102:

#!/usr/bin/env bash

cat << EOF > test.txt
This is some file content
EOF

Test output:

Testing: test.sh (heredoc with space)
Before: [nil, nil, 1, nil, nil]
After:  [nil, nil, 1, 1, 1]

✓ SUCCESS: All 3 heredoc lines covered!

Co-authored-by: Cursor <cursoragent@cursor.com>
@sonikaarora sonikaarora marked this pull request as draft February 16, 2026 03:04
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Feb 16, 2026

📝 Walkthrough

Walkthrough

The heredoc detection regex in lib/bashcov/lexer.rb was modified to permit optional spaces between the heredoc operator (<<) and the delimiter. The pattern now recognizes heredocs with spacing that were previously unmatched, while maintaining the same capture group behavior.

Changes

Cohort / File(s) Summary
Heredoc Detection
lib/bashcov/lexer.rb
Modified regex pattern to allow optional spaces after heredoc operator by adding \s* after <<-?

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A space here, a space there,
Now heredocs don't cause despair!
The lexer sees them all the same,
Pattern matching—what a game! ✨

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix heredoc with space not reported as covered (fixes #102)' directly and clearly describes the main change: fixing heredoc detection when spaces are present after the heredoc operator.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into master

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
lib/bashcov/lexer.rb (1)

44-44: Good fix — consider [ \t]* instead of \s* to avoid matching newlines.

\s* matches newlines, which could theoretically cause a false match if <<- appears at the end of a line (since the delimiter must be on the same line in bash). Using [ \t]* would be more precise:

Suggested tweak
-          /\A[^\n]+<<-?\s*'?(\w+)'?.*$.*\1/m
+          /\A[^\n]+<<-?[ \t]*'?(\w+)'?.*$.*\1/m

Low risk either way — just a minor robustness improvement.

@sonikaarora sonikaarora marked this pull request as ready for review February 16, 2026 03:06
@sonikaarora
Copy link
Copy Markdown
Contributor Author

🧹 Nitpick comments (1)

While technically more precise, \s* vs [ \t]* won't impact behavior here since the [^\n]+ prefix already prevents matching across newlines. The heredoc delimiter must be on the same line in valid bash, so real-world scripts won't trigger the edge case. Leaving as-is for now.

@sonikaarora
Copy link
Copy Markdown
Contributor Author

@infertux Can you help with the review please

@infertux infertux self-assigned this Feb 17, 2026
@infertux
Copy link
Copy Markdown
Owner

@sonikaarora Did you use AI to create the pull request? The formatting looks suspiciously nice ;)

Either way, it's a valid bug fix so thank you very much. In the future, it would be nice to add a test case to prevent any regressions though.

@infertux infertux merged commit b60c1b1 into infertux:master Feb 17, 2026
12 of 13 checks passed
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.

Heredoc is not reported as covered

2 participants