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

Commit 3c6a4ea2 authored by Paul Duffin's avatar Paul Duffin
Browse files

Convert android/neverallow_test.go to test fixtures

Bug: 182885307
Test: m nothing
Change-Id: I5679cb517151ceaf468f2fe2e0ccab718cc7b4a3
parent d6597007
Loading
Loading
Loading
Loading
+24 −29
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ var neverallowTests = []struct {
	rules []Rule

	// Additional contents to add to the virtual filesystem used by the tests.
	fs map[string][]byte
	fs MockFS

	// The expected error patterns. If empty then no errors are expected, otherwise each error
	// reported must be matched by at least one of these patterns. A pattern matches if the error
@@ -285,41 +285,36 @@ var neverallowTests = []struct {
	},
}

var prepareForNeverAllowTest = GroupFixturePreparers(
	FixtureRegisterWithContext(func(ctx RegistrationContext) {
		ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
		ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
		ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
		ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
		ctx.RegisterModuleType("makefile_goal", newMockMakefileGoalModule)
		ctx.PostDepsMutators(RegisterNeverallowMutator)
	}),
)

func TestNeverallow(t *testing.T) {
	for _, test := range neverallowTests {
		// Create a test per config to allow for test specific config, e.g. test rules.
		config := TestConfig(buildDir, nil, "", test.fs)

		t.Run(test.name, func(t *testing.T) {
			emptyTestFixtureFactory.
				ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)).
				RunTest(t,
					prepareForNeverAllowTest,
					FixtureModifyConfig(func(config Config) {
						// If the test has its own rules then use them instead of the default ones.
						if test.rules != nil {
							SetTestNeverallowRules(config, test.rules)
						}
			_, errs := testNeverallow(config)
			CheckErrorsAgainstExpectations(t, errs, test.expectedErrors)
					}),
					test.fs.AddToFixture(),
				)
		})
	}
}

func testNeverallow(config Config) (*TestContext, []error) {
	ctx := NewTestContext(config)
	ctx.RegisterModuleType("cc_library", newMockCcLibraryModule)
	ctx.RegisterModuleType("java_library", newMockJavaLibraryModule)
	ctx.RegisterModuleType("java_library_host", newMockJavaLibraryModule)
	ctx.RegisterModuleType("java_device_for_host", newMockJavaLibraryModule)
	ctx.RegisterModuleType("makefile_goal", newMockMakefileGoalModule)
	ctx.PostDepsMutators(RegisterNeverallowMutator)
	ctx.Register()

	_, errs := ctx.ParseBlueprintsFiles("Android.bp")
	if len(errs) > 0 {
		return ctx, errs
	}

	_, errs = ctx.PrepareBuildActions(config)
	return ctx, errs
}

type mockCcLibraryProperties struct {
	Include_dirs     []string
	Vendor_available *bool