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

os: fix os.dir, os.base, os.file_name, when the argument contains / on windows

This commit is contained in:
Delyan Angelov 2021-07-31 13:52:54 +03:00
parent 1231e5df20
commit d82d41d804
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 18 additions and 6 deletions

View File

@ -165,10 +165,11 @@ pub fn file_ext(path string) string {
// If the path is empty, dir returns ".". If the path consists entirely of separators,
// dir returns a single separator.
// The returned path does not end in a separator unless it is the root directory.
pub fn dir(path string) string {
if path == '' {
pub fn dir(opath string) string {
if opath == '' {
return '.'
}
path := opath.replace_each(['/', path_separator, r'\', path_separator])
pos := path.last_index(path_separator) or { return '.' }
if pos == 0 && path_separator == '/' {
return '/'
@ -180,10 +181,11 @@ pub fn dir(path string) string {
// Trailing path separators are removed before extracting the last element.
// If the path is empty, base returns ".". If the path consists entirely of separators, base returns a
// single separator.
pub fn base(path string) string {
if path == '' {
pub fn base(opath string) string {
if opath == '' {
return '.'
}
path := opath.replace_each(['/', path_separator, r'\', path_separator])
if path == path_separator {
return path_separator
}
@ -198,7 +200,8 @@ pub fn base(path string) string {
// file_name will return all characters found after the last occurence of `path_separator`.
// file extension is included.
pub fn file_name(path string) string {
pub fn file_name(opath string) string {
path := opath.replace_each(['/', path_separator, r'\', path_separator])
return path.all_after_last(path_separator)
}
@ -363,7 +366,8 @@ fn executable_fallback() string {
}
}
if !is_abs_path(exepath) {
if exepath.contains(path_separator) {
rexepath := exepath.replace_each(['/', path_separator, r'\', path_separator])
if rexepath.contains(path_separator) {
exepath = join_path(os.wd_at_startup, exepath)
} else {
// no choice but to try to walk the PATH folders :-| ...

View File

@ -533,11 +533,15 @@ fn test_dir() {
$if windows {
assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b'
assert os.dir('C:\\a\\b\\') == 'C:\\a\\b'
assert os.dir('C:/a/b/c') == 'C:\\a\\b'
assert os.dir('C:/a/b/') == 'C:\\a\\b'
} $else {
assert os.dir('/') == '/'
assert os.dir('/abc') == '/'
assert os.dir('/var/tmp/foo') == '/var/tmp'
assert os.dir('/var/tmp/') == '/var/tmp'
assert os.dir('C:\\a\\b\\c') == 'C:/a/b'
assert os.dir('C:\\a\\b\\') == 'C:/a/b'
}
assert os.dir('os') == '.'
}
@ -546,9 +550,13 @@ fn test_base() {
$if windows {
assert os.base('v\\vlib\\os') == 'os'
assert os.base('v\\vlib\\os\\') == 'os'
assert os.base('v/vlib/os') == 'os'
assert os.base('v/vlib/os/') == 'os'
} $else {
assert os.base('v/vlib/os') == 'os'
assert os.base('v/vlib/os/') == 'os'
assert os.base('v\\vlib\\os') == 'os'
assert os.base('v\\vlib\\os\\') == 'os'
}
assert os.base('filename') == 'filename'
}