Skip to content

Auto discover coverage paths from test file names#571

Merged
Chemaclass merged 5 commits into
mainfrom
feat/auto-discover-coverage-paths-from-test-file-names
Dec 21, 2025
Merged

Auto discover coverage paths from test file names#571
Chemaclass merged 5 commits into
mainfrom
feat/auto-discover-coverage-paths-from-test-file-names

Conversation

@Chemaclass

@Chemaclass Chemaclass commented Dec 20, 2025

Copy link
Copy Markdown
Member

📚 Description

This PR introduces automatic source file discovery for coverage tracking based on test file naming conventions. Previously, users had to manually specify BASHUNIT_COVERAGE_PATHS to indicate which files to track. Now, bashunit intelligently discovers source files by analyzing test file names.

The Problem

When running coverage on a specific test file like:

./bashunit tests/unit/assert_test.sh --coverage

Users had to explicitly set BASHUNIT_COVERAGE_PATHS="src/assert.sh" to track the corresponding source file. This was tedious and error-prone, especially when running individual tests.

The Solution

bashunit now automatically discovers source files based on test file naming conventions:

Test File Discovered Source Files
tests/unit/assert_test.sh src/assert.sh, src/assert_*.sh
tests/unit/RunnerTest.sh src/Runner.sh, src/Runner*.sh

The auto-discovery:

  • Supports both _test.sh and Test.sh suffixes
  • Recursively searches the project for matching files
  • Automatically excludes test files, vendor directories, and node_modules
  • Only activates when BASHUNIT_COVERAGE_PATHS is not explicitly set

🔖 Changes

Auto-discovery feature

  • Added bashunit::coverage::auto_discover_paths() function in src/coverage.sh
  • Integrated auto-discovery into runner when coverage is enabled but no paths specified
  • Changed default _BASHUNIT_DEFAULT_COVERAGE_PATHS from "src/" to "" to enable auto-discovery

Coverage line detection improvements

  • Added exclusions for control flow keywords: then, else, fi, do, done, esac, in
  • Added exclusions for case statement patterns: ;;, ;&, ;;&
  • Added exclusions for case patterns like --option) and *)
  • Added exclusion for standalone ) characters

Bug fixes

  • Fixed array expansion issues in HTML report generation for empty arrays (Bash 3.2 compatibility)
  • Removed subshell level tracking that was causing issues with command substitution

Tests

  • Added 13 new tests for control flow keyword and case pattern exclusions
  • Updated existing test to reflect new default empty coverage paths

✅ To-do list

  • I updated the CHANGELOG.md to reflect the new feature or fix
  • I updated the documentation to reflect the changes

  When --coverage-paths is not specified, automatically find source files
  matching the test files base name (e.g., assert_test.sh -> assert*.sh).

  Priority: CLI flag > env var > auto-discovery
  - Remove subshell check that blocked coverage inside $(...) substitutions
  - Exclude control flow keywords from executable lines: then, else, fi,
    do, done, esac, in, ;;, ;&, ;;&
  - Exclude case patterns (--option), *)) and standalone )
  - Fix empty array handling for Bash 3.2 compatibility
@Chemaclass Chemaclass added the enhancement New feature or request label Dec 20, 2025
@Chemaclass Chemaclass self-assigned this Dec 20, 2025
@Chemaclass Chemaclass added the enhancement New feature or request label Dec 20, 2025
@Chemaclass Chemaclass marked this pull request as ready for review December 21, 2025 14:56
@Chemaclass Chemaclass force-pushed the feat/auto-discover-coverage-paths-from-test-file-names branch from 4ac1e30 to 9a0b61c Compare December 21, 2025 15:04
@Chemaclass Chemaclass merged commit 50272bd into main Dec 21, 2025
16 checks passed
@Chemaclass Chemaclass deleted the feat/auto-discover-coverage-paths-from-test-file-names branch December 21, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants