| Cole Faust | ba48366 | 2024-06-17 15:03:33 -0700 | [diff] [blame] | 1 | // Copyright 2024 Google Inc. All rights reserved. | 
|  | 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 android | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "testing" | 
|  | 19 | ) | 
|  | 20 |  | 
|  | 21 | var testCases []struct { | 
|  | 22 | name          string | 
|  | 23 | fs            MockFS | 
|  | 24 | expectedError string | 
|  | 25 | } = []struct { | 
|  | 26 | name          string | 
|  | 27 | fs            MockFS | 
|  | 28 | expectedError string | 
|  | 29 | }{ | 
|  | 30 | { | 
|  | 31 | name: "Can't reference variable before assignment", | 
|  | 32 | fs: map[string][]byte{ | 
|  | 33 | "Android.bp": []byte(` | 
|  | 34 | x = foo | 
|  | 35 | foo = "hello" | 
|  | 36 | `), | 
|  | 37 | }, | 
|  | 38 | expectedError: "undefined variable foo", | 
|  | 39 | }, | 
|  | 40 | { | 
|  | 41 | name: "Can't append to variable before assigned to", | 
|  | 42 | fs: map[string][]byte{ | 
|  | 43 | "Android.bp": []byte(` | 
|  | 44 | foo += "world" | 
|  | 45 | foo = "hello" | 
|  | 46 | `), | 
|  | 47 | }, | 
|  | 48 | expectedError: "modified non-existent variable \"foo\" with \\+=", | 
|  | 49 | }, | 
|  | 50 | { | 
|  | 51 | name: "Can't reassign variable", | 
|  | 52 | fs: map[string][]byte{ | 
|  | 53 | "Android.bp": []byte(` | 
|  | 54 | foo = "hello" | 
|  | 55 | foo = "world" | 
|  | 56 | `), | 
|  | 57 | }, | 
|  | 58 | expectedError: "variable already set, previous assignment:", | 
|  | 59 | }, | 
|  | 60 | { | 
|  | 61 | name: "Can't reassign variable in inherited scope", | 
|  | 62 | fs: map[string][]byte{ | 
|  | 63 | "Android.bp": []byte(` | 
|  | 64 | foo = "hello" | 
|  | 65 | `), | 
|  | 66 | "foo/Android.bp": []byte(` | 
|  | 67 | foo = "world" | 
|  | 68 | `), | 
|  | 69 | }, | 
|  | 70 | expectedError: "variable already set in inherited scope, previous assignment:", | 
|  | 71 | }, | 
|  | 72 | { | 
|  | 73 | name: "Can't modify variable in inherited scope", | 
|  | 74 | fs: map[string][]byte{ | 
|  | 75 | "Android.bp": []byte(` | 
|  | 76 | foo = "hello" | 
|  | 77 | `), | 
|  | 78 | "foo/Android.bp": []byte(` | 
|  | 79 | foo += "world" | 
|  | 80 | `), | 
|  | 81 | }, | 
|  | 82 | expectedError: "modified non-local variable \"foo\" with \\+=", | 
|  | 83 | }, | 
|  | 84 | { | 
|  | 85 | name: "Can't modify variable after referencing", | 
|  | 86 | fs: map[string][]byte{ | 
|  | 87 | "Android.bp": []byte(` | 
|  | 88 | foo = "hello" | 
|  | 89 | x = foo | 
|  | 90 | foo += "world" | 
|  | 91 | `), | 
|  | 92 | }, | 
|  | 93 | expectedError: "modified variable \"foo\" with \\+= after referencing", | 
|  | 94 | }, | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | func TestBlueprintErrors(t *testing.T) { | 
|  | 98 | for _, tc := range testCases { | 
|  | 99 | t.Run(tc.name, func(t *testing.T) { | 
|  | 100 | fixtures := FixtureMergeMockFs(tc.fs) | 
|  | 101 | fixtures = fixtures.ExtendWithErrorHandler(FixtureExpectsOneErrorPattern(tc.expectedError)) | 
|  | 102 | fixtures.RunTest(t) | 
|  | 103 | }) | 
|  | 104 | } | 
|  | 105 | } |