OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "testing/gtest/include/gtest/gtest.h" |
| 6 #include "tools/gn/analyzer.h" |
| 7 #include "tools/gn/build_settings.h" |
| 8 #include "tools/gn/builder.h" |
| 9 #include "tools/gn/loader.h" |
| 10 #include "tools/gn/settings.h" |
| 11 #include "tools/gn/source_file.h" |
| 12 |
| 13 |
| 14 namespace { |
| 15 |
| 16 class MockLoader : public Loader { |
| 17 public: |
| 18 MockLoader() {} |
| 19 |
| 20 void Load(const SourceFile& file, |
| 21 const LocationRange& origin, |
| 22 const Label& toolchain_name) override {} |
| 23 void ToolchainLoaded(const Toolchain* toolchain) override {} |
| 24 Label GetDefaultToolchain() const override { |
| 25 return Label(SourceDir("//tc/"), "default"); |
| 26 } |
| 27 const Settings* GetToolchainSettings(const Label& label) const override { |
| 28 return nullptr; |
| 29 } |
| 30 |
| 31 private: |
| 32 ~MockLoader() override {} |
| 33 }; |
| 34 |
| 35 class AnalyzerTest : public testing::Test { |
| 36 public: |
| 37 AnalyzerTest() |
| 38 : loader_(new MockLoader), |
| 39 builder_(loader_.get()), |
| 40 settings_(&build_settings_, std::string()) { |
| 41 build_settings_.SetBuildDir(SourceDir("//out/")); |
| 42 settings_.set_toolchain_label(Label(SourceDir("//tc/"), "default")); |
| 43 settings_.set_default_toolchain_label(settings_.toolchain_label()); |
| 44 tc_dir_ = settings_.toolchain_label().dir(); |
| 45 tc_name_ = settings_.toolchain_label().name(); |
| 46 } |
| 47 |
| 48 Target* MakeTarget(const std::string dir, |
| 49 const std::string name, |
| 50 Target::OutputType type, |
| 51 const std::vector<std::string>& sources, |
| 52 const std::vector<Target*>& deps) { |
| 53 Label lbl(SourceDir(dir), name, tc_dir_, tc_name_); |
| 54 Target* target = new Target(&settings_, lbl); |
| 55 target->set_output_type(type); |
| 56 for (const auto& s : sources) |
| 57 target->sources().push_back(SourceFile(s)); |
| 58 for (const auto* d : deps) |
| 59 target->public_deps().push_back(LabelTargetPair(d)); |
| 60 builder_.ItemDefined(std::unique_ptr<Item>(target)); |
| 61 return target; |
| 62 } |
| 63 |
| 64 void AddSource(Target* a, std::string path) {} |
| 65 |
| 66 void AddDep(Target* a, Target* b) {} |
| 67 |
| 68 void SetUpABasicBuildGraph() { |
| 69 std::vector<std::string> no_sources; |
| 70 std::vector<Target*> no_deps; |
| 71 |
| 72 // All of the targets below are owned by the builder, so none of them |
| 73 // get leaked. |
| 74 |
| 75 // Ignore the returned target since nothing depends on it. |
| 76 MakeTarget("//", "a", Target::EXECUTABLE, {"//a.cc"}, no_deps); |
| 77 |
| 78 Target* b = |
| 79 MakeTarget("//d", "b", Target::SOURCE_SET, {"//d/b.cc"}, no_deps); |
| 80 |
| 81 Target* b_unittests = MakeTarget("//d", "b_unittests", Target::EXECUTABLE, |
| 82 {"//d/b_unittest.cc"}, {b}); |
| 83 |
| 84 Target* c = MakeTarget("//d", "c", Target::EXECUTABLE, {"//d/c.cc"}, {b}); |
| 85 |
| 86 Target* b_unittests_and_c = |
| 87 MakeTarget("//d", "b_unittests_and_c", Target::GROUP, no_sources, |
| 88 {b_unittests, c}); |
| 89 |
| 90 Target* e = |
| 91 MakeTarget("//d", "e", Target::EXECUTABLE, {"//d/e.cc"}, no_deps); |
| 92 |
| 93 // Also ignore this returned target since nothing depends on it. |
| 94 MakeTarget("//d", "d", Target::GROUP, no_sources, {b_unittests_and_c, e}); |
| 95 } |
| 96 |
| 97 void RunBasicTest(const std::string& input, |
| 98 const std::string& expected_output) { |
| 99 SetUpABasicBuildGraph(); |
| 100 Err err; |
| 101 std::string actual_output = Analyzer(builder_).Analyze(input, &err); |
| 102 EXPECT_EQ(err.has_error(), false); |
| 103 EXPECT_EQ(expected_output, actual_output); |
| 104 } |
| 105 |
| 106 protected: |
| 107 scoped_refptr<MockLoader> loader_; |
| 108 Builder builder_; |
| 109 BuildSettings build_settings_; |
| 110 Settings settings_; |
| 111 SourceDir tc_dir_; |
| 112 std::string tc_name_; |
| 113 }; |
| 114 |
| 115 } // namespace |
| 116 |
| 117 // TODO: clean this up when raw string literals are allowed. |
| 118 |
| 119 TEST_F(AnalyzerTest, AllWasPruned) { |
| 120 RunBasicTest( |
| 121 "{" |
| 122 " \"files\": [ \"//d/b.cc\" ]," |
| 123 " \"compile_targets\": [ \"all\" ]," |
| 124 " \"test_targets\": [ ]" |
| 125 "}", |
| 126 "{" |
| 127 "\"compile_targets\":[\"//d:b_unittests\",\"//d:c\"]," |
| 128 "\"status\":\"Found dependency\"," |
| 129 "\"test_targets\":[]" |
| 130 "}"); |
| 131 } |
| 132 |
| 133 TEST_F(AnalyzerTest, NoDependency) { |
| 134 RunBasicTest( |
| 135 "{" |
| 136 " \"files\":[ \"//missing.cc\" ]," |
| 137 " \"compile_targets\": [ \"all\" ]," |
| 138 " \"test_targets\": [ \"//:a\" ]" |
| 139 "}", |
| 140 "{" |
| 141 "\"compile_targets\":[]," |
| 142 "\"status\":\"No dependency\"," |
| 143 "\"test_targets\":[]" |
| 144 "}"); |
| 145 } |
| 146 |
| 147 TEST_F(AnalyzerTest, NoFilesNoTargets) { |
| 148 RunBasicTest( |
| 149 "{" |
| 150 " \"files\": []," |
| 151 " \"compile_targets\": []," |
| 152 " \"test_targets\": []" |
| 153 "}", |
| 154 "{" |
| 155 "\"compile_targets\":[]," |
| 156 "\"status\":\"No dependency\"," |
| 157 "\"test_targets\":[]" |
| 158 "}"); |
| 159 } |
| 160 |
| 161 TEST_F(AnalyzerTest, OneTestTargetModified) { |
| 162 RunBasicTest( |
| 163 "{" |
| 164 " \"files\": [ \"//a.cc\" ]," |
| 165 " \"compile_targets\": []," |
| 166 " \"test_targets\": [ \"//:a\" ]" |
| 167 "}", |
| 168 "{" |
| 169 "\"compile_targets\":[]," |
| 170 "\"status\":\"Found dependency\"," |
| 171 "\"test_targets\":[\"//:a\"]" |
| 172 "}"); |
| 173 } |
OLD | NEW |