Merge "Allow customization of the env configuration in a test fixture"

This commit is contained in:
Paul Duffin 2021-03-10 08:17:14 +00:00 committed by Gerrit Code Review
commit 6f3d6df708
1 changed files with 27 additions and 0 deletions

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.
//