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. 4 5 #include <sstream> 6 7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "tools/gn/ninja_target_writer.h" 9 #include "tools/gn/test_with_scope.h" 10 11 namespace { 12 13 class TestingNinjaTargetWriter : public NinjaTargetWriter { 14 public: 15 TestingNinjaTargetWriter(const Target* target, 16 const Toolchain* toolchain, 17 std::ostream& out) 18 : NinjaTargetWriter(target, toolchain, out) { 19 } 20 21 virtual void Run() OVERRIDE {} 22 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); 27 } 28 }; 29 30 } // namespace 31 32 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) { 33 TestWithScope setup; 34 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); 38 base_target.action_values().set_script(SourceFile("//foo/script.py")); 39 40 // Dependent target that also includes a source prerequisite (should get 41 // included) and a source (should not be included). 42 Target target(setup.settings(), Label(SourceDir("//foo/"), "target")); 43 target.set_output_type(Target::EXECUTABLE); 44 target.inputs().push_back(SourceFile("//foo/input.txt")); 45 target.sources().push_back(SourceFile("//foo/source.txt")); 46 target.deps().push_back(LabelTargetPair(&base_target)); 47 48 // Dependent action to test that action sources will be treated the same as 49 // inputs. 50 Target action(setup.settings(), Label(SourceDir("//foo/"), "action")); 51 action.set_output_type(Target::ACTION); 52 action.action_values().set_script(SourceFile("//foo/script.py")); 53 action.sources().push_back(SourceFile("//foo/action_source.txt")); 54 action.deps().push_back(LabelTargetPair(&target)); 55 56 base_target.OnResolved(); 57 target.OnResolved(); 58 action.OnResolved(); 59 60 // Input deps for the base (should be only the script itself). 61 { 62 std::ostringstream stream; 63 TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream); 64 std::string dep = 65 writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>()); 66 67 EXPECT_EQ(" | obj/foo/base.inputdeps.stamp", dep); 68 EXPECT_EQ("build obj/foo/base.inputdeps.stamp: stamp " 69 "../../foo/script.py\n", 70 stream.str()); 71 } 72 73 // Input deps for the target (should depend on the base). 74 { 75 std::ostringstream stream; 76 TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream); 77 std::string dep = 78 writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>()); 79 80 EXPECT_EQ(" | obj/foo/target.inputdeps.stamp", dep); 81 EXPECT_EQ("build obj/foo/target.inputdeps.stamp: stamp " 82 "../../foo/input.txt obj/foo/base.stamp\n", 83 stream.str()); 84 } 85 86 // Input deps for action which should depend on the base since its a hard dep 87 // that is a (indirect) dependency, as well as the the action source. 88 { 89 std::ostringstream stream; 90 TestingNinjaTargetWriter writer(&action, setup.toolchain(), stream); 91 std::string dep = 92 writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>()); 93 94 EXPECT_EQ(" | obj/foo/action.inputdeps.stamp", dep); 95 EXPECT_EQ("build obj/foo/action.inputdeps.stamp: stamp ../../foo/script.py " 96 "../../foo/action_source.txt obj/foo/base.stamp\n", 97 stream.str()); 98 } 99 } 100