Skip to content

Commit e58f4b8

Browse files
committed
Add rm_dir and test it
1 parent 0642aff commit e58f4b8

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/stdlib_io_filesystem.F90

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module stdlib_io_filesystem
66
implicit none
77
private
88

9-
public :: temp_dir, is_windows, exists, list_dir, run
9+
public :: temp_dir, is_windows, exists, list_dir, rm_dir, run
1010

1111
character(*), parameter :: temp_dir = 'temp'
1212
character(*), parameter :: listed_contents = temp_dir//'/listed_contents.txt'
@@ -100,6 +100,20 @@ subroutine list_dir(dir, files, iostat, iomsg)
100100
close(unit, status="delete")
101101
end
102102

103+
!> Version: experimental
104+
!>
105+
!> Remove a directory and its contents.
106+
!> [Specification](../page/specs/stdlib_io.html#rm_dir)
107+
subroutine rm_dir(dir)
108+
character(len=*), intent(in) :: dir
109+
110+
if (is_windows()) then
111+
call run('rmdir /s/q '//dir)
112+
else
113+
call run('rm -rf '//dir)
114+
end if
115+
end
116+
103117
!> Version: experimental
104118
!>
105119
!> Run a command in the shell.

test/io/test_filesystem.f90

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ subroutine collect_filesystem(testsuite)
2727
new_unittest("fs_list_dir_empty", fs_list_dir_empty), &
2828
new_unittest("fs_list_dir_one_file", fs_list_dir_one_file), &
2929
new_unittest("fs_list_dir_two_files", fs_list_dir_two_files), &
30-
new_unittest("fs_list_dir_one_file_one_dir", fs_list_dir_one_file_one_dir) &
30+
new_unittest("fs_list_dir_one_file_one_dir", fs_list_dir_one_file_one_dir), &
31+
new_unittest("fs_rm_dir_empty", fs_rm_dir_empty), &
32+
new_unittest("fs_rm_dir_with_contents", fs_rm_dir_with_contents) &
3133
]
3234
end
3335

@@ -239,6 +241,37 @@ subroutine fs_list_dir_one_file_one_dir(error)
239241
call run('rm -rf '//temp_list_dir, iostat=stat)
240242
end
241243

244+
subroutine fs_rm_dir_empty(error)
245+
type(error_type), allocatable, intent(out) :: error
246+
247+
character(*), parameter :: filename = "empty_dir_to_remove"
248+
249+
call rm_dir(filename)
250+
call check(error, .not. exists(filename), "Directory should not exist.")
251+
call run('mkdir '//filename)
252+
call check(error, exists(filename), "Directory should exist.")
253+
call rm_dir(filename)
254+
call check(error, .not. exists(filename), "Directory should not exist.")
255+
end
256+
257+
subroutine fs_rm_dir_with_contents(error)
258+
type(error_type), allocatable, intent(out) :: error
259+
260+
character(*), parameter :: filename = "dir_with_contents_to_remove"
261+
262+
call rm_dir(filename)
263+
call check(error, .not. exists(filename), "Directory should not exist.")
264+
call run('mkdir '//filename)
265+
call check(error, exists(filename), "Directory should exist.")
266+
if (is_windows()) then
267+
call run('mkdir '//filename//'\'//'another_dir')
268+
else
269+
call run('mkdir '//filename//'/'//'another_dir')
270+
end if
271+
call rm_dir(filename)
272+
call check(error, .not. exists(filename), "Directory should not exist.")
273+
end
274+
242275
subroutine delete_file(filename)
243276
character(len=*), intent(in) :: filename
244277

@@ -247,7 +280,6 @@ subroutine delete_file(filename)
247280
open(newunit=io, file=filename)
248281
close(io, status="delete")
249282
end
250-
251283
end
252284

253285
program tester

0 commit comments

Comments
 (0)