Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 2795244

Browse files
authored
Merge pull request #1948 from jpentland/repeated-separators
Improve handling of paths containing repeated separators (fix #1946).
2 parents aace561 + 64f48c9 commit 2795244

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (c *Ctx) detectGOPATH(path string) (string, error) {
302302
return "", errors.Wrap(err, "failed to detect GOPATH")
303303
}
304304
if isPrefix {
305-
return gp, nil
305+
return filepath.Clean(gp), nil
306306
}
307307
}
308308
return "", errors.Errorf("%s is not within a known GOPATH/src", path)

context_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,14 @@ func TestDetectGOPATH(t *testing.T) {
524524
th.TempDir(filepath.Join("code", "src", "github.com", "username", "package"))
525525
th.TempDir(filepath.Join("go", "src", "github.com", "username", "package"))
526526
th.TempDir(filepath.Join("gotwo", "src", "github.com", "username", "package"))
527+
th.TempDir(filepath.Join("gothree", "sep", "src", "github.com", "username", "package"))
528+
529+
sep := string(os.PathSeparator)
527530

528531
ctx := &Ctx{GOPATHs: []string{
529532
th.Path("go"),
530533
th.Path("gotwo"),
534+
th.Path("gothree") + sep + sep + "sep",
531535
}}
532536

533537
testcases := []struct {
@@ -538,6 +542,8 @@ func TestDetectGOPATH(t *testing.T) {
538542
{th.Path("go"), th.Path(filepath.Join("go", "src", "github.com", "username", "package")), false},
539543
{th.Path("go"), th.Path(filepath.Join("go", "src", "github.com", "username", "package")), false},
540544
{th.Path("gotwo"), th.Path(filepath.Join("gotwo", "src", "github.com", "username", "package")), false},
545+
{th.Path(filepath.Join("gothree", "sep")),
546+
th.Path(filepath.Join("gothree", "sep", "src", "github.com", "username", "package")), false},
541547
{"", th.Path(filepath.Join("code", "src", "github.com", "username", "package")), true},
542548
}
543549

@@ -547,7 +553,7 @@ func TestDetectGOPATH(t *testing.T) {
547553
t.Error("expected error but got none")
548554
}
549555
if GOPATH != tc.GOPATH {
550-
t.Errorf("expected GOPATH to be %s, got %s", GOPATH, tc.GOPATH)
556+
t.Errorf("expected GOPATH to be %s, got %s", tc.GOPATH, GOPATH)
551557
}
552558
}
553559
}

internal/fs/fs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func HasFilepathPrefix(path, prefix string) (bool, error) {
6161
dn = filepath.Dir(path)
6262
}
6363

64-
dn = strings.TrimSuffix(dn, string(os.PathSeparator))
65-
prefix = strings.TrimSuffix(prefix, string(os.PathSeparator))
64+
dn = filepath.Clean(dn)
65+
prefix = filepath.Clean(prefix)
6666

6767
// [1:] in the lines below eliminates empty string on *nix and volume name on Windows
6868
dirs := strings.Split(dn, string(os.PathSeparator))[1:]

internal/fs/fs_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,19 @@ func TestHasFilepathPrefix(t *testing.T) {
4747
dir2 = dir
4848
}
4949

50+
// For testing trailing and repeated separators
51+
sep := string(os.PathSeparator)
52+
5053
cases := []struct {
5154
path string
5255
prefix string
5356
want bool
5457
}{
5558
{filepath.Join(dir, "a", "b"), filepath.Join(dir2), true},
59+
{filepath.Join(dir, "a", "b"), dir2 + sep + sep + "a", true},
60+
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "a") + sep, true},
61+
{filepath.Join(dir, "a", "b") + sep, filepath.Join(dir2), true},
62+
{dir + sep + sep + filepath.Join("a", "b"), filepath.Join(dir2, "a"), true},
5663
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "a"), true},
5764
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "a", "b"), true},
5865
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "c"), false},

0 commit comments

Comments
 (0)