-
Notifications
You must be signed in to change notification settings - Fork 191
system: is_directory
#946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
system: is_directory
#946
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
38e2337
implement `is_directory`
perazz ff42248
add tests
perazz f33e46a
add specs
perazz 3debb1f
add example
perazz 8dfaac7
ensure trimmed path
perazz 5e93fa5
Merge branch 'master' into system_is_dir
perazz 8ff26be
Update src/stdlib_system.F90
perazz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
! Demonstrate usage of `is_directory` | ||
program example_is_directory | ||
use stdlib_system, only: is_directory | ||
implicit none | ||
! Test a directory path | ||
if (is_directory("/path/to/check")) then | ||
print *, "The specified path is a directory." | ||
else | ||
print *, "The specified path is not a directory." | ||
end if | ||
end program example_is_directory |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
ADDTEST(filesystem) | ||
ADDTEST(os) | ||
ADDTEST(sleep) | ||
ADDTEST(subprocess) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
module test_filesystem | ||
use testdrive, only : new_unittest, unittest_type, error_type, check, skip_test | ||
use stdlib_system, only: is_directory | ||
|
||
implicit none | ||
|
||
contains | ||
|
||
!> Collect all exported unit tests | ||
subroutine collect_suite(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
new_unittest("fs_is_directory_dir", test_is_directory_dir), & | ||
new_unittest("fs_is_directory_file", test_is_directory_file) & | ||
] | ||
end subroutine collect_suite | ||
|
||
! Test `is_directory` for a directory | ||
subroutine test_is_directory_dir(error) | ||
type(error_type), allocatable, intent(out) :: error | ||
character(len=256) :: dirname | ||
integer :: ios, iocmd | ||
character(len=512) :: msg | ||
|
||
dirname = "this_test_dir_tmp" | ||
|
||
! Create a directory | ||
call execute_command_line("mkdir " // dirname, exitstat=ios, cmdstat=iocmd, cmdmsg=msg) | ||
call check(error, ios == 0 .and. iocmd == 0, "Cannot create test directory: " // trim(msg)) | ||
if (allocated(error)) return | ||
|
||
! Verify `is_directory` identifies it as a directory | ||
call check(error, is_directory(dirname), "is_directory did not recognize a valid directory") | ||
if (allocated(error)) return | ||
|
||
! Clean up: remove the directory | ||
call execute_command_line("rmdir " // dirname, exitstat=ios, cmdstat=iocmd, cmdmsg=msg) | ||
call check(error, ios == 0 .and. iocmd == 0, "Cannot remove test directory: " // trim(msg)) | ||
end subroutine test_is_directory_dir | ||
|
||
! Test `is_directory` for a regular file | ||
subroutine test_is_directory_file(error) | ||
type(error_type), allocatable, intent(out) :: error | ||
character(len=256) :: filename | ||
logical :: result | ||
integer :: ios, iunit | ||
character(len=512) :: msg | ||
|
||
filename = "test_file.txt" | ||
|
||
! Create a file | ||
open(newunit=iunit, file=filename, status="replace", iostat=ios, iomsg=msg) | ||
call check(error, ios == 0, "Cannot create test file: " // trim(msg)) | ||
if (allocated(error)) return | ||
|
||
! Verify `is_directory` identifies it as not a directory | ||
result = is_directory(filename) | ||
call check(error, .not. result, "is_directory falsely recognized a regular file as a directory") | ||
if (allocated(error)) return | ||
|
||
! Clean up: remove the file | ||
close(iunit,status='delete',iostat=ios,iomsg=msg) | ||
call check(error, ios == 0, "Cannot delete test file: " // trim(msg)) | ||
if (allocated(error)) return | ||
|
||
end subroutine test_is_directory_file | ||
|
||
|
||
end module test_filesystem | ||
|
||
program tester | ||
use, intrinsic :: iso_fortran_env, only : error_unit | ||
use testdrive, only : run_testsuite, new_testsuite, testsuite_type | ||
use test_filesystem, only : collect_suite | ||
|
||
implicit none | ||
|
||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("filesystem", collect_suite) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write(error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
end program |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.