Skip to content

Commit e701d68

Browse files
committed
Improve tests
1 parent 84fca8b commit e701d68

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

test/io/test_filesystem.f90

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ subroutine collect_filesystem(testsuite)
1717
type(unittest_type), allocatable, intent(out) :: testsuite(:)
1818

1919
testsuite = [ &
20-
new_unittest("fs_file_is_windows", fs_is_windows), &
20+
new_unittest("fs_is_windows", fs_is_windows), &
21+
new_unittest("fs_path_separator", fs_path_separator), &
2122
new_unittest("fs_file_not_exists", fs_file_not_exists, should_fail=.true.), &
2223
new_unittest("fs_file_exists", fs_file_exists), &
2324
new_unittest("fs_current_dir_exists", fs_current_dir_exists), &
24-
new_unittest("fs_path_separator", fs_path_separator), &
25+
new_unittest("fs_use_path_separator", fs_path_separator), &
2526
new_unittest("fs_run_invalid_command", fs_run_invalid_command, should_fail=.true.), &
2627
new_unittest("fs_run_with_invalid_option", fs_run_with_invalid_option, should_fail=.true.), &
2728
new_unittest("fs_run_valid_command", fs_run_valid_command), &
@@ -48,6 +49,20 @@ subroutine fs_is_windows(error)
4849
end if
4950
end subroutine
5051

52+
subroutine fs_path_separator(error)
53+
type(error_type), allocatable, intent(out) :: error
54+
55+
character(len=255) :: value
56+
integer :: length, stat
57+
58+
call get_environment_variable('HOMEDRIVE', value, length, stat)
59+
if (stat == 0 .and. length > 0) then
60+
call check(error, path_separator == '\\', "Path separator should be set for Windows.")
61+
else
62+
call check(error, path_separator == '/', "Path separator should not be set for non-Windows.")
63+
end if
64+
end subroutine
65+
5166
subroutine fs_file_not_exists(error)
5267
type(error_type), allocatable, intent(out) :: error
5368

@@ -61,12 +76,9 @@ subroutine fs_file_exists(error)
6176
type(error_type), allocatable, intent(out) :: error
6277

6378
logical :: is_existing
64-
integer :: unit
6579
character(*), parameter :: filename = "file.tmp"
6680

67-
open(newunit=unit, file=filename)
68-
close(unit)
69-
81+
call create_file(filename)
7082
is_existing = exists(filename)
7183
call check(error, is_existing, "An existing file should not fail.")
7284
call delete_file(filename)
@@ -81,7 +93,7 @@ subroutine fs_current_dir_exists(error)
8193
call check(error, is_existing, "Current directory should not fail.")
8294
end subroutine
8395

84-
subroutine fs_path_separator(error)
96+
subroutine fs_use_path_separator(error)
8597
type(error_type), allocatable, intent(out) :: error
8698

8799
character(*), parameter :: outer_dir = "path_separator_outer"
@@ -156,10 +168,7 @@ subroutine fs_list_dir_one_file(error)
156168
call test_failed(error, "Creating directory '"//temp_list_dir//"' failed."); return
157169
end if
158170

159-
call run('touch '//temp_list_dir//'/'//filename, iostat=stat)
160-
if (stat /= 0) then
161-
call test_failed(error, "Creating file'"//filename//"' in directory '"//temp_list_dir//"' failed."); return
162-
end if
171+
call create_file(temp_list_dir//path_separator//filename)
163172

164173
call list_dir(temp_list_dir, files, stat)
165174
call check(error, stat, "Listing the contents of an empty directory shouldn't fail.")
@@ -184,15 +193,8 @@ subroutine fs_list_dir_two_files(error)
184193
call test_failed(error, "Creating directory '"//temp_list_dir//"' failed."); return
185194
end if
186195

187-
call run('touch '//temp_list_dir//'/'//filename1, iostat=stat)
188-
if (stat /= 0) then
189-
call test_failed(error, "Creating file 1 in directory '"//temp_list_dir//"' failed."); return
190-
end if
191-
192-
call run('touch '//temp_list_dir//'/'//filename2, iostat=stat)
193-
if (stat /= 0) then
194-
call test_failed(error, "Creating file 2 in directory '"//temp_list_dir//"' failed."); return
195-
end if
196+
call create_file(temp_list_dir//path_separator//filename1)
197+
call create_file(temp_list_dir//path_separator//filename2)
196198

197199
call list_dir(temp_list_dir, files, stat)
198200
call check(error, stat, "Listing the contents of an empty directory shouldn't fail.")
@@ -209,7 +211,7 @@ subroutine fs_list_dir_one_file_one_dir(error)
209211
integer :: stat
210212

211213
type(string_type), allocatable :: contents(:)
212-
character(*), parameter :: filename1 = 'abc.txt'
214+
character(*), parameter :: filename = 'abc.txt'
213215
character(*), parameter :: dir = 'xyz'
214216

215217
call rmdir(temp_list_dir)
@@ -218,24 +220,16 @@ subroutine fs_list_dir_one_file_one_dir(error)
218220
call test_failed(error, "Creating directory '"//temp_list_dir//"' failed."); return
219221
end if
220222

221-
call run('touch '//temp_list_dir//'/'//filename1, iostat=stat)
222-
if (stat /= 0) then
223-
call test_failed(error, "Creating file 1 in directory '"//temp_list_dir//"' failed."); return
224-
end if
225-
226-
if (is_windows) then
227-
call mkdir(temp_list_dir//'\'//dir, stat)
228-
else
229-
call mkdir(temp_list_dir//'/'//dir, stat)
230-
end if
223+
call create_file(temp_list_dir//path_separator//filename)
224+
call mkdir(temp_list_dir//path_separator//dir, stat)
231225
if (stat /= 0) then
232226
call test_failed(error, "Creating dir in directory '"//temp_list_dir//"' failed."); return
233227
end if
234228

235229
call list_dir(temp_list_dir, contents, stat)
236230
call check(error, stat, "Listing the contents of an empty directory shouldn't fail.")
237231
call check(error, size(contents) == 2, "The directory should contain two files.")
238-
call check(error, char(contents(1)) == filename1, "The file should be '"//filename1//"'.")
232+
call check(error, char(contents(1)) == filename, "The file should be '"//filename//"'.")
239233
call check(error, char(contents(2)) == dir, "The file should be '"//dir//"'.")
240234

241235
call rmdir(temp_list_dir)
@@ -263,15 +257,20 @@ subroutine fs_rmdir_with_contents(error)
263257
call check(error, .not. exists(dir), "Directory should not exist.")
264258
call mkdir(dir)
265259
call check(error, exists(dir), "Directory should exist.")
266-
if (is_windows) then
267-
call mkdir(dir//'\'//'another_dir')
268-
else
269-
call mkdir(dir//'/'//'another_dir')
270-
end if
260+
call mkdir(dir//path_separator//'another_dir')
271261
call rmdir(dir)
272262
call check(error, .not. exists(dir), "Directory should not exist.")
273263
end subroutine
274264

265+
subroutine create_file(filename)
266+
character(len=*), intent(in) :: filename
267+
268+
integer :: io
269+
270+
open(newunit=io, file=filename)
271+
close(io)
272+
end subroutine
273+
275274
subroutine delete_file(filename)
276275
character(len=*), intent(in) :: filename
277276

0 commit comments

Comments
 (0)