Support test fixtures in cc package

Restructures the cc package test setup code to create FixturePreparer
instances for setting up a test fixture and converts some tests to
use it.

The goal with this change is not to switch all the cc tests over to
directly using the new model but instead to ensure that the majority of
the cc tests run with the new model, to allow existing tests to easily
switch to the new model when needed and to allow dependent packages to
be switched to the new model.

Bug: 181070625
Test: m nothing
Change-Id: I00415f10fb44c1b9e78e1317e7f50bb61984d3a4
This commit is contained in:
Paul Duffin 2021-02-24 18:51:54 +00:00
parent 72018ad136
commit 02a3d65402
2 changed files with 199 additions and 59 deletions

View File

@ -52,29 +52,50 @@ func TestMain(m *testing.M) {
os.Exit(run())
}
var ccFixtureFactory = android.NewFixtureFactory(
&buildDir,
PrepareForTestWithCcIncludeVndk,
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.DeviceVndkVersion = StringPtr("current")
variables.ProductVndkVersion = StringPtr("current")
variables.Platform_vndk_version = StringPtr("VER")
}),
)
// testCcWithConfig runs tests using the ccFixtureFactory
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
t.Helper()
ctx := CreateTestContext(config)
ctx.Register()
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
android.FailIfErrored(t, errs)
_, errs = ctx.PrepareBuildActions(config)
android.FailIfErrored(t, errs)
return ctx
result := ccFixtureFactory.RunTestWithConfig(t, config)
return result.TestContext
}
// testCc runs tests using the ccFixtureFactory
//
// Do not add any new usages of this, instead use the ccFixtureFactory directly as it makes it much
// easier to customize the test behavior.
//
// If it is necessary to customize the behavior of an existing test that uses this then please first
// convert the test to using ccFixtureFactory first and then in a following change add the
// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
// that it did not change the test behavior unexpectedly.
//
// deprecated
func testCc(t *testing.T, bp string) *android.TestContext {
t.Helper()
config := TestConfig(buildDir, android.Android, nil, bp, nil)
config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
config.TestProductVariables.ProductVndkVersion = StringPtr("current")
config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
return testCcWithConfig(t, config)
result := ccFixtureFactory.RunTestWithBp(t, bp)
return result.TestContext
}
// testCcNoVndk runs tests using the ccFixtureFactory
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
t.Helper()
config := TestConfig(buildDir, android.Android, nil, bp, nil)
@ -83,6 +104,11 @@ func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
return testCcWithConfig(t, config)
}
// testCcNoProductVndk runs tests using the ccFixtureFactory
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
t.Helper()
config := TestConfig(buildDir, android.Android, nil, bp, nil)
@ -92,27 +118,24 @@ func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
return testCcWithConfig(t, config)
}
// testCcErrorWithConfig runs tests using the ccFixtureFactory
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
t.Helper()
ctx := CreateTestContext(config)
ctx.Register()
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
if len(errs) > 0 {
android.FailIfNoMatchingErrors(t, pattern, errs)
return
}
_, errs = ctx.PrepareBuildActions(config)
if len(errs) > 0 {
android.FailIfNoMatchingErrors(t, pattern, errs)
return
}
t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
ccFixtureFactory.Extend().
ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
RunTestWithConfig(t, config)
}
// testCcError runs tests using the ccFixtureFactory
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func testCcError(t *testing.T, pattern string, bp string) {
t.Helper()
config := TestConfig(buildDir, android.Android, nil, bp, nil)
@ -122,6 +145,11 @@ func testCcError(t *testing.T, pattern string, bp string) {
return
}
// testCcErrorProductVndk runs tests using the ccFixtureFactory
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
t.Helper()
config := TestConfig(buildDir, android.Android, nil, bp, nil)

View File

@ -37,7 +37,31 @@ func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
}
func GatherRequiredDepsForTest(oses ...android.OsType) string {
ret := `
ret := commonDefaultModules()
supportLinuxBionic := false
for _, os := range oses {
if os == android.Fuchsia {
ret += withFuchsiaModules()
}
if os == android.Windows {
ret += withWindowsModules()
}
if os == android.LinuxBionic {
supportLinuxBionic = true
ret += withLinuxBionic()
}
}
if !supportLinuxBionic {
ret += withoutLinuxBionic()
}
return ret
}
func commonDefaultModules() string {
return `
toolchain_library {
name: "libatomic",
defaults: ["linux_bionic_supported"],
@ -475,23 +499,10 @@ func GatherRequiredDepsForTest(oses ...android.OsType) string {
name: "note_memtag_heap_sync",
}
`
}
supportLinuxBionic := false
for _, os := range oses {
if os == android.Fuchsia {
ret += `
cc_library {
name: "libbioniccompat",
stl: "none",
}
cc_library {
name: "libcompiler_rt",
stl: "none",
}
`
}
if os == android.Windows {
ret += `
func withWindowsModules() string {
return `
toolchain_library {
name: "libwinpthread",
host_supported: true,
@ -505,9 +516,22 @@ func GatherRequiredDepsForTest(oses ...android.OsType) string {
}
`
}
if os == android.LinuxBionic {
supportLinuxBionic = true
ret += `
func withFuchsiaModules() string {
return `
cc_library {
name: "libbioniccompat",
stl: "none",
}
cc_library {
name: "libcompiler_rt",
stl: "none",
}
`
}
func withLinuxBionic() string {
return `
cc_binary {
name: "linker",
defaults: ["linux_bionic_supported"],
@ -548,22 +572,103 @@ func GatherRequiredDepsForTest(oses ...android.OsType) string {
}
`
}
}
if !supportLinuxBionic {
ret += `
func withoutLinuxBionic() string {
return `
cc_defaults {
name: "linux_bionic_supported",
}
`
}
return ret
}
func GatherRequiredFilesForTest(fs map[string][]byte) {
}
// The directory in which cc linux bionic default modules will be defined.
//
// Placing them here ensures that their location does not conflict with default test modules
// defined by other packages.
const linuxBionicDefaultsPath = "defaults/cc/linux-bionic/Android.bp"
// The directory in which the default cc common test modules will be defined.
//
// Placing them here ensures that their location does not conflict with default test modules
// defined by other packages.
const DefaultCcCommonTestModulesDir = "defaults/cc/common/"
// Test fixture preparer that will register most cc build components.
//
// Singletons and mutators should only be added here if they are needed for a majority of cc
// 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 PrepareForTestWithCcBuildComponents = android.GroupFixturePreparers(
android.PrepareForTestWithAndroidBuildComponents,
android.FixtureRegisterWithContext(RegisterRequiredBuildComponentsForTest),
android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
ctx.RegisterModuleType("cc_fuzz", FuzzFactory)
ctx.RegisterModuleType("cc_test", TestFactory)
ctx.RegisterModuleType("cc_test_library", TestLibraryFactory)
ctx.RegisterModuleType("llndk_headers", llndkHeadersFactory)
ctx.RegisterModuleType("vendor_public_library", vendorPublicLibraryFactory)
ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
RegisterVndkLibraryTxtTypes(ctx)
}),
)
// Preparer that will define default cc modules, e.g. standard prebuilt modules.
var PrepareForTestWithCcDefaultModules = android.GroupFixturePreparers(
PrepareForTestWithCcBuildComponents,
// Place the default cc test modules that are common to all platforms in a location that will not
// conflict with default test modules defined by other packages.
android.FixtureAddTextFile(DefaultCcCommonTestModulesDir+"Android.bp", commonDefaultModules()),
// Disable linux bionic by default.
android.FixtureAddTextFile(linuxBionicDefaultsPath, withoutLinuxBionic()),
)
// Prepare a fixture to use all cc 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 PrepareForIntegrationTestWithCc = android.GroupFixturePreparers(
android.PrepareForIntegrationTestWithAndroid,
genrule.PrepareForIntegrationTestWithGenrule,
PrepareForTestWithCcDefaultModules,
)
// The preparer to include if running a cc related test for windows.
var PrepareForTestOnWindows = android.GroupFixturePreparers(
// Place the default cc test modules for windows platforms in a location that will not conflict
// with default test modules defined by other packages.
android.FixtureAddTextFile("defaults/cc/windows/Android.bp", withWindowsModules()),
)
// The preparer to include if running a cc related test for linux bionic.
var PrepareForTestOnLinuxBionic = android.GroupFixturePreparers(
// Enable linux bionic.
android.FixtureAddTextFile(linuxBionicDefaultsPath, withLinuxBionic()),
)
// This adds some additional modules and singletons which might negatively impact the performance
// of tests so they are not included in the PrepareForIntegrationTestWithCc.
var PrepareForTestWithCcIncludeVndk = android.GroupFixturePreparers(
PrepareForIntegrationTestWithCc,
android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
vendorSnapshotImageSingleton.init(ctx)
recoverySnapshotImageSingleton.init(ctx)
ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
}),
)
// TestConfig is the legacy way of creating a test Config for testing cc modules.
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func TestConfig(buildDir string, os android.OsType, env map[string]string,
bp string, fs map[string][]byte) android.Config {
@ -588,6 +693,11 @@ func TestConfig(buildDir string, os android.OsType, env map[string]string,
return config
}
// CreateTestContext is the legacy way of creating a TestContext for testing cc modules.
//
// See testCc for an explanation as to how to stop using this deprecated method.
//
// deprecated
func CreateTestContext(config android.Config) *android.TestContext {
ctx := android.NewTestArchContext(config)
genrule.RegisterGenruleBuildComponents(ctx)
@ -598,13 +708,15 @@ func CreateTestContext(config android.Config) *android.TestContext {
ctx.RegisterModuleType("vendor_public_library", vendorPublicLibraryFactory)
ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
ctx.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
vendorSnapshotImageSingleton.init(ctx)
recoverySnapshotImageSingleton.init(ctx)
ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
RegisterVndkLibraryTxtTypes(ctx)
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
android.RegisterPrebuiltMutators(ctx)
RegisterRequiredBuildComponentsForTest(ctx)
ctx.RegisterSingletonType("vndk-snapshot", VndkSnapshotSingleton)
return ctx
}