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