Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 | func init() { |
| 22 | // This variable uses ExistentPathForSource on a PackageVarContext, which is a PathContext |
| 23 | // that is not a PathGlobContext. That requires the deps to be stored in the Config. |
| 24 | pctx.VariableFunc("test_ninja_deps_variable", func(ctx PackageVarContext) string { |
| 25 | // Using ExistentPathForSource to look for a file that does not exist in a directory that |
| 26 | // does exist (test_ninja_deps) from a PackageVarContext adds a dependency from build.ninja |
| 27 | // to the directory. |
| 28 | if ExistentPathForSource(ctx, "test_ninja_deps/does_not_exist").Valid() { |
| 29 | return "true" |
| 30 | } else { |
| 31 | return "false" |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | func testNinjaDepsSingletonFactory() Singleton { |
| 37 | return testNinjaDepsSingleton{} |
| 38 | } |
| 39 | |
| 40 | type testNinjaDepsSingleton struct{} |
| 41 | |
| 42 | func (testNinjaDepsSingleton) GenerateBuildActions(ctx SingletonContext) { |
| 43 | // Reference the test_ninja_deps_variable in a build statement so Blueprint is forced to |
| 44 | // evaluate it. |
| 45 | ctx.Build(pctx, BuildParams{ |
| 46 | Rule: Cp, |
| 47 | Input: PathForTesting("foo"), |
| 48 | Output: PathForOutput(ctx, "test_ninja_deps_out"), |
| 49 | Args: map[string]string{ |
| 50 | "cpFlags": "${test_ninja_deps_variable}", |
| 51 | }, |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | func TestNinjaDeps(t *testing.T) { |
Paul Duffin | d9ec7d2 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 56 | fs := MockFS{ |
Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 57 | "test_ninja_deps/exists": nil, |
| 58 | } |
Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 59 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame^] | 60 | result := GroupFixturePreparers( |
Paul Duffin | d9ec7d2 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 61 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 62 | ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory) |
| 63 | ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory) |
| 64 | }), |
| 65 | fs.AddToFixture(), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame^] | 66 | ).RunTest(t) |
Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 67 | |
| 68 | // Verify that the ninja file has a dependency on the test_ninja_deps directory. |
Paul Duffin | d9ec7d2 | 2021-03-16 23:57:12 +0000 | [diff] [blame] | 69 | if g, w := result.NinjaDeps, "test_ninja_deps"; !InList(w, g) { |
Colin Cross | 1212929 | 2020-10-29 18:23:58 -0700 | [diff] [blame] | 70 | t.Errorf("expected %q in %q", w, g) |
| 71 | } |
| 72 | } |