package_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package android
  2. import (
  3. "testing"
  4. )
  5. var packageTests = []struct {
  6. name string
  7. fs map[string][]byte
  8. expectedErrors []string
  9. }{
  10. // Package default_visibility handling is tested in visibility_test.go
  11. {
  12. name: "package must not accept visibility and name properties",
  13. fs: map[string][]byte{
  14. "top/Blueprints": []byte(`
  15. package {
  16. name: "package",
  17. visibility: ["//visibility:private"],
  18. }`),
  19. },
  20. expectedErrors: []string{
  21. `top/Blueprints:3:10: unrecognized property "name"`,
  22. `top/Blueprints:4:16: unrecognized property "visibility"`,
  23. },
  24. },
  25. {
  26. name: "multiple packages in separate directories",
  27. fs: map[string][]byte{
  28. "top/Blueprints": []byte(`
  29. package {
  30. }`),
  31. "other/Blueprints": []byte(`
  32. package {
  33. }`),
  34. "other/nested/Blueprints": []byte(`
  35. package {
  36. }`),
  37. },
  38. },
  39. {
  40. name: "package must not be specified more than once per package",
  41. fs: map[string][]byte{
  42. "top/Blueprints": []byte(`
  43. package {
  44. default_visibility: ["//visibility:private"],
  45. }
  46. package {
  47. }`),
  48. },
  49. expectedErrors: []string{
  50. `module "//top" already defined`,
  51. },
  52. },
  53. }
  54. func TestPackage(t *testing.T) {
  55. for _, test := range packageTests {
  56. t.Run(test.name, func(t *testing.T) {
  57. _, errs := testPackage(test.fs)
  58. expectedErrors := test.expectedErrors
  59. if expectedErrors == nil {
  60. FailIfErrored(t, errs)
  61. } else {
  62. for _, expectedError := range expectedErrors {
  63. FailIfNoMatchingErrors(t, expectedError, errs)
  64. }
  65. if len(errs) > len(expectedErrors) {
  66. t.Errorf("additional errors found, expected %d, found %d", len(expectedErrors), len(errs))
  67. for i, expectedError := range expectedErrors {
  68. t.Errorf("expectedErrors[%d] = %s", i, expectedError)
  69. }
  70. for i, err := range errs {
  71. t.Errorf("errs[%d] = %s", i, err)
  72. }
  73. }
  74. }
  75. })
  76. }
  77. }
  78. func testPackage(fs map[string][]byte) (*TestContext, []error) {
  79. // Create a new config per test as visibility information is stored in the config.
  80. config := TestArchConfig(buildDir, nil, "", fs)
  81. ctx := NewTestArchContext()
  82. RegisterPackageBuildComponents(ctx)
  83. ctx.Register(config)
  84. _, errs := ctx.ParseBlueprintsFiles(".")
  85. if len(errs) > 0 {
  86. return ctx, errs
  87. }
  88. _, errs = ctx.PrepareBuildActions(config)
  89. return ctx, errs
  90. }