| 1 |
module test_filesystem |
| 2 |
use testsuite, only: new_unittest, unittest_t, error_t, test_failed |
| 3 |
use fpm_filesystem, only: canon_path, is_dir, mkdir, os_delete_dir, & |
| 4 |
join_path, is_absolute_path, get_home, & |
| 5 |
delete_file, read_lines, get_temp_filename |
| 6 |
use fpm_environment, only: OS_WINDOWS, get_os_type, os_is_unix |
| 7 |
use fpm_strings, only: string_t, split_lines_first_last |
| 8 |
implicit none |
| 9 |
private |
| 10 |
|
| 11 |
public :: collect_filesystem |
| 12 |
|
| 13 |
contains |
| 14 |
|
| 15 |
!> Collect all exported unit tests |
| 16 |
subroutine collect_filesystem(tests) |
| 17 |
|
| 18 |
!> Collection of tests |
| 19 |
type(unittest_t), allocatable, intent(out) :: tests(:) |
| 20 |
|
| 21 |
tests = [ & |
| 22 |
& new_unittest("canon-path", test_canon_path), & |
| 23 |
& new_unittest("create-delete-directory", test_mkdir_rmdir), & |
| 24 |
& new_unittest("test-is-absolute-path", test_is_absolute_path), & |
| 25 |
& new_unittest("test-get-home", test_get_home), & |
| 26 |
& new_unittest("test-split-lines-first-last", test_split_lines_first_last), & |
| 27 |
& new_unittest("test-crlf-lines", test_dir_with_crlf) & |
| 28 |
] |
| 29 |
|
| 30 |
end subroutine collect_filesystem |
| 31 |
|
| 32 |
subroutine test_canon_path(error) |
| 33 |
|
| 34 |
!> Error handling |
| 35 |
type(error_t), allocatable, intent(out) :: error |
| 36 |
|
| 37 |
call check_string(error, & |
| 38 |
& canon_path("git/project/src/origin"), "git/project/src/origin") |
| 39 |
if (allocated(error)) return |
| 40 |
|
| 41 |
call check_string(error, & |
| 42 |
& canon_path("./project/src/origin"), "project/src/origin") |
| 43 |
if (allocated(error)) return |
| 44 |
|
| 45 |
call check_string(error, & |
| 46 |
& canon_path("./project/src///origin/"), "project/src/origin") |
| 47 |
if (allocated(error)) return |
| 48 |
|
| 49 |
call check_string(error, & |
| 50 |
& canon_path("../project/./src/origin/"), "../project/src/origin") |
| 51 |
if (allocated(error)) return |
| 52 |
|
| 53 |
call check_string(error, & |
| 54 |
& canon_path("/project//src/origin/"), "/project/src/origin") |
| 55 |
if (allocated(error)) return |
| 56 |
|
| 57 |
call check_string(error, & |
| 58 |
& canon_path("/project/src/../origin/"), "/project/origin") |
| 59 |
if (allocated(error)) return |
| 60 |
|
| 61 |
call check_string(error, & |
| 62 |
& canon_path("/project/src/../origin/.."), "/project") |
| 63 |
if (allocated(error)) return |
| 64 |
|
| 65 |
call check_string(error, & |
| 66 |
& canon_path("/project/src//../origin/."), "/project/origin") |
| 67 |
if (allocated(error)) return |
| 68 |
|
| 69 |
call check_string(error, & |
| 70 |
& canon_path("../project/src/./../origin/."), "../project/origin") |
| 71 |
if (allocated(error)) return |
| 72 |
|
| 73 |
call check_string(error, & |
| 74 |
& canon_path("../project/src/../../../origin/."), "../../origin") |
| 75 |
if (allocated(error)) return |
| 76 |
|
| 77 |
call check_string(error, & |
| 78 |
& canon_path("/../.."), "/") |
| 79 |
if (allocated(error)) return |
| 80 |
|
| 81 |
call check_string(error, & |
| 82 |
& canon_path("././././././/////a/b/.///././////.///c/../../../"), ".") |
| 83 |
if (allocated(error)) return |
| 84 |
|
| 85 |
call check_string(error, & |
| 86 |
& canon_path("/./././././/////a/b/.///././////.///c/../../../"), "/") |
| 87 |
if (allocated(error)) return |
| 88 |
|
| 89 |
end subroutine test_canon_path |
| 90 |
|
| 91 |
!> Check a character variable against a reference value |
| 92 |
subroutine check_string(error, actual, expected) |
| 93 |
|
| 94 |
!> Error handling |
| 95 |
type(error_t), allocatable, intent(out) :: error |
| 96 |
|
| 97 |
!> Actual string value |
| 98 |
character(len=*), intent(in) :: actual |
| 99 |
|
| 100 |
!> Expected string value |
| 101 |
character(len=*), intent(in) :: expected |
| 102 |
|
| 103 |
if (actual /= expected) then |
| 104 |
call test_failed(error, & |
| 105 |
"Character value mismatch "// & |
| 106 |
"expected '"//expected//"' but got '"//actual//"'") |
| 107 |
end if |
| 108 |
|
| 109 |
end subroutine check_string |
| 110 |
|
| 111 |
subroutine test_mkdir_rmdir(error) |
| 112 |
|
| 113 |
!> Error handling |
| 114 |
type(error_t), allocatable, intent(out) :: error |
| 115 |
|
| 116 |
call check_mkdir(error, join_path("tmpdir", "subdir")) |
| 117 |
if (allocated(error)) return |
| 118 |
|
| 119 |
call check_rmdir(error, "tmpdir") |
| 120 |
if (allocated(error)) return |
| 121 |
|
| 122 |
end subroutine test_mkdir_rmdir |
| 123 |
|
| 124 |
!> Create a directory and verify its existence |
| 125 |
subroutine check_mkdir(error, path) |
| 126 |
|
| 127 |
!> Error handling |
| 128 |
type(error_t), allocatable, intent(out) :: error |
| 129 |
|
| 130 |
!> Directory path |
| 131 |
character(len=*), intent(in) :: path |
| 132 |
|
| 133 |
! Directory shouldn't exist before it's created |
| 134 |
if (is_dir(path)) then |
| 135 |
call test_failed(error, & |
| 136 |
"Directory path "//path//" already exists before its creation") |
| 137 |
return |
| 138 |
end if |
| 139 |
|
| 140 |
! Create directory |
| 141 |
call mkdir(path) |
| 142 |
|
| 143 |
! Check that directory is indeed created |
| 144 |
if (.not. is_dir(path)) then |
| 145 |
call test_failed(error, & |
| 146 |
"Directory path "//path//" cannot be created") |
| 147 |
end if |
| 148 |
|
| 149 |
end subroutine check_mkdir |
| 150 |
|
| 151 |
!> Create a directory and verify its existence |
| 152 |
subroutine check_rmdir(error, path) |
| 153 |
|
| 154 |
!> Error handling |
| 155 |
type(error_t), allocatable, intent(out) :: error |
| 156 |
|
| 157 |
!> Directory path |
| 158 |
character(len=*), intent(in) :: path |
| 159 |
|
| 160 |
! Directory should exist before it's deleted |
| 161 |
if (.not. is_dir(path)) then |
| 162 |
call test_failed(error, & |
| 163 |
"Directory path "//path//" doesn't exist before its deletion") |
| 164 |
return |
| 165 |
end if |
| 166 |
|
| 167 |
! Delete directory |
| 168 |
call os_delete_dir(os_is_unix(), path) |
| 169 |
|
| 170 |
! Check that directory is indeed deleted |
| 171 |
if (is_dir(path)) then |
| 172 |
call test_failed(error, & |
| 173 |
"Directory path "//path//" cannot be deleted") |
| 174 |
end if |
| 175 |
|
| 176 |
end subroutine check_rmdir |
| 177 |
|
| 178 |
subroutine test_is_absolute_path(error) |
| 179 |
type(error_t), allocatable, intent(out) :: error |
| 180 |
|
| 181 |
! Unix tests |
| 182 |
if (is_absolute_path('.', is_unix=.true.)) then |
| 183 |
call test_failed(error, "Path '.' isn't absolute") |
| 184 |
return |
| 185 |
end if |
| 186 |
|
| 187 |
if (is_absolute_path('abc', is_unix=.true.)) then |
| 188 |
call test_failed(error, "Path 'abc' isn't absolute") |
| 189 |
return |
| 190 |
end if |
| 191 |
|
| 192 |
if (is_absolute_path('~a', is_unix=.true.)) then |
| 193 |
call test_failed(error, "Path '~a' isn't absolute") |
| 194 |
return |
| 195 |
end if |
| 196 |
|
| 197 |
if (is_absolute_path('C:', is_unix=.true.)) then |
| 198 |
call test_failed(error, "Path 'C:' isn't absolute on Unix") |
| 199 |
return |
| 200 |
end if |
| 201 |
|
| 202 |
if (is_absolute_path('~', is_unix=.true.)) then |
| 203 |
call test_failed(error, "Path '~' isn't absolute") |
| 204 |
return |
| 205 |
end if |
| 206 |
|
| 207 |
if (is_absolute_path('~/', is_unix=.true.)) then |
| 208 |
call test_failed(error, "Path '~/' isn't absolute") |
| 209 |
return |
| 210 |
end if |
| 211 |
|
| 212 |
if (.not. is_absolute_path('/', is_unix=.true.)) then |
| 213 |
call test_failed(error, "Path '/' is absolute") |
| 214 |
return |
| 215 |
end if |
| 216 |
|
| 217 |
if (.not. is_absolute_path('/a', is_unix=.true.)) then |
| 218 |
call test_failed(error, "Path '/a' is absolute") |
| 219 |
return |
| 220 |
end if |
| 221 |
|
| 222 |
! Windows tests |
| 223 |
if (is_absolute_path('abc', is_unix=.false.)) then |
| 224 |
call test_failed(error, "Path 'abc' isn't absolute") |
| 225 |
return |
| 226 |
end if |
| 227 |
|
| 228 |
if (is_absolute_path('..', is_unix=.false.)) then |
| 229 |
call test_failed(error, "Path '..' isn't absolute") |
| 230 |
return |
| 231 |
end if |
| 232 |
|
| 233 |
if (is_absolute_path('~', is_unix=.false.)) then |
| 234 |
call test_failed(error, "Path '~' isn't absolute") |
| 235 |
return |
| 236 |
end if |
| 237 |
|
| 238 |
if (is_absolute_path('/', is_unix=.false.)) then |
| 239 |
call test_failed(error, "Path '/' isn't absolute on Windows") |
| 240 |
return |
| 241 |
end if |
| 242 |
|
| 243 |
if (is_absolute_path('c/', is_unix=.false.)) then |
| 244 |
call test_failed(error, "Path 'c/' isn't absolute") |
| 245 |
return |
| 246 |
end if |
| 247 |
|
| 248 |
if (is_absolute_path('1:', is_unix=.false.)) then |
| 249 |
call test_failed(error, "Path '1:' isn't absolute") |
| 250 |
return |
| 251 |
end if |
| 252 |
|
| 253 |
if (is_absolute_path('C', is_unix=.false.)) then |
| 254 |
call test_failed(error, "Path 'C' isn't absolute") |
| 255 |
return |
| 256 |
end if |
| 257 |
|
| 258 |
if (.not. is_absolute_path('C:', is_unix=.false.)) then |
| 259 |
call test_failed(error, "Path 'C:' is absolute") |
| 260 |
return |
| 261 |
end if |
| 262 |
|
| 263 |
if (.not. is_absolute_path('x:', is_unix=.false.)) then |
| 264 |
call test_failed(error, "Path 'x:' is absolute") |
| 265 |
return |
| 266 |
end if |
| 267 |
|
| 268 |
if (.not. is_absolute_path('x:xyz', is_unix=.false.)) then |
| 269 |
call test_failed(error, "Path 'x:xyz' is absolute") |
| 270 |
return |
| 271 |
end if |
| 272 |
|
| 273 |
end subroutine test_is_absolute_path |
| 274 |
|
| 275 |
subroutine test_get_home(error) |
| 276 |
type(error_t), allocatable, intent(out) :: error |
| 277 |
character(len=:), allocatable :: home |
| 278 |
character(len=*), parameter :: letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' |
| 279 |
|
| 280 |
call get_home(home, error) |
| 281 |
if (allocated(error)) return |
| 282 |
|
| 283 |
if (os_is_unix()) then |
| 284 |
if (home(1:1) /= '/') then |
| 285 |
call test_failed(error, "This doesn't seem to be the correct home path: '"//home//"'") |
| 286 |
return |
| 287 |
end if |
| 288 |
else |
| 289 |
if (index(letters, home(1:1)) == 0 .or. home(2:2) /= ':') then |
| 290 |
call test_failed(error, "This doesn't seem to be the correct home path: '"//home//"'") |
| 291 |
return |
| 292 |
end if |
| 293 |
end if |
| 294 |
|
| 295 |
end subroutine test_get_home |
| 296 |
|
| 297 |
! Test line splitting on MS windows |
| 298 |
subroutine test_split_lines_first_last(error) |
| 299 |
!> Error handling |
| 300 |
type(error_t), allocatable, intent(out) :: error |
| 301 |
|
| 302 |
character, parameter :: CR = achar(13) |
| 303 |
character, parameter :: LF = new_line('A') |
| 304 |
character(*), parameter :: CRLF = CR//LF |
| 305 |
integer, allocatable :: first(:), last(:) |
| 306 |
|
| 307 |
call split_lines_first_last(CR//LF//'line1'//CR//'line2'//LF//'line3'//CR//LF//'hello', first, last) |
| 308 |
if (.not.(all(first==[3,9,15,22]) .and. all(last==[7,13,19,26]))) then |
| 309 |
call test_failed(error, "Test split_lines_first_last #1 failed") |
| 310 |
return |
| 311 |
end if |
| 312 |
|
| 313 |
call split_lines_first_last('single_line', first, last) |
| 314 |
if (.not.(all(first==[1]) .and. all(last==[11]))) then |
| 315 |
call test_failed(error, "Test split_lines_first_last #2 failed") |
| 316 |
return |
| 317 |
end if |
| 318 |
|
| 319 |
call split_lines_first_last(CR//LF//CR//LF//'test', first, last) |
| 320 |
if (.not.(all(first == [5]) .and. all(last == [8]))) then |
| 321 |
call test_failed(error, "Test split_lines_first_last #3 failed") |
| 322 |
return |
| 323 |
end if |
| 324 |
|
| 325 |
call split_lines_first_last('a'//CR//'b'//LF//'c'//CR//LF//'d', first, last) |
| 326 |
if (.not.(all(first == [1, 3, 5, 8]) .and. all(last == [1, 3, 5, 8]))) then |
| 327 |
call test_failed(error, "Test split_lines_first_last #4 failed") |
| 328 |
return |
| 329 |
end if |
| 330 |
|
| 331 |
call split_lines_first_last('', first, last) |
| 332 |
if (.not.(size(first) == 0 .and. size(last) == 0)) then |
| 333 |
call test_failed(error, "Test split_lines_first_last #5 failed") |
| 334 |
return |
| 335 |
end if |
| 336 |
|
| 337 |
call split_lines_first_last('build.f90'//CRLF//& |
| 338 |
'dependency.f90'//CRLF//& |
| 339 |
'example.f90'//CRLF//& |
| 340 |
'executable.f90'//CRLF//& |
| 341 |
'fortran.f90'//CRLF, & |
| 342 |
first, last) |
| 343 |
|
| 344 |
if (.not.(all(first == [1,12,28,41,57]) .and. all(last == [9,25,38,54,67]))) then |
| 345 |
call test_failed(error, "Test split_lines_first_last #6 failed") |
| 346 |
return |
| 347 |
end if |
| 348 |
|
| 349 |
end subroutine test_split_lines_first_last |
| 350 |
|
| 351 |
! On MS windows, directory listings are printed to files with CR//LF endings. |
| 352 |
! Check that the lines can be properly read back from such files. |
| 353 |
subroutine test_dir_with_crlf(error) |
| 354 |
type(error_t), allocatable, intent(out) :: error |
| 355 |
|
| 356 |
character, parameter :: CR = achar(13) |
| 357 |
character, parameter :: LF = new_line('A') |
| 358 |
character(*), parameter :: CRLF = CR//LF |
| 359 |
|
| 360 |
character(*), parameter :: test_lines = 'build.f90'//CRLF//& |
| 361 |
'dependency.f90'//CRLF//& |
| 362 |
'example.f90'//CRLF//& |
| 363 |
'executable.f90'//CRLF//& |
| 364 |
'fortran.f90'//CRLF |
| 365 |
|
| 366 |
type(string_t), allocatable :: lines(:) |
| 367 |
character(len=:), allocatable :: temp_file |
| 368 |
character(256) :: msg |
| 369 |
integer :: unit, i, ios |
| 370 |
|
| 371 |
temp_file = get_temp_filename() |
| 372 |
|
| 373 |
open(newunit=unit,file=temp_file,access='stream',action='write',iostat=ios) |
| 374 |
if (ios/=0) then |
| 375 |
call test_failed(error, "cannot create temporary file") |
| 376 |
return |
| 377 |
end if |
| 378 |
|
| 379 |
write(unit,iostat=ios) test_lines |
| 380 |
if (ios/=0) then |
| 381 |
call test_failed(error, "cannot write to temporary file") |
| 382 |
return |
| 383 |
end if |
| 384 |
|
| 385 |
close(unit,iostat=ios) |
| 386 |
if (ios/=0) then |
| 387 |
call test_failed(error, "cannot close temporary file") |
| 388 |
return |
| 389 |
end if |
| 390 |
|
| 391 |
lines = read_lines(temp_file) |
| 392 |
|
| 393 |
if (.not.allocated(lines)) then |
| 394 |
write(msg, 1) 'no output' |
| 395 |
call test_failed(error, msg) |
| 396 |
return |
| 397 |
end if |
| 398 |
|
| 399 |
if (size(lines)/=5) then |
| 400 |
write(msg, 1) 'wrong number of lines: expected ',5,', actual ',size(lines) |
| 401 |
call test_failed(error, msg) |
| 402 |
return |
| 403 |
end if |
| 404 |
|
| 405 |
if (lines(1)%s/='build.f90') then |
| 406 |
call test_failed(error, "Failed reading file with CRLF: at build.f90") |
| 407 |
return |
| 408 |
end if |
| 409 |
if (lines(2)%s/='dependency.f90') then |
| 410 |
call test_failed(error, "Failed reading file with CRLF: at dependency.f90") |
| 411 |
return |
| 412 |
end if |
| 413 |
if (lines(3)%s/='example.f90') then |
| 414 |
call test_failed(error, "Failed reading file with CRLF: at example.f90") |
| 415 |
return |
| 416 |
end if |
| 417 |
if (lines(4)%s/='executable.f90') then |
| 418 |
call test_failed(error, "Failed reading file with CRLF: at executable.f90") |
| 419 |
return |
| 420 |
end if |
| 421 |
if (lines(5)%s/='fortran.f90') then |
| 422 |
call test_failed(error, "Failed reading file with CRLF: at fortran.f90") |
| 423 |
return |
| 424 |
end if |
| 425 |
|
| 426 |
call delete_file(temp_file) |
| 427 |
|
| 428 |
1 format("Failed reading file with CRLF: ",a,:,i0,:,a,:,i0) |
| 429 |
|
| 430 |
end subroutine test_dir_with_crlf |
| 431 |
|
| 432 |
|
| 433 |
end module test_filesystem |