Paul Duffin | 1812294 | 2021-08-24 19:01:25 +0100 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "testing" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | // Tests for build_release.go |
| 25 | |
| 26 | var ( |
| 27 | // Some additional test specific releases that are added after the currently supported ones and |
| 28 | // so are treated as being for future releases. |
| 29 | buildReleaseFuture1 = initBuildRelease("F1") |
| 30 | buildReleaseFuture2 = initBuildRelease("F2") |
| 31 | ) |
| 32 | |
| 33 | func TestNameToRelease(t *testing.T) { |
| 34 | t.Run("single release", func(t *testing.T) { |
| 35 | release, err := nameToRelease("S") |
| 36 | android.AssertDeepEquals(t, "errors", nil, err) |
| 37 | android.AssertDeepEquals(t, "release", buildReleaseS, release) |
| 38 | }) |
| 39 | t.Run("invalid release", func(t *testing.T) { |
| 40 | release, err := nameToRelease("A") |
| 41 | android.AssertDeepEquals(t, "release", (*buildRelease)(nil), release) |
| 42 | // Uses a wildcard in the error message to allow for additional build releases to be added to |
| 43 | // the supported set without breaking this test. |
| 44 | android.FailIfNoMatchingErrors(t, `unknown release "A", expected one of \[S,T.*,F1,F2\]`, []error{err}) |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | func TestParseBuildReleaseSet(t *testing.T) { |
| 49 | t.Run("single release", func(t *testing.T) { |
| 50 | set, err := parseBuildReleaseSet("S") |
| 51 | android.AssertDeepEquals(t, "errors", nil, err) |
| 52 | android.AssertStringEquals(t, "set", "[S]", set.String()) |
| 53 | }) |
| 54 | t.Run("open range", func(t *testing.T) { |
| 55 | set, err := parseBuildReleaseSet("F1+") |
| 56 | android.AssertDeepEquals(t, "errors", nil, err) |
| 57 | android.AssertStringEquals(t, "set", "[F1,F2]", set.String()) |
| 58 | }) |
| 59 | t.Run("closed range", func(t *testing.T) { |
| 60 | set, err := parseBuildReleaseSet("S-F1") |
| 61 | android.AssertDeepEquals(t, "errors", nil, err) |
| 62 | android.AssertStringEquals(t, "set", "[S,T,F1]", set.String()) |
| 63 | }) |
| 64 | invalidAReleaseMessage := `unknown release "A", expected one of ` + allBuildReleaseSet.String() |
| 65 | t.Run("invalid release", func(t *testing.T) { |
| 66 | set, err := parseBuildReleaseSet("A") |
| 67 | android.AssertDeepEquals(t, "set", (*buildReleaseSet)(nil), set) |
| 68 | android.AssertStringDoesContain(t, "errors", fmt.Sprint(err), invalidAReleaseMessage) |
| 69 | }) |
| 70 | t.Run("invalid release in open range", func(t *testing.T) { |
| 71 | set, err := parseBuildReleaseSet("A+") |
| 72 | android.AssertDeepEquals(t, "set", (*buildReleaseSet)(nil), set) |
| 73 | android.AssertStringDoesContain(t, "errors", fmt.Sprint(err), invalidAReleaseMessage) |
| 74 | }) |
| 75 | t.Run("invalid release in closed range start", func(t *testing.T) { |
| 76 | set, err := parseBuildReleaseSet("A-S") |
| 77 | android.AssertDeepEquals(t, "set", (*buildReleaseSet)(nil), set) |
| 78 | android.AssertStringDoesContain(t, "errors", fmt.Sprint(err), invalidAReleaseMessage) |
| 79 | }) |
| 80 | t.Run("invalid release in closed range end", func(t *testing.T) { |
| 81 | set, err := parseBuildReleaseSet("T-A") |
| 82 | android.AssertDeepEquals(t, "set", (*buildReleaseSet)(nil), set) |
| 83 | android.AssertStringDoesContain(t, "errors", fmt.Sprint(err), invalidAReleaseMessage) |
| 84 | }) |
| 85 | t.Run("invalid closed range reversed", func(t *testing.T) { |
| 86 | set, err := parseBuildReleaseSet("F1-S") |
| 87 | android.AssertDeepEquals(t, "set", (*buildReleaseSet)(nil), set) |
| 88 | android.AssertStringDoesContain(t, "errors", fmt.Sprint(err), `invalid closed range, start release "F1" is later than end release "S"`) |
| 89 | }) |
| 90 | } |
| 91 | |
| 92 | func TestBuildReleaseSetContains(t *testing.T) { |
| 93 | t.Run("contains", func(t *testing.T) { |
| 94 | set, _ := parseBuildReleaseSet("F1-F2") |
| 95 | android.AssertBoolEquals(t, "set contains F1", true, set.contains(buildReleaseFuture1)) |
| 96 | android.AssertBoolEquals(t, "set does not contain S", false, set.contains(buildReleaseS)) |
| 97 | android.AssertBoolEquals(t, "set contains F2", true, set.contains(buildReleaseFuture2)) |
| 98 | android.AssertBoolEquals(t, "set does not contain T", false, set.contains(buildReleaseT)) |
| 99 | }) |
| 100 | } |
Paul Duffin | 0c3acbf | 2021-08-24 19:01:25 +0100 | [diff] [blame] | 101 | |
| 102 | func TestPropertyPrunerInvalidTag(t *testing.T) { |
| 103 | type brokenStruct struct { |
| 104 | Broken string `supported_build_releases:"A"` |
| 105 | } |
| 106 | type containingStruct struct { |
| 107 | Nested brokenStruct |
| 108 | } |
| 109 | |
| 110 | t.Run("broken struct", func(t *testing.T) { |
| 111 | android.AssertPanicMessageContains(t, "error", "invalid `supported_build_releases` tag on Broken of *sdk.brokenStruct: unknown release \"A\"", func() { |
| 112 | newPropertyPrunerByBuildRelease(&brokenStruct{}, buildReleaseS) |
| 113 | }) |
| 114 | }) |
| 115 | |
| 116 | t.Run("nested broken struct", func(t *testing.T) { |
| 117 | android.AssertPanicMessageContains(t, "error", "invalid `supported_build_releases` tag on Nested.Broken of *sdk.containingStruct: unknown release \"A\"", func() { |
| 118 | newPropertyPrunerByBuildRelease(&containingStruct{}, buildReleaseS) |
| 119 | }) |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | func TestPropertyPrunerByBuildRelease(t *testing.T) { |
| 124 | type nested struct { |
| 125 | F1_only string `supported_build_releases:"F1"` |
| 126 | } |
| 127 | |
| 128 | type testBuildReleasePruner struct { |
| 129 | Default string |
| 130 | S_and_T_only string `supported_build_releases:"S-T"` |
| 131 | T_later string `supported_build_releases:"T+"` |
| 132 | Nested nested |
| 133 | } |
| 134 | |
| 135 | input := testBuildReleasePruner{ |
| 136 | Default: "Default", |
| 137 | S_and_T_only: "S_and_T_only", |
| 138 | T_later: "T_later", |
| 139 | Nested: nested{ |
| 140 | F1_only: "F1_only", |
| 141 | }, |
| 142 | } |
| 143 | |
| 144 | t.Run("target S", func(t *testing.T) { |
| 145 | testStruct := input |
| 146 | pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseS) |
| 147 | pruner.pruneProperties(&testStruct) |
| 148 | |
| 149 | expected := input |
| 150 | expected.T_later = "" |
| 151 | expected.Nested.F1_only = "" |
| 152 | android.AssertDeepEquals(t, "test struct", expected, testStruct) |
| 153 | }) |
| 154 | |
| 155 | t.Run("target T", func(t *testing.T) { |
| 156 | testStruct := input |
| 157 | pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseT) |
| 158 | pruner.pruneProperties(&testStruct) |
| 159 | |
| 160 | expected := input |
| 161 | expected.Nested.F1_only = "" |
| 162 | android.AssertDeepEquals(t, "test struct", expected, testStruct) |
| 163 | }) |
| 164 | |
| 165 | t.Run("target F1", func(t *testing.T) { |
| 166 | testStruct := input |
| 167 | pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseFuture1) |
| 168 | pruner.pruneProperties(&testStruct) |
| 169 | |
| 170 | expected := input |
| 171 | expected.S_and_T_only = "" |
| 172 | android.AssertDeepEquals(t, "test struct", expected, testStruct) |
| 173 | }) |
| 174 | |
| 175 | t.Run("target F2", func(t *testing.T) { |
| 176 | testStruct := input |
| 177 | pruner := newPropertyPrunerByBuildRelease(&testStruct, buildReleaseFuture2) |
| 178 | pruner.pruneProperties(&testStruct) |
| 179 | |
| 180 | expected := input |
| 181 | expected.S_and_T_only = "" |
| 182 | expected.Nested.F1_only = "" |
| 183 | android.AssertDeepEquals(t, "test struct", expected, testStruct) |
| 184 | }) |
| 185 | } |