From 095327134e0e02b8cc645e5e4f57b10349807c62 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 12 Dec 2020 17:59:43 +0800 Subject: [PATCH] os: fix trailing slash in dir() (#7283) --- vlib/os/os.v | 5 +---- vlib/os/os_test.v | 2 ++ 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index b82f6b4b3e..73a647ce9a 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -651,12 +651,9 @@ pub fn dir(path string) string { if path == '' { return '.' } - mut pos := path.last_index(path_separator) or { + pos := path.last_index(path_separator) or { return '.' } - if path.ends_with(path_separator) { - pos-- - } return path[..pos] } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 9bd8e37af2..38990fb6d5 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -440,8 +440,10 @@ fn test_join() { fn test_dir() { $if windows { assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b' + assert os.dir('C:\\a\\b\\') == 'C:\\a\\b' } $else { assert os.dir('/var/tmp/foo') == '/var/tmp' + assert os.dir('/var/tmp/') == '/var/tmp' } assert os.dir('os') == '.' }