From 5c2c9312a9dae856001a6c254d6f0852e19194a1 Mon Sep 17 00:00:00 2001 From: Rayan MARMAR Date: Tue, 14 May 2024 17:28:13 +0200 Subject: [PATCH] fixed path uniformize method --- internal/utils/stringutil/string.go | 13 ++----------- internal/utils/stringutil/string_test.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/internal/utils/stringutil/string.go b/internal/utils/stringutil/string.go index c01a5c1..271ebb0 100644 --- a/internal/utils/stringutil/string.go +++ b/internal/utils/stringutil/string.go @@ -4,7 +4,6 @@ import ( "fmt" "path/filepath" "regexp" - "runtime" "strings" ) @@ -50,17 +49,9 @@ func PathRemoveSpecialCharacter(path string) string { // PathUniformize returns uniformized path regarding the device OS. func PathUniformize(path string) string { - // Replace backslashes with forward slashes - // Resolve dots and double slashes - - // Handling platform-specific behavior since filepath.Clean behaves differently for each - if runtime.GOOS == "windows" { - path = filepath.ToSlash(path) - } - path = PathRemoveSpecialCharacter(path) path = filepath.Clean(path) - - return path + // Replace backslashes with forward slashes + return filepath.ToSlash(path) } // ParseOptions parses a string containing options in various formats diff --git a/internal/utils/stringutil/string_test.go b/internal/utils/stringutil/string_test.go index 20bfec0..49743ec 100644 --- a/internal/utils/stringutil/string_test.go +++ b/internal/utils/stringutil/string_test.go @@ -47,16 +47,16 @@ func TestPathUniformize_Success(t *testing.T) { input string expected string }{ - {"C:\\path\\to\\file", "C:/path/to/file"}, - {"C:\\path\\to\\..\\file", "C:/path/file"}, - {"C:\\path\\to\\dir\\..\\file", "C:/path/to/file"}, - {"C:\\path\\with\\double\\\\slashes", "C:/path/with/double/slashes"}, - {"C:\\path\\with\\dots\\..", "C:/path/with"}, - {"C:\\path\\with\\dots\\..\\..", "C:/path"}, - {"C:\\path\\with\\dots\\.", "C:/path/with/dots"}, - {"C:\\path\\with\\dots\\.\\.", "C:/path/with/dots"}, - {"C:\\path\\with\\dots\\.\\.\\..", "C:/path/with"}, - {"C:\\path\\with\\dots\\.\\.\\..\\file", "C:/path/with/file"}, + {"C:/path/to/file", "C:/path/to/file"}, + {"C:/path/to/../file", "C:/path/file"}, + {"C:/path/to/dir/../file", "C:/path/to/file"}, + {"C:/path/with/double/slashes", "C:/path/with/double/slashes"}, + {"C:/path/with/dots/..", "C:/path/with"}, + {"C:/path/with/dots/../..", "C:/path"}, + {"C:/path/with/dots/.", "C:/path/with/dots"}, + {"C:/path/with/dots/./.", "C:/path/with/dots"}, + {"C:/path/with/dots/././..", "C:/path/with"}, + {"C:/path/with/dots/././../file", "C:/path/with/file"}, } for _, item := range items {