Debian Codegraph @

test/cli_test/cli_test.f90

raw
1 program main
2
3 ! for each set of command options, call this command recursively which will print the resulting parameters with a
4 ! given test command CMD from the TEST() array.
5 !
6 ! Then read the expected values as a NAMELIST group from the test array and compare the expected
7 ! results with the actual results.
8 !
9 ! the PARSE() subroutine is a copy of the app/main.f90 program except it creates and writes a NAMELIST file instead
10 ! of actually calling the subcommands.
11 !
12 ! The program will exit with a non-zero status if any of the tests fail
13
14 use, intrinsic :: iso_fortran_env, only : compiler_version, compiler_options
15 implicit none
16
17 ! convenient arbitrary sizes for test
18
19 ! assuming no name over 15 characters to make output have shorter lines
20 character(len=15),allocatable :: name(:),act_name(:) ; namelist/act_cli/act_name
21 character(len=15),allocatable :: features(:),act_features(:) ; namelist/act_cli/act_features
22 integer,parameter :: max_names=10
23
24 character(len=:),allocatable :: command
25 character(len=:),allocatable :: cmd
26 integer :: cstat, estat
27 integer :: act_cstat, act_estat
28 integer :: i, ios
29 logical :: w_e,act_w_e ; namelist/act_cli/act_w_e
30 logical :: w_t,act_w_t ; namelist/act_cli/act_w_t
31 logical :: c_s,act_c_s ; namelist/act_cli/act_c_s
32 logical :: c_a,act_c_a ; namelist/act_cli/act_c_a
33 logical :: c_t,act_c_t ; namelist/act_cli/act_c_t
34 logical :: c_apps,act_c_apps ; namelist/act_cli/act_c_apps
35 logical :: c_ex,act_c_ex ; namelist/act_cli/act_c_ex
36 logical :: reg_c,act_reg_c ; namelist/act_cli/act_reg_c
37 logical :: show_v,act_show_v ; namelist/act_cli/act_show_v
38 logical :: show_u_d,act_show_u_d; namelist/act_cli/act_show_u_d
39 logical :: dry_run,act_dry_run ; namelist/act_cli/act_dry_run
40 character(len=:), allocatable :: token, act_token ; namelist/act_cli/act_token
41
42 character(len=:), allocatable :: profile,act_profile ; namelist/act_cli/act_profile
43 character(len=:), allocatable :: args,act_args ; namelist/act_cli/act_args
44 namelist/expected/cmd,cstat,estat,w_e,w_t,c_s,c_a,c_t,c_apps,c_ex,reg_c,name,features,profile,args,show_v,show_u_d,dry_run,token
45 integer :: lun
46 logical,allocatable :: tally(:)
47 logical,allocatable :: subtally(:)
48 character(len=256) :: message
49
50 ! table of arguments to pass to program and expected non-default values for that execution in NAMELIST group format
51 character(len=*),parameter :: tests(*)= [ character(len=256) :: &
52
53 'CMD="new", ESTAT=1,', &
54 !'CMD="new -unknown", ESTAT=2,', &
55 'CMD="new my_project another yet_another -test", ESTAT=2,', &
56 'CMD="new my_project --app", W_E=T, NAME="my_project",', &
57 'CMD="new my_project --app --test", W_E=T,W_T=T, NAME="my_project",', &
58 'CMD="new my_project --test", W_T=T, NAME="my_project",', &
59 'CMD="new my_project", W_E=T,W_T=T, NAME="my_project",', &
60
61 'CMD="run", ', &
62 'CMD="run my_project", NAME="my_project", ', &
63 'CMD="run proj1 p2 project3", NAME="proj1","p2","project3", ', &
64 'CMD="run proj1 p2 project3 --profile debug", NAME="proj1","p2","project3",profile="debug",', &
65 'CMD="run proj1 p2 project3 --profile release", NAME="proj1","p2","project3",profile="release",', &
66 'CMD="run proj1 p2 project3 --profile release -- arg1 -x ""and a long one""", &
67 &NAME="proj1","p2","project3",profile="release",ARGS="""arg1"" ""-x"" ""and a long one""", ', &
68
69 'CMD="test", ', &
70 'CMD="test my_project", NAME="my_project", ', &
71 'CMD="test proj1 p2 project3", NAME="proj1","p2","project3", ', &
72 'CMD="test proj1 p2 project3 --profile debug", NAME="proj1","p2","project3",profile="debug",', &
73 'CMD="test proj1 p2 project3 --profile release", NAME="proj1","p2","project3",profile="release",', &
74 'CMD="test proj1 p2 project3 --profile release -- arg1 -x ""and a long one""", &
75 &NAME="proj1","p2","project3",profile="release",ARGS="""arg1"" ""-x"" ""and a long one""", ', &
76
77 'CMD="build", NAME=, profile="",features=,ARGS="",', &
78 'CMD="build --profile release", NAME=, profile="release",features=,ARGS="",', &
79 'CMD="build --features debug,mpi", NAME=, profile="",features="debug","mpi",ARGS="",', &
80 'CMD="build --features single_feature", NAME=, profile="",features="single_feature",ARGS="",', &
81 'CMD="test --features debug,openmp", NAME=, profile="",features="debug","openmp",ARGS="",', &
82
83 'CMD="clean", NAME=, ARGS="",', &
84 'CMD="clean --skip", C_S=T, NAME=, ARGS="",', &
85 'CMD="clean --all", C_A=T, NAME=, ARGS="",', &
86 'CMD="clean --test", C_T=T, NAME=, ARGS="",', &
87 'CMD="clean --apps", C_APPS=T, NAME=, ARGS="",', &
88 'CMD="clean --examples", C_EX=T, NAME=, ARGS="",', &
89 'CMD="clean --test --apps", C_T=T, C_APPS=T, NAME=, ARGS="",', &
90 'CMD="clean --registry-cache", REG_C=T, NAME=, ARGS="",', &
91 'CMD="publish --token abc --show-package-version", SHOW_V=T, NAME=, token="abc",ARGS="",', &
92 'CMD="publish --token abc --show-upload-data", SHOW_U_D=T, NAME=, token="abc",ARGS="",', &
93 'CMD="publish --token abc --dry-run", DRY_RUN=T, NAME=, token="abc",ARGS="",', &
94 'CMD="publish --token abc", NAME=, token="abc",ARGS="",', &
95 ' ' ]
96 character(len=256) :: readme(3)
97
98 readme(1)='&EXPECTED' ! top and bottom line for a NAMELIST group read from TEST() used to set the expected values
99 readme(3)=' /'
100 tally=[logical ::] ! an array that tabulates the command test results as pass or fail.
101
102 if(command_argument_count()==0)then ! assume if called with no arguments to do the tests. This means you cannot
103 ! have a test of no parameters. Could improve on this.
104 ! if called with parameters assume this is a test and call the routine to
105 ! parse the resulting values after calling the CLI command line parser
106 ! and write the NAMELIST group so it can be read and tested against the
107 ! expected results
108 write(*,*)'start tests of the CLI command line parser'
109 command=repeat(' ',4096)
110 call get_command_argument(0,command)
111 command=trim(command)
112 write(*,*)'command=',command
113
114 do i=1,size(tests)
115 if(tests(i)==' ')then
116 open(file='_test_cli',newunit=lun,delim='quote')
117 close(unit=lun,status='delete')
118 exit
119 endif
120 ! blank out name group EXPECTED
121 name=[(repeat(' ',len(name)),i=1,max_names)] ! the words on the command line sans the subcommand name
122 if(.not.allocated(features)) allocate(character(len=15) :: features(max_names))
123 features=[(repeat(' ',15),i=1,max_names)] ! the features on the command line
124 profile='' ! --profile PROF
125 w_e=.false. ! --app
126 w_t=.false. ! --test
127 c_s=.false. ! --skip
128 c_a=.false. ! --all
129 reg_c=.false. ! --registry-cache
130 show_v=.false. ! --show-package-version
131 show_u_d=.false. ! --show-upload-data
132 dry_run=.false. ! --dry-run
133 token='' ! --token TOKEN
134 args=repeat(' ',132) ! -- ARGS
135 cmd=repeat(' ',132) ! the command line arguments to test
136 cstat=0 ! status values from EXECUTE_COMMAND_LINE()
137 estat=0
138 readme(2)=' '//tests(i) ! select command options to test for CMD and set nondefault expected values
139 read(readme,nml=expected)
140
141 write(*,'(*(g0))')'START: TEST ',i,' CMD=',trim(cmd)
142 ! call this program which will crack command line and write results to scratch file _test_cli
143 call execute_command_line(command//' '//trim(cmd),cmdstat=act_cstat,exitstat=act_estat)
144 if(cstat==act_cstat.and.estat==act_estat)then
145 if(estat==0)then
146 open(file='_test_cli',newunit=lun,delim='quote')
147 act_name=[(repeat(' ',len(act_name)),i=1,max_names)]
148 if(.not.allocated(act_features)) allocate(character(len=15) :: act_features(max_names))
149 act_features=[(repeat(' ',15),i=1,max_names)]
150 act_profile=''
151 act_w_e=.false.
152 act_w_t=.false.
153 act_c_s=.false.
154 act_c_a=.false.
155 act_reg_c=.false.
156 act_show_v=.false.
157 act_show_u_d=.false.
158 act_dry_run=.false.
159 act_token=''
160 act_args=repeat(' ',132)
161 read(lun,nml=act_cli,iostat=ios,iomsg=message)
162 if(ios/=0)then
163 write(*,'(a)')'ERROR:',trim(message)
164 endif
165 close(unit=lun)
166 ! compare results to expected values
167 subtally=[logical ::]
168 call test_test('NAME',all(act_name==name))
169 call test_test('FEATURES',all(act_features==features))
170 call test_test('PROFILE',act_profile==profile)
171 call test_test('SKIP',act_c_s.eqv.c_s)
172 call test_test('ALL',act_c_a.eqv.c_a)
173 call test_test('REGISTRY-CACHE',act_reg_c.eqv.reg_c)
174 call test_test('WITH_EXPECTED',act_w_e.eqv.w_e)
175 call test_test('WITH_TESTED',act_w_t.eqv.w_t)
176 call test_test('WITH_TEST',act_w_t.eqv.w_t)
177 call test_test('SHOW-PACKAGE-VERSION',act_show_v.eqv.show_v)
178 call test_test('SHOW-UPLOAD-DATA',act_show_u_d.eqv.show_u_d)
179 call test_test('DRY-RUN',act_dry_run.eqv.dry_run)
180 call test_test('TOKEN',act_token==token)
181 call test_test('ARGS',act_args==args)
182 if(all(subtally))then
183 write(*,'(*(g0))')'PASSED: TEST ',i,' STATUS: expected ',cstat,' ',estat,' actual ',act_cstat,' ',act_estat,&
184 & ' for [',trim(cmd),']'
185 tally=[tally,.true.]
186 else
187 write(*,'(*(g0))')'FAILED: TEST ',i,' STATUS: expected ',cstat,' ',estat,' actual ',act_cstat,' ',act_estat,&
188 & ' for [',trim(cmd),']'
189 print '(4a)', &
190 'This file was compiled by ', &
191 compiler_version(), &
192 ' using the options ', &
193 compiler_options()
194 write(*,nml=act_cli,delim='quote')
195 tally=[tally,.false.]
196 endif
197 else
198 write(*,'(*(g0))')'PASSED: TEST ',i,' EXPECTED BAD STATUS: expected ',cstat,' ',estat, &
199 ' actual ',act_cstat,' ',act_estat,' for [',trim(cmd),']'
200 tally=[tally,.true.]
201 endif
202 else
203 write(*,'(*(g0))')'FAILED: TEST ',i,'BAD STATUS: expected ',cstat,' ',estat,' actual ',act_cstat,' ',act_estat,&
204 ' for [',trim(cmd),']'
205 tally=[tally,.false.]
206 endif
207 enddo
208 ! write up total results and if anything failed exit with a non-zero status
209 write(*,'(*(g0))')'TALLY;',tally
210 if(all(tally))then
211 write(*,'(*(g0))')'PASSED: all ',count(tally),' tests passed '
212 else
213 write(*,*)'FAILED: PASSED=',count(tally),' FAILED=',count(.not.tally)
214 stop 4
215 endif
216 else
217 ! call this program with arguments
218 !=============================================
219 debugit: block
220 integer :: j, ilen
221 character(len=256) :: big_argument
222 write(*,*)'arguments seen directly by program'
223 do j=1,command_argument_count()
224 call get_command_argument(number=j,value=big_argument,length=ilen)
225 write(*,'(*(g0))')j,'[',big_argument(:ilen),']'
226 enddo
227 end block debugit
228 !=============================================
229 call parse()
230 endif
231
232 contains
233
234 subroutine test_test(name,tst)
235 character(len=*) :: name
236 logical,intent(in) :: tst
237 !!write(*,'(*(g0,1x))')' SUBTEST ',name,' ',merge('PASSED','FAILED',tst)
238 subtally=[subtally,tst]
239 end subroutine test_test
240
241 subroutine parse()
242 ! all the extended types for settings from the main program
243 use fpm_command_line, only: &
244 fpm_cmd_settings, &
245 fpm_new_settings, &
246 fpm_build_settings, &
247 fpm_run_settings, &
248 fpm_test_settings, &
249 fpm_clean_settings, &
250 fpm_install_settings, &
251 get_command_line_settings, &
252 fpm_publish_settings
253 use fpm, only: cmd_run, cmd_clean
254 use fpm_cmd_install, only: cmd_install
255 use fpm_cmd_new, only: cmd_new
256 use fpm_cmd_publish, only: cmd_publish
257 class(fpm_cmd_settings), allocatable :: cmd_settings
258 ! duplicates the calls as seen in the main program for fpm
259 call get_command_line_settings(cmd_settings)
260
261 allocate (character(len=len(name)) :: act_name(0) )
262 allocate(character(len=15) :: act_features(max_names))
263 act_features=[(repeat(' ',15),i=1,max_names)]
264 act_args=''
265 act_w_e=.false.
266 act_w_t=.false.
267 act_c_s=.false.
268 act_c_a=.false.
269 act_reg_c=.false.
270 act_show_v=.false.
271 act_show_u_d=.false.
272 act_dry_run=.false.
273 act_token=''
274 act_profile=''
275
276 select type(settings=>cmd_settings)
277 type is (fpm_new_settings)
278 act_w_e=settings%with_executable
279 act_w_t=settings%with_test
280 act_name=[trim(settings%name)]
281 type is (fpm_build_settings)
282 if (allocated(settings%profile)) act_profile=settings%profile
283 if (allocated(settings%features)) then
284 do i = 1, min(size(settings%features),size(act_features))
285 act_features(i) = settings%features(i)%s
286 end do
287 end if
288 type is (fpm_run_settings)
289 if (allocated(settings%profile)) act_profile=settings%profile
290 if (allocated(settings%features)) then
291 do i = 1, min(size(settings%features),size(act_features))
292 act_features(i) = settings%features(i)%s
293 end do
294 end if
295 act_name=settings%name
296 if (allocated(settings%args)) act_args=settings%args
297 type is (fpm_test_settings)
298 if (allocated(settings%profile)) act_profile=settings%profile
299 if (allocated(settings%features)) then
300 do i = 1, min(size(settings%features),size(act_features))
301 act_features(i) = settings%features(i)%s
302 end do
303 end if
304 act_name=settings%name
305 if (allocated(settings%args)) act_args=settings%args
306 type is (fpm_clean_settings)
307 act_c_s=settings%clean_skip
308 act_c_a=settings%clean_all
309 act_c_t=settings%clean_test
310 act_c_apps=settings%clean_apps
311 act_c_ex=settings%clean_examples
312 act_reg_c=settings%registry_cache
313 type is (fpm_install_settings)
314 type is (fpm_publish_settings)
315 act_show_v=settings%show_package_version
316 act_show_u_d=settings%show_upload_data
317 act_dry_run=settings%is_dry_run
318 act_token=settings%token
319 end select
320
321 open(file='_test_cli',newunit=lun,delim='quote')
322 write(lun,nml=act_cli,delim='quote')
323 close(unit=lun)
324
325 end subroutine parse
326
327 end program main