Move filesystem into Config

The filesystem object was available through ModuleContext.Fs(), but
gives too much access to the filesystem without enforicing correct
dependencies.  In order to support sandboxing the soong_build
process move the filesystem into the Config.  The next change will
make it private.

Bug: 146437378
Test: all Soong tests
Change-Id: I5d3ae9108f120fd335b21efd612aefa078378813
diff --git a/android/androidmk_test.go b/android/androidmk_test.go
index 940e324..71f8020 100644
--- a/android/androidmk_test.go
+++ b/android/androidmk_test.go
@@ -43,14 +43,6 @@
 }
 
 func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testing.T) {
-	config := TestConfig(buildDir, nil)
-	config.inMake = true // Enable androidmk Singleton
-
-	ctx := NewTestContext()
-	ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
-	ctx.RegisterModuleType("custom", customModuleFactory)
-	ctx.Register()
-
 	bp := `
 	custom {
 		name: "foo",
@@ -60,9 +52,13 @@
 	}
 	`
 
-	ctx.MockFileSystem(map[string][]byte{
-		"Android.bp": []byte(bp),
-	})
+	config := TestConfig(buildDir, nil, bp, nil)
+	config.inMake = true // Enable androidmk Singleton
+
+	ctx := NewTestContext()
+	ctx.RegisterSingletonType("androidmk", AndroidMkSingleton)
+	ctx.RegisterModuleType("custom", customModuleFactory)
+	ctx.Register(config)
 
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
diff --git a/android/arch_test.go b/android/arch_test.go
index b41e1ab..98b0534 100644
--- a/android/arch_test.go
+++ b/android/arch_test.go
@@ -289,10 +289,6 @@
 		}
 	`
 
-	mockFS := map[string][]byte{
-		"Android.bp": []byte(bp),
-	}
-
 	testCases := []struct {
 		name        string
 		config      func(Config)
@@ -337,11 +333,11 @@
 
 	for _, tt := range testCases {
 		t.Run(tt.name, func(t *testing.T) {
+			config := TestArchConfig(buildDir, nil, bp, nil)
+
 			ctx := NewTestArchContext()
 			ctx.RegisterModuleType("module", archTestModuleFactory)
-			ctx.MockFileSystem(mockFS)
-			ctx.Register()
-			config := TestArchConfig(buildDir, nil)
+			ctx.Register(config)
 			if tt.config != nil {
 				tt.config(config)
 			}
diff --git a/android/config.go b/android/config.go
index e1db55d..a9833da 100644
--- a/android/config.go
+++ b/android/config.go
@@ -25,7 +25,9 @@
 	"strings"
 	"sync"
 
+	"github.com/google/blueprint"
 	"github.com/google/blueprint/bootstrap"
+	"github.com/google/blueprint/pathtools"
 	"github.com/google/blueprint/proptools"
 )
 
@@ -115,6 +117,9 @@
 
 	stopBefore bootstrap.StopBefore
 
+	fs         pathtools.FileSystem
+	mockBpList string
+
 	OncePer
 }
 
@@ -200,7 +205,7 @@
 }
 
 // TestConfig returns a Config object suitable for using for tests
-func TestConfig(buildDir string, env map[string]string) Config {
+func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
 	envCopy := make(map[string]string)
 	for k, v := range env {
 		envCopy[k] = v
@@ -231,6 +236,8 @@
 	}
 	config.TestProductVariables = &config.productVariables
 
+	config.mockFileSystem(bp, fs)
+
 	if err := config.fromEnv(); err != nil {
 		panic(err)
 	}
@@ -238,8 +245,8 @@
 	return Config{config}
 }
 
-func TestArchConfigNativeBridge(buildDir string, env map[string]string) Config {
-	testConfig := TestArchConfig(buildDir, env)
+func TestArchConfigNativeBridge(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
+	testConfig := TestArchConfig(buildDir, env, bp, fs)
 	config := testConfig.config
 
 	config.Targets[Android] = []Target{
@@ -252,8 +259,8 @@
 	return testConfig
 }
 
-func TestArchConfigFuchsia(buildDir string, env map[string]string) Config {
-	testConfig := TestConfig(buildDir, env)
+func TestArchConfigFuchsia(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
+	testConfig := TestConfig(buildDir, env, bp, fs)
 	config := testConfig.config
 
 	config.Targets = map[OsType][]Target{
@@ -269,8 +276,8 @@
 }
 
 // TestConfig returns a Config object suitable for using for tests that need to run the arch mutator
-func TestArchConfig(buildDir string, env map[string]string) Config {
-	testConfig := TestConfig(buildDir, env)
+func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
+	testConfig := TestConfig(buildDir, env, bp, fs)
 	config := testConfig.config
 
 	config.Targets = map[OsType][]Target{
@@ -312,6 +319,8 @@
 		srcDir:            srcDir,
 		buildDir:          buildDir,
 		multilibConflicts: make(map[ArchType]bool),
+
+		fs: pathtools.OsFs,
 	}
 
 	config.deviceConfig = &deviceConfig{
@@ -387,6 +396,36 @@
 	return Config{config}, nil
 }
 
+// mockFileSystem replaces all reads with accesses to the provided map of
+// filenames to contents stored as a byte slice.
+func (c *config) mockFileSystem(bp string, fs map[string][]byte) {
+	mockFS := map[string][]byte{}
+
+	if _, exists := mockFS["Android.bp"]; !exists {
+		mockFS["Android.bp"] = []byte(bp)
+	}
+
+	for k, v := range fs {
+		mockFS[k] = v
+	}
+
+	// no module list file specified; find every file named Blueprints or Android.bp
+	pathsToParse := []string{}
+	for candidate := range mockFS {
+		base := filepath.Base(candidate)
+		if base == "Blueprints" || base == "Android.bp" {
+			pathsToParse = append(pathsToParse, candidate)
+		}
+	}
+	if len(pathsToParse) < 1 {
+		panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", mockFS))
+	}
+	mockFS[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
+
+	c.fs = pathtools.MockFs(mockFS)
+	c.mockBpList = blueprint.MockModuleListFile
+}
+
 func (c *config) fromEnv() error {
 	switch c.Getenv("EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9") {
 	case "", "true":
diff --git a/android/csuite_config_test.go b/android/csuite_config_test.go
index 5f86bbb..bf1a19a 100644
--- a/android/csuite_config_test.go
+++ b/android/csuite_config_test.go
@@ -19,15 +19,11 @@
 )
 
 func testCSuiteConfig(test *testing.T, bpFileContents string) *TestContext {
-	config := TestArchConfig(buildDir, nil)
+	config := TestArchConfig(buildDir, nil, bpFileContents, nil)
 
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("csuite_config", CSuiteConfigFactory)
-	ctx.Register()
-	mockFiles := map[string][]byte{
-		"Android.bp": []byte(bpFileContents),
-	}
-	ctx.MockFileSystem(mockFiles)
+	ctx.Register(config)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(test, errs)
 	_, errs = ctx.PrepareBuildActions(config)
diff --git a/android/defaults_test.go b/android/defaults_test.go
index 80980f7..ba607ef 100644
--- a/android/defaults_test.go
+++ b/android/defaults_test.go
@@ -58,19 +58,6 @@
 }
 
 func TestDefaultsAllowMissingDependencies(t *testing.T) {
-	config := TestConfig(buildDir, nil)
-	config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
-
-	ctx := NewTestContext()
-	ctx.SetAllowMissingDependencies(true)
-
-	ctx.RegisterModuleType("test", defaultsTestModuleFactory)
-	ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
-
-	ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
-
-	ctx.Register()
-
 	bp := `
 		defaults {
 			name: "defaults",
@@ -91,9 +78,18 @@
 		}
 	`
 
-	ctx.MockFileSystem(map[string][]byte{
-		"Android.bp": []byte(bp),
-	})
+	config := TestConfig(buildDir, nil, bp, nil)
+	config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
+
+	ctx := NewTestContext()
+	ctx.SetAllowMissingDependencies(true)
+
+	ctx.RegisterModuleType("test", defaultsTestModuleFactory)
+	ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory)
+
+	ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
+
+	ctx.Register(config)
 
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
diff --git a/android/module_test.go b/android/module_test.go
index fef1766..6e648d7 100644
--- a/android/module_test.go
+++ b/android/module_test.go
@@ -178,15 +178,9 @@
 		}
 	`
 
