require namespaces to be declared only in files named Android.bp

Bug: 65683273
Test: m -j nothing # which runs unit tests

Change-Id: I5edf0e0482809f5ac9fb9dfff342fb404e1c52da
diff --git a/android/namespace_test.go b/android/namespace_test.go
index b10b528..13da88b 100644
--- a/android/namespace_test.go
+++ b/android/namespace_test.go
@@ -151,7 +151,7 @@
 
 	expectedErrors := []error{
 		errors.New(
-			`dir3/Blueprints:4:4: "b" depends on undefined module "a"
+			`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 "a" can be found in these namespaces: ["dir1" "dir2"]`),
 	}
@@ -373,7 +373,7 @@
 
 	// should complain about the missing namespace and not complain about the unresolvable dependency
 	expectedErrors := []error{
-		errors.New(`dir1/Blueprints:2:4: module "soong_namespace": namespace a_nonexistent_namespace does not exist`),
+		errors.New(`dir1/Android.bp:2:4: module "soong_namespace": namespace a_nonexistent_namespace does not exist`),
 	}
 	if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
 		t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
@@ -402,7 +402,7 @@
 	)
 
 	expectedErrors := []error{
-		errors.New(`dir1/subdir1/Blueprints:4:4: "b" depends on undefined module "a"
+		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 "a" can be found in these namespaces: ["dir1"]`),
 	}
@@ -465,7 +465,7 @@
 	)
 
 	expectedErrors := []error{
-		errors.New(`dir3/Blueprints:5:4: "c" depends on undefined module "a"
+		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 "a" can be found in these namespaces: ["dir1"]`),
 	}
@@ -487,7 +487,7 @@
 	)
 
 	expectedErrors := []error{
-		errors.New(`dir1/Blueprints:4:4: namespace dir1 already exists`),
+		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)
@@ -508,7 +508,7 @@
 	)
 
 	expectedErrors := []error{
-		errors.New(`dir1/Blueprints:5:4: a namespace must be the first module in the file`),
+		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)
@@ -532,26 +532,48 @@
 	)
 
 	expectedErrors := []error{
-		errors.New(`dir1/Blueprints:7:4: module "a" already defined
-       dir1/Blueprints:4:4 <-- previous definition here`),
+		errors.New(`dir1/Android.bp:7:4: module "a" already defined
+       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) {
+	_, errs := setupTestFromFiles(
+		map[string][]byte{
+			"Android.bp": []byte(`
+				build = ["include.bp"]
+			`),
+			"include.bp": []byte(`
+				soong_namespace {
+				}
+			`),
+		},
+	)
+
+	expectedErrors := []error{
+		errors.New(`include.bp:2:5: A namespace may only be declared in a file named Android.bp`),
+	}
+
+	if len(errs) != 1 || errs[0].Error() != expectedErrors[0].Error() {
+		t.Errorf("Incorrect errors. Expected:\n%v\n, got:\n%v\n", expectedErrors, errs)
+	}
+}
+
 // some utils to support the tests
 
 func mockFiles(bps map[string]string) (files map[string][]byte) {
 	files = make(map[string][]byte, len(bps))
-	files["Blueprints"] = []byte("")
+	files["Android.bp"] = []byte("")
 	for dir, text := range bps {
-		files[filepath.Join(dir, "Blueprints")] = []byte(text)
+		files[filepath.Join(dir, "Android.bp")] = []byte(text)
 	}
 	return files
 }
 
-func setupTestExpectErrs(bps map[string]string) (ctx *TestContext, errs []error) {
+func setupTestFromFiles(bps map[string][]byte) (ctx *TestContext, errs []error) {
 	buildDir, err := ioutil.TempDir("", "soong_namespace_test")
 	if err != nil {
 		return nil, []error{err}
@@ -561,13 +583,13 @@
 	config := TestConfig(buildDir, nil)
 
 	ctx = NewTestContext()
-	ctx.MockFileSystem(mockFiles(bps))
+	ctx.MockFileSystem(bps)
 	ctx.RegisterModuleType("test_module", ModuleFactoryAdaptor(newTestModule))
 	ctx.RegisterModuleType("soong_namespace", ModuleFactoryAdaptor(NamespaceFactory))
 	ctx.PreDepsMutators(RegisterNamespaceMutator)
 	ctx.Register()
 
-	_, errs = ctx.ParseBlueprintsFiles("Blueprints")
+	_, errs = ctx.ParseBlueprintsFiles("Android.bp")
 	if len(errs) > 0 {
 		return ctx, errs
 	}
@@ -575,6 +597,15 @@
 	return ctx, errs
 }
 
+func setupTestExpectErrs(bps map[string]string) (ctx *TestContext, errs []error) {
+	files := make(map[string][]byte, len(bps))
+	files["Android.bp"] = []byte("")
+	for dir, text := range bps {
+		files[filepath.Join(dir, "Android.bp")] = []byte(text)
+	}
+	return setupTestFromFiles(files)
+}
+
 func setupTest(t *testing.T, bps map[string]string) (ctx *TestContext) {
 	ctx, errs := setupTestExpectErrs(bps)
 	failIfErrored(t, errs)