OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 #include <utility> | 6 #include <utility> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 for (const auto& cur : pending_) { | 101 for (const auto& cur : pending_) { |
102 CannedResponseMap::const_iterator found = canned_responses_.find(cur.first); | 102 CannedResponseMap::const_iterator found = canned_responses_.find(cur.first); |
103 if (found == canned_responses_.end()) | 103 if (found == canned_responses_.end()) |
104 cur.second.Run(&block); | 104 cur.second.Run(&block); |
105 else | 105 else |
106 cur.second.Run(found->second->root.get()); | 106 cur.second.Run(found->second->root.get()); |
107 } | 107 } |
108 pending_.clear(); | 108 pending_.clear(); |
109 } | 109 } |
110 | 110 |
| 111 class MockBuilder { |
| 112 public: |
| 113 void OnItemDefined(std::unique_ptr<Item> item); |
| 114 |
| 115 bool HasOneDefined(std::set<std::string> source_files) const; |
| 116 void Cleanup(); |
| 117 |
| 118 private: |
| 119 std::vector<std::unique_ptr<Item>> items_; |
| 120 }; |
| 121 |
| 122 void MockBuilder::OnItemDefined(std::unique_ptr<Item> item) { |
| 123 items_.push_back(std::move(item)); |
| 124 } |
| 125 |
| 126 bool MockBuilder::HasOneDefined(std::set<std::string> source_files) const { |
| 127 if (items_.size() != 1u) |
| 128 return false; |
| 129 |
| 130 const auto& input_files = items_[0]->input_files(); |
| 131 for (const InputFile* input_file : input_files) { |
| 132 auto found_source_file = source_files.find(input_file->name().value()); |
| 133 if (found_source_file == source_files.end()) |
| 134 return false; |
| 135 |
| 136 source_files.erase(found_source_file); |
| 137 } |
| 138 |
| 139 return source_files.empty(); |
| 140 }; |
| 141 |
| 142 void MockBuilder::Cleanup() { |
| 143 items_.clear(); |
| 144 } |
| 145 |
111 // LoaderTest ------------------------------------------------------------------ | 146 // LoaderTest ------------------------------------------------------------------ |
112 | 147 |
113 class LoaderTest : public testing::Test { | 148 class LoaderTest : public testing::Test { |
114 public: | 149 public: |
115 LoaderTest() { | 150 LoaderTest() { |
116 build_settings_.SetBuildDir(SourceDir("//out/Debug/")); | 151 build_settings_.SetBuildDir(SourceDir("//out/Debug/")); |
117 } | 152 } |
118 | 153 |
119 protected: | 154 protected: |
120 Scheduler scheduler_; | 155 Scheduler scheduler_; |
121 BuildSettings build_settings_; | 156 BuildSettings build_settings_; |
| 157 MockBuilder mock_builder_; |
122 MockInputFileManager mock_ifm_; | 158 MockInputFileManager mock_ifm_; |
| 159 std::vector<std::unique_ptr<Item>> items_; |
123 }; | 160 }; |
124 | 161 |
125 } // namespace | 162 } // namespace |
126 | 163 |
127 // ----------------------------------------------------------------------------- | 164 // ----------------------------------------------------------------------------- |
128 | 165 |
129 TEST_F(LoaderTest, Foo) { | 166 TEST_F(LoaderTest, Foo) { |
130 SourceFile build_config("//build/config/BUILDCONFIG.gn"); | 167 SourceFile build_config("//build/config/BUILDCONFIG.gn"); |
131 build_settings_.set_build_config_file(build_config); | 168 build_settings_.set_build_config_file(build_config); |
132 | 169 |
(...skipping 27 matching lines...) Expand all Loading... |
160 EXPECT_TRUE(mock_ifm_.HasOnePending(SourceFile("//tc2/BUILD.gn"))); | 197 EXPECT_TRUE(mock_ifm_.HasOnePending(SourceFile("//tc2/BUILD.gn"))); |
161 | 198 |
162 // Running the toolchain file should schedule the build config file to load | 199 // Running the toolchain file should schedule the build config file to load |
163 // for that toolchain. | 200 // for that toolchain. |
164 mock_ifm_.IssueAllPending(); | 201 mock_ifm_.IssueAllPending(); |
165 base::RunLoop().RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
166 | 203 |
167 // We have to tell it we have a toolchain definition now (normally the | 204 // We have to tell it we have a toolchain definition now (normally the |
168 // builder would do this). | 205 // builder would do this). |
169 const Settings* default_settings = loader->GetToolchainSettings(Label()); | 206 const Settings* default_settings = loader->GetToolchainSettings(Label()); |
170 Toolchain second_tc_object(default_settings, second_tc); | 207 Toolchain second_tc_object(default_settings, second_tc, {}); |
171 loader->ToolchainLoaded(&second_tc_object); | 208 loader->ToolchainLoaded(&second_tc_object); |
172 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); | 209 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); |
173 | 210 |
174 // Scheduling a second file to load in that toolchain should not make it | 211 // Scheduling a second file to load in that toolchain should not make it |
175 // pending yet (it's waiting for the build config). | 212 // pending yet (it's waiting for the build config). |
176 SourceFile third_file("//bar/BUILD.gn"); | 213 SourceFile third_file("//bar/BUILD.gn"); |
177 loader->Load(third_file, LocationRange(), second_tc); | 214 loader->Load(third_file, LocationRange(), second_tc); |
178 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); | 215 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); |
179 | 216 |
180 // Running the build config file should make our third file pending. | 217 // Running the build config file should make our third file pending. |
181 mock_ifm_.IssueAllPending(); | 218 mock_ifm_.IssueAllPending(); |
182 base::RunLoop().RunUntilIdle(); | 219 base::RunLoop().RunUntilIdle(); |
183 EXPECT_TRUE(mock_ifm_.HasTwoPending(second_file, third_file)); | 220 EXPECT_TRUE(mock_ifm_.HasTwoPending(second_file, third_file)); |
184 | 221 |
185 EXPECT_FALSE(scheduler_.is_failed()); | 222 EXPECT_FALSE(scheduler_.is_failed()); |
186 } | 223 } |
| 224 |
| 225 TEST_F(LoaderTest, SourceFilesAreCollected) { |
| 226 SourceFile build_config("//build/config/BUILDCONFIG.gn"); |
| 227 SourceFile root_build("//BUILD.gn"); |
| 228 build_settings_.set_build_config_file(build_config); |
| 229 |
| 230 build_settings_.set_item_defined_callback(base::Bind( |
| 231 &MockBuilder::OnItemDefined, base::Unretained(&mock_builder_))); |
| 232 |
| 233 scoped_refptr<LoaderImpl> loader(new LoaderImpl(&build_settings_)); |
| 234 |
| 235 mock_ifm_.AddCannedResponse(build_config, |
| 236 "set_default_toolchain(\"//tc:tc\")"); |
| 237 mock_ifm_.AddCannedResponse(root_build, |
| 238 "executable(\"a\") { sources = [ \"a.cc\" ] }"); |
| 239 |
| 240 loader->set_async_load_file(mock_ifm_.GetCallback()); |
| 241 |
| 242 // Request the root build file be loaded. This should kick off the default |
| 243 // build config loading. |
| 244 loader->Load(root_build, LocationRange(), Label()); |
| 245 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); |
| 246 |
| 247 // Completing the build config load should kick off the root build file load. |
| 248 mock_ifm_.IssueAllPending(); |
| 249 base::RunLoop().RunUntilIdle(); |
| 250 EXPECT_TRUE(mock_ifm_.HasOnePending(root_build)); |
| 251 |
| 252 // Completing the root build file should define a target which must have |
| 253 // set of source files hashes. |
| 254 mock_ifm_.IssueAllPending(); |
| 255 base::RunLoop().RunUntilIdle(); |
| 256 EXPECT_TRUE(mock_builder_.HasOneDefined({"//BUILD.gn"})); |
| 257 |
| 258 EXPECT_FALSE(scheduler_.is_failed()); |
| 259 } |
OLD | NEW |