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 | } |