Debian Codegraph @

test/new_test/new_test.f90

raw
1 program new_test
2 use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, stdout=>output_unit, stderr=>error_unit
3 use fpm_filesystem, only : is_dir, list_files, exists, windows_path, join_path, &
4 dirname, run
5 use fpm_strings, only : string_t, operator(.in.)
6 use fpm_environment, only : get_os_type
7 use fpm_environment, only : OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD, OS_WINDOWS
8 implicit none
9 type(string_t), allocatable :: file_names(:)
10 integer :: i, j, k
11 character(len=:),allocatable :: cmdpath
12 character(len=:),allocatable :: path
13 character(len=*),parameter :: scr = 'fpm_scratch_'
14 character(len=*),parameter :: cmds(*) = [character(len=80) :: &
15 ! run a variety of "fpm new" variations and verify expected files are generated
16 'new', &
17 'new name-with-hyphens', &
18 'new '//scr//'A', &
19 'new '//scr//'B --lib', &
20 'new '//scr//'C --app', &
21 'new '//scr//'D --test', &
22 'new '//scr//'E --lib --test ', &
23 'new '//scr//'F --lib --app', &
24 'new '//scr//'G --test --app', &
25 'new '//scr//'H --example', &
26 'new '//scr//'BB --lib', &
27 'new '//scr//'BB --test ', &
28 'new '//scr//'BB --backfill --test', &
29 'new '//scr//'CC --test --src --app', &
30 'new --version', &
31 'new --help']
32 integer :: estat, cstat
33 character(len=256) :: message
34 character(len=:),allocatable :: directories(:)
35 character(len=:),allocatable :: shortdirs(:)
36 character(len=:),allocatable :: expected(:)
37 logical,allocatable :: tally(:)
38 logical :: IS_OS_WINDOWS
39 character(len=*),parameter :: dirs_to_be_removed = 'fpm_scratch_* name-with-hyphens'
40 character(len=:),allocatable :: rm_command
41 write(*,'(g0:,1x)')'TEST new SUBCOMMAND (draft):'
42
43 cmdpath = get_command_path()
44 allocate(tally(0))
45 shortdirs=[character(len=80) :: 'A','B','C','D','E','F','G','H','BB','CC']
46 allocate(character(len=80) :: directories(size(shortdirs)))
47
48 !! SEE IF EXPECTED FILES ARE GENERATED
49 !! Issues:
50 !! o assuming fpm command is in expected path and the new version
51 !! o DOS versus POSIX filenames
52 is_os_windows=.false.
53 select case (get_os_type())
54 case (OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD)
55 call execute_command_line('rm -rf fpm_scratch_*',exitstat=estat,cmdstat=cstat,cmdmsg=message)
56 path=cmdpath
57 case (OS_WINDOWS)
58 path=windows_path(cmdpath)
59 is_os_windows=.true.
60 do i=1,size(directories)
61 call execute_command_line('rmdir /s /q fpm_scratch_'//trim(shortdirs(i)),exitstat=estat,&
62 cmdstat=cstat,cmdmsg=message)
63 end do
64 case default
65 write(*,*)'ERROR: unknown OS. Stopping test'
66 stop 2
67 end select
68 do i=1,size(directories)
69 directories(i)=scr//trim(shortdirs(i))
70 if( is_dir(trim(directories(i))) ) then
71 write(*,*)'ERROR:',trim( directories(i) ),' already exists'
72 write(*,*)' you must remove scratch directories before performing this test'
73 write(*,'(*(g0:,1x))')'directories:',(trim(directories(j)),j=1,size(directories)),'name-with-hyphens'
74 stop
75 endif
76 enddo
77 ! execute the fpm(1) commands
78 do i=1,size(cmds)
79 message=''
80 write(*,*)path//' '//cmds(i)
81 call execute_command_line(path//' '//cmds(i),exitstat=estat,cmdstat=cstat,cmdmsg=message)
82 write(*,'(*(g0))')'CMD=',trim(cmds(i)),' EXITSTAT=',estat,' CMDSTAT=',cstat,' MESSAGE=',trim(message)
83 enddo
84
85 if( is_dir('name-with-hyphens') ) then
86 tally=[tally,.true.]
87
88 else
89 write(*,*)'ERROR: directory name-with-hyphens/ exists'
90 tally=[tally,.false.]
91 endif
92
93 ! assuming hidden files in .git and .gitignore are ignored for now
94 TESTS: do i=1,size(directories)
95 ! test if expected directory exists
96 if( .not. is_dir(trim( directories(i))) ) then
97 tally=[tally,.false.]
98 write(*,*)'ERROR:',trim( directories(i) ),' is not a directory'
99 else
100 select case(shortdirs(i))
101 case('A'); expected=[ character(len=80)::&
102 &'A/app','A/fpm.toml','A/README.md','A/src','A/test','A/app/main.f90','A/src/'//scr//'A.f90','A/test/check.f90']
103 case('B'); expected=[ character(len=80)::&
104 &'B/fpm.toml','B/README.md','B/src','B/src/'//scr//'B.f90']
105 case('C'); expected=[ character(len=80)::&
106 &'C/app','C/fpm.toml','C/README.md','C/app/main.f90']
107 case('D'); expected=[ character(len=80)::&
108 &'D/fpm.toml','D/README.md','D/test','D/test/check.f90']
109 case('E'); expected=[ character(len=80)::&
110 &'E/fpm.toml','E/README.md','E/src','E/test','E/src/'//scr//'E.f90','E/test/check.f90']
111 case('F'); expected=[ character(len=80)::&
112 &'F/app','F/fpm.toml','F/README.md','F/src','F/app/main.f90','F/src/'//scr//'F.f90']
113 case('G'); expected=[ character(len=80)::&
114 &'G/app','G/fpm.toml','G/README.md','G/test','G/app/main.f90','G/test/check.f90']
115 case('H'); expected=[ character(len=80)::&
116 &'H/example','H/fpm.toml','H/README.md','H/example/demo.f90']
117 case('BB'); expected=[ character(len=80)::&
118 &'BB/fpm.toml','BB/README.md','BB/src','BB/test','BB/src/'//scr//'BB.f90','BB/test/check.f90']
119 case('CC'); expected=[ character(len=80)::&
120 &'CC/app','CC/fpm.toml','CC/README.md','CC/src','CC/test','CC/app/main.f90','CC/src/'//scr//'CC.f90','CC/test/check.f90']
121 case default
122 write(*,*)'ERROR: internal error. unknown directory name ',trim(shortdirs(i))
123 stop 4
124 end select
125 !! MSwindows has hidden files in it
126 !! Warning: This only looks for expected files. If there are more files than expected it does not fail
127 call list_files(trim(directories(i)), file_names,recurse=.true.)
128
129 if(size(expected)/=size(file_names))then
130 write(*,*)'WARNING: unexpected number of files in file list=',size(file_names),' expected ',size(expected)
131 write(*,'("EXPECTED: ",*(g0:,","))')(scr//trim(expected(j)),j=1,size(expected))
132 write(*,'("FOUND: ",*(g0:,","))')(trim(file_names(j)%s),j=1,size(file_names))
133 endif
134
135 do j=1,size(expected)
136
137 expected(j)=scr//expected(j)
138 if(is_os_windows) expected(j)=windows_path(expected(j))
139 if( .not.(trim(expected(j)).in.file_names) )then
140 tally=[tally,.false.]
141 write(*,'("ERROR: FOUND ",*(g0:,", "))')( trim(file_names(k)%s), k=1,size(file_names) )
142 write(*,'(*(g0))')' BUT NO MATCH FOR ',expected(j)
143 tally=[tally,.false.]
144 cycle TESTS
145 endif
146 enddo
147 tally=[tally,.true.]
148 endif
149 enddo TESTS
150
151 ! Test that generated manifests are valid by building one of the packages
152 write(*,'(a)') ' <INFO> Testing that generated manifest is valid (can be built)...'
153 call execute_command_line(path//' build '//scr//'A', exitstat=estat, cmdstat=cstat, cmdmsg=message)
154 if (estat == 0) then
155 write(*,'(a)') ' <INFO> Generated manifest is valid - build succeeded'
156 tally = [tally, .true.]
157 else
158 write(*,'(a,i0)') ' ERROR: Generated manifest is invalid - build failed with exit status ', estat
159 tally = [tally, .false.]
160 end if
161
162 ! clean up scratch files; might want an option to leave them for inspection
163 select case (get_os_type())
164 case (OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD)
165 rm_command = 'rm -rf ' // dirs_to_be_removed
166 case (OS_WINDOWS)
167 do i=1,size(directories)
168 rm_command = 'rmdir /s /q fpm_scratch_'//trim(shortdirs(i))
169 call execute_command_line('rmdir /s /q fpm_scratch_'//trim(shortdirs(i)),exitstat=estat,&
170 cmdstat=cstat,cmdmsg=message)
171 end do
172 rm_command = 'rmdir /s /q name-with-hyphens'
173 end select
174 call execute_command_line(rm_command, exitstat=estat,cmdstat=cstat,cmdmsg=message)
175
176 write(*,'("new TEST TALLY=",*(g0))')tally
177 if(all(tally))then
178 write(*,'(*(g0))')'PASSED: all ',count(tally),' tests passed '
179 else
180 write(*,*)'FAILED: PASSED=',count(tally),' FAILED=',count(.not.tally)
181 stop 5
182 endif
183 contains
184 function get_command_path() result(prog)
185 character(len=:), allocatable :: prog
186
187 character(len=:), allocatable :: path
188 integer :: length
189
190 ! FIXME: Super hacky way to get the name of the fpm executable,
191 ! it works better than invoking fpm again but should be replaced ASAP.
192 call get_command_argument(0, length=length)
193 allocate(character(len=length) :: prog)
194 call get_command_argument(0, prog)
195 path = dirname(prog)
196 if (get_os_type() == OS_WINDOWS) then
197 prog = join_path(path, "..", "app", "fpm.exe")
198 if (.not.exists(prog)) then
199 prog = join_path(path, "..", "..", "app", "fpm.exe")
200 end if
201 else
202 prog = join_path(path, "..", "app", "fpm")
203 if (.not.exists(prog)) then
204 prog = join_path(path, "..", "..", "app", "fpm")
205 end if
206 end if
207
208 end function
209 end program new_test