Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/e2e_pycobertura.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
name: Compare FortCov vs Cobertura (gcovr)
runs-on: ubuntu-latest
timeout-minutes: 10
env:
FORTCOV_ALLOW_AUTOTEST: "1"

steps:
- name: Checkout repository
Expand Down
5 changes: 5 additions & 0 deletions doc/coverage_workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ By default, `fortcov` analyzes existing `.gcov` files and does not invoke
`gcov` or run your tests. Use the built-in bridge `--gcov` (alias:
`--discover-and-gcov`) to auto-discover FPM/CMake build directories, run under
coverage, generate `.gcov`, and then analyze them.

Test-environment guard: When `fortcov` detects it is running under a test
harness (e.g., `fpm test`), it skips auto-running tests to avoid recursion.
In controlled CI jobs where this is desired, set the environment variable
`FORTCOV_ALLOW_AUTOTEST=1` to explicitly allow the auto-test bridge.
5 changes: 5 additions & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ main = "main.f90"
name = "cli_flags_rejection_minimal"
source-dir = "test"
main = "test_cli_flags_rejection_minimal.f90"

[[test]]
name = "test_env_guard_override"
source-dir = "test"
main = "test_test_env_guard_override.f90"
9 changes: 7 additions & 2 deletions src/utils/runtime/test_env_guard.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module test_env_guard
!! Runtime test-environment detection to prevent recursive execution
!! Used to avoid spawning external test runners (e.g., fpm test) when
!! fortcov itself is being run under a test harness.
use iso_fortran_env, only: error_unit
implicit none
private

Expand All @@ -18,6 +17,13 @@ function running_under_test_env() result(is_test_env)

is_test_env = .false.

! Allow explicit override to enable auto-test/bridge in controlled environments
call get_environment_variable('FORTCOV_ALLOW_AUTOTEST', env_value, status=status)
if (status == 0 .and. len_trim(env_value) > 0) then
is_test_env = .false.
return
end if

call get_environment_variable('FPM_TEST', env_value, status=status)
if (status == 0 .and. len_trim(env_value) > 0) then
is_test_env = .true.
Expand Down Expand Up @@ -50,4 +56,3 @@ function running_under_test_env() result(is_test_env)
end function running_under_test_env

end module test_env_guard

42 changes: 42 additions & 0 deletions test/test_test_env_guard_override.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
program test_test_env_guard_override
use iso_fortran_env, only: error_unit
use iso_c_binding, only: c_int, c_char
use test_env_guard, only: running_under_test_env
implicit none

interface
function c_setenv(name, value, overwrite) bind(C, name="setenv") result(ret)
import :: c_int, c_char
character(kind=c_char), dimension(*) :: name
character(kind=c_char), dimension(*) :: value
integer(c_int), value :: overwrite
integer(c_int) :: ret
end function c_setenv
end interface

logical :: is_test

if (c_setenv('FORTCOV_TEST_MODE' // char(0), '1' // char(0), 1_c_int) /= 0_c_int) then
write(error_unit, '(A)') 'Failed to set FORTCOV_TEST_MODE'
stop 1
end if

is_test = running_under_test_env()
if (.not. is_test) then
write(error_unit, '(A)') 'Expected test environment detection to be TRUE'
stop 2
end if

if (c_setenv('FORTCOV_ALLOW_AUTOTEST' // char(0), '1' // char(0), 1_c_int) /= 0_c_int) then
write(error_unit, '(A)') 'Failed to set FORTCOV_ALLOW_AUTOTEST'
stop 3
end if

is_test = running_under_test_env()
if (is_test) then
write(error_unit, '(A)') 'Override did not bypass test environment guard'
stop 4
end if

print *, 'OK: test_env_guard override bypasses test detection when enabled'
end program test_test_env_guard_override