Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tools/gn/loader_unittest.cc

Issue 2940873002: Implement tracking of BUILD.gn files used to define target, toolchain or (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(const 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(
127 const std::set<std::string>& source_files) const {
128 if (items_.size() != 1u)
129 return false;
130
131 const auto& source_files_hashes = items_[0]->source_files_hashes();
132 if (source_files.size() != source_files_hashes.size())
133 return false;
134
135 for (const std::string& source_file : source_files) {
136 if (source_files_hashes.find(base::Hash(source_file)) ==
137 source_files_hashes.end())
138 return false;
139 }
140
141 return true;
142 };
143
144 void MockBuilder::Cleanup() {
145 items_.clear();
146 }
147
111 // LoaderTest ------------------------------------------------------------------ 148 // LoaderTest ------------------------------------------------------------------
112 149
113 class LoaderTest : public testing::Test { 150 class LoaderTest : public testing::Test {
114 public: 151 public:
115 LoaderTest() { 152 LoaderTest() {
116 build_settings_.SetBuildDir(SourceDir("//out/Debug/")); 153 build_settings_.SetBuildDir(SourceDir("//out/Debug/"));
117 } 154 }
118 155
119 protected: 156 protected:
120 Scheduler scheduler_; 157 Scheduler scheduler_;
121 BuildSettings build_settings_; 158 BuildSettings build_settings_;
159 MockBuilder mock_builder_;
122 MockInputFileManager mock_ifm_; 160 MockInputFileManager mock_ifm_;
161 std::vector<std::unique_ptr<Item>> items_;
123 }; 162 };
124 163
125 } // namespace 164 } // namespace
126 165
127 // ----------------------------------------------------------------------------- 166 // -----------------------------------------------------------------------------
128 167
129 TEST_F(LoaderTest, Foo) { 168 TEST_F(LoaderTest, Foo) {
130 SourceFile build_config("//build/config/BUILDCONFIG.gn"); 169 SourceFile build_config("//build/config/BUILDCONFIG.gn");
131 build_settings_.set_build_config_file(build_config); 170 build_settings_.set_build_config_file(build_config);
132 171
(...skipping 27 matching lines...) Expand all
160 EXPECT_TRUE(mock_ifm_.HasOnePending(SourceFile("//tc2/BUILD.gn"))); 199 EXPECT_TRUE(mock_ifm_.HasOnePending(SourceFile("//tc2/BUILD.gn")));
161 200
162 // Running the toolchain file should schedule the build config file to load 201 // Running the toolchain file should schedule the build config file to load
163 // for that toolchain. 202 // for that toolchain.
164 mock_ifm_.IssueAllPending(); 203 mock_ifm_.IssueAllPending();
165 base::RunLoop().RunUntilIdle(); 204 base::RunLoop().RunUntilIdle();
166 205
167 // We have to tell it we have a toolchain definition now (normally the 206 // We have to tell it we have a toolchain definition now (normally the
168 // builder would do this). 207 // builder would do this).
169 const Settings* default_settings = loader->GetToolchainSettings(Label()); 208 const Settings* default_settings = loader->GetToolchainSettings(Label());
170 Toolchain second_tc_object(default_settings, second_tc); 209 Toolchain second_tc_object(default_settings, second_tc, {});
171 loader->ToolchainLoaded(&second_tc_object); 210 loader->ToolchainLoaded(&second_tc_object);
172 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); 211 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config));
173 212
174 // Scheduling a second file to load in that toolchain should not make it 213 // Scheduling a second file to load in that toolchain should not make it
175 // pending yet (it's waiting for the build config). 214 // pending yet (it's waiting for the build config).
176 SourceFile third_file("//bar/BUILD.gn"); 215 SourceFile third_file("//bar/BUILD.gn");
177 loader->Load(third_file, LocationRange(), second_tc); 216 loader->Load(third_file, LocationRange(), second_tc);
178 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config)); 217 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config));
179 218
180 // Running the build config file should make our third file pending. 219 // Running the build config file should make our third file pending.
181 mock_ifm_.IssueAllPending(); 220 mock_ifm_.IssueAllPending();
182 base::RunLoop().RunUntilIdle(); 221 base::RunLoop().RunUntilIdle();
183 EXPECT_TRUE(mock_ifm_.HasTwoPending(second_file, third_file)); 222 EXPECT_TRUE(mock_ifm_.HasTwoPending(second_file, third_file));
184 223
185 EXPECT_FALSE(scheduler_.is_failed()); 224 EXPECT_FALSE(scheduler_.is_failed());
186 } 225 }
226
227 TEST_F(LoaderTest, SourceFilesAreCollected) {
228 SourceFile build_config("//build/config/BUILDCONFIG.gn");
229 SourceFile root_build("//BUILD.gn");
230 build_settings_.set_build_config_file(build_config);
231
232 build_settings_.set_item_defined_callback(base::Bind(
233 &MockBuilder::OnItemDefined, base::Unretained(&mock_builder_)));
234
235 scoped_refptr<LoaderImpl> loader(new LoaderImpl(&build_settings_));
236
237 mock_ifm_.AddCannedResponse(build_config,
238 "set_default_toolchain(\"//tc:tc\")");
239 mock_ifm_.AddCannedResponse(root_build,
240 "executable(\"a\") { sources = [ \"a.cc\" ] }");
241
242 loader->set_async_load_file(mock_ifm_.GetCallback());
243
244 // Request the root build file be loaded. This should kick off the default
245 // build config loading.
246 loader->Load(root_build, LocationRange(), Label());
247 EXPECT_TRUE(mock_ifm_.HasOnePending(build_config));
248
249 // Completing the build config load should kick off the root build file load.
250 mock_ifm_.IssueAllPending();
251 base::RunLoop().RunUntilIdle();
252 EXPECT_TRUE(mock_ifm_.HasOnePending(root_build));
253
254 // Completing the root build file should define a target which must have
255 // set of source files hashes.
256 mock_ifm_.IssueAllPending();
257 base::RunLoop().RunUntilIdle();
258 EXPECT_TRUE(mock_builder_.HasOneDefined(
259 {"//build/config/BUILDCONFIG.gn", "//BUILD.gn"}));
260
261 EXPECT_FALSE(scheduler_.is_failed());
262 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698