Fix MaybeRel when OUT is absolute
MaybeRel was failing with an error when comparing an absolute out path to a relative source path. Make it return false instead, and add tests. Bug: 119412419 Test: paths_test.go Change-Id: I462182638bd57b1367b5bfb0718e975c11ae66f7
This commit is contained in:
parent
e98d11f733
commit
fee2bff77c
|
@ -1054,6 +1054,10 @@ func Rel(ctx PathContext, basePath string, targetPath string) string {
|
||||||
// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
|
// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
|
||||||
// targetPath is not inside basePath.
|
// targetPath is not inside basePath.
|
||||||
func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
|
func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
|
||||||
|
// filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
|
||||||
|
if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
rel, err := filepath.Rel(basePath, targetPath)
|
rel, err := filepath.Rel(basePath, targetPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
reportPathError(ctx, err)
|
reportPathError(ctx, err)
|
||||||
|
|
|
@ -573,3 +573,60 @@ func TestDirectorySortedPaths(t *testing.T) {
|
||||||
t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
|
t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMaybeRel(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
base string
|
||||||
|
target string
|
||||||
|
out string
|
||||||
|
isRel bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "normal",
|
||||||
|
base: "a/b/c",
|
||||||
|
target: "a/b/c/d",
|
||||||
|
out: "d",
|
||||||
|
isRel: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "parent",
|
||||||
|
base: "a/b/c/d",
|
||||||
|
target: "a/b/c",
|
||||||
|
isRel: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not relative",
|
||||||
|
base: "a/b",
|
||||||
|
target: "c/d",
|
||||||
|
isRel: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "abs1",
|
||||||
|
base: "/a",
|
||||||
|
target: "a",
|
||||||
|
isRel: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "abs2",
|
||||||
|
base: "a",
|
||||||
|
target: "/a",
|
||||||
|
isRel: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
ctx := &configErrorWrapper{}
|
||||||
|
out, isRel := MaybeRel(ctx, testCase.base, testCase.target)
|
||||||
|
if len(ctx.errors) > 0 {
|
||||||
|
t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v",
|
||||||
|
testCase.base, testCase.target, ctx.errors)
|
||||||
|
}
|
||||||
|
if isRel != testCase.isRel || out != testCase.out {
|
||||||
|
t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v",
|
||||||
|
testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue