Merge "Prevent mock filesystem files being overridden by accident"

This commit is contained in:
Paul Duffin
2021-03-12 08:04:43 +00:00
committed by Gerrit Code Review
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. // A set of mock files to add to the mock file system.
type MockFS map[string][]byte 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) { func (fs MockFS) Merge(extra map[string][]byte) {
for p, c := range extra { 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 fs[p] = c
} }
} }
@@ -289,17 +295,40 @@ func FixtureMergeMockFs(mockFS MockFS) FixturePreparer {
} }
// Add a file to the mock filesystem // 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 { func FixtureAddFile(path string, contents []byte) FixturePreparer {
return FixtureModifyMockFS(func(fs MockFS) { 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 fs[path] = contents
}) })
} }
// Add a text file to the mock filesystem // 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 { func FixtureAddTextFile(path string, contents string) FixturePreparer {
return FixtureAddFile(path, []byte(contents)) 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. // Add the root Android.bp file with the supplied contents.
func FixtureWithRootAndroidBp(contents string) FixturePreparer { func FixtureWithRootAndroidBp(contents string) FixturePreparer {
return FixtureAddTextFile("Android.bp", contents) 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. // The preparer to include if running a cc related test for linux bionic.
var PrepareForTestOnLinuxBionic = android.GroupFixturePreparers( var PrepareForTestOnLinuxBionic = android.GroupFixturePreparers(
// Enable linux bionic. // Enable linux bionic
android.FixtureAddTextFile(linuxBionicDefaultsPath, withLinuxBionic()), //
// 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. // The preparer to include if running a cc related test for fuchsia.