1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/ninja_target_writer.h"
9 #include "tools/gn/test_with_scope.h"
13 class TestingNinjaTargetWriter : public NinjaTargetWriter {
15 TestingNinjaTargetWriter(const Target* target,
16 const Toolchain* toolchain,
18 : NinjaTargetWriter(target, toolchain, out) {
21 virtual void Run() OVERRIDE {}
23 // Make this public so the test can call it.
24 std::string WriteInputDepsStampAndGetDep(
25 const std::vector<const Target*>& extra_hard_deps) {
26 return NinjaTargetWriter::WriteInputDepsStampAndGetDep(extra_hard_deps);
32 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
35 // Make a base target that's a hard dep (action).
36 Target base_target(setup.settings(), Label(SourceDir("//foo/"), "base"));
37 base_target.set_output_type(Target::ACTION);
39 // Dependent target that also includes a source prerequisite (should get
40 // included) and a source (should not be included).
41 Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
42 target.set_output_type(Target::EXECUTABLE);
43 target.source_prereqs().push_back(SourceFile("//foo/input.txt"));
44 target.sources().push_back(SourceFile("//foo/source.txt"));
45 target.deps().push_back(LabelTargetPair(&base_target));
47 // Dependent action to test that action sources will be treated the same as
49 Target action(setup.settings(), Label(SourceDir("//foo/"), "action"));
50 action.set_output_type(Target::ACTION);
51 action.sources().push_back(SourceFile("//foo/action_source.txt"));
52 action.deps().push_back(LabelTargetPair(&target));
54 base_target.OnResolved();
58 // Input deps for the base (should be nothing, it has no hard deps).
60 std::ostringstream stream;
61 TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream);
63 writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>());
65 EXPECT_TRUE(dep.empty());
66 EXPECT_TRUE(stream.str().empty());
69 // Input deps for the target (should depend on the base).
71 std::ostringstream stream;
72 TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
74 writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>());
76 EXPECT_EQ(" | obj/foo/target.inputdeps.stamp", dep);
77 EXPECT_EQ("build obj/foo/target.inputdeps.stamp: stamp "
78 "../../foo/input.txt obj/foo/base.stamp\n",
82 // Input deps for action which should depend on the base since its a hard dep
83 // that is a (indirect) dependency, as well as the the action source.
85 std::ostringstream stream;
86 TestingNinjaTargetWriter writer(&action, setup.toolchain(), stream);
88 writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>());
90 EXPECT_EQ(" | obj/foo/action.inputdeps.stamp", dep);
91 EXPECT_EQ("build obj/foo/action.inputdeps.stamp: stamp "
92 "../../foo/action_source.txt obj/foo/base.stamp\n",