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

Commit 98be1bb0 authored by Colin Cross's avatar Colin Cross
Browse files

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
parent 572aeed6
Loading
Loading
Loading
Loading
+7 −11
Original line number Diff line number Diff line
@@ -43,14 +43,6 @@ func customModuleFactory() Module {
}

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 @@ func TestAndroidMkSingleton_PassesUpdatedAndroidMkDataToCustomCallback(t *testin
	}
	`

	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)
+3 −7
Original line number Diff line number Diff line
@@ -289,10 +289,6 @@ func TestArchMutator(t *testing.T) {
		}
	`

	mockFS := map[string][]byte{
		"Android.bp": []byte(bp),
	}

	testCases := []struct {
		name        string
		config      func(Config)
@@ -337,11 +333,11 @@ func TestArchMutator(t *testing.T) {

	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)
			}
+46 −7
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@ import (
	"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 @@ type config struct {

	stopBefore bootstrap.StopBefore

	fs         pathtools.FileSystem
	mockBpList string

	OncePer
}

@@ -200,7 +205,7 @@ func saveToConfigFile(config jsonConfigurable, filename string) error {
}

// 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 @@ func TestConfig(buildDir string, env map[string]string) Config {
	}
	config.TestProductVariables = &config.productVariables

	config.mockFileSystem(bp, fs)

	if err := config.fromEnv(); err != nil {
		panic(err)
	}
@@ -238,8 +245,8 @@ func TestConfig(buildDir string, env map[string]string) Config {
	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 @@ func TestArchConfigNativeBridge(buildDir string, env map[string]string) Config {
	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 @@ func TestArchConfigFuchsia(buildDir string, env map[string]string) Config {
}

// 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 @@ func NewConfig(srcDir, buildDir string) (Config, error) {
		srcDir:            srcDir,
		buildDir:          buildDir,
		multilibConflicts: make(map[ArchType]bool),

		fs: pathtools.OsFs,
	}

	config.deviceConfig = &deviceConfig{
@@ -387,6 +396,36 @@ func NewConfig(srcDir, buildDir string) (Config, error) {
	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":
+2 −6
Original line number Diff line number Diff line
@@ -19,15 +19,11 @@ import (
)

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)
+12 −16
Original line number Diff line number Diff line
@@ -58,19 +58,6 @@ func defaultsTestDefaultsFactory() Module {
}

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 @@ func TestDefaultsAllowMissingDependencies(t *testing.T) {
		}
	`

	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)
Loading