Skip to content

Commit 1613fb5

Browse files
committed
new test for is_regular_file
1 parent 9f5655a commit 1613fb5

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

test/system/test_filesystem.f90

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module test_filesystem
22
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test
33
use stdlib_system, only: is_directory, delete_file, FS_ERROR, FS_ERROR_CODE, &
44
make_directory, remove_directory, make_directory_all, is_windows, OS_TYPE, &
5-
OS_WINDOWS, exists, fs_type_unknown, fs_type_regular_file, fs_type_directory, fs_type_symlink
5+
OS_WINDOWS, exists, fs_type_unknown, fs_type_regular_file, fs_type_directory, fs_type_symlink, &
6+
is_regular_file
67
use stdlib_error, only: state_type, STDLIB_FS_ERROR
78
use stdlib_strings, only: to_string
89

@@ -21,6 +22,7 @@ subroutine collect_suite(testsuite)
2122
new_unittest("fs_exists_reg_file", test_exists_reg_file), &
2223
new_unittest("fs_exists_dir", test_exists_dir), &
2324
new_unittest("fs_exists_symlink", test_exists_symlink), &
25+
new_unittest("fs_is_regular_file", test_is_regular_file), &
2426
new_unittest("fs_is_directory_dir", test_is_directory_dir), &
2527
new_unittest("fs_is_directory_file", test_is_directory_file), &
2628
new_unittest("fs_delete_non_existent", test_delete_file_non_existent), &
@@ -85,7 +87,7 @@ subroutine test_exists_reg_file(error)
8587
if (allocated(error)) then
8688
! Clean up: remove the file
8789
close(iunit,status='delete',iostat=ios,iomsg=msg)
88-
call check(error, ios == 0, err%message// " and cannot delete test file: " // trim(msg))
90+
call check(error, ios == 0, error%message// " and cannot delete test file: " // trim(msg))
8991
return
9092
end if
9193

@@ -95,7 +97,7 @@ subroutine test_exists_reg_file(error)
9597
if (allocated(error)) then
9698
! Clean up: remove the file
9799
close(iunit,status='delete',iostat=ios,iomsg=msg)
98-
call check(error, ios == 0, err%message// " and cannot delete test file: " // trim(msg))
100+
call check(error, ios == 0, error%message// " and cannot delete test file: " // trim(msg))
99101
return
100102
end if
101103

@@ -105,6 +107,37 @@ subroutine test_exists_reg_file(error)
105107
if (allocated(error)) return
106108
end subroutine test_exists_reg_file
107109

110+
subroutine test_is_regular_file(error)
111+
type(error_type), allocatable, intent(out) :: error
112+
character(len=256) :: filename
113+
integer :: ios, iunit
114+
character(len=512) :: msg
115+
116+
logical :: is_file
117+
118+
filename = "test_file.txt"
119+
120+
! Create a file
121+
open(newunit=iunit, file=filename, status="replace", iostat=ios, iomsg=msg)
122+
call check(error, ios == 0, "Cannot init test_is_regular_file: " // trim(msg))
123+
if (allocated(error)) return
124+
125+
is_file = is_regular_file(filename)
126+
call check(error, is_file, "is_regular_file could not identify a file")
127+
128+
if (allocated(error)) then
129+
! Clean up: remove the file
130+
close(iunit,status='delete',iostat=ios,iomsg=msg)
131+
call check(error, ios == 0, error%message// " and cannot delete test file: " // trim(msg))
132+
return
133+
end if
134+
135+
! Clean up: remove the file
136+
close(iunit,status='delete',iostat=ios,iomsg=msg)
137+
call check(error, ios == 0, "Cannot delete test file: " // trim(msg))
138+
if (allocated(error)) return
139+
end subroutine test_is_regular_file
140+
108141
subroutine test_exists_dir(error)
109142
type(error_type), allocatable, intent(out) :: error
110143
type(state_type) :: err
@@ -125,7 +158,7 @@ subroutine test_exists_dir(error)
125158
if (allocated(error)) then
126159
! Clean up: remove the directory
127160
call execute_command_line("rmdir " // dirname, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
128-
call check(error, ios == 0 .and. iocmd == 0, err%message // " and &
161+
call check(error, ios == 0 .and. iocmd == 0, error%message // " and &
129162
& cannot cleanup test_exists_dir: " // trim(msg))
130163
return
131164
end if
@@ -136,7 +169,7 @@ subroutine test_exists_dir(error)
136169
if (allocated(error)) then
137170
! Clean up: remove the directory
138171
call execute_command_line("rmdir " // dirname, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
139-
call check(error, ios == 0 .and. iocmd == 0, err%message // " and &
172+
call check(error, ios == 0 .and. iocmd == 0, error%message // " and &
140173
& cannot cleanup test_exists_dir: " // trim(msg))
141174
return
142175
end if
@@ -174,7 +207,7 @@ subroutine test_exists_symlink(error)
174207
if (allocated(error)) then
175208
! Clean up: remove the target
176209
close(iunit,status='delete',iostat=ios,iomsg=msg)
177-
call check(error, ios == 0, err%message // " and cannot delete target: " // trim(msg))
210+
call check(error, ios == 0, error%message // " and cannot delete target: " // trim(msg))
178211
return
179212
end if
180213

@@ -184,12 +217,12 @@ subroutine test_exists_symlink(error)
184217
if (allocated(error)) then
185218
! Clean up: remove the link
186219
call execute_command_line("rm " // link_name, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
187-
call check(error, ios == 0 .and. iocmd == 0, err%message // " and &
220+
call check(error, ios == 0 .and. iocmd == 0, error%message // " and &
188221
& cannot delete link: " // trim(msg))
189222

190223
! Clean up: remove the target
191224
close(iunit,status='delete',iostat=ios,iomsg=msg)
192-
call check(error, ios == 0, err%message // " and cannot delete target: " // trim(msg))
225+
call check(error, ios == 0, error%message // " and cannot delete target: " // trim(msg))
193226
return
194227
end if
195228

@@ -199,12 +232,12 @@ subroutine test_exists_symlink(error)
199232
if (allocated(error)) then
200233
! Clean up: remove the link
201234
call execute_command_line("rm " // link_name, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
202-
call check(error, ios == 0 .and. iocmd == 0, err%message // " and &
235+
call check(error, ios == 0 .and. iocmd == 0, error%message // " and &
203236
& cannot delete link: " // trim(msg))
204237

205238
! Clean up: remove the target
206239
close(iunit,status='delete',iostat=ios,iomsg=msg)
207-
call check(error, ios == 0, err%message // " and cannot delete target: " // trim(msg))
240+
call check(error, ios == 0, error%message // " and cannot delete target: " // trim(msg))
208241
return
209242
end if
210243

@@ -215,7 +248,7 @@ subroutine test_exists_symlink(error)
215248
if (allocated(error)) then
216249
! Clean up: remove the target
217250
close(iunit,status='delete',iostat=ios,iomsg=msg)
218-
call check(error, ios == 0, err%message // " and cannot delete target: " // trim(msg))
251+
call check(error, ios == 0, error%message // " and cannot delete target: " // trim(msg))
219252
end if
220253

221254
! Clean up: remove the target

0 commit comments

Comments
 (0)