-	mockFS := map[string][]byte{
-		"Android.bp": []byte(bp),
-	}
+	config := TestConfig(buildDir, nil, bp, nil)
 
-	ctx.MockFileSystem(mockFS)
-
-	ctx.Register()
-
-	config := TestConfig(buildDir, nil)
+	ctx.Register(config)
 
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
diff --git a/android/mutator_test.go b/android/mutator_test.go
index 2350fdb..d179f9d 100644
--- a/android/mutator_test.go
+++ b/android/mutator_test.go
@@ -56,7 +56,15 @@
 }
 
 func TestMutatorAddMissingDependencies(t *testing.T) {
-	config := TestConfig(buildDir, nil)
+	bp := `
+		test {
+			name: "foo",
+			deps_missing_deps: ["regular_missing_dep"],
+			mutator_missing_deps: ["added_missing_dep"],
+		}
+	`
+
+	config := TestConfig(buildDir, nil, bp, nil)
 	config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
 
 	ctx := NewTestContext()
@@ -67,21 +75,7 @@
 		ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator)
 	})
 
-	bp := `
-		test {
-			name: "foo",
-			deps_missing_deps: ["regular_missing_dep"],
-			mutator_missing_deps: ["added_missing_dep"],
-		}
-	`
-
-	mockFS := map[string][]byte{
-		"Android.bp": []byte(bp),
-	}
-
-	ctx.MockFileSystem(mockFS)
-
-	ctx.Register()
+	ctx.Register(config)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
 	_, errs = ctx.PrepareBuildActions(config)
