Skip to content

Commit

Permalink
test(command_line_t): improve test robustness
Browse files Browse the repository at this point in the history
This commit replaces the previously fragile unit tests for the
command_line_t type with tests that no longer require launching an
fpm subprocess via execute_command_line and checking that child
process's exit status.  The test main program also now prints
instructions for how to run the command_line_t tests if the flags
for running those tests is not detected.
  • Loading branch information
rouson committed Dec 17, 2024
1 parent 814fa6a commit 34db8b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
34 changes: 8 additions & 26 deletions test/command_line_test.F90
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function results() result(test_results)
test_descriptions = [ &
test_description_t(string_t("returning the value passed after a command-line flag"), check_flag_value), &
test_description_t(string_t("returning an empty string when a flag value is missing"), handle_missing_flag_value), &
test_description_t(string_t("detecting a present command-line argument"), check_command_line_argument) &
test_description_t(string_t("detecting a present command-line argument"), check_argument_present) &
]
#else
! Work around missing Fortran 2008 feature: associating a procedure actual argument with a procedure pointer dummy argument:
Expand All @@ -57,38 +57,20 @@ function results() result(test_results)

function check_flag_value() result(test_passes)
logical test_passes
integer exit_status, command_status
character(len=132) command_message

call execute_command_line( &
command = "fpm run --example get-flag-value -- --input-file some_file_name", &
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
)
test_passes = exit_status == 0
type(command_line_t) command_line
test_passes = command_line%flag_value("--test") == "command_line_t"
end function

function handle_missing_flag_value() result(test_passes)
logical test_passes
integer exit_status, command_status
character(len=132) command_message

call execute_command_line( &
command = "fpm run --example handle-missing-flag -- --empty-flag", &
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
)
test_passes = exit_status == 0
type(command_line_t) command_line
test_passes = command_line%flag_value("--type") == ""
end function

function check_command_line_argument() result(test_passes)
function check_argument_present() result(test_passes)
logical test_passes
integer exit_status, command_status
character(len=132) command_message

call execute_command_line( &
command = "fpm run --example check-command-line-argument -- --some-argument", &
wait = .true., exitstat = exit_status, cmdstat = command_status, cmdmsg = command_message &
)
test_passes = exit_status == 0
type(command_line_t) command_line
test_passes = command_line%argument_present(["--type"])
end function

end module command_line_test_m
35 changes: 23 additions & 12 deletions test/main.F90
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,38 @@ program main
type(test_description_test_t) test_description_test
type(vector_test_description_test_t) vector_test_description_test

type(command_line_t) command_line

integer :: passes=0, tests=0

block
type(command_line_t) command_line
character(len=*), parameter :: usage = &
new_line('') // new_line('') // &
'Usage: fpm test -- [--help] | [--contains <substring>]' // &
new_line('') // new_line('') // &
'where square brackets ([]) denote optional arguments, a pipe (|) separates alternative arguments,' // new_line('') // &
'angular brackets (<>) denote a user-provided value, and passing a substring limits execution to' // new_line('') // &
'the tests with test subjects or test descriptions containing the user-specified substring.' // new_line('')
if (command_line%argument_present([character(len=len("--help"))::"--help","-h"])) stop usage
end block
character(len=*), parameter :: usage = &
new_line('') // new_line('') // &
'Usage: fpm test -- [--help] | [--contains <substring>]' // &
new_line('') // new_line('') // &
'where square brackets ([]) denote optional arguments, a pipe (|) separates alternative arguments,' // new_line('') // &
'angular brackets (<>) denote a user-provided value, and passing a substring limits execution to' // new_line('') // &
'the tests with test subjects or test descriptions containing the user-specified substring.' // new_line('')

if (command_line%argument_present([character(len=len("--help"))::"--help","-h"])) stop usage

call bin_test%report(passes, tests)
call formats_test%report(passes, tests)
call string_test%report(passes, tests)
call test_result_test%report(passes, tests)
call test_description_test%report(passes, tests)
call vector_test_description_test%report(passes,tests)
if (.not. GitHub_CI()) call command_line_test%report(passes, tests)

if (.not. GitHub_CI()) then
if (command_line%argument_present(["--test"])) then
call command_line_test%report(passes, tests)
else
write(*,"(a)") &
new_line("") // &
"To also test Julienne's command_line_t type, append the following to your fpm test command:" // &
new_line("") // &
"-- --test command_line_t --type"
end if
end if

#if HAVE_MULTI_IMAGE_SUPPORT
if (this_image()==1) then
Expand Down

0 comments on commit 34db8b1

Please sign in to comment.