diff options
Diffstat (limited to 'android')
| -rw-r--r-- | android/paths.go | 19 | ||||
| -rw-r--r-- | android/testing.go | 31 |
2 files changed, 50 insertions, 0 deletions
diff --git a/android/paths.go b/android/paths.go index 024432eae..c841372dc 100644 --- a/android/paths.go +++ b/android/paths.go @@ -118,6 +118,9 @@ type Path interface { type WritablePath interface { Path + // return the path to the build directory. + buildDir() string + // the writablePath method doesn't directly do anything, // but it allows a struct to distinguish between whether or not it implements the WritablePath interface writablePath() @@ -848,7 +851,12 @@ func (p OutputPath) WithoutRel() OutputPath { return p } +func (p OutputPath) buildDir() string { + return p.config.buildDir +} + var _ Path = OutputPath{} +var _ WritablePath = OutputPath{} // PathForOutput joins the provided paths and returns an OutputPath that is // validated to not escape the build dir. @@ -1151,6 +1159,13 @@ type InstallPath struct { baseDir string // "../" for Make paths to convert "out/soong" to "out", "" for Soong paths } +func (p InstallPath) buildDir() string { + return p.config.buildDir +} + +var _ Path = InstallPath{} +var _ WritablePath = InstallPath{} + func (p InstallPath) writablePath() {} func (p InstallPath) String() string { @@ -1302,6 +1317,10 @@ type PhonyPath struct { func (p PhonyPath) writablePath() {} +func (p PhonyPath) buildDir() string { + return p.config.buildDir +} + var _ Path = PhonyPath{} var _ WritablePath = PhonyPath{} diff --git a/android/testing.go b/android/testing.go index aaf98f595..4f0591b1d 100644 --- a/android/testing.go +++ b/android/testing.go @@ -16,6 +16,7 @@ package android import ( "fmt" + "path/filepath" "regexp" "strings" "testing" @@ -411,3 +412,33 @@ func AndroidMkDataForTest(t *testing.T, config Config, bpPath string, mod bluepr data.fillInData(config, bpPath, mod) return data } + +// Normalize the path for testing. +// +// If the path is relative to the build directory then return the relative path +// to avoid tests having to deal with the dynamically generated build directory. +// +// Otherwise, return the supplied path as it is almost certainly a source path +// that is relative to the root of the source tree. +// +// The build and source paths should be distinguishable based on their contents. +func NormalizePathForTesting(path Path) string { + p := path.String() + if w, ok := path.(WritablePath); ok { + rel, err := filepath.Rel(w.buildDir(), p) + if err != nil { + panic(err) + } + return rel + } + return p +} + +func NormalizePathsForTesting(paths Paths) []string { + var result []string + for _, path := range paths { + relative := NormalizePathForTesting(path) + result = append(result, relative) + } + return result +} |