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 15package android 16 17import ( 18 "testing" 19) 20 21var 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(` 34x = foo 35foo = "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(` 44foo += "world" 45foo = "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(` 54foo = "hello" 55foo = "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(` 64foo = "hello" 65`), 66 "foo/Android.bp": []byte(` 67foo = "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(` 76foo = "hello" 77`), 78 "foo/Android.bp": []byte(` 79foo += "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(` 88foo = "hello" 89x = foo 90foo += "world" 91`), 92 }, 93 expectedError: "modified variable \"foo\" with \\+= after referencing", 94 }, 95} 96 97func 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} 106