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
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 := MockFS{
57		"test_ninja_deps/exists": nil,
58	}
59
60	result := GroupFixturePreparers(
61		FixtureRegisterWithContext(func(ctx RegistrationContext) {
62			ctx.RegisterSingletonType("test_ninja_deps_singleton", testNinjaDepsSingletonFactory)
63			ctx.RegisterSingletonType("ninja_deps_singleton", ninjaDepsSingletonFactory)
64		}),
65		fs.AddToFixture(),
66	).RunTest(t)
67
68	// Verify that the ninja file has a dependency on the test_ninja_deps directory.
69	if g, w := result.NinjaDeps, "test_ninja_deps"; !InList(w, g) {
70		t.Errorf("expected %q in %q", w, g)
71	}
72}
73