Merge "Add support for converting OptionalPath to Paths" am: 17ccf26748 am: 1d579e9694 am: 472325bf8d

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1706747

Change-Id: I60d5cdc9e77ed68e9c365986057608649eee770b
This commit is contained in:
Paul Duffin 2021-05-13 20:31:13 +00:00 committed by Automerger Merge Worker
commit 011d8e670c
2 changed files with 33 additions and 0 deletions

View File

@ -287,6 +287,17 @@ func (p OptionalPath) Path() Path {
return p.path return p.path
} }
// AsPaths converts the OptionalPath into Paths.
//
// It returns nil if this is not valid, or a single length slice containing the Path embedded in
// this OptionalPath.
func (p OptionalPath) AsPaths() Paths {
if !p.valid {
return nil
}
return Paths{p.path}
}
// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the // RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
// result of calling Path.RelativeToTop on it. // result of calling Path.RelativeToTop on it.
func (p OptionalPath) RelativeToTop() OptionalPath { func (p OptionalPath) RelativeToTop() OptionalPath {

View File

@ -141,6 +141,9 @@ func TestOptionalPath(t *testing.T) {
path = OptionalPathForPath(nil) path = OptionalPathForPath(nil)
checkInvalidOptionalPath(t, path) checkInvalidOptionalPath(t, path)
path = OptionalPathForPath(PathForTesting("path"))
checkValidOptionalPath(t, path, "path")
} }
func checkInvalidOptionalPath(t *testing.T, path OptionalPath) { func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
@ -151,6 +154,10 @@ func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
if path.String() != "" { if path.String() != "" {
t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String()) t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
} }
paths := path.AsPaths()
if len(paths) != 0 {
t.Errorf("Uninitialized OptionalPath AsPaths() should return empty Paths, not %q", paths)
}
defer func() { defer func() {
if r := recover(); r == nil { if r := recover(); r == nil {
t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath") t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath")
@ -159,6 +166,21 @@ func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
path.Path() path.Path()
} }
func checkValidOptionalPath(t *testing.T, path OptionalPath, expectedString string) {
t.Helper()
if !path.Valid() {
t.Errorf("Initialized OptionalPath should not be invalid")
}
if path.String() != expectedString {
t.Errorf("Initialized OptionalPath String() should return %q, not %q", expectedString, path.String())
}
paths := path.AsPaths()
if len(paths) != 1 {
t.Errorf("Initialized OptionalPath AsPaths() should return Paths with length 1, not %q", paths)
}
path.Path()
}
func check(t *testing.T, testType, testString string, func check(t *testing.T, testType, testString string,
got interface{}, err []error, got interface{}, err []error,
expected interface{}, expectedErr []error) { expected interface{}, expectedErr []error) {