Merge "Prevent mock filesystem files being overridden by accident" am: 412a209d87 am: a517bddd85

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

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Ifbd40ef893086f613239e657a8130224e8fd33db
This commit is contained in:
Paul Duffin 2021-03-12 09:02:44 +00:00 committed by Automerger Merge Worker
commit 53b805d276
2 changed files with 34 additions and 2 deletions

View File

@ -237,8 +237,14 @@ func NewFixtureFactory(buildDirSupplier *string, preparers ...FixturePreparer) F
// A set of mock files to add to the mock file system.
type MockFS map[string][]byte
// Merge adds the extra entries from the supplied map to this one.
//
// Fails if the supplied map files with the same paths are present in both of them.
func (fs MockFS) Merge(extra map[string][]byte) {
for p, c := range extra {
if _, ok := fs[p]; ok {
panic(fmt.Errorf("attempted to add file %s to the mock filesystem but it already exists", p))
}
fs[p] = c
}
}
@ -289,17 +295,40 @@ func FixtureMergeMockFs(mockFS MockFS) FixturePreparer {
}
// Add a file to the mock filesystem
//
// Fail if the filesystem already contains a file with that path, use FixtureOverrideFile instead.
func FixtureAddFile(path string, contents []byte) FixturePreparer {
return FixtureModifyMockFS(func(fs MockFS) {
if _, ok := fs[path]; ok {
panic(fmt.Errorf("attempted to add file %s to the mock filesystem but it already exists, use FixtureOverride*File instead", path))
}
fs[path] = contents
})
}
// Add a text file to the mock filesystem
//
// Fail if the filesystem already contains a file with that path.
func FixtureAddTextFile(path string, contents string) FixturePreparer {
return FixtureAddFile(path, []byte(contents))
}
// Override a file in the mock filesystem
//
// If the file does not exist this behaves as FixtureAddFile.
func FixtureOverrideFile(path string, contents []byte) FixturePreparer {
return FixtureModifyMockFS(func(fs MockFS) {
fs[path] = contents
})
}
// Override a text file in the mock filesystem
//
// If the file does not exist this behaves as FixtureAddTextFile.
func FixtureOverrideTextFile(path string, contents string) FixturePreparer {
return FixtureOverrideFile(path, []byte(contents))
}
// Add the root Android.bp file with the supplied contents.
func FixtureWithRootAndroidBp(contents string) FixturePreparer {
return FixtureAddTextFile("Android.bp", contents)

View File

@ -649,8 +649,11 @@ var PrepareForTestOnWindows = android.GroupFixturePreparers(
// The preparer to include if running a cc related test for linux bionic.
var PrepareForTestOnLinuxBionic = android.GroupFixturePreparers(
// Enable linux bionic.
android.FixtureAddTextFile(linuxBionicDefaultsPath, withLinuxBionic()),
// Enable linux bionic
//
// Can be used after PrepareForTestWithCcDefaultModules to override its default behavior of
// disabling linux bionic, hence why this uses FixtureOverrideTextFile.
android.FixtureOverrideTextFile(linuxBionicDefaultsPath, withLinuxBionic()),
)
// The preparer to include if running a cc related test for fuchsia.