-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
- Loading branch information
Showing
4 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Package paths contains utility functions for handling file paths independently from the local OS unlike the path/filepath package. | ||
package paths | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var multipleSlashesRegex = regexp.MustCompile("/{2,}") | ||
|
||
// Dir is like filepath.Dir but it doesn't use the local OS path separator. | ||
func Dir(path string) string { | ||
// Normalize path by replacing multiple slashes with a single slash | ||
normalizedPath := multipleSlashesRegex.ReplaceAllString(path, "/") | ||
|
||
// Your existing logic here | ||
if normalizedPath == "/" { | ||
return "/" | ||
} | ||
if strings.HasSuffix(normalizedPath, "/") { | ||
return strings.TrimSuffix(normalizedPath, "/") | ||
} | ||
idx := strings.LastIndex(normalizedPath, "/") | ||
switch idx { | ||
case 0: | ||
return "/" | ||
case -1: | ||
return "." | ||
default: | ||
return normalizedPath[:idx] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package paths | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestDir(t *testing.T) { | ||
testCases := []struct { | ||
path string | ||
expected string | ||
}{ | ||
{"/usr/local/bin", "/usr/local"}, | ||
{"/usr/local/bin/", "/usr/local/bin"}, | ||
{"usr/local/bin/", "usr/local/bin"}, | ||
{"usr/local/bin", "usr/local"}, | ||
{"/usr", "/"}, | ||
{"usr", "."}, | ||
{"/", "/"}, | ||
{"", "."}, | ||
{"filename.txt", "."}, | ||
{"./file.txt", "."}, | ||
{"../sibling.txt", ".."}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.path, func(t *testing.T) { | ||
result := Dir(tc.path) | ||
if result != tc.expected { | ||
t.Errorf("Dir(%q) = %q, want %q", tc.path, result, tc.expected) | ||
} | ||
}) | ||
} | ||
} |