Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a517bddd authored by Paul Duffin's avatar Paul Duffin Committed by Automerger Merge Worker
Browse files

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

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

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I6f27928b7142c141c91ebd6b6a2b2fa3ea27c572
parents b1ef3e2c 412a209d
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -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)
+5 −2
Original line number Diff line number Diff line
@@ -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.