@@ -139,15 +133,9 @@
 		}
 	`
 
-	mockFS := map[string][]byte{
-		"Android.bp": []byte(bp),
-	}
+	config := TestConfig(buildDir, nil, bp, nil)
 
-	ctx.MockFileSystem(mockFS)
-
-	ctx.Register()
-
-	config := TestConfig(buildDir, nil)
+	ctx.Register(config)
 
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
diff --git a/android/namespace_test.go b/android/namespace_test.go
index 90058e3..66c0d89 100644
--- a/android/namespace_test.go
+++ b/android/namespace_test.go
@@ -633,10 +633,9 @@
 }
 
 func setupTestFromFiles(bps map[string][]byte) (ctx *TestContext, errs []error) {
-	config := TestConfig(buildDir, nil)
+	config := TestConfig(buildDir, nil, "", bps)
 
 	ctx = NewTestContext()
-	ctx.MockFileSystem(bps)
 	ctx.RegisterModuleType("test_module", newTestModule)
 	ctx.RegisterModuleType("soong_namespace", NamespaceFactory)
 	ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule)
@@ -644,7 +643,7 @@
 	ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
 		ctx.BottomUp("rename", renameMutator)
 	})
-	ctx.Register()
+	ctx.Register(config)
 
 	_, errs = ctx.ParseBlueprintsFiles("Android.bp")
 	if len(errs) > 0 {
diff --git a/android/neverallow_test.go b/android/neverallow_test.go
index bd94e37..6f07a4a 100644
--- a/android/neverallow_test.go
+++ b/android/neverallow_test.go
@@ -44,11 +44,11 @@
 			NeverAllow().InDirectDeps("not_allowed_in_direct_deps"),
 		},
 		fs: map[string][]byte{
-			"top/Blueprints": []byte(`
+			"top/Android.bp": []byte(`
 				cc_library {
 					name: "not_allowed_in_direct_deps",
 				}`),
-			"other/Blueprints": []byte(`
+			"other/Android.bp": []byte(`
 				cc_library {
 					name: "libother",
 					static_libs: ["not_allowed_in_direct_deps"],
@@ -65,7 +65,7 @@
 	{
 		name: "include_dir not allowed to reference art",
 		fs: map[string][]byte{
-			"other/Blueprints": []byte(`
+			"other/Android.bp": []byte(`
 				cc_library {
 					name: "libother",
 					include_dirs: ["art/libdexfile/include"],
@@ -78,7 +78,7 @@
 	{
 		name: "include_dir can reference another location",
 		fs: map[string][]byte{
-			"other/Blueprints": []byte(`
+			"other/Android.bp": []byte(`
 				cc_library {
 					name: "libother",
 					include_dirs: ["another/include"],
@@ -89,7 +89,7 @@
 	{
 		name: "no vndk.enabled under vendor directory",
 		fs: map[string][]byte{
-			"vendor/Blueprints": []byte(`
+			"vendor/Android.bp": []byte(`
 				cc_library {
 					name: "libvndk",
 					vendor_available: true,
@@ -105,7 +105,7 @@
 	{
 		name: "no vndk.enabled under device directory",
 		fs: map[string][]byte{
-			"device/Blueprints": []byte(`
+			"device/Android.bp": []byte(`
 				cc_library {
 					name: "libvndk",
 					vendor_available: true,
@@ -121,7 +121,7 @@
 	{
 		name: "vndk-ext under vendor or device directory",
 		fs: map[string][]byte{
-			"device/Blueprints": []byte(`
+			"device/Android.bp": []byte(`
 				cc_library {
 					name: "libvndk1_ext",
 					vendor: true,
@@ -129,7 +129,7 @@
 						enabled: true,
 					},
 				}`),
-			"vendor/Blueprints": []byte(`
+			"vendor/Android.bp": []byte(`
 				cc_library {
 					name: "libvndk2_ext",
 					vendor: true,
@@ -143,7 +143,7 @@
 	{
 		name: "no enforce_vintf_manifest.cflags",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				cc_library {
 					name: "libexample",
 					product_variables: {
@@ -161,7 +161,7 @@
 	{
 		name: "no treble_linker_namespaces.cflags",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				cc_library {
 					name: "libexample",
 					product_variables: {
@@ -178,7 +178,7 @@
 	{
 		name: "libc_bionic_ndk treble_linker_namespaces.cflags",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				cc_library {
 					name: "libc_bionic_ndk",
 					product_variables: {
@@ -192,7 +192,7 @@
 	{
 		name: "dependency on updatable-media",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				java_library {
 					name: "needs_updatable_media",
 					libs: ["updatable-media"],
@@ -205,7 +205,7 @@
 	{
 		name: "java_device_for_host",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				java_device_for_host {
 					name: "device_for_host",
 					libs: ["core-libart"],
@@ -219,7 +219,7 @@
 	{
 		name: "sdk_version: \"none\" inside core libraries",
 		fs: map[string][]byte{
-			"libcore/Blueprints": []byte(`
+			"libcore/Android.bp": []byte(`
 				java_library {
 					name: "inside_core_libraries",
 					sdk_version: "none",
@@ -229,7 +229,7 @@
 	{
 		name: "sdk_version: \"none\" outside core libraries",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				java_library {
 					name: "outside_core_libraries",
 					sdk_version: "none",
@@ -242,7 +242,7 @@
 	{
 		name: "sdk_version: \"current\"",
 		fs: map[string][]byte{
-			"Blueprints": []byte(`
+			"Android.bp": []byte(`
 				java_library {
 					name: "outside_core_libraries",
 					sdk_version: "current",
@@ -254,31 +254,29 @@
 func TestNeverallow(t *testing.T) {
 	for _, test := range neverallowTests {
 		// Create a test per config to allow for test specific config, e.g. test rules.
-		config := TestConfig(buildDir, nil)
+		config := TestConfig(buildDir, nil, "", test.fs)
 
 		t.Run(test.name, func(t *testing.T) {
 			// If the test has its own rules then use them instead of the default ones.
 			if test.rules != nil {
 				setTestNeverallowRules(config, test.rules)
 			}
-			_, errs := testNeverallow(config, test.fs)
+			_, errs := testNeverallow(config)
 			CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
 		})
 	}
 }
 
-func testNeverallow(config Config, fs map[string][]byte) (*TestContext, []error) {
+func testNeverallow(config Config) (*TestContext, []error) {
 	ctx := NewTestContext()
 	ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
 	ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
 	ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
 	ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
 	ctx.PostDepsMutators(registerNeverallowMutator)
-	ctx.Register()
+	ctx.Register(config)
 
-	ctx.MockFileSystem(fs)
-
-	_, errs := ctx.ParseBlueprintsFiles("Blueprints")
+	_, errs := ctx.ParseBlueprintsFiles("Android.bp")
 	if len(errs) > 0 {
 		return ctx, errs
 	}
diff --git a/android/package_test.go b/android/package_test.go
index 8071c51..bc66928 100644
--- a/android/package_test.go
+++ b/android/package_test.go
@@ -84,14 +84,12 @@
 func testPackage(fs map[string][]byte) (*TestContext, []error) {
 
 	// Create a new config per test as visibility information is stored in the config.
-	config := TestArchConfig(buildDir, nil)
+	config := TestArchConfig(buildDir, nil, "", fs)
 
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("package", PackageFactory)
 	ctx.PreArchMutators(RegisterPackageRenamer)
-	ctx.Register()
-
-	ctx.MockFileSystem(fs)
+	ctx.Register(config)
 
 	_, errs := ctx.ParseBlueprintsFiles(".")
 	if len(errs) > 0 {
diff --git a/android/path_properties_test.go b/android/path_properties_test.go
index c859bc5..f367b82 100644
--- a/android/path_properties_test.go
+++ b/android/path_properties_test.go
@@ -97,12 +97,6 @@
 
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {
-			config := TestArchConfig(buildDir, nil)
-			ctx := NewTestArchContext()
-
-			ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
-			ctx.RegisterModuleType("filegroup", FileGroupFactory)
-
 			bp := test.bp + `
 				filegroup {
 					name: "a",
@@ -121,13 +115,13 @@
 				}
 			`
 
-			mockFS := map[string][]byte{
-				"Android.bp": []byte(bp),
-			}
+			config := TestArchConfig(buildDir, nil, bp, nil)
+			ctx := NewTestArchContext()
 
-			ctx.MockFileSystem(mockFS)
+			ctx.RegisterModuleType("test", pathDepsMutatorTestModuleFactory)
+			ctx.RegisterModuleType("filegroup", FileGroupFactory)
 
-			ctx.Register()
+			ctx.Register(config)
 			_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 			FailIfErrored(t, errs)
 			_, errs = ctx.PrepareBuildActions(config)
diff --git a/android/paths.go b/android/paths.go
index 85c861d..a04dc6b 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -1353,19 +1353,17 @@
 
 type testPathContext struct {
 	config Config
-	fs     pathtools.FileSystem
 }
 
-func (x *testPathContext) Fs() pathtools.FileSystem   { return x.fs }
+func (x *testPathContext) Fs() pathtools.FileSystem   { return x.config.fs }
 func (x *testPathContext) Config() Config             { return x.config }
 func (x *testPathContext) AddNinjaFileDeps(...string) {}
 
 // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
 // PathForOutput.
-func PathContextForTesting(config Config, fs map[string][]byte) PathContext {
+func PathContextForTesting(config Config) PathContext {
 	return &testPathContext{
 		config: config,
-		fs:     pathtools.MockFs(fs),
 	}
 }
 
diff --git a/android/paths_test.go b/android/paths_test.go
index a8560a1..5ff5f99 100644
--- a/android/paths_test.go
+++ b/android/paths_test.go
@@ -241,8 +241,12 @@
 	return false
 }
 
+func pathTestConfig(buildDir string) Config {
+	return TestConfig(buildDir, nil, "", nil)
+}
+
 func TestPathForModuleInstall(t *testing.T) {
-	testConfig := TestConfig("", nil)
+	testConfig := pathTestConfig("")
 
 	hostTarget := Target{Os: Linux}
 	deviceTarget := Target{Os: Android}
@@ -579,18 +583,19 @@
 }
 
 func TestDirectorySortedPaths(t *testing.T) {
-	config := TestConfig("out", nil)
-
-	ctx := PathContextForTesting(config, map[string][]byte{
-		"a.txt":   nil,
-		"a/txt":   nil,
-		"a/b/c":   nil,
-		"a/b/d":   nil,
-		"b":       nil,
-		"b/b.txt": nil,
-		"a/a.txt": nil,
+	config := TestConfig("out", nil, "", map[string][]byte{
+		"Android.bp": nil,
+		"a.txt":      nil,
+		"a/txt":      nil,
+		"a/b/c":      nil,
+		"a/b/d":      nil,
+		"b":          nil,
+		"b/b.txt":    nil,
+		"a/a.txt":    nil,
 	})
 
+	ctx := PathContextForTesting(config)
+
 	makePaths := func() Paths {
 		return Paths{
 			PathForSource(ctx, "a.txt"),
@@ -754,7 +759,7 @@
 		t.Run(f.name, func(t *testing.T) {
 			for _, test := range testCases {
 				t.Run(test.name, func(t *testing.T) {
-					testConfig := TestConfig(test.buildDir, nil)
+					testConfig := pathTestConfig(test.buildDir)
 					ctx := &configErrorWrapper{config: testConfig}
 					_, err := f.f(ctx, test.src)
 					if len(ctx.errors) > 0 {
@@ -886,7 +891,6 @@
 func testPathForModuleSrc(t *testing.T, buildDir string, tests []pathForModuleSrcTestCase) {
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {
-			config := TestConfig(buildDir, nil)
 			ctx := NewTestContext()
 
 			ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
@@ -920,9 +924,9 @@
 				"foo/src_special/$": nil,
 			}
 
-			ctx.MockFileSystem(mockFS)
+			config := TestConfig(buildDir, nil, "", mockFS)
 
-			ctx.Register()
+			ctx.Register(config)
 			_, errs := ctx.ParseFileList(".", []string{"fg/Android.bp", "foo/Android.bp", "ofp/Android.bp"})
 			FailIfErrored(t, errs)
 			_, errs = ctx.PrepareBuildActions(config)
@@ -1097,14 +1101,6 @@
 }
 
 func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) {
-	config := TestConfig(buildDir, nil)
-	config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
-
-	ctx := NewTestContext()
-	ctx.SetAllowMissingDependencies(true)
-
-	ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
-
 	bp := `
 		test {
 			name: "foo",
@@ -1121,13 +1117,16 @@
 		}
 	`
 
-	mockFS := map[string][]byte{
-		"Android.bp": []byte(bp),
-	}
+	config := TestConfig(buildDir, nil, bp, nil)
+	config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
 
-	ctx.MockFileSystem(mockFS)
+	ctx := NewTestContext()
+	ctx.SetAllowMissingDependencies(true)
 
-	ctx.Register()
+	ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
+
+	ctx.Register(config)
+
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
 	_, errs = ctx.PrepareBuildActions(config)
@@ -1160,7 +1159,7 @@
 
 func ExampleOutputPath_ReplaceExtension() {
 	ctx := &configErrorWrapper{
-		config: TestConfig("out", nil),
+		config: TestConfig("out", nil, "", nil),
 	}
 	p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
 	p2 := p.ReplaceExtension(ctx, "oat")
@@ -1174,7 +1173,7 @@
 
 func ExampleOutputPath_FileInSameDir() {
 	ctx := &configErrorWrapper{
-		config: TestConfig("out", nil),
+		config: TestConfig("out", nil, "", nil),
 	}
 	p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
 	p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex")
diff --git a/android/prebuilt_etc_test.go b/android/prebuilt_etc_test.go
index 3c466a1..6e751e7 100644
--- a/android/prebuilt_etc_test.go
+++ b/android/prebuilt_etc_test.go
@@ -21,7 +21,14 @@
 )
 
 func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
-	config := TestArchConfig(buildDir, nil)
+	fs := map[string][]byte{
+		"foo.conf": nil,
+		"bar.conf": nil,
+		"baz.conf": nil,
+	}
+
+	config := TestArchConfig(buildDir, nil, bp, fs)
+
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
 	ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
@@ -29,14 +36,7 @@
 	ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
 	ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
 	ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
-	ctx.Register()
-	mockFiles := map[string][]byte{
-		"Android.bp": []byte(bp),
-		"foo.conf":   nil,
-		"bar.conf":   nil,
-		"baz.conf":   nil,
-	}
-	ctx.MockFileSystem(mockFiles)
+	ctx.Register(config)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
 	_, errs = ctx.PrepareBuildActions(config)
diff --git a/android/prebuilt_test.go b/android/prebuilt_test.go
index 81fb278..600f078 100644
--- a/android/prebuilt_test.go
+++ b/android/prebuilt_test.go
@@ -125,29 +125,30 @@
 }
 
 func TestPrebuilts(t *testing.T) {
-	config := TestConfig(buildDir, nil)
+	fs := map[string][]byte{
+		"prebuilt_file": nil,
+		"source_file":   nil,
+	}
 
 	for _, test := range prebuiltsTests {
 		t.Run(test.name, func(t *testing.T) {
+			bp := `
+				source {
+					name: "foo",
+					deps: [":bar"],
+				}
+				` + test.modules
+			config := TestConfig(buildDir, nil, bp, fs)
+
 			ctx := NewTestContext()
 			ctx.PreArchMutators(RegisterPrebuiltsPreArchMutators)
 			ctx.PostDepsMutators(RegisterPrebuiltsPostDepsMutators)
 			ctx.RegisterModuleType("filegroup", FileGroupFactory)
 			ctx.RegisterModuleType("prebuilt", newPrebuiltModule)
 			ctx.RegisterModuleType("source", newSourceModule)
-			ctx.Register()
-			ctx.MockFileSystem(map[string][]byte{
-				"prebuilt_file": nil,
-				"source_file":   nil,
-				"Blueprints": []byte(`
-					source {
-						name: "foo",
-						deps: [":bar"],
-					}
-					` + test.modules),
-			})
+			ctx.Register(config)
 
-			_, errs := ctx.ParseBlueprintsFiles("Blueprints")
+			_, errs := ctx.ParseBlueprintsFiles("Android.bp")
 			FailIfErrored(t, errs)
 			_, errs = ctx.PrepareBuildActions(config)
 			FailIfErrored(t, errs)
diff --git a/android/rule_builder_test.go b/android/rule_builder_test.go
index 52c32df..acf8127 100644
--- a/android/rule_builder_test.go
+++ b/android/rule_builder_test.go
@@ -27,19 +27,18 @@
 )
 
 func pathContext() PathContext {
-	return PathContextForTesting(TestConfig("out", nil),
-		map[string][]byte{
-			"ld":      nil,
-			"a.o":     nil,
-			"b.o":     nil,
-			"cp":      nil,
-			"a":       nil,
-			"b":       nil,
-			"ls":      nil,
-			"turbine": nil,
-			"java":    nil,
-			"javac":   nil,
-		})
+	return PathContextForTesting(TestConfig("out", nil, "", map[string][]byte{
+		"ld":      nil,
+		"a.o":     nil,
+		"b.o":     nil,
+		"cp":      nil,
+		"a":       nil,
+		"b":       nil,
+		"ls":      nil,
+		"turbine": nil,
+		"java":    nil,
+		"javac":   nil,
+	}))
 }
 
 func ExampleRuleBuilder() {
@@ -276,7 +275,7 @@
 		"input3":    nil,
 	}
 
-	ctx := PathContextForTesting(TestConfig("out", nil), fs)
+	ctx := PathContextForTesting(TestConfig("out", nil, "", fs))
 
 	addCommands := func(rule *RuleBuilder) {
 		cmd := rule.Command().
@@ -445,6 +444,11 @@
 }
 
 func TestRuleBuilder_Build(t *testing.T) {
+	fs := map[string][]byte{
+		"bar": nil,
+		"cp":  nil,
+	}
+
 	bp := `
 		rule_builder_test {
 			name: "foo",
@@ -458,16 +462,11 @@
 		}
 	`
 
-	config := TestConfig(buildDir, nil)
+	config := TestConfig(buildDir, nil, bp, fs)
 	ctx := NewTestContext()
-	ctx.MockFileSystem(map[string][]byte{
-		"Android.bp": []byte(bp),
-		"bar":        nil,
-		"cp":         nil,
-	})
 	ctx.RegisterModuleType("rule_builder_test", testRuleBuilderFactory)
 	ctx.RegisterSingletonType("rule_builder_test", testRuleBuilderSingletonFactory)
-	ctx.Register()
+	ctx.Register(config)
 
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
diff --git a/android/sh_binary_test.go b/android/sh_binary_test.go
index 8488fe4..137e773 100644
--- a/android/sh_binary_test.go
+++ b/android/sh_binary_test.go
@@ -6,19 +6,18 @@
 )
 
 func testShBinary(t *testing.T, bp string) (*TestContext, Config) {
-	config := TestArchConfig(buildDir, nil)
-
-	ctx := NewTestArchContext()
-	ctx.RegisterModuleType("sh_test", ShTestFactory)
-	ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
-	ctx.Register()
-	mockFiles := map[string][]byte{
-		"Android.bp":         []byte(bp),
+	fs := map[string][]byte{
 		"test.sh":            nil,
 		"testdata/data1":     nil,
 		"testdata/sub/data2": nil,
 	}
-	ctx.MockFileSystem(mockFiles)
+
+	config := TestArchConfig(buildDir, nil, bp, fs)
+
+	ctx := NewTestArchContext()
+	ctx.RegisterModuleType("sh_test", ShTestFactory)
+	ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
+	ctx.Register(config)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
 	_, errs = ctx.PrepareBuildActions(config)
diff --git a/android/testing.go b/android/testing.go
index 26f1e4d..aaf98f5 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -16,7 +16,6 @@
 
 import (
 	"fmt"
-	"path/filepath"
 	"regexp"
 	"strings"
 	"testing"
@@ -68,7 +67,11 @@
 	ctx.postDeps = append(ctx.postDeps, f)
 }
 
-func (ctx *TestContext) Register() {
+func (ctx *TestContext) Register(config Config) {
+	ctx.SetFs(config.fs)
+	if config.mockBpList != "" {
+		ctx.SetModuleListFile(config.mockBpList)
+	}
 	registerMutators(ctx.Context.Context, ctx.preArch, ctx.preDeps, ctx.postDeps)
 
 	ctx.RegisterSingletonType("env", EnvSingleton)
@@ -132,25 +135,6 @@
 		"\nall singletons: %v", name, allSingletonNames))
 }
 
-// MockFileSystem causes the Context to replace all reads with accesses to the provided map of
-// filenames to contents stored as a byte slice.
-func (ctx *TestContext) MockFileSystem(files map[string][]byte) {
-	// no module list file specified; find every file named Blueprints or Android.bp
-	pathsToParse := []string{}
-	for candidate := range files {
-		base := filepath.Base(candidate)
-		if base == "Blueprints" || base == "Android.bp" {
-			pathsToParse = append(pathsToParse, candidate)
-		}
-	}
-	if len(pathsToParse) < 1 {
-		panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", files))
-	}
-	files[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
-
-	ctx.Context.MockFileSystem(files)
-}
-
 type testBuildProvider interface {
 	BuildParamsForTests() []BuildParams
 	RuleParamsForTests() map[blueprint.Rule]blueprint.RuleParams
diff --git a/android/variable_test.go b/android/variable_test.go
index 1826e39..451d43d 100644
--- a/android/variable_test.go
+++ b/android/variable_test.go
@@ -198,18 +198,11 @@
 			name: "baz",
 		}
 	`
-
-	mockFS := map[string][]byte{
-		"Android.bp": []byte(bp),
-	}
-
-	ctx.MockFileSystem(mockFS)
-
-	ctx.Register()
-
-	config := TestConfig(buildDir, nil)
+	config := TestConfig(buildDir, nil, bp, nil)
 	config.TestProductVariables.Eng = proptools.BoolPtr(true)
 
+	ctx.Register(config)
+
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(t, errs)
 	_, errs = ctx.PrepareBuildActions(config)
diff --git a/android/visibility_test.go b/android/visibility_test.go
index 1984a21..fbf2fb7 100644
--- a/android/visibility_test.go
+++ b/android/visibility_test.go
@@ -868,7 +868,7 @@
 func testVisibility(buildDir string, fs map[string][]byte) (*TestContext, []error) {
 
 	// Create a new config per test as visibility information is stored in the config.
-	config := TestArchConfig(buildDir, nil)
+	config := TestArchConfig(buildDir, nil, "", fs)
 
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("package", PackageFactory)
@@ -879,9 +879,7 @@
 	ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
 	ctx.PreArchMutators(RegisterVisibilityRuleGatherer)
 	ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer)
-	ctx.Register()
-
-	ctx.MockFileSystem(fs)
+	ctx.Register(config)
 
 	_, errs := ctx.ParseBlueprintsFiles(".")
 	if len(errs) > 0 {
diff --git a/android/vts_config_test.go b/android/vts_config_test.go
index 162944d..254fa92 100644
--- a/android/vts_config_test.go
+++ b/android/vts_config_test.go
@@ -19,15 +19,11 @@
 )
 
 func testVtsConfig(test *testing.T, bpFileContents string) *TestContext {
-	config := TestArchConfig(buildDir, nil)
+	config := TestArchConfig(buildDir, nil, bpFileContents, nil)
 
 	ctx := NewTestArchContext()
 	ctx.RegisterModuleType("vts_config", VtsConfigFactory)
-	ctx.Register()
-	mockFiles := map[string][]byte{
-		"Android.bp": []byte(bpFileContents),
-	}
-	ctx.MockFileSystem(mockFiles)
+	ctx.Register(config)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	FailIfErrored(test, errs)
 	_, errs = ctx.PrepareBuildActions(config)