Allow customization of the env configuration in a test fixture

Adds FixtureModifyEnv and FixtureMergeEnv.

Bug: 181070625
Test: m nothing
Change-Id: I1b6eb88907efa2476d96912961fea2df2d902659
This commit is contained in:
Paul Duffin
2021-03-03 00:44:00 +00:00
parent ec3292bebe
commit bbccfcfb70

View File

@@ -15,6 +15,7 @@
package android
import (
"fmt"
"reflect"
"strings"
"testing"
@@ -286,6 +287,32 @@ func FixtureWithRootAndroidBp(contents string) FixturePreparer {
return FixtureAddTextFile("Android.bp", contents)
}
// Merge some environment variables into the fixture.
func FixtureMergeEnv(env map[string]string) FixturePreparer {
return FixtureModifyConfig(func(config Config) {
for k, v := range env {
if k == "PATH" {
panic("Cannot set PATH environment variable")
}
config.env[k] = v
}
})
}
// Modify the env.
//
// Will panic if the mutator changes the PATH environment variable.
func FixtureModifyEnv(mutator func(env map[string]string)) FixturePreparer {
return FixtureModifyConfig(func(config Config) {
oldPath := config.env["PATH"]
mutator(config.env)
newPath := config.env["PATH"]
if newPath != oldPath {
panic(fmt.Errorf("Cannot change PATH environment variable from %q to %q", oldPath, newPath))
}
})
}
// GroupFixturePreparers creates a composite FixturePreparer that is equivalent to applying each of
// the supplied FixturePreparer instances in order.
//