Ninja
clean_test.cc
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "clean.h"
16 #include "build.h"
17 
18 #include "test.h"
19 
23  virtual void SetUp() {
25  }
26 };
27 
28 TEST_F(CleanTest, CleanAll) {
29  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
30 "build in1: cat src1\n"
31 "build out1: cat in1\n"
32 "build in2: cat src2\n"
33 "build out2: cat in2\n"));
34  fs_.Create("in1", "");
35  fs_.Create("out1", "");
36  fs_.Create("in2", "");
37  fs_.Create("out2", "");
38 
39  Cleaner cleaner(&state_, config_, &fs_);
40 
41  ASSERT_EQ(0, cleaner.cleaned_files_count());
42  EXPECT_EQ(0, cleaner.CleanAll());
43  EXPECT_EQ(4, cleaner.cleaned_files_count());
44  EXPECT_EQ(4u, fs_.files_removed_.size());
45 
46  // Check they are removed.
47  EXPECT_EQ(0, fs_.Stat("in1"));
48  EXPECT_EQ(0, fs_.Stat("out1"));
49  EXPECT_EQ(0, fs_.Stat("in2"));
50  EXPECT_EQ(0, fs_.Stat("out2"));
51  fs_.files_removed_.clear();
52 
53  EXPECT_EQ(0, cleaner.CleanAll());
54  EXPECT_EQ(0, cleaner.cleaned_files_count());
55  EXPECT_EQ(0u, fs_.files_removed_.size());
56 }
57 
58 TEST_F(CleanTest, CleanAllDryRun) {
59  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
60 "build in1: cat src1\n"
61 "build out1: cat in1\n"
62 "build in2: cat src2\n"
63 "build out2: cat in2\n"));
64  fs_.Create("in1", "");
65  fs_.Create("out1", "");
66  fs_.Create("in2", "");
67  fs_.Create("out2", "");
68 
69  config_.dry_run = true;
70  Cleaner cleaner(&state_, config_, &fs_);
71 
72  ASSERT_EQ(0, cleaner.cleaned_files_count());
73  EXPECT_EQ(0, cleaner.CleanAll());
74  EXPECT_EQ(4, cleaner.cleaned_files_count());
75  EXPECT_EQ(0u, fs_.files_removed_.size());
76 
77  // Check they are not removed.
78  EXPECT_NE(0, fs_.Stat("in1"));
79  EXPECT_NE(0, fs_.Stat("out1"));
80  EXPECT_NE(0, fs_.Stat("in2"));
81  EXPECT_NE(0, fs_.Stat("out2"));
82  fs_.files_removed_.clear();
83 
84  EXPECT_EQ(0, cleaner.CleanAll());
85  EXPECT_EQ(4, cleaner.cleaned_files_count());
86  EXPECT_EQ(0u, fs_.files_removed_.size());
87 }
88 
89 TEST_F(CleanTest, CleanTarget) {
90  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
91 "build in1: cat src1\n"
92 "build out1: cat in1\n"
93 "build in2: cat src2\n"
94 "build out2: cat in2\n"));
95  fs_.Create("in1", "");
96  fs_.Create("out1", "");
97  fs_.Create("in2", "");
98  fs_.Create("out2", "");
99 
100  Cleaner cleaner(&state_, config_, &fs_);
101 
102  ASSERT_EQ(0, cleaner.cleaned_files_count());
103  ASSERT_EQ(0, cleaner.CleanTarget("out1"));
104  EXPECT_EQ(2, cleaner.cleaned_files_count());
105  EXPECT_EQ(2u, fs_.files_removed_.size());
106 
107  // Check they are removed.
108  EXPECT_EQ(0, fs_.Stat("in1"));
109  EXPECT_EQ(0, fs_.Stat("out1"));
110  EXPECT_NE(0, fs_.Stat("in2"));
111  EXPECT_NE(0, fs_.Stat("out2"));
112  fs_.files_removed_.clear();
113 
114  ASSERT_EQ(0, cleaner.CleanTarget("out1"));
115  EXPECT_EQ(0, cleaner.cleaned_files_count());
116  EXPECT_EQ(0u, fs_.files_removed_.size());
117 }
118 
119 TEST_F(CleanTest, CleanTargetDryRun) {
120  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
121 "build in1: cat src1\n"
122 "build out1: cat in1\n"
123 "build in2: cat src2\n"
124 "build out2: cat in2\n"));
125  fs_.Create("in1", "");
126  fs_.Create("out1", "");
127  fs_.Create("in2", "");
128  fs_.Create("out2", "");
129 
130  config_.dry_run = true;
131  Cleaner cleaner(&state_, config_, &fs_);
132 
133  ASSERT_EQ(0, cleaner.cleaned_files_count());
134  ASSERT_EQ(0, cleaner.CleanTarget("out1"));
135  EXPECT_EQ(2, cleaner.cleaned_files_count());
136  EXPECT_EQ(0u, fs_.files_removed_.size());
137 
138  // Check they are removed.
139  EXPECT_NE(0, fs_.Stat("in1"));
140  EXPECT_NE(0, fs_.Stat("out1"));
141  EXPECT_NE(0, fs_.Stat("in2"));
142  EXPECT_NE(0, fs_.Stat("out2"));
143  fs_.files_removed_.clear();
144 
145  ASSERT_EQ(0, cleaner.CleanTarget("out1"));
146  EXPECT_EQ(2, cleaner.cleaned_files_count());
147  EXPECT_EQ(0u, fs_.files_removed_.size());
148 }
149 
150 TEST_F(CleanTest, CleanRule) {
151  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
152 "rule cat_e\n"
153 " command = cat -e $in > $out\n"
154 "build in1: cat_e src1\n"
155 "build out1: cat in1\n"
156 "build in2: cat_e src2\n"
157 "build out2: cat in2\n"));
158  fs_.Create("in1", "");
159  fs_.Create("out1", "");
160  fs_.Create("in2", "");
161  fs_.Create("out2", "");
162 
163  Cleaner cleaner(&state_, config_, &fs_);
164 
165  ASSERT_EQ(0, cleaner.cleaned_files_count());
166  ASSERT_EQ(0, cleaner.CleanRule("cat_e"));
167  EXPECT_EQ(2, cleaner.cleaned_files_count());
168  EXPECT_EQ(2u, fs_.files_removed_.size());
169 
170  // Check they are removed.
171  EXPECT_EQ(0, fs_.Stat("in1"));
172  EXPECT_NE(0, fs_.Stat("out1"));
173  EXPECT_EQ(0, fs_.Stat("in2"));
174  EXPECT_NE(0, fs_.Stat("out2"));
175  fs_.files_removed_.clear();
176 
177  ASSERT_EQ(0, cleaner.CleanRule("cat_e"));
178  EXPECT_EQ(0, cleaner.cleaned_files_count());
179  EXPECT_EQ(0u, fs_.files_removed_.size());
180 }
181 
182 TEST_F(CleanTest, CleanRuleDryRun) {
183  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
184 "rule cat_e\n"
185 " command = cat -e $in > $out\n"
186 "build in1: cat_e src1\n"
187 "build out1: cat in1\n"
188 "build in2: cat_e src2\n"
189 "build out2: cat in2\n"));
190  fs_.Create("in1", "");
191  fs_.Create("out1", "");
192  fs_.Create("in2", "");
193  fs_.Create("out2", "");
194 
195  config_.dry_run = true;
196  Cleaner cleaner(&state_, config_, &fs_);
197 
198  ASSERT_EQ(0, cleaner.cleaned_files_count());
199  ASSERT_EQ(0, cleaner.CleanRule("cat_e"));
200  EXPECT_EQ(2, cleaner.cleaned_files_count());
201  EXPECT_EQ(0u, fs_.files_removed_.size());
202 
203  // Check they are removed.
204  EXPECT_NE(0, fs_.Stat("in1"));
205  EXPECT_NE(0, fs_.Stat("out1"));
206  EXPECT_NE(0, fs_.Stat("in2"));
207  EXPECT_NE(0, fs_.Stat("out2"));
208  fs_.files_removed_.clear();
209 
210  ASSERT_EQ(0, cleaner.CleanRule("cat_e"));
211  EXPECT_EQ(2, cleaner.cleaned_files_count());
212  EXPECT_EQ(0u, fs_.files_removed_.size());
213 }
214 
215 TEST_F(CleanTest, CleanRuleGenerator) {
216  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
217 "rule regen\n"
218 " command = cat $in > $out\n"
219 " generator = 1\n"
220 "build out1: cat in1\n"
221 "build out2: regen in2\n"));
222  fs_.Create("out1", "");
223  fs_.Create("out2", "");
224 
225  Cleaner cleaner(&state_, config_, &fs_);
226  EXPECT_EQ(0, cleaner.CleanAll());
227  EXPECT_EQ(1, cleaner.cleaned_files_count());
228  EXPECT_EQ(1u, fs_.files_removed_.size());
229 
230  fs_.Create("out1", "");
231 
232  EXPECT_EQ(0, cleaner.CleanAll(/*generator=*/true));
233  EXPECT_EQ(2, cleaner.cleaned_files_count());
234  EXPECT_EQ(2u, fs_.files_removed_.size());
235 }
236 
237 TEST_F(CleanTest, CleanDepFile) {
238  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
239 "rule cc\n"
240 " command = cc $in > $out\n"
241 " depfile = $out.d\n"
242 "build out1: cc in1\n"));
243  fs_.Create("out1", "");
244  fs_.Create("out1.d", "");
245 
246  Cleaner cleaner(&state_, config_, &fs_);
247  EXPECT_EQ(0, cleaner.CleanAll());
248  EXPECT_EQ(2, cleaner.cleaned_files_count());
249  EXPECT_EQ(2u, fs_.files_removed_.size());
250 }
251 
252 TEST_F(CleanTest, CleanDepFileOnCleanTarget) {
253  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
254 "rule cc\n"
255 " command = cc $in > $out\n"
256 " depfile = $out.d\n"
257 "build out1: cc in1\n"));
258  fs_.Create("out1", "");
259  fs_.Create("out1.d", "");
260 
261  Cleaner cleaner(&state_, config_, &fs_);
262  EXPECT_EQ(0, cleaner.CleanTarget("out1"));
263  EXPECT_EQ(2, cleaner.cleaned_files_count());
264  EXPECT_EQ(2u, fs_.files_removed_.size());
265 }
266 
267 TEST_F(CleanTest, CleanDepFileOnCleanRule) {
268  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
269 "rule cc\n"
270 " command = cc $in > $out\n"
271 " depfile = $out.d\n"
272 "build out1: cc in1\n"));
273  fs_.Create("out1", "");
274  fs_.Create("out1.d", "");
275 
276  Cleaner cleaner(&state_, config_, &fs_);
277  EXPECT_EQ(0, cleaner.CleanRule("cc"));
278  EXPECT_EQ(2, cleaner.cleaned_files_count());
279  EXPECT_EQ(2u, fs_.files_removed_.size());
280 }
281 
282 TEST_F(CleanTest, CleanRspFile) {
283  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
284 "rule cc\n"
285 " command = cc $in > $out\n"
286 " rspfile = $rspfile\n"
287 " rspfile_content=$in\n"
288 "build out1: cc in1\n"
289 " rspfile = cc1.rsp\n"
290 " rspfile_content=$in\n"));
291  fs_.Create("out1", "");
292  fs_.Create("cc1.rsp", "");
293 
294  Cleaner cleaner(&state_, config_, &fs_);
295  EXPECT_EQ(0, cleaner.CleanAll());
296  EXPECT_EQ(2, cleaner.cleaned_files_count());
297  EXPECT_EQ(2u, fs_.files_removed_.size());
298 }
299 
300 TEST_F(CleanTest, CleanRsp) {
301  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
302 "rule cat_rsp \n"
303 " command = cat $rspfile > $out\n"
304 " rspfile = $rspfile\n"
305 " rspfile_content = $in\n"
306 "build in1: cat src1\n"
307 "build out1: cat in1\n"
308 "build in2: cat_rsp src2\n"
309 " rspfile=in2.rsp\n"
310 " rspfile_content=$in\n"
311 "build out2: cat_rsp in2\n"
312 " rspfile=out2.rsp\n"
313 " rspfile_content=$in\n"));
314  fs_.Create("in1", "");
315  fs_.Create("out1", "");
316  fs_.Create("in2.rsp", "");
317  fs_.Create("out2.rsp", "");
318  fs_.Create("in2", "");
319  fs_.Create("out2", "");
320 
321  Cleaner cleaner(&state_, config_, &fs_);
322  ASSERT_EQ(0, cleaner.cleaned_files_count());
323  ASSERT_EQ(0, cleaner.CleanTarget("out1"));
324  EXPECT_EQ(2, cleaner.cleaned_files_count());
325  ASSERT_EQ(0, cleaner.CleanTarget("in2"));
326  EXPECT_EQ(2, cleaner.cleaned_files_count());
327  ASSERT_EQ(0, cleaner.CleanRule("cat_rsp"));
328  EXPECT_EQ(2, cleaner.cleaned_files_count());
329 
330  EXPECT_EQ(6u, fs_.files_removed_.size());
331 
332  // Check they are removed.
333  EXPECT_EQ(0, fs_.Stat("in1"));
334  EXPECT_EQ(0, fs_.Stat("out1"));
335  EXPECT_EQ(0, fs_.Stat("in2"));
336  EXPECT_EQ(0, fs_.Stat("out2"));
337  EXPECT_EQ(0, fs_.Stat("in2.rsp"));
338  EXPECT_EQ(0, fs_.Stat("out2.rsp"));
339 
340  fs_.files_removed_.clear();
341 }
342 
343 TEST_F(CleanTest, CleanFailure) {
344  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
345  "build dir: cat src1\n"));
346  fs_.MakeDir("dir");
347  Cleaner cleaner(&state_, config_, &fs_);
348  EXPECT_NE(0, cleaner.CleanAll());
349 }
350 
351 TEST_F(CleanTest, CleanPhony) {
352  ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
353 "build phony: phony t1 t2\n"
354 "build t1: cat\n"
355 "build t2: cat\n"));
356 
357  fs_.Create("phony", "");
358  fs_.Create("t1", "");
359  fs_.Create("t2", "");
360 
361  // Check that CleanAll does not remove "phony".
362  Cleaner cleaner(&state_, config_, &fs_);
363  EXPECT_EQ(0, cleaner.CleanAll());
364  EXPECT_EQ(2, cleaner.cleaned_files_count());
365  EXPECT_NE(0, fs_.Stat("phony"));
366 
367  fs_.Create("t1", "");
368  fs_.Create("t2", "");
369 
370  // Check that CleanTarget does not remove "phony".
371  EXPECT_EQ(0, cleaner.CleanTarget("phony"));
372  EXPECT_EQ(2, cleaner.cleaned_files_count());
373  EXPECT_NE(0, fs_.Stat("phony"));
374 }
An implementation of DiskInterface that uses an in-memory representation of disk state.
Definition: test.h:49
Verbosity verbosity
Definition: build.h:133
BuildConfig config_
Definition: clean_test.cc:22
void AssertParse(State *state, const char *input)
Definition: test.cc:90
A base test fixture that includes a State object with a builtin "cat" rule.
Definition: test.h:30
int CleanAll(bool generator=false)
Clean all built files, except for files created by generator rules.
Definition: clean.cc:108
int CleanRule(const Rule *rule)
Clean all the file built with the given rule rule.
Definition: clean.cc:209
VirtualFileSystem fs_
Definition: clean_test.cc:21
TEST_F(CleanTest, CleanAll)
Definition: clean_test.cc:28
Options (e.g. verbosity, parallelism) passed to a build.
Definition: build.h:124
virtual void SetUp()
Definition: clean_test.cc:23
int cleaned_files_count() const
Definition: clean.h:66
Definition: clean.h:30
int CleanTarget(Node *target)
Clean the given target and all the file built for it.
Definition: clean.cc:151