1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

filepath: path separator (#3756)

This commit is contained in:
yuyi
2020-02-18 03:31:23 +08:00
committed by GitHub
parent 6079025985
commit 6849a4e770
26 changed files with 105 additions and 99 deletions

View File

@@ -797,7 +797,7 @@ pub fn home_dir() string {
}
home += homepath
}
home += path_separator
home += filepath.separator
return home
}
@@ -1000,7 +1000,7 @@ pub fn walk_ext(path, ext string) []string {
panic(err)
}
mut res := []string
separator := if path.ends_with(path_separator) { '' } else { path_separator }
separator := if path.ends_with(filepath.separator) { '' } else { filepath.separator }
for i, file in files {
if file.starts_with('.') {
continue
@@ -1026,7 +1026,7 @@ pub fn walk(path string, f fn(path string)) {
panic(err)
}
for file in files {
p := path + os.path_separator + file
p := path + filepath.separator + file
if os.is_dir(p) && !os.is_link(p) {
walk(p, f)
}
@@ -1087,9 +1087,9 @@ pub fn flush_stdout() {
}
pub fn mkdir_all(path string) {
mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
for subdir in path.split(os.path_separator) {
p += subdir + os.path_separator
mut p := if path.starts_with(filepath.separator) { filepath.separator } else { '' }
for subdir in path.split(filepath.separator) {
p += subdir + filepath.separator
if !os.is_dir(p) {
os.mkdir(p) or {
panic(err)

View File

@@ -7,6 +7,10 @@ import strings
#include <fcntl.h>
pub const (
/**
* This constant is deprecated. Use `filepath.separator` instead.
* FIXME Remove this separator, as it a part of `filepath` module.
*/
path_separator = '/'
)
@@ -241,7 +245,7 @@ pub fn (f mut File) write_bytes(data voidptr, size int) {
return
}
}
*/
*/
C.fwrite(data, 1, size, f.cfile)
}

View File

@@ -1,4 +1,5 @@
import os
import filepath
fn test_aaa_setup(){
cleanup_leftovers()
@@ -34,7 +35,7 @@ fn test_open_file() {
mut file := os.open_file(filename, "w+", 0666) or { panic(err) }
file.write(hello)
file.close()
assert hello.len == os.file_size(filename)
read_hello := os.read_file(filename) or {
@@ -116,14 +117,14 @@ fn walk_callback(file string) {
if file == '.' || file == '..' {
return
}
assert file == 'test_walk'+os.path_separator+'test1'
assert file == 'test_walk' + filepath.separator + 'test1'
}
fn test_walk() {
folder := 'test_walk'
os.mkdir(folder) or { panic(err) }
file1 := folder+os.path_separator+'test1'
file1 := folder + filepath.separator + 'test1'
os.write_file(file1,'test-1')
@@ -172,7 +173,7 @@ fn test_tmpdir(){
assert t.len > 0
assert os.is_dir(t)
tfile := t + os.path_separator + 'tmpfile.txt'
tfile := t + filepath.separator + 'tmpfile.txt'
os.rm(tfile) // just in case
@@ -256,25 +257,25 @@ fn test_symlink() {
}
fn test_is_executable_writable_readable() {
file_name := os.tmpdir() + os.path_separator + 'rwxfile.exe'
file_name := os.tmpdir() + filepath.separator + 'rwxfile.exe'
mut f := os.create(file_name) or {
eprintln('failed to create file $file_name')
return
}
f.close()
$if !windows {
os.chmod(file_name, 0600) // mark as readable && writable, but NOT executable
os.chmod(file_name, 0600) // mark as readable && writable, but NOT executable
assert os.is_writable(file_name)
assert os.is_readable(file_name)
assert !os.is_executable(file_name)
assert !os.is_executable(file_name)
os.chmod(file_name, 0700) // mark as executable too
assert os.is_executable(file_name)
} $else {
assert os.is_writable(file_name)
assert os.is_readable(file_name)
assert os.is_executable(file_name)
assert os.is_executable(file_name)
}
// We finally delete the test file.

View File

@@ -6,6 +6,10 @@ import strings
#include <winsock2.h>
pub const (
/**
* This constant is deprecated. Use `filepath.separator` instead.
* FIXME Remove this separator, as it a part of `filepath` module.
*/
path_separator = '\\'
)