Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 1 | package android |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/google/blueprint" |
| 7 | ) |
| 8 | |
| 9 | var licenseKindTests = []struct { |
| 10 | name string |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame^] | 11 | fs MockFS |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 12 | expectedErrors []string |
| 13 | }{ |
| 14 | { |
| 15 | name: "license_kind must not accept licenses property", |
| 16 | fs: map[string][]byte{ |
| 17 | "top/Blueprints": []byte(` |
| 18 | license_kind { |
| 19 | name: "top_license", |
| 20 | licenses: ["other_license"], |
| 21 | }`), |
| 22 | }, |
| 23 | expectedErrors: []string{ |
| 24 | `top/Blueprints:4:14: unrecognized property "licenses"`, |
| 25 | }, |
| 26 | }, |
| 27 | { |
| 28 | name: "bad license_kind", |
| 29 | fs: map[string][]byte{ |
| 30 | "top/Blueprints": []byte(` |
| 31 | license_kind { |
| 32 | name: "top_notice", |
| 33 | conditions: ["notice"], |
| 34 | }`), |
| 35 | "other/Blueprints": []byte(` |
| 36 | mock_license { |
| 37 | name: "other_notice", |
| 38 | license_kinds: ["notice"], |
| 39 | }`), |
| 40 | }, |
| 41 | expectedErrors: []string{ |
| 42 | `other/Blueprints:2:5: "other_notice" depends on undefined module "notice"`, |
| 43 | }, |
| 44 | }, |
| 45 | { |
| 46 | name: "good license kind", |
| 47 | fs: map[string][]byte{ |
| 48 | "top/Blueprints": []byte(` |
| 49 | license_kind { |
| 50 | name: "top_by_exception_only", |
| 51 | conditions: ["by_exception_only"], |
| 52 | } |
| 53 | |
| 54 | mock_license { |
| 55 | name: "top_proprietary", |
| 56 | license_kinds: ["top_by_exception_only"], |
| 57 | }`), |
| 58 | "other/Blueprints": []byte(` |
| 59 | mock_license { |
| 60 | name: "other_proprietary", |
| 61 | license_kinds: ["top_proprietary"], |
| 62 | }`), |
| 63 | }, |
| 64 | }, |
| 65 | { |
| 66 | name: "multiple license kinds", |
| 67 | fs: map[string][]byte{ |
| 68 | "top/Blueprints": []byte(` |
| 69 | license_kind { |
| 70 | name: "top_notice", |
| 71 | conditions: ["notice"], |
| 72 | } |
| 73 | |
| 74 | license_kind { |
| 75 | name: "top_by_exception_only", |
| 76 | conditions: ["by_exception_only"], |
| 77 | } |
| 78 | |
| 79 | mock_license { |
| 80 | name: "top_allowed_as_notice", |
| 81 | license_kinds: ["top_notice"], |
| 82 | } |
| 83 | |
| 84 | mock_license { |
| 85 | name: "top_proprietary", |
| 86 | license_kinds: ["top_by_exception_only"], |
| 87 | }`), |
| 88 | "other/Blueprints": []byte(` |
| 89 | mock_license { |
| 90 | name: "other_rule", |
| 91 | license_kinds: ["top_by_exception_only"], |
| 92 | }`), |
| 93 | }, |
| 94 | }, |
| 95 | } |
| 96 | |
| 97 | func TestLicenseKind(t *testing.T) { |
| 98 | for _, test := range licenseKindTests { |
| 99 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | 8bd2865 | 2021-03-03 00:42:36 +0000 | [diff] [blame^] | 100 | licenseTestFixtureFactory. |
| 101 | Extend( |
| 102 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 103 | ctx.RegisterModuleType("mock_license", newMockLicenseModule) |
| 104 | }), |
| 105 | test.fs.AddToFixture(), |
| 106 | ).ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern(test.expectedErrors)). |
| 107 | RunTest(t) |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 112 | type mockLicenseProperties struct { |
| 113 | License_kinds []string |
| 114 | } |
| 115 | |
| 116 | type mockLicenseModule struct { |
| 117 | ModuleBase |
| 118 | DefaultableModuleBase |
| 119 | |
| 120 | properties mockLicenseProperties |
| 121 | } |
| 122 | |
| 123 | func newMockLicenseModule() Module { |
| 124 | m := &mockLicenseModule{} |
| 125 | m.AddProperties(&m.properties) |
| 126 | InitAndroidArchModule(m, HostAndDeviceSupported, MultilibCommon) |
| 127 | InitDefaultableModule(m) |
| 128 | return m |
| 129 | } |
| 130 | |
| 131 | type licensekindTag struct { |
| 132 | blueprint.BaseDependencyTag |
| 133 | } |
| 134 | |
| 135 | func (j *mockLicenseModule) DepsMutator(ctx BottomUpMutatorContext) { |
| 136 | m, ok := ctx.Module().(Module) |
| 137 | if !ok { |
| 138 | return |
| 139 | } |
| 140 | ctx.AddDependency(m, licensekindTag{}, j.properties.License_kinds...) |
| 141 | } |
| 142 | |
| 143 | func (p *mockLicenseModule) GenerateAndroidBuildActions(ModuleContext) { |
| 144 | } |