Skip to content

Commit

Permalink
fix: panic when calling SplitFilepath on windows
Browse files Browse the repository at this point in the history
Unix unit test improved. Windows unit test included.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Oct 21, 2022
1 parent 189ec01 commit 78c10d2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 6 deletions.
18 changes: 13 additions & 5 deletions pkg/util/path.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"unicode/utf8"
)
Expand Down Expand Up @@ -37,20 +38,27 @@ func ExpandPath(path string) string {
}

func SplitFilepath(path string) (result []string) {
separator := os.PathSeparator

path = filepath.FromSlash(path)
path = filepath.Clean(path)

if filepath.IsAbs(path) {
p, err := filepath.Rel(string(os.PathSeparator), path)
if runtime.GOOS == "windows" {
sepStr := string(separator)
uncRootPath := fmt.Sprintf("%s%s", sepStr, sepStr)

path = strings.TrimPrefix(path, uncRootPath)
path = strings.TrimPrefix(path, sepStr)
path = strings.TrimSuffix(path, sepStr)
} else if filepath.IsAbs(path) {
p, err := filepath.Rel(string(separator), path)
if err != nil {
panic(fmt.Sprintf("unable to get relative path for %q", path))
}
path = p
}

separator := os.PathSeparator

if path == "." || path == string(separator) {
if path == "" || path == "." || path == string(separator) {
return nil
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/util/path_test.go → pkg/util/path_unix_test.go
@@ -1,3 +1,5 @@
//go:build !windows

package util_test

import (
Expand Down Expand Up @@ -55,7 +57,7 @@ type splitPathTest struct {
expectedPathParts []string
}

var _ = DescribeTable("split path",
var _ = DescribeTable("split unix path",
func(t splitPathTest) {
parts := util.SplitFilepath(t.path)
Expect(parts).To(Equal(t.expectedPathParts))
Expand All @@ -64,6 +66,10 @@ var _ = DescribeTable("split path",
path: "/",
expectedPathParts: nil,
}),
Entry("root dir", splitPathTest{
path: "/mydir/",
expectedPathParts: []string{"mydir"},
}),
Entry("unnormalized root path", splitPathTest{
path: "////",
expectedPathParts: nil,
Expand Down
58 changes: 58 additions & 0 deletions pkg/util/path_windows_test.go
@@ -0,0 +1,58 @@
//go:build windows

package util_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/werf/werf/pkg/util"
)

type splitPathTest struct {
path string
expectedPathParts []string
}

var _ = DescribeTable("split windows path",
func(t splitPathTest) {
parts := util.SplitFilepath(t.path)
Expect(parts).To(Equal(t.expectedPathParts))
},
Entry("UNC absolute path", splitPathTest{
path: `\\Server2\Share\Test\Foo.txt`,
expectedPathParts: []string{"Server2", "Share", "Test", "Foo.txt"},
}),
Entry("UNC absolute non-normalized path", splitPathTest{
path: `\\\\\\Server2\\\Share\\\Test\Foo.txt`,
expectedPathParts: []string{"Server2", "Share", "Test", "Foo.txt"},
}),
Entry("UNC root path", splitPathTest{
path: `\\`,
expectedPathParts: nil,
}),
Entry("root disk path", splitPathTest{
path: `D:\`,
expectedPathParts: []string{`D:`},
}),
Entry("unnormalized root disk path", splitPathTest{
path: `D:\\\\\\`,
expectedPathParts: []string{`D:`},
}),
Entry("empty path", splitPathTest{
path: "",
expectedPathParts: nil,
}),
Entry("absolute path to file", splitPathTest{
path: `D:\path\to\file`,
expectedPathParts: []string{`D:`, "path", "to", "file"},
}),
Entry("absolute path to dir", splitPathTest{
path: `D:\path\to\dir\`,
expectedPathParts: []string{`D:`, "path", "to", "dir"},
}),
Entry("relative path", splitPathTest{
path: `path\to\dir\or\file`,
expectedPathParts: []string{"path", "to", "dir", "or", "file"},
}),
)

0 comments on commit 78c10d2

Please sign in to comment.