|
| 1 | +module test_filesystem |
| 2 | + use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test |
| 3 | + use stdlib_system, only: is_directory |
| 4 | + |
| 5 | + implicit none |
| 6 | + |
| 7 | +contains |
| 8 | + |
| 9 | + !> Collect all exported unit tests |
| 10 | + subroutine collect_suite(testsuite) |
| 11 | + !> Collection of tests |
| 12 | + type(unittest_type), allocatable, intent(out) :: testsuite(:) |
| 13 | + |
| 14 | + testsuite = [ & |
| 15 | + new_unittest("fs_is_directory_dir", test_is_directory_dir), & |
| 16 | + new_unittest("fs_is_directory_file", test_is_directory_file) & |
| 17 | + ] |
| 18 | + end subroutine collect_suite |
| 19 | + |
| 20 | + ! Test `is_directory` for a directory |
| 21 | + subroutine test_is_directory_dir(error) |
| 22 | + type(error_type), allocatable, intent(out) :: error |
| 23 | + character(len=256) :: dirname |
| 24 | + integer :: ios, iocmd |
| 25 | + character(len=512) :: msg |
| 26 | + |
| 27 | + dirname = "this_test_dir_tmp" |
| 28 | + |
| 29 | + ! Create a directory |
| 30 | + call execute_command_line("mkdir " // dirname, exitstat=ios, cmdstat=iocmd, cmdmsg=msg) |
| 31 | + call check(error, ios == 0 .and. iocmd == 0, "Cannot create test directory: " // trim(msg)) |
| 32 | + if (allocated(error)) return |
| 33 | + |
| 34 | + ! Verify `is_directory` identifies it as a directory |
| 35 | + call check(error, is_directory(dirname), "is_directory did not recognize a valid directory") |
| 36 | + if (allocated(error)) return |
| 37 | + |
| 38 | + ! Clean up: remove the directory |
| 39 | + call execute_command_line("rmdir " // dirname, exitstat=ios, cmdstat=iocmd, cmdmsg=msg) |
| 40 | + call check(error, ios == 0 .and. iocmd == 0, "Cannot remove test directory: " // trim(msg)) |
| 41 | + end subroutine test_is_directory_dir |
| 42 | + |
| 43 | + ! Test `is_directory` for a regular file |
| 44 | + subroutine test_is_directory_file(error) |
| 45 | + type(error_type), allocatable, intent(out) :: error |
| 46 | + character(len=256) :: filename |
| 47 | + logical :: result |
| 48 | + integer :: ios, iunit |
| 49 | + character(len=512) :: msg |
| 50 | + |
| 51 | + filename = "test_file.txt" |
| 52 | + |
| 53 | + ! Create a file |
| 54 | + open(newunit=iunit, file=filename, status="replace", iostat=ios, iomsg=msg) |
| 55 | + call check(error, ios == 0, "Cannot create test file: " // trim(msg)) |
| 56 | + if (allocated(error)) return |
| 57 | + |
| 58 | + ! Verify `is_directory` identifies it as not a directory |
| 59 | + result = is_directory(filename) |
| 60 | + call check(error, .not. result, "is_directory falsely recognized a regular file as a directory") |
| 61 | + if (allocated(error)) return |
| 62 | + |
| 63 | + ! Clean up: remove the file |
| 64 | + close(iunit,status='delete',iostat=ios,iomsg=msg) |
| 65 | + call check(error, ios == 0, "Cannot delete test file: " // trim(msg)) |
| 66 | + if (allocated(error)) return |
| 67 | + |
| 68 | + end subroutine test_is_directory_file |
| 69 | + |
| 70 | + |
| 71 | +end module test_filesystem |
| 72 | + |
| 73 | +program tester |
| 74 | + use, intrinsic :: iso_fortran_env, only : error_unit |
| 75 | + use testdrive, only : run_testsuite, new_testsuite, testsuite_type |
| 76 | + use test_filesystem, only : collect_suite |
| 77 | + |
| 78 | + implicit none |
| 79 | + |
| 80 | + integer :: stat, is |
| 81 | + type(testsuite_type), allocatable :: testsuites(:) |
| 82 | + character(len=*), parameter :: fmt = '("#", *(1x, a))' |
| 83 | + |
| 84 | + stat = 0 |
| 85 | + |
| 86 | + testsuites = [ & |
| 87 | + new_testsuite("filesystem", collect_suite) & |
| 88 | + ] |
| 89 | + |
| 90 | + do is = 1, size(testsuites) |
| 91 | + write(error_unit, fmt) "Testing:", testsuites(is)%name |
| 92 | + call run_testsuite(testsuites(is)%collect, error_unit, stat) |
| 93 | + end do |
| 94 | + |
| 95 | + if (stat > 0) then |
| 96 | + write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" |
| 97 | + error stop |
| 98 | + end if |
| 99 | +end program |
0 commit comments