os: is_file() (#6301)

pull/6303/head
Larpon 2020-09-04 22:27:52 +02:00 committed by GitHub
parent efa49bfbb7
commit 81778e507f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -1028,6 +1028,11 @@ pub fn is_dir(path string) bool {
}
}
// is_file returns a `bool` indicating whether the given `path` is a file.
pub fn is_file(path string) bool {
return exists(path) && !is_dir(path)
}
// is_link returns a boolean indicating whether `path` is a link.
pub fn is_link(path string) bool {
$if windows {

View File

@ -54,7 +54,37 @@ fn test_create_file() {
assert hello.len == os.file_size(filename)
os.rm(filename)
}
/*
fn test_is_file() {
// Setup
work_dir := os.join_path(os.getwd(),'is_file_test')
os.mkdir_all(work_dir)
tfile := os.join_path(work_dir,'tmp_file')
// Test things that shouldn't be a file
assert os.is_file(work_dir) == false
assert os.is_file('non-existent_file.tmp') == false
// Test file
tfile_content := 'temporary file'
os.write_file(tfile, tfile_content)
assert os.is_file(tfile)
// Test dir symlinks
$if windows {
assert true
} $else {
dsymlink := os.join_path(work_dir,'dir_symlink')
os.system('ln -s $work_dir $dsymlink')
assert os.is_file(dsymlink) == false
}
// Test file symlinks
$if windows {
assert true
} $else {
fsymlink := os.join_path(work_dir,'file_symlink')
os.system('ln -s $tfile $fsymlink')
assert os.is_file(fsymlink)
}
}
*/
fn test_write_and_read_string_to_file() {
filename := './test1.txt'
hello := 'hello world!'