Switch the namespace tests to fully use test fixtures

Indented the bp contents to make it easier to differentiate between
them and the directory in which they belong.

Bug: 181070625
Test: m nothing
Change-Id: Iae7495fb7c88769dc688006a41f4d21f57cf03b8
This commit is contained in:
Paul Duffin
2021-07-06 22:36:33 +01:00
parent 61c6eef064
commit 0fc6d32c82
2 changed files with 443 additions and 469 deletions

View File

@@ -586,6 +586,18 @@ func FixtureExpectsAllErrorsToMatchAPattern(patterns []string) FixtureErrorHandl
}) })
} }
// FixtureExpectsOneErrorPattern returns an error handler that will cause the test to fail
// if there is more than one error or the error does not match the pattern.
//
// If the test fails this handler will call `result.FailNow()` which will exit the goroutine within
// which the test is being run which means that the RunTest() method will not return.
func FixtureExpectsOneErrorPattern(pattern string) FixtureErrorHandler {
return FixtureCustomErrorHandler(func(t *testing.T, result *TestResult) {
t.Helper()
CheckErrorsAgainstExpectations(t, result.Errs, []string{pattern})
})
}
// FixtureCustomErrorHandler creates a custom error handler // FixtureCustomErrorHandler creates a custom error handler
func FixtureCustomErrorHandler(function func(t *testing.T, result *TestResult)) FixtureErrorHandler { func FixtureCustomErrorHandler(function func(t *testing.T, result *TestResult)) FixtureErrorHandler {
return simpleErrorHandler{ return simpleErrorHandler{

View File

@@ -15,7 +15,6 @@
package android package android
import ( import (
"errors"
"path/filepath" "path/filepath"
"reflect" "reflect"
"testing" "testing"
@@ -24,8 +23,9 @@ import (
) )
func TestDependingOnModuleInSameNamespace(t *testing.T) { func TestDependingOnModuleInSameNamespace(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -37,19 +37,20 @@ func TestDependingOnModuleInSameNamespace(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).RunTest(t)
a := getModule(ctx, "a") a := getModule(result, "a")
b := getModule(ctx, "b") b := getModule(result, "b")
if !dependsOn(ctx, b, a) { if !dependsOn(result, b, a) {
t.Errorf("module b does not depend on module a in the same namespace") t.Errorf("module b does not depend on module a in the same namespace")
} }
} }
func TestDependingOnModuleInRootNamespace(t *testing.T) { func TestDependingOnModuleInRootNamespace(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
".": ` ".": `
test_module { test_module {
name: "b", name: "b",
@@ -59,19 +60,20 @@ func TestDependingOnModuleInRootNamespace(t *testing.T) {
name: "a", name: "a",
} }
`, `,
}, }),
) ).RunTest(t)
a := getModule(ctx, "a") a := getModule(result, "a")
b := getModule(ctx, "b") b := getModule(result, "b")
if !dependsOn(ctx, b, a) { if !dependsOn(result, b, a) {
t.Errorf("module b in root namespace does not depend on module a in the root namespace") t.Errorf("module b in root namespace does not depend on module a in the root namespace")
} }
} }
func TestImplicitlyImportRootNamespace(t *testing.T) { func TestImplicitlyImportRootNamespace(t *testing.T) {
_ = setupTest(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
".": ` ".": `
test_module { test_module {
name: "a", name: "a",
@@ -85,15 +87,16 @@ func TestImplicitlyImportRootNamespace(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).RunTest(t)
// setupTest will report any errors // RunTest will report any errors
} }
func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) { func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) {
_ = setupTest(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
".": ` ".": `
blueprint_test_module { blueprint_test_module {
name: "a", name: "a",
@@ -107,15 +110,16 @@ func TestDependingOnBlueprintModuleInRootNamespace(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).RunTest(t)
// setupTest will report any errors // RunTest will report any errors
} }
func TestDependingOnModuleInImportedNamespace(t *testing.T) { func TestDependingOnModuleInImportedNamespace(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -132,19 +136,20 @@ func TestDependingOnModuleInImportedNamespace(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).RunTest(t)
a := getModule(ctx, "a") a := getModule(result, "a")
b := getModule(ctx, "b") b := getModule(result, "b")
if !dependsOn(ctx, b, a) { if !dependsOn(result, b, a) {
t.Errorf("module b does not depend on module a in the same namespace") t.Errorf("module b does not depend on module a in the same namespace")
} }
} }
func TestDependingOnModuleInNonImportedNamespace(t *testing.T) { func TestDependingOnModuleInNonImportedNamespace(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -167,24 +172,18 @@ func TestDependingOnModuleInNonImportedNamespace(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).
ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir3/Android.bp:4:5: "b" depends on undefined module "a"
expectedErrors := []error{
errors.New(
`dir3/Android.bp:4:4: "b" depends on undefined module "a"
Module "b" is defined in namespace "dir3" which can read these 2 namespaces: ["dir3" "."] Module "b" is defined in namespace "dir3" which can read these 2 namespaces: ["dir3" "."]
Module "a" can be found in these namespaces: ["dir1" "dir2"]`), Module "a" can be found in these namespaces: ["dir1" "dir2"]\E`)).
} RunTest(t)
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) { func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -200,18 +199,20 @@ func TestDependingOnModuleByFullyQualifiedReference(t *testing.T) {
deps: ["//dir1:a"], deps: ["//dir1:a"],
} }
`, `,
}, }),
) ).RunTest(t)
a := getModule(ctx, "a")
b := getModule(ctx, "b") a := getModule(result, "a")
if !dependsOn(ctx, b, a) { b := getModule(result, "b")
if !dependsOn(result, b, a) {
t.Errorf("module b does not depend on module a") t.Errorf("module b does not depend on module a")
} }
} }
func TestSameNameInTwoNamespaces(t *testing.T) { func TestSameNameInTwoNamespaces(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -238,30 +239,31 @@ func TestSameNameInTwoNamespaces(t *testing.T) {
id:"4", id:"4",
} }
`, `,
}, }),
) ).RunTest(t)
one := findModuleById(ctx, "1") one := findModuleById(result, "1")
two := findModuleById(ctx, "2") two := findModuleById(result, "2")
three := findModuleById(ctx, "3") three := findModuleById(result, "3")
four := findModuleById(ctx, "4") four := findModuleById(result, "4")
if !dependsOn(ctx, two, one) { if !dependsOn(result, two, one) {
t.Fatalf("Module 2 does not depend on module 1 in its namespace") t.Fatalf("Module 2 does not depend on module 1 in its namespace")
} }
if dependsOn(ctx, two, three) { if dependsOn(result, two, three) {
t.Fatalf("Module 2 depends on module 3 in another namespace") t.Fatalf("Module 2 depends on module 3 in another namespace")
} }
if !dependsOn(ctx, four, three) { if !dependsOn(result, four, three) {
t.Fatalf("Module 4 does not depend on module 3 in its namespace") t.Fatalf("Module 4 does not depend on module 3 in its namespace")
} }
if dependsOn(ctx, four, one) { if dependsOn(result, four, one) {
t.Fatalf("Module 4 depends on module 1 in another namespace") t.Fatalf("Module 4 depends on module 1 in another namespace")
} }
} }
func TestSearchOrder(t *testing.T) { func TestSearchOrder(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -326,30 +328,31 @@ func TestSearchOrder(t *testing.T) {
deps: ["a", "b", "c", "d"], deps: ["a", "b", "c", "d"],
} }
`, `,
}, }),
) ).RunTest(t)
testMe := findModuleById(ctx, "0") testMe := findModuleById(result, "0")
if !dependsOn(ctx, testMe, findModuleById(ctx, "1")) { if !dependsOn(result, testMe, findModuleById(result, "1")) {
t.Errorf("test_me doesn't depend on id 1") t.Errorf("test_me doesn't depend on id 1")
} }
if !dependsOn(ctx, testMe, findModuleById(ctx, "3")) { if !dependsOn(result, testMe, findModuleById(result, "3")) {
t.Errorf("test_me doesn't depend on id 3") t.Errorf("test_me doesn't depend on id 3")
} }
if !dependsOn(ctx, testMe, findModuleById(ctx, "6")) { if !dependsOn(result, testMe, findModuleById(result, "6")) {
t.Errorf("test_me doesn't depend on id 6") t.Errorf("test_me doesn't depend on id 6")
} }
if !dependsOn(ctx, testMe, findModuleById(ctx, "10")) { if !dependsOn(result, testMe, findModuleById(result, "10")) {
t.Errorf("test_me doesn't depend on id 10") t.Errorf("test_me doesn't depend on id 10")
} }
if numDeps(ctx, testMe) != 4 { if numDeps(result, testMe) != 4 {
t.Errorf("num dependencies of test_me = %v, not 4\n", numDeps(ctx, testMe)) t.Errorf("num dependencies of test_me = %v, not 4\n", numDeps(result, testMe))
} }
} }
func TestTwoNamespacesCanImportEachOther(t *testing.T) { func TestTwoNamespacesCanImportEachOther(t *testing.T) {
_ = setupTest(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
imports: ["dir2"] imports: ["dir2"]
@@ -371,15 +374,16 @@ func TestTwoNamespacesCanImportEachOther(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).RunTest(t)
// setupTest will report any errors // RunTest will report any errors
} }
func TestImportingNonexistentNamespace(t *testing.T) { func TestImportingNonexistentNamespace(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
imports: ["a_nonexistent_namespace"] imports: ["a_nonexistent_namespace"]
@@ -389,21 +393,17 @@ func TestImportingNonexistentNamespace(t *testing.T) {
deps: ["a_nonexistent_module"] deps: ["a_nonexistent_module"]
} }
`, `,
}, }),
) ).
// should complain about the missing namespace and not complain about the unresolvable dependency // should complain about the missing namespace and not complain about the unresolvable dependency
expectedErrors := []error{ ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:2:5: module "soong_namespace": namespace a_nonexistent_namespace does not exist\E`)).
errors.New(`dir1/Android.bp:2:4: module "soong_namespace": namespace a_nonexistent_namespace does not exist`), RunTest(t)
}
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestNamespacesDontInheritParentNamespaces(t *testing.T) { func TestNamespacesDontInheritParentNamespaces(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -419,22 +419,18 @@ func TestNamespacesDontInheritParentNamespaces(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).
ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/subdir1/Android.bp:4:5: "b" depends on undefined module "a"
expectedErrors := []error{
errors.New(`dir1/subdir1/Android.bp:4:4: "b" depends on undefined module "a"
Module "b" is defined in namespace "dir1/subdir1" which can read these 2 namespaces: ["dir1/subdir1" "."] Module "b" is defined in namespace "dir1/subdir1" which can read these 2 namespaces: ["dir1/subdir1" "."]
Module "a" can be found in these namespaces: ["dir1"]`), Module "a" can be found in these namespaces: ["dir1"]\E`)).
} RunTest(t)
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestModulesDoReceiveParentNamespace(t *testing.T) { func TestModulesDoReceiveParentNamespace(t *testing.T) {
_ = setupTest(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -448,15 +444,16 @@ func TestModulesDoReceiveParentNamespace(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).RunTest(t)
// setupTest will report any errors // RunTest will report any errors
} }
func TestNamespaceImportsNotTransitive(t *testing.T) { func TestNamespaceImportsNotTransitive(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -482,42 +479,34 @@ func TestNamespaceImportsNotTransitive(t *testing.T) {
deps: ["a"], deps: ["a"],
} }
`, `,
}, }),
) ).
ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir3/Android.bp:5:5: "c" depends on undefined module "a"
expectedErrors := []error{
errors.New(`dir3/Android.bp:5:4: "c" depends on undefined module "a"
Module "c" is defined in namespace "dir3" which can read these 3 namespaces: ["dir3" "dir2" "."] Module "c" is defined in namespace "dir3" which can read these 3 namespaces: ["dir3" "dir2" "."]
Module "a" can be found in these namespaces: ["dir1"]`), Module "a" can be found in these namespaces: ["dir1"]\E`)).
} RunTest(t)
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestTwoNamepacesInSameDir(t *testing.T) { func TestTwoNamepacesInSameDir(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
soong_namespace { soong_namespace {
} }
`, `,
}, }),
) ).
ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:4:5: namespace dir1 already exists\E`)).
expectedErrors := []error{ RunTest(t)
errors.New(`dir1/Android.bp:4:4: namespace dir1 already exists`),
}
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestNamespaceNotAtTopOfFile(t *testing.T) { func TestNamespaceNotAtTopOfFile(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
test_module { test_module {
name: "a" name: "a"
@@ -525,20 +514,16 @@ func TestNamespaceNotAtTopOfFile(t *testing.T) {
soong_namespace { soong_namespace {
} }
`, `,
}, }),
) ).
ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:5:5: a namespace must be the first module in the file\E`)).
expectedErrors := []error{ RunTest(t)
errors.New(`dir1/Android.bp:5:4: a namespace must be the first module in the file`),
}
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) { func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) {
_, errs := setupTestExpectErrs(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -549,52 +534,44 @@ func TestTwoModulesWithSameNameInSameNamespace(t *testing.T) {
name: "a" name: "a"
} }
`, `,
}, }),
) ).
ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(`\Qdir1/Android.bp:7:5: module "a" already defined
expectedErrors := []error{ dir1/Android.bp:4:5 <-- previous definition here\E`)).
errors.New(`dir1/Android.bp:7:4: module "a" already defined RunTest(t)
dir1/Android.bp:4:4 <-- previous definition here`),
}
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
func TestDeclaringNamespaceInNonAndroidBpFile(t *testing.T) { func TestDeclaringNamespaceInNonAndroidBpFile(t *testing.T) {
_, errs := setupTestFromFiles(t, GroupFixturePreparers(
map[string][]byte{ prepareForTestWithNamespace,
"Android.bp": []byte(` FixtureWithRootAndroidBp(`
build = ["include.bp"] build = ["include.bp"]
`), `),
"include.bp": []byte(` FixtureAddTextFile("include.bp", `
soong_namespace { soong_namespace {
} }
`), `),
}, ).
) ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(
`\Qinclude.bp:2:5: A namespace may only be declared in a file named Android.bp\E`,
expectedErrors := []error{ )).
errors.New(`include.bp:2:5: A namespace may only be declared in a file named Android.bp`), RunTest(t)
}
if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
}
} }
// so that the generated .ninja file will have consistent names // so that the generated .ninja file will have consistent names
func TestConsistentNamespaceNames(t *testing.T) { func TestConsistentNamespaceNames(t *testing.T) {
ctx := setupTest(t, result := GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": "soong_namespace{}", "dir1": "soong_namespace{}",
"dir2": "soong_namespace{}", "dir2": "soong_namespace{}",
"dir3": "soong_namespace{}", "dir3": "soong_namespace{}",
}) }),
).RunTest(t)
ns1, _ := ctx.NameResolver.namespaceAt("dir1") ns1, _ := result.NameResolver.namespaceAt("dir1")
ns2, _ := ctx.NameResolver.namespaceAt("dir2") ns2, _ := result.NameResolver.namespaceAt("dir2")
ns3, _ := ctx.NameResolver.namespaceAt("dir3") ns3, _ := result.NameResolver.namespaceAt("dir3")
actualIds := []string{ns1.id, ns2.id, ns3.id} actualIds := []string{ns1.id, ns2.id, ns3.id}
expectedIds := []string{"1", "2", "3"} expectedIds := []string{"1", "2", "3"}
if !reflect.DeepEqual(actualIds, expectedIds) { if !reflect.DeepEqual(actualIds, expectedIds) {
@@ -604,8 +581,9 @@ func TestConsistentNamespaceNames(t *testing.T) {
// so that the generated .ninja file will have consistent names // so that the generated .ninja file will have consistent names
func TestRename(t *testing.T) { func TestRename(t *testing.T) {
_ = setupTest(t, GroupFixturePreparers(
map[string]string{ prepareForTestWithNamespace,
dirBpToPreparer(map[string]string{
"dir1": ` "dir1": `
soong_namespace { soong_namespace {
} }
@@ -617,23 +595,20 @@ func TestRename(t *testing.T) {
name: "b", name: "b",
rename: "c", rename: "c",
} }
`}) `,
// setupTest will report any errors }),
).RunTest(t)
// RunTest will report any errors
} }
// some utils to support the tests // some utils to support the tests
func mockFiles(bps map[string]string) (files map[string][]byte) { var prepareForTestWithNamespace = GroupFixturePreparers(
files = make(map[string][]byte, len(bps)) FixtureRegisterWithContext(registerNamespaceBuildComponents),
files["Android.bp"] = []byte("") FixtureRegisterWithContext(func(ctx RegistrationContext) {
for dir, text := range bps { ctx.PreArchMutators(RegisterNamespaceMutator)
files[filepath.Join(dir, "Android.bp")] = []byte(text) }),
}
return files
}
func setupTestFromFiles(t *testing.T, bps MockFS) (ctx *TestContext, errs []error) {
result := GroupFixturePreparers(
FixtureModifyContext(func(ctx *TestContext) { FixtureModifyContext(func(ctx *TestContext) {
ctx.RegisterModuleType("test_module", newTestModule) ctx.RegisterModuleType("test_module", newTestModule)
ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule) ctx.Context.RegisterModuleType("blueprint_test_module", newBlueprintTestModule)
@@ -641,66 +616,53 @@ func setupTestFromFiles(t *testing.T, bps MockFS) (ctx *TestContext, errs []erro
ctx.BottomUp("rename", renameMutator) ctx.BottomUp("rename", renameMutator)
}) })
}), }),
PrepareForTestWithNamespace, )
bps.AddToFixture(),
).
// Ignore errors for now so tests can check them later.
ExtendWithErrorHandler(FixtureIgnoreErrors).
RunTest(t)
return result.TestContext, result.Errs // dirBpToPreparer takes a map from directory to the contents of the Android.bp file and produces a
} // FixturePreparer.
func dirBpToPreparer(bps map[string]string) FixturePreparer {
func setupTestExpectErrs(t *testing.T, bps map[string]string) (ctx *TestContext, errs []error) { files := make(MockFS, len(bps))
files := make(map[string][]byte, len(bps))
files["Android.bp"] = []byte("") files["Android.bp"] = []byte("")
for dir, text := range bps { for dir, text := range bps {
files[filepath.Join(dir, "Android.bp")] = []byte(text) files[filepath.Join(dir, "Android.bp")] = []byte(text)
} }
return setupTestFromFiles(t, files) return files.AddToFixture()
} }
func setupTest(t *testing.T, bps map[string]string) (ctx *TestContext) { func dependsOn(result *TestResult, module TestingModule, possibleDependency TestingModule) bool {
t.Helper()
ctx, errs := setupTestExpectErrs(t, bps)
FailIfErrored(t, errs)
return ctx
}
func dependsOn(ctx *TestContext, module TestingModule, possibleDependency TestingModule) bool {
depends := false depends := false
visit := func(dependency blueprint.Module) { visit := func(dependency blueprint.Module) {
if dependency == possibleDependency.module { if dependency == possibleDependency.module {
depends = true depends = true
} }
} }
ctx.VisitDirectDeps(module.module, visit) result.VisitDirectDeps(module.module, visit)
return depends return depends
} }
func numDeps(ctx *TestContext, module TestingModule) int { func numDeps(result *TestResult, module TestingModule) int {
count := 0 count := 0
visit := func(dependency blueprint.Module) { visit := func(dependency blueprint.Module) {
count++ count++
} }
ctx.VisitDirectDeps(module.module, visit) result.VisitDirectDeps(module.module, visit)
return count return count
} }
func getModule(ctx *TestContext, moduleName string) TestingModule { func getModule(result *TestResult, moduleName string) TestingModule {
return ctx.ModuleForTests(moduleName, "") return result.ModuleForTests(moduleName, "")
} }
func findModuleById(ctx *TestContext, id string) (module TestingModule) { func findModuleById(result *TestResult, id string) (module TestingModule) {
visit := func(candidate blueprint.Module) { visit := func(candidate blueprint.Module) {
testModule, ok := candidate.(*testModule) testModule, ok := candidate.(*testModule)
if ok { if ok {
if testModule.properties.Id == id { if testModule.properties.Id == id {
module = newTestingModule(ctx.config, testModule) module = newTestingModule(result.config, testModule)
} }
} }
} }
ctx.VisitAllModules(visit) result.VisitAllModules(visit)
return module return module
} }
@@ -747,7 +709,7 @@ type blueprintTestModule struct {
} }
} }
func (b *blueprintTestModule) DynamicDependencies(ctx blueprint.DynamicDependerModuleContext) []string { func (b *blueprintTestModule) DynamicDependencies(_ blueprint.DynamicDependerModuleContext) []string {
return b.properties.Deps return b.properties.Deps
} }