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

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

View File

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