Convert genrule tests to use test fixtures

Bug: 181070625
Test: m nothing
Change-Id: I7dae0aa0e493e2073286360038ddaafa2d90093c
This commit is contained in:
Paul Duffin 2021-03-03 02:30:37 +00:00
parent 46e37741a9
commit 672cb9fbc7
2 changed files with 93 additions and 141 deletions

View File

@ -37,6 +37,27 @@ func init() {
RegisterGenruleBuildComponents(android.InitRegistrationContext) RegisterGenruleBuildComponents(android.InitRegistrationContext)
} }
// Test fixture preparer that will register most genrule build components.
//
// Singletons and mutators should only be added here if they are needed for a majority of genrule
// module types, otherwise they should be added under a separate preparer to allow them to be
// selected only when needed to reduce test execution time.
//
// Module types do not have much of an overhead unless they are used so this should include as many
// module types as possible. The exceptions are those module types that require mutators and/or
// singletons in order to function in which case they should be kept together in a separate
// preparer.
var PrepareForTestWithGenRuleBuildComponents = android.GroupFixturePreparers(
android.FixtureRegisterWithContext(RegisterGenruleBuildComponents),
)
// Prepare a fixture to use all genrule module types, mutators and singletons fully.
//
// This should only be used by tests that want to run with as much of the build enabled as possible.
var PrepareForIntegrationTestWithGenrule = android.GroupFixturePreparers(
PrepareForTestWithGenRuleBuildComponents,
)
func RegisterGenruleBuildComponents(ctx android.RegistrationContext) { func RegisterGenruleBuildComponents(ctx android.RegistrationContext) {
ctx.RegisterModuleType("genrule_defaults", defaultsFactory) ctx.RegisterModuleType("genrule_defaults", defaultsFactory)

View File

@ -17,8 +17,7 @@ package genrule
import ( import (
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "regexp"
"strings"
"testing" "testing"
"android/soong/android" "android/soong/android"
@ -51,22 +50,30 @@ func TestMain(m *testing.M) {
os.Exit(run()) os.Exit(run())
} }
func testContext(config android.Config) *android.TestContext { var genruleFixtureFactory = android.NewFixtureFactory(
&buildDir,
android.PrepareForTestWithArchMutator,
android.PrepareForTestWithDefaults,
ctx := android.NewTestArchContext(config) android.PrepareForTestWithFilegroup,
ctx.RegisterModuleType("filegroup", android.FileGroupFactory) PrepareForTestWithGenRuleBuildComponents,
android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("tool", toolFactory) ctx.RegisterModuleType("tool", toolFactory)
}),
android.FixtureMergeMockFs(android.MockFS{
"tool": nil,
"tool_file1": nil,
"tool_file2": nil,
"in1": nil,
"in2": nil,
"in1.txt": nil,
"in2.txt": nil,
"in3.txt": nil,
}),
)
RegisterGenruleBuildComponents(ctx) func testGenruleBp() string {
return `
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
ctx.Register()
return ctx
}
func testConfig(bp string, fs map[string][]byte) android.Config {
bp += `
tool { tool {
name: "tool", name: "tool",
} }
@ -105,23 +112,6 @@ func testConfig(bp string, fs map[string][]byte) android.Config {
name: "empty", name: "empty",
} }
` `
mockFS := map[string][]byte{
"tool": nil,
"tool_file1": nil,
"tool_file2": nil,
"in1": nil,
"in2": nil,
"in1.txt": nil,
"in2.txt": nil,
"in3.txt": nil,
}
for k, v := range fs {
mockFS[k] = v
}
return android.TestArchConfig(buildDir, nil, bp, mockFS)
} }
func TestGenruleCmd(t *testing.T) { func TestGenruleCmd(t *testing.T) {
@ -466,38 +456,28 @@ func TestGenruleCmd(t *testing.T) {
bp += test.prop bp += test.prop
bp += "}\n" bp += "}\n"
config := testConfig(bp, nil) var expectedErrors []string
config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(test.allowMissingDependencies) if test.err != "" {
expectedErrors = append(expectedErrors, regexp.QuoteMeta(test.err))
}
ctx := testContext(config) result := genruleFixtureFactory.Extend(
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Allow_missing_dependencies = proptools.BoolPtr(test.allowMissingDependencies)
}),
android.FixtureModifyContext(func(ctx *android.TestContext) {
ctx.SetAllowMissingDependencies(test.allowMissingDependencies) ctx.SetAllowMissingDependencies(test.allowMissingDependencies)
}),
).
ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(expectedErrors)).
RunTestWithBp(t, testGenruleBp()+bp)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) if expectedErrors != nil {
if errs == nil {
_, errs = ctx.PrepareBuildActions(config)
}
if errs == nil && test.err != "" {
t.Fatalf("want error %q, got no error", test.err)
} else if errs != nil && test.err == "" {
android.FailIfErrored(t, errs)
} else if test.err != "" {
if len(errs) != 1 {
t.Errorf("want 1 error, got %d errors:", len(errs))
for _, err := range errs {
t.Errorf(" %s", err.Error())
}
t.FailNow()
}
if !strings.Contains(errs[0].Error(), test.err) {
t.Fatalf("want %q, got %q", test.err, errs[0].Error())
}
return return
} }
gen := ctx.ModuleForTests("gen", "").Module().(*Module) gen := result.Module("gen", "").(*Module)
if g, w := gen.rawCommands[0], test.expect; w != g { result.AssertStringEquals("raw commands", test.expect, gen.rawCommands[0])
t.Errorf("want %q, got %q", w, g)
}
}) })
} }
} }
@ -557,25 +537,16 @@ func TestGenruleHashInputs(t *testing.T) {
}, },
} }
config := testConfig(bp, nil) result := genruleFixtureFactory.RunTestWithBp(t, testGenruleBp()+bp)
ctx := testContext(config)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
if errs == nil {
_, errs = ctx.PrepareBuildActions(config)
}
if errs != nil {
t.Fatal(errs)
}
for _, test := range testcases { for _, test := range testcases {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
gen := ctx.ModuleForTests(test.name, "") subResult := result.ResultForSubTest(t)
gen := subResult.ModuleForTests(test.name, "")
manifest := android.RuleBuilderSboxProtoForTests(t, gen.Output("genrule.sbox.textproto")) manifest := android.RuleBuilderSboxProtoForTests(t, gen.Output("genrule.sbox.textproto"))
hash := manifest.Commands[0].GetInputHash() hash := manifest.Commands[0].GetInputHash()
if g, w := hash, test.expectedHash; g != w { subResult.AssertStringEquals("hash", test.expectedHash, hash)
t.Errorf("Expected has %q, got %q", w, g)
}
}) })
} }
} }
@ -630,46 +601,27 @@ func TestGenSrcs(t *testing.T) {
bp += test.prop bp += test.prop
bp += "}\n" bp += "}\n"
config := testConfig(bp, nil) var expectedErrors []string
ctx := testContext(config) if test.err != "" {
expectedErrors = append(expectedErrors, regexp.QuoteMeta(test.err))
}
_, errs := ctx.ParseFileList(".", []string{"Android.bp"}) result := genruleFixtureFactory.
if errs == nil { ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(expectedErrors)).
_, errs = ctx.PrepareBuildActions(config) RunTestWithBp(t, testGenruleBp()+bp)
}
if errs == nil && test.err != "" { if expectedErrors != nil {
t.Fatalf("want error %q, got no error", test.err)
} else if errs != nil && test.err == "" {
android.FailIfErrored(t, errs)
} else if test.err != "" {
if len(errs) != 1 {
t.Errorf("want 1 error, got %d errors:", len(errs))
for _, err := range errs {
t.Errorf(" %s", err.Error())
}
t.FailNow()
}
if !strings.Contains(errs[0].Error(), test.err) {
t.Fatalf("want %q, got %q", test.err, errs[0].Error())
}
return return
} }
gen := ctx.ModuleForTests("gen", "").Module().(*Module) gen := result.Module("gen", "").(*Module)
if g, w := gen.rawCommands, test.cmds; !reflect.DeepEqual(w, g) { result.AssertDeepEquals("cmd", test.cmds, gen.rawCommands)
t.Errorf("want %q, got %q", w, g)
}
if g, w := gen.outputDeps.Strings(), test.deps; !reflect.DeepEqual(w, g) { result.AssertDeepEquals("deps", test.deps, gen.outputDeps.Strings())
t.Errorf("want deps %q, got %q", w, g)
}
if g, w := gen.outputFiles.Strings(), test.files; !reflect.DeepEqual(w, g) { result.AssertDeepEquals("files", test.files, gen.outputFiles.Strings())
t.Errorf("want files %q, got %q", w, g)
}
}) })
} }
} }
func TestGenruleDefaults(t *testing.T) { func TestGenruleDefaults(t *testing.T) {
@ -690,26 +642,16 @@ func TestGenruleDefaults(t *testing.T) {
defaults: ["gen_defaults1", "gen_defaults2"], defaults: ["gen_defaults1", "gen_defaults2"],
} }
` `
config := testConfig(bp, nil)
ctx := testContext(config) result := genruleFixtureFactory.RunTestWithBp(t, testGenruleBp()+bp)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
if errs == nil { gen := result.Module("gen", "").(*Module)
_, errs = ctx.PrepareBuildActions(config)
}
if errs != nil {
t.Fatal(errs)
}
gen := ctx.ModuleForTests("gen", "").Module().(*Module)
expectedCmd := "cp in1 __SBOX_SANDBOX_DIR__/out/out" expectedCmd := "cp in1 __SBOX_SANDBOX_DIR__/out/out"
if gen.rawCommands[0] != expectedCmd { result.AssertStringEquals("cmd", expectedCmd, gen.rawCommands[0])
t.Errorf("Expected cmd: %q, actual: %q", expectedCmd, gen.rawCommands[0])
}
expectedSrcs := []string{"in1"} expectedSrcs := []string{"in1"}
if !reflect.DeepEqual(expectedSrcs, gen.properties.Srcs) { result.AssertDeepEquals("srcs", expectedSrcs, gen.properties.Srcs)
t.Errorf("Expected srcs: %q, actual: %q", expectedSrcs, gen.properties.Srcs)
}
} }
func TestGenruleWithBazel(t *testing.T) { func TestGenruleWithBazel(t *testing.T) {
@ -721,29 +663,18 @@ func TestGenruleWithBazel(t *testing.T) {
} }
` `
config := testConfig(bp, nil) result := genruleFixtureFactory.Extend(android.FixtureModifyConfig(func(config android.Config) {
config.BazelContext = android.MockBazelContext{ config.BazelContext = android.MockBazelContext{
AllFiles: map[string][]string{ AllFiles: map[string][]string{
"//foo/bar:bar": []string{"bazelone.txt", "bazeltwo.txt"}}} "//foo/bar:bar": []string{"bazelone.txt", "bazeltwo.txt"}}}
})).RunTestWithBp(t, testGenruleBp()+bp)
ctx := testContext(config) gen := result.Module("foo", "").(*Module)
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
if errs == nil {
_, errs = ctx.PrepareBuildActions(config)
}
if errs != nil {
t.Fatal(errs)
}
gen := ctx.ModuleForTests("foo", "").Module().(*Module)
expectedOutputFiles := []string{"outputbase/execroot/__main__/bazelone.txt", expectedOutputFiles := []string{"outputbase/execroot/__main__/bazelone.txt",
"outputbase/execroot/__main__/bazeltwo.txt"} "outputbase/execroot/__main__/bazeltwo.txt"}
if !reflect.DeepEqual(gen.outputFiles.Strings(), expectedOutputFiles) { result.AssertDeepEquals("output files", expectedOutputFiles, gen.outputFiles.Strings())
t.Errorf("Expected output files: %q, actual: %q", expectedOutputFiles, gen.outputFiles) result.AssertDeepEquals("output deps", expectedOutputFiles, gen.outputDeps.Strings())
}
if !reflect.DeepEqual(gen.outputDeps.Strings(), expectedOutputFiles) {
t.Errorf("Expected output deps: %q, actual: %q", expectedOutputFiles, gen.outputDeps)
}
} }
type testTool struct { type testTool struct {