diff --git a/android/paths.go b/android/paths.go index 5d458cbb1..fb751175e 100644 --- a/android/paths.go +++ b/android/paths.go @@ -287,6 +287,17 @@ func (p OptionalPath) Path() 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 // result of calling Path.RelativeToTop on it. func (p OptionalPath) RelativeToTop() OptionalPath { diff --git a/android/paths_test.go b/android/paths_test.go index f8ccc7789..6f5d79e7e 100644 --- a/android/paths_test.go +++ b/android/paths_test.go @@ -141,6 +141,9 @@ func TestOptionalPath(t *testing.T) { path = OptionalPathForPath(nil) checkInvalidOptionalPath(t, path) + + path = OptionalPathForPath(PathForTesting("path")) + checkValidOptionalPath(t, path, "path") } func checkInvalidOptionalPath(t *testing.T, path OptionalPath) { @@ -151,6 +154,10 @@ func checkInvalidOptionalPath(t *testing.T, path OptionalPath) { if 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() { if r := recover(); r == nil { 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() } +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, got interface{}, err []error, expected interface{}, expectedErr []error) {