Skip to content

Commit a92b0ff

Browse files
committed
fix(coverage): ignore comment lines
1 parent 9df4230 commit a92b0ff

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

docs/coverage.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,10 @@ The `coverage/lcov.info` file uses the industry-standard LCOV format, compatible
242242
```
243243
TN:
244244
SF:/path/to/source/file.sh
245-
DA:1,0
246245
DA:2,5
247-
DA:3,5
248-
LF:3
249-
LH:2
246+
DA:3,0
247+
LF:2
248+
LH:1
250249
end_of_record
251250
```
252251
@@ -266,7 +265,7 @@ end_of_record
266265
Given this source file `src/math.sh`:
267266
268267
```bash
269-
#!/usr/bin/env bash # Line 1 - executable (shebang)
268+
#!/usr/bin/env bash # Line 1 - not executable (comment/shebang)
270269
function add() { # Line 2 - not executable (function declaration)
271270
echo $(($1 + $2)) # Line 3 - executable
272271
} # Line 4 - not executable (closing brace)
@@ -280,19 +279,17 @@ If tests call `add` twice but never call `multiply`, the LCOV output would be:
280279
```
281280
TN:
282281
SF:/path/to/src/math.sh
283-
DA:1,0
284282
DA:3,2
285283
DA:6,0
286-
LF:3
284+
LF:2
287285
LH:1
288286
end_of_record
289287
```
290288

291289
**Interpretation:**
292-
- Line 1 (shebang): 0 hits (only executed when script is run directly)
293290
- Line 3 (`add` body): 2 hits
294291
- Line 6 (`multiply` body): 0 hits
295-
- 3 executable lines found, 1 line was hit (33% coverage)
292+
- 2 executable lines found, 1 line was hit (50% coverage)
296293

297294
## Parallel Execution
298295

@@ -318,15 +315,14 @@ Coverage percentages should be identical whether running in parallel or sequenti
318315
### Executable Lines
319316

320317
bashunit counts these as executable lines:
321-
- Shebang line (`#!/usr/bin/env bash`)
322318
- Commands and statements
323319
- Single-line function bodies (`function foo() { echo "hi"; }`)
324320

325321
### Non-Executable Lines (Skipped)
326322

327323
These lines are not counted toward coverage:
328324
- Empty lines
329-
- Comment-only lines (except shebang)
325+
- Comment lines (including shebang `#!/usr/bin/env bash`)
330326
- Function declaration lines (`function foo() {`)
331327
- Lines with only braces (`{` or `}`)
332328

src/coverage.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,14 @@ function bashunit::coverage::is_executable_line() {
227227
local line="$1"
228228
local lineno="$2"
229229

230+
# Unused but kept for API compatibility
231+
: "$lineno"
232+
230233
# Skip empty lines (line with only whitespace)
231234
[[ -z "${line// }" ]] && return 1
232235

233-
# Skip comment-only lines (but not shebang on line 1)
234-
if [[ "$line" =~ ^[[:space:]]*# ]] && [[ $lineno -ne 1 ]]; then
235-
return 1
236-
fi
236+
# Skip comment-only lines (including shebang)
237+
[[ "$line" =~ ^[[:space:]]*# ]] && return 1
237238

238239
# Skip function declaration lines (but not single-line functions with body)
239240
[[ "$line" =~ $_BASHUNIT_COVERAGE_FUNC_PATTERN ]] && return 1

tests/unit/coverage_test.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,19 @@ my_func
112112
EOF
113113

114114
# Expected executable lines:
115-
# Line 1: shebang (counted)
115+
# Line 1: shebang (not counted - it's a comment)
116116
# Line 3: comment (not counted)
117117
# Line 4: function declaration (not counted)
118118
# Line 5: echo "hello" (counted)
119119
# Line 6: echo "world" (counted)
120120
# Line 7: } (not counted)
121121
# Line 9: my_func (counted)
122-
# Total: 4 executable lines
122+
# Total: 3 executable lines
123123

124124
local count
125125
count=$(bashunit::coverage::get_executable_lines "$temp_file")
126126

127-
assert_equals "4" "$count"
127+
assert_equals "3" "$count"
128128

129129
rm -f "$temp_file"
130130
}
@@ -203,10 +203,11 @@ function test_coverage_is_executable_line_returns_false_for_comments() {
203203
assert_equals "no" "$result"
204204
}
205205

206-
function test_coverage_is_executable_line_returns_true_for_shebang() {
206+
function test_coverage_is_executable_line_returns_false_for_shebang() {
207+
# Shebang is a comment line, not executable (only runs when script invoked directly)
207208
local result
208209
result=$(bashunit::coverage::is_executable_line '#!/usr/bin/env bash' 1 && echo "yes" || echo "no")
209-
assert_equals "yes" "$result"
210+
assert_equals "no" "$result"
210211
}
211212

212213
function test_coverage_is_executable_line_returns_false_for_function_declaration() {
@@ -282,11 +283,12 @@ EOF
282283
local content
283284
content=$(cat "$report_file")
284285

286+
# Line 1 (shebang) is not counted - only lines 2 and 3 are executable
285287
assert_contains "TN:" "$content"
286288
assert_contains "SF:${temp_file}" "$content"
287-
assert_contains "DA:1," "$content"
288289
assert_contains "DA:2," "$content"
289-
assert_contains "LF:3" "$content"
290+
assert_contains "DA:3," "$content"
291+
assert_contains "LF:2" "$content"
290292
assert_contains "end_of_record" "$content"
291293

292294
rm -f "$temp_file" "$report_file"

0 commit comments

Comments
 (0)