Skip to content

Commit 7b161c7

Browse files
authored
Return program's exit code on unsuccessful command (#852)
2 parents 251383a + a4813e6 commit 7b161c7

File tree

6 files changed

+101
-7
lines changed

6 files changed

+101
-7
lines changed

ci/run_tests.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,41 @@ pushd cpp_files
158158
"$fpm" test
159159
popd
160160

161+
# Test app exit codes
162+
pushd fpm_test_exit_code
163+
"$fpm" build
164+
165+
# odd number -> success!
166+
EXIT_CODE=0
167+
"$fpm" run -- 1 || EXIT_CODE=$?
168+
test $EXIT_CODE -eq 0
169+
170+
# even number -> error 3
171+
EXIT_CODE=0
172+
"$fpm" run -- 512 || EXIT_CODE=$?
173+
test $EXIT_CODE -eq 3
174+
175+
# even number -> error 3
176+
EXIT_CODE=0
177+
"$fpm" run -- 0 || EXIT_CODE=$?
178+
test $EXIT_CODE -eq 3
179+
180+
# not an integer -> error 2
181+
EXIT_CODE=0
182+
"$fpm" run -- 3.1415 || EXIT_CODE=$?
183+
test $EXIT_CODE -eq 2
184+
185+
# not a number -> error 2
186+
EXIT_CODE=0
187+
"$fpm" run -- notanumber || EXIT_CODE=$?
188+
test $EXIT_CODE -eq 2
189+
190+
# no arguments -> error 1
191+
EXIT_CODE=0
192+
"$fpm" run || EXIT_CODE=$?
193+
test $EXIT_CODE -eq 1
194+
popd
195+
196+
161197
# Cleanup
162198
rm -rf ./*/build
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# fpm_test_exit_code
2+
Test program for application exit codes
3+
see https://github.com/fortran-lang/fpm/issues/848
4+
5+
This app expects to receive an integer command line argument, to check whether it is odd or even.
6+
It returns 0 on success (odd input), or among a few error codes otherwise.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
! Test program for application exit codes
2+
! see https://github.com/fortran-lang/fpm/issues/848
3+
4+
! This app expects to receive an integer command line argument, to check whether it is odd or even.
5+
! It returns 0 on success (odd input), or among a few error codes otherwise.
6+
7+
program check_odd_number
8+
implicit none
9+
10+
integer, parameter :: SUCCESS = 0
11+
integer, parameter :: INVALID_ARGUMENT = 1
12+
integer, parameter :: NOT_AN_INTEGER = 2
13+
integer, parameter :: NOT_ODD = 3
14+
15+
character(len=1024) :: buffer
16+
integer :: ierr,ln,the_number
17+
18+
! If the argument is missing or not an integer, return an error flag
19+
if (command_argument_count()/=1) stop INVALID_ARGUMENT
20+
21+
! Get command argument
22+
call get_command_argument(1,value=buffer,length=ln,status=ierr)
23+
24+
! On invalid string
25+
if (ln<1 .or. ierr/=0) stop INVALID_ARGUMENT
26+
27+
! Read to int
28+
read(buffer(:ln),*,iostat=ierr) the_number
29+
30+
! On invalid integer
31+
if (ierr/=0) stop NOT_AN_INTEGER
32+
33+
! Check if it is odd or even
34+
if (mod(the_number,2)==0) then
35+
! Is even
36+
stop NOT_ODD
37+
else
38+
! Is odd
39+
stop SUCCESS
40+
end if
41+
42+
end program check_odd_number
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name = "fpm_test_exit_code"
2+
version = "0.1.0"
3+
license = "license"
4+
author = "Federico Perini"
5+
maintainer = "[email protected]"
6+
copyright = "Copyright 2023, Jane Doe"
7+
[build]
8+
auto-executables = true
9+
[install]
10+
library = false

src/fpm.f90

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ subroutine cmd_run(settings,test)
445445
type(string_t), allocatable :: executables(:)
446446
type(build_target_t), pointer :: exe_target
447447
type(srcfile_t), pointer :: exe_source
448-
integer :: run_scope
448+
integer :: run_scope,firsterror
449449
integer, allocatable :: stat(:)
450450
character(len=:),allocatable :: line
451451
logical :: toomany
@@ -590,10 +590,12 @@ subroutine cmd_run(settings,test)
590590
if (any(stat /= 0)) then
591591
do i=1,size(stat)
592592
if (stat(i) /= 0) then
593-
write(stderr,'(*(g0:,1x))') '<ERROR> Execution failed for object "',basename(executables(i)%s),'"'
593+
write(stderr,'(*(g0:,1x))') '<ERROR> Execution for object "',basename(executables(i)%s),&
594+
'" returned exit code ',stat(i)
594595
end if
595596
end do
596-
call fpm_stop(1,'*cmd_run*:stopping due to failed executions')
597+
firsterror = findloc(stat/=0,value=.true.,dim=1)
598+
call fpm_stop(stat(firsterror),'*cmd_run*:stopping due to failed executions')
597599
end if
598600

599601
endif

src/fpm_filesystem.F90

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,8 @@ subroutine run(cmd,echo,exitstat,verbose,redirect)
924924

925925
if (present(exitstat)) then
926926
exitstat = stat
927-
else
928-
if (stat /= 0) then
929-
call fpm_stop(1,'*run*:Command failed')
930-
end if
927+
elseif (stat /= 0) then
928+
call fpm_stop(stat,'*run*: Command '//cmd//redirect_str//' returned a non-zero status code')
931929
end if
932930

933931
end subroutine run

0 commit comments

Comments
 (0)