Skip to content

Commit 41ac1fd

Browse files
committed
add test
1 parent b272c74 commit 41ac1fd

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

test/system/test_filesystem.f90

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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
5+
OS_WINDOWS, get_cwd, set_cwd, operator(/)
66
use stdlib_error, only: state_type, STDLIB_FS_ERROR
77

88
implicit none
@@ -25,7 +25,8 @@ subroutine collect_suite(testsuite)
2525
new_unittest("fs_make_dir_existing_dir", test_make_directory_existing), &
2626
new_unittest("fs_make_dir_all", test_make_directory_all), &
2727
new_unittest("fs_remove_dir", test_remove_directory), &
28-
new_unittest("fs_remove_dir_non_existent", test_remove_directory_nonexistent) &
28+
new_unittest("fs_remove_dir_non_existent", test_remove_directory_nonexistent), &
29+
new_unittest("fs_cwd", test_cwd) &
2930
]
3031
end subroutine collect_suite
3132

@@ -279,6 +280,56 @@ subroutine test_remove_directory_nonexistent(error)
279280
if (allocated(error)) return
280281
end subroutine test_remove_directory_nonexistent
281282

283+
subroutine test_cwd(error)
284+
type(error_type), allocatable, intent(out) :: error
285+
type(state_type) :: err
286+
character(len=256) :: dir_name
287+
integer :: ios,iocmd
288+
character(len=512) :: msg
289+
290+
character(:), allocatable :: pwd1, pwd2, abs_dir_name
291+
292+
! get the initial cwd
293+
call get_cwd(pwd1, err)
294+
call check(error, err%ok(), 'Could not get current working directory: '//err%print())
295+
if (allocated(error)) return
296+
297+
! create a temporary directory for use by `set_cwd`
298+
dir_name = "test_directory"
299+
300+
call execute_command_line('mkdir ' // dir_name, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
301+
call check(error, ios==0 .and. iocmd==0, 'Cannot init cwd test: '//trim(msg))
302+
if (allocated(error)) return
303+
304+
abs_dir_name = pwd1 / dir_name
305+
call set_cwd(abs_dir_name, err)
306+
call check(error, err%ok(), 'Could not set current working directory: '//err%print())
307+
if (allocated(error)) return
308+
309+
! get the new cwd -> should be same as (pwd1 / dir_name)
310+
call get_cwd(pwd2, err)
311+
call check(error, err%ok(), 'Could not get current working directory: '//err%print())
312+
if (allocated(error)) return
313+
314+
call check(error, pwd2 == abs_dir_name, 'Working directory is wrong, &
315+
& expected: '//abs_dir_name//" got: "//pwd2)
316+
if (allocated(error)) return
317+
318+
! cleanup: set the cwd back to the initial value
319+
call set_cwd(pwd1, err)
320+
call check(error, err%ok(), 'Could not clean up cwd test, could not set the cwd back: '//err%print())
321+
if (allocated(error)) then
322+
! our cwd now is `./test_directory`
323+
! there is no way of removing the empty test directory
324+
return
325+
end if
326+
327+
! cleanup: remove the empty directory
328+
call execute_command_line('rmdir ' // dir_name, exitstat=ios, cmdstat=iocmd, cmdmsg=msg)
329+
call check(error, ios==0 .and. iocmd==0, 'Cannot cleanup cwd test, cannot remove empty dir: '//trim(msg))
330+
if (allocated(error)) return
331+
end subroutine test_cwd
332+
282333
end module test_filesystem
283334

284335
program tester

0 commit comments

Comments
 (0)