blob: d3775edd9d474d19462b5483a1a52bba9ffaee7b [file] [log] [blame]
Colin Cross12129292020-10-29 18:23:58 -07001// 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
15package android
16
17import (
18 "testing"
19)
20
21func 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
36func testNinjaDepsSingletonFactory() Singleton {
37 return testNinjaDepsSingleton{}
38}
39
40type testNinjaDepsSingleton struct{}
41
42func (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
55func TestNinjaDeps(t *testing.T) {
56 fs := map[string][]byte{
57 "test_ninja_deps/exists": nil,
58 }
59 config := TestConfig(buildDir, nil, "", fs)
60
61 ctx := NewTestContext(config)
62 ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory)
63 ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory)
64 ctx.Register()
65
66 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
67 FailIfErrored(t, errs)
68 ninjaDeps, errs := ctx.PrepareBuildActions(config)
69 FailIfErrored(t, errs)
70
71 // Verify that the ninja file has a dependency on the test_ninja_deps directory.
72 if g, w := ninjaDeps, "test_ninja_deps"; !InList(w, g) {
73 t.Errorf("expected %q in %q", w, g)
74 }
75}