Convert license tests to use test fixtures
Used to exercise the new functions to allow the env configuration of a
test fixture to be customized.
Bug: 181070625
Test: m nothing
Change-Id: Iea1d7b20498d690fcade4b6699a70773ea9175c2
diff --git a/android/license_kind_test.go b/android/license_kind_test.go
index 767b64e..83e83ce 100644
--- a/android/license_kind_test.go
+++ b/android/license_kind_test.go
@@ -8,7 +8,7 @@
var licenseKindTests = []struct {
name string
- fs map[string][]byte
+ fs MockFS
expectedErrors []string
}{
{
@@ -97,48 +97,18 @@
func TestLicenseKind(t *testing.T) {
for _, test := range licenseKindTests {
t.Run(test.name, func(t *testing.T) {
- _, errs := testLicenseKind(test.fs)
-
- expectedErrors := test.expectedErrors
- if expectedErrors == nil {
- FailIfErrored(t, errs)
- } else {
- for _, expectedError := range expectedErrors {
- FailIfNoMatchingErrors(t, expectedError, errs)
- }
- if len(errs) > len(expectedErrors) {
- t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
- for i, expectedError := range expectedErrors {
- t.Errorf("expectedErrors[%d] = %s", i, expectedError)
- }
- for i, err := range errs {
- t.Errorf("errs[%d] = %s", i, err)
- }
- }
- }
+ licenseTestFixtureFactory.
+ Extend(
+ FixtureRegisterWithContext(func(ctx RegistrationContext) {
+ ctx.RegisterModuleType("mock_license", newMockLicenseModule)
+ }),
+ test.fs.AddToFixture(),
+ ).ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
+ RunTest(t)
})
}
}
-func testLicenseKind(fs map[string][]byte) (*TestContext, []error) {
-
- // Create a new config per test as license_kind information is stored in the config.
- config := TestArchConfig(buildDir, nil, "", fs)
-
- ctx := NewTestArchContext(config)
- RegisterLicenseKindBuildComponents(ctx)
- ctx.RegisterModuleType("mock_license", newMockLicenseModule)
- ctx.Register()
-
- _, errs := ctx.ParseBlueprintsFiles(".")
- if len(errs) > 0 {
- return ctx, errs
- }
-
- _, errs = ctx.PrepareBuildActions(config)
- return ctx, errs
-}
-
type mockLicenseProperties struct {
License_kinds []string
}
diff --git a/android/license_test.go b/android/license_test.go
index 9f68713..a564827 100644
--- a/android/license_test.go
+++ b/android/license_test.go
@@ -4,9 +4,24 @@
"testing"
)
+// Common test set up for license tests.
+var licenseTestFixtureFactory = emptyTestFixtureFactory.Extend(
+ // General preparers in alphabetical order.
+ PrepareForTestWithDefaults,
+ prepareForTestWithLicenses,
+ PrepareForTestWithOverrides,
+ PrepareForTestWithPackageModule,
+ PrepareForTestWithPrebuilts,
+ PrepareForTestWithVisibility,
+
+ // Additional test specific stuff
+ prepareForTestWithFakePrebuiltModules,
+ FixtureMergeEnv(map[string]string{"ANDROID_REQUIRE_LICENSES": "1"}),
+)
+
var licenseTests = []struct {
name string
- fs map[string][]byte
+ fs MockFS
expectedErrors []string
}{
{
@@ -163,58 +178,20 @@
func TestLicense(t *testing.T) {
for _, test := range licenseTests {
t.Run(test.name, func(t *testing.T) {
- _, errs := testLicense(test.fs)
-
- expectedErrors := test.expectedErrors
- if expectedErrors == nil {
- FailIfErrored(t, errs)
- } else {
- for _, expectedError := range expectedErrors {
- FailIfNoMatchingErrors(t, expectedError, errs)
- }
- if len(errs) > len(expectedErrors) {
- t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
- for i, expectedError := range expectedErrors {
- t.Errorf("expectedErrors[%d] = %s", i, expectedError)
- }
- for i, err := range errs {
- t.Errorf("errs[%d] = %s", i, err)
- }
- }
- }
+ // Customize the common license text fixture factory.
+ licenseTestFixtureFactory.Extend(
+ FixtureRegisterWithContext(func(ctx RegistrationContext) {
+ ctx.RegisterModuleType("rule", newMockRuleModule)
+ }),
+ test.fs.AddToFixture(),
+ ).
+ ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
+ RunTest(t)
})
}
}
-func testLicense(fs map[string][]byte) (*TestContext, []error) {
-
- // Create a new config per test as visibility information is stored in the config.
- env := make(map[string]string)
- env["ANDROID_REQUIRE_LICENSES"] = "1"
- config := TestArchConfig(buildDir, env, "", fs)
-
- ctx := NewTestArchContext(config)
- RegisterPackageBuildComponents(ctx)
- registerTestPrebuiltBuildComponents(ctx)
- RegisterLicenseKindBuildComponents(ctx)
- RegisterLicenseBuildComponents(ctx)
- ctx.RegisterModuleType("rule", newMockRuleModule)
- ctx.PreArchMutators(RegisterVisibilityRuleChecker)
- ctx.PreArchMutators(RegisterLicensesPackageMapper)
- ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
- ctx.PreArchMutators(RegisterLicensesPropertyGatherer)
- ctx.PreArchMutators(RegisterVisibilityRuleGatherer)
- ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer)
- ctx.PostDepsMutators(RegisterLicensesDependencyChecker)
- ctx.Register()
-
- _, errs := ctx.ParseBlueprintsFiles(".")
- if len(errs) > 0 {
- return ctx, errs
- }
-
- _, errs = ctx.PrepareBuildActions(config)
- return ctx, errs
+func testLicense(t *testing.T, fs MockFS, expectedErrors []string) {
}
type mockRuleModule struct {
diff --git a/android/licenses_test.go b/android/licenses_test.go
index c043791..d0e3533 100644
--- a/android/licenses_test.go
+++ b/android/licenses_test.go
@@ -6,9 +6,21 @@
"github.com/google/blueprint"
)
+var prepareForTestWithLicenses = GroupFixturePreparers(
+ FixtureRegisterWithContext(RegisterLicenseKindBuildComponents),
+ FixtureRegisterWithContext(RegisterLicenseBuildComponents),
+ FixtureRegisterWithContext(registerLicenseMutators),
+)
+
+func registerLicenseMutators(ctx RegistrationContext) {
+ ctx.PreArchMutators(RegisterLicensesPackageMapper)
+ ctx.PreArchMutators(RegisterLicensesPropertyGatherer)
+ ctx.PostDepsMutators(RegisterLicensesDependencyChecker)
+}
+
var licensesTests = []struct {
name string
- fs map[string][]byte
+ fs MockFS
expectedErrors []string
effectiveLicenses map[string][]string
effectiveInheritedLicenses map[string][]string
@@ -457,40 +469,48 @@
func TestLicenses(t *testing.T) {
for _, test := range licensesTests {
t.Run(test.name, func(t *testing.T) {
- ctx, errs := testLicenses(buildDir, test.fs)
-
- CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
+ // Customize the common license text fixture factory.
+ result := licenseTestFixtureFactory.Extend(
+ FixtureRegisterWithContext(func(ctx RegistrationContext) {
+ ctx.RegisterModuleType("mock_bad_module", newMockLicensesBadModule)
+ ctx.RegisterModuleType("mock_library", newMockLicensesLibraryModule)
+ ctx.RegisterModuleType("mock_defaults", defaultsLicensesFactory)
+ }),
+ test.fs.AddToFixture(),
+ ).
+ ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
+ RunTest(t)
if test.effectiveLicenses != nil {
- checkEffectiveLicenses(t, ctx, test.effectiveLicenses)
+ checkEffectiveLicenses(result, test.effectiveLicenses)
}
if test.effectivePackage != nil {
- checkEffectivePackage(t, ctx, test.effectivePackage)
+ checkEffectivePackage(result, test.effectivePackage)
}
if test.effectiveNotices != nil {
- checkEffectiveNotices(t, ctx, test.effectiveNotices)
+ checkEffectiveNotices(result, test.effectiveNotices)
}
if test.effectiveKinds != nil {
- checkEffectiveKinds(t, ctx, test.effectiveKinds)
+ checkEffectiveKinds(result, test.effectiveKinds)
}
if test.effectiveConditions != nil {
- checkEffectiveConditions(t, ctx, test.effectiveConditions)
+ checkEffectiveConditions(result, test.effectiveConditions)
}
if test.effectiveInheritedLicenses != nil {
- checkEffectiveInheritedLicenses(t, ctx, test.effectiveInheritedLicenses)
+ checkEffectiveInheritedLicenses(result, test.effectiveInheritedLicenses)
}
})
}
}
-func checkEffectiveLicenses(t *testing.T, ctx *TestContext, effectiveLicenses map[string][]string) {
+func checkEffectiveLicenses(result *TestResult, effectiveLicenses map[string][]string) {
actualLicenses := make(map[string][]string)
- ctx.Context.Context.VisitAllModules(func(m blueprint.Module) {
+ result.Context.Context.VisitAllModules(func(m blueprint.Module) {
if _, ok := m.(*licenseModule); ok {
return
}
@@ -502,7 +522,7 @@
}
module, ok := m.(Module)
if !ok {
- t.Errorf("%q not a module", m.Name())
+ result.Errorf("%q not a module", m.Name())
return
}
base := module.base()
@@ -518,14 +538,14 @@
licenses = []string{}
}
if !compareUnorderedStringArrays(expectedLicenses, licenses) {
- t.Errorf("effective licenses mismatch for module %q: expected %q, found %q", moduleName, expectedLicenses, licenses)
+ result.Errorf("effective licenses mismatch for module %q: expected %q, found %q", moduleName, expectedLicenses, licenses)
}
}
}
-func checkEffectiveInheritedLicenses(t *testing.T, ctx *TestContext, effectiveInheritedLicenses map[string][]string) {
+func checkEffectiveInheritedLicenses(result *TestResult, effectiveInheritedLicenses map[string][]string) {
actualLicenses := make(map[string][]string)
- ctx.Context.Context.VisitAllModules(func(m blueprint.Module) {
+ result.Context.Context.VisitAllModules(func(m blueprint.Module) {
if _, ok := m.(*licenseModule); ok {
return
}
@@ -537,7 +557,7 @@
}
module, ok := m.(Module)
if !ok {
- t.Errorf("%q not a module", m.Name())
+ result.Errorf("%q not a module", m.Name())
return
}
base := module.base()
@@ -548,7 +568,7 @@
for _, l := range base.commonProperties.Effective_licenses {
inherited[l] = true
}
- ctx.Context.Context.VisitDepsDepthFirst(m, func(c blueprint.Module) {
+ result.Context.Context.VisitDepsDepthFirst(m, func(c blueprint.Module) {
if _, ok := c.(*licenseModule); ok {
return
}
@@ -560,7 +580,7 @@
}
cmodule, ok := c.(Module)
if !ok {
- t.Errorf("%q not a module", c.Name())
+ result.Errorf("%q not a module", c.Name())
return
}
cbase := cmodule.base()
@@ -583,14 +603,14 @@
licenses = []string{}
}
if !compareUnorderedStringArrays(expectedInheritedLicenses, licenses) {
- t.Errorf("effective inherited licenses mismatch for module %q: expected %q, found %q", moduleName, expectedInheritedLicenses, licenses)
+ result.Errorf("effective inherited licenses mismatch for module %q: expected %q, found %q", moduleName, expectedInheritedLicenses, licenses)
}
}
}
-func checkEffectivePackage(t *testing.T, ctx *TestContext, effectivePackage map[string]string) {
+func checkEffectivePackage(result *TestResult, effectivePackage map[string]string) {
actualPackage := make(map[string]string)
- ctx.Context.Context.VisitAllModules(func(m blueprint.Module) {
+ result.Context.Context.VisitAllModules(func(m blueprint.Module) {
if _, ok := m.(*licenseModule); ok {
return
}
@@ -602,7 +622,7 @@
}
module, ok := m.(Module)
if !ok {
- t.Errorf("%q not a module", m.Name())
+ result.Errorf("%q not a module", m.Name())
return
}
base := module.base()
@@ -623,14 +643,14 @@
packageName = ""
}
if expectedPackage != packageName {
- t.Errorf("effective package mismatch for module %q: expected %q, found %q", moduleName, expectedPackage, packageName)
+ result.Errorf("effective package mismatch for module %q: expected %q, found %q", moduleName, expectedPackage, packageName)
}
}
}
-func checkEffectiveNotices(t *testing.T, ctx *TestContext, effectiveNotices map[string][]string) {
+func checkEffectiveNotices(result *TestResult, effectiveNotices map[string][]string) {
actualNotices := make(map[string][]string)
- ctx.Context.Context.VisitAllModules(func(m blueprint.Module) {
+ result.Context.Context.VisitAllModules(func(m blueprint.Module) {
if _, ok := m.(*licenseModule); ok {
return
}
@@ -642,7 +662,7 @@
}
module, ok := m.(Module)
if !ok {
- t.Errorf("%q not a module", m.Name())
+ result.Errorf("%q not a module", m.Name())
return
}
base := module.base()
@@ -658,14 +678,14 @@
notices = []string{}
}
if !compareUnorderedStringArrays(expectedNotices, notices) {
- t.Errorf("effective notice files mismatch for module %q: expected %q, found %q", moduleName, expectedNotices, notices)
+ result.Errorf("effective notice files mismatch for module %q: expected %q, found %q", moduleName, expectedNotices, notices)
}
}
}
-func checkEffectiveKinds(t *testing.T, ctx *TestContext, effectiveKinds map[string][]string) {
+func checkEffectiveKinds(result *TestResult, effectiveKinds map[string][]string) {
actualKinds := make(map[string][]string)
- ctx.Context.Context.VisitAllModules(func(m blueprint.Module) {
+ result.Context.Context.VisitAllModules(func(m blueprint.Module) {
if _, ok := m.(*licenseModule); ok {
return
}
@@ -677,7 +697,7 @@
}
module, ok := m.(Module)
if !ok {
- t.Errorf("%q not a module", m.Name())
+ result.Errorf("%q not a module", m.Name())
return
}
base := module.base()
@@ -693,14 +713,14 @@
kinds = []string{}
}
if !compareUnorderedStringArrays(expectedKinds, kinds) {
- t.Errorf("effective license kinds mismatch for module %q: expected %q, found %q", moduleName, expectedKinds, kinds)
+ result.Errorf("effective license kinds mismatch for module %q: expected %q, found %q", moduleName, expectedKinds, kinds)
}
}
}
-func checkEffectiveConditions(t *testing.T, ctx *TestContext, effectiveConditions map[string][]string) {
+func checkEffectiveConditions(result *TestResult, effectiveConditions map[string][]string) {
actualConditions := make(map[string][]string)
- ctx.Context.Context.VisitAllModules(func(m blueprint.Module) {
+ result.Context.Context.VisitAllModules(func(m blueprint.Module) {
if _, ok := m.(*licenseModule); ok {
return
}
@@ -712,7 +732,7 @@
}
module, ok := m.(Module)
if !ok {
- t.Errorf("%q not a module", m.Name())
+ result.Errorf("%q not a module", m.Name())
return
}
base := module.base()
@@ -728,7 +748,7 @@
conditions = []string{}
}
if !compareUnorderedStringArrays(expectedConditions, conditions) {
- t.Errorf("effective license conditions mismatch for module %q: expected %q, found %q", moduleName, expectedConditions, conditions)
+ result.Errorf("effective license conditions mismatch for module %q: expected %q, found %q", moduleName, expectedConditions, conditions)
}
}
}
@@ -754,41 +774,6 @@
return true
}
-func testLicenses(buildDir string, fs map[string][]byte) (*TestContext, []error) {
-
- // Create a new config per test as licenses information is stored in the config.
- env := make(map[string]string)
- env["ANDROID_REQUIRE_LICENSES"] = "1"
- config := TestArchConfig(buildDir, env, "", fs)
-
- ctx := NewTestArchContext(config)
- ctx.RegisterModuleType("mock_bad_module", newMockLicensesBadModule)
- ctx.RegisterModuleType("mock_library", newMockLicensesLibraryModule)
- ctx.RegisterModuleType("mock_defaults", defaultsLicensesFactory)
-
- // Order of the following method calls is significant.
- RegisterPackageBuildComponents(ctx)
- registerTestPrebuiltBuildComponents(ctx)
- RegisterLicenseKindBuildComponents(ctx)
- RegisterLicenseBuildComponents(ctx)
- ctx.PreArchMutators(RegisterVisibilityRuleChecker)
- ctx.PreArchMutators(RegisterLicensesPackageMapper)
- ctx.PreArchMutators(RegisterDefaultsPreArchMutators)
- ctx.PreArchMutators(RegisterLicensesPropertyGatherer)
- ctx.PreArchMutators(RegisterVisibilityRuleGatherer)
- ctx.PostDepsMutators(RegisterVisibilityRuleEnforcer)
- ctx.PostDepsMutators(RegisterLicensesDependencyChecker)
- ctx.Register()
-
- _, errs := ctx.ParseBlueprintsFiles(".")
- if len(errs) > 0 {
- return ctx, errs
- }
-
- _, errs = ctx.PrepareBuildActions(config)
- return ctx, errs
-}
-
type mockLicensesBadProperties struct {
Visibility []string
}