From 4cb34bb4bd924cd4e0a9f17ffb5deb272415831d Mon Sep 17 00:00:00 2001 From: spf13 Date: Thu, 23 Apr 2026 14:31:30 -0400 Subject: [PATCH] fix: normalize path separators in CleanPath for cross-platform consistency CleanPath was using filepath.Separator to split and join path components, which is '\' on Windows. This caused two problems: - Paths using '/' (e.g. 'foo/bar/baz') were not split on Windows - Paths using '\' (e.g. 'foo\bar\baz') were incorrectly split on Windows but the backslashes were stripped as invalid chars on Unix Fix: normalize both '\' and '/' to '/' before splitting, and always join with '/'. This produces consistent output on all platforms regardless of which separator style is used in the input. Also update the 'several dirs2' test case expected value from 'foobarbaz' to 'foo/bar/baz', since backslash-separated input now correctly splits into path components rather than having the backslashes stripped as invalid chars. Fixes #87 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pathological.go | 9 +++++---- pathological_test.go | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pathological.go b/pathological.go index e987caf..36938a7 100644 --- a/pathological.go +++ b/pathological.go @@ -22,7 +22,6 @@ package pathologize // https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words import ( - "path/filepath" "regexp" "strings" "unicode/utf8" @@ -41,12 +40,14 @@ var ( ) func CleanPath(path string) string { - pathParts := strings.Split(path, string(filepath.Separator)) + // Normalize backslashes to forward slashes so both separator styles work + // consistently on all platforms. + path = strings.ReplaceAll(path, "\\", "/") + pathParts := strings.Split(path, "/") for i, part := range pathParts { pathParts[i] = Clean(part) } - - return strings.Join(pathParts, string(filepath.Separator)) + return strings.Join(pathParts, "/") } func IsClean(filename string) bool { diff --git a/pathological_test.go b/pathological_test.go index 2692216..baa67f5 100644 --- a/pathological_test.go +++ b/pathological_test.go @@ -169,7 +169,7 @@ func Test_paths(t *testing.T) { {"short", "foo", "foo"}, {"long", "foo.bar.baz", "foo.bar.baz"}, {"several dirs", "foo/bar/baz", "foo/bar/baz"}, - {"several dirs2", "foo\\bar\\baz", "foobarbaz"}, + {"several dirs2", "foo\\bar\\baz", "foo/bar/baz"}, {"dirs with spaces", "foo bar/baz", "foo bar/baz"}, {"dirs with bad chars", "foo*bar/baz", "foobar/baz"}, {"example", "foo/bar:baz*qux", "foo/barbazqux"},