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:
Colin Cross
2018-12-13 13:03:29 -08:00
parent e98d11f733
commit fee2bff77c
2 changed files with 61 additions and 0 deletions

View File

@@ -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
// targetPath is not inside basePath.
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)
if err != nil {
reportPathError(ctx, err)