Ninja
build_log_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 "build_log.h"
16 
17 #include "util.h"
18 #include "test.h"
19 
20 #ifdef _WIN32
21 #include <fcntl.h>
22 #include <share.h>
23 #else
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #endif
28 
29 namespace {
30 
31 const char kTestFilename[] = "BuildLogTest-tempfile";
32 
33 struct BuildLogTest : public StateTestWithBuiltinRules, public BuildLogUser {
34  virtual void SetUp() {
35  // In case a crashing test left a stale file behind.
36  unlink(kTestFilename);
37  }
38  virtual void TearDown() {
39  unlink(kTestFilename);
40  }
41  virtual bool IsPathDead(StringPiece s) const { return false; }
42 };
43 
44 TEST_F(BuildLogTest, WriteRead) {
45  AssertParse(&state_,
46 "build out: cat mid\n"
47 "build mid: cat in\n");
48 
49  BuildLog log1;
50  string err;
51  EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
52  ASSERT_EQ("", err);
53  log1.RecordCommand(state_.edges_[0], 15, 18);
54  log1.RecordCommand(state_.edges_[1], 20, 25);
55  log1.Close();
56 
57  BuildLog log2;
58  EXPECT_TRUE(log2.Load(kTestFilename, &err));
59  ASSERT_EQ("", err);
60 
61  ASSERT_EQ(2u, log1.entries().size());
62  ASSERT_EQ(2u, log2.entries().size());
63  BuildLog::LogEntry* e1 = log1.LookupByOutput("out");
64  ASSERT_TRUE(e1);
65  BuildLog::LogEntry* e2 = log2.LookupByOutput("out");
66  ASSERT_TRUE(e2);
67  ASSERT_TRUE(*e1 == *e2);
68  ASSERT_EQ(15, e1->start_time);
69  ASSERT_EQ("out", e1->output);
70 }
71 
72 TEST_F(BuildLogTest, FirstWriteAddsSignature) {
73  const char kExpectedVersion[] = "# ninja log vX\n";
74  const size_t kVersionPos = strlen(kExpectedVersion) - 2; // Points at 'X'.
75 
76  BuildLog log;
77  string contents, err;
78 
79  EXPECT_TRUE(log.OpenForWrite(kTestFilename, *this, &err));
80  ASSERT_EQ("", err);
81  log.Close();
82 
83  ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
84  ASSERT_EQ("", err);
85  if (contents.size() >= kVersionPos)
86  contents[kVersionPos] = 'X';
87  EXPECT_EQ(kExpectedVersion, contents);
88 
89  // Opening the file anew shouldn't add a second version string.
90  EXPECT_TRUE(log.OpenForWrite(kTestFilename, *this, &err));
91  ASSERT_EQ("", err);
92  log.Close();
93 
94  contents.clear();
95  ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
96  ASSERT_EQ("", err);
97  if (contents.size() >= kVersionPos)
98  contents[kVersionPos] = 'X';
99  EXPECT_EQ(kExpectedVersion, contents);
100 }
101 
102 TEST_F(BuildLogTest, DoubleEntry) {
103  FILE* f = fopen(kTestFilename, "wb");
104  fprintf(f, "# ninja log v4\n");
105  fprintf(f, "0\t1\t2\tout\tcommand abc\n");
106  fprintf(f, "3\t4\t5\tout\tcommand def\n");
107  fclose(f);
108 
109  string err;
110  BuildLog log;
111  EXPECT_TRUE(log.Load(kTestFilename, &err));
112  ASSERT_EQ("", err);
113 
114  BuildLog::LogEntry* e = log.LookupByOutput("out");
115  ASSERT_TRUE(e);
116  ASSERT_NO_FATAL_FAILURE(AssertHash("command def", e->command_hash));
117 }
118 
119 TEST_F(BuildLogTest, Truncate) {
120  AssertParse(&state_,
121 "build out: cat mid\n"
122 "build mid: cat in\n");
123 
124  BuildLog log1;
125  string err;
126  EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
127  ASSERT_EQ("", err);
128  log1.RecordCommand(state_.edges_[0], 15, 18);
129  log1.RecordCommand(state_.edges_[1], 20, 25);
130  log1.Close();
131 
132  struct stat statbuf;
133  ASSERT_EQ(0, stat(kTestFilename, &statbuf));
134  ASSERT_GT(statbuf.st_size, 0);
135 
136  // For all possible truncations of the input file, assert that we don't
137  // crash when parsing.
138  for (off_t size = statbuf.st_size; size > 0; --size) {
139  BuildLog log2;
140  string err;
141  EXPECT_TRUE(log2.OpenForWrite(kTestFilename, *this, &err));
142  ASSERT_EQ("", err);
143  log2.RecordCommand(state_.edges_[0], 15, 18);
144  log2.RecordCommand(state_.edges_[1], 20, 25);
145  log2.Close();
146 
147  ASSERT_TRUE(Truncate(kTestFilename, size, &err));
148 
149  BuildLog log3;
150  err.clear();
151  ASSERT_TRUE(log3.Load(kTestFilename, &err) || !err.empty());
152  }
153 }
154 
155 TEST_F(BuildLogTest, ObsoleteOldVersion) {
156  FILE* f = fopen(kTestFilename, "wb");
157  fprintf(f, "# ninja log v3\n");
158  fprintf(f, "123 456 0 out command\n");
159  fclose(f);
160 
161  string err;
162  BuildLog log;
163  EXPECT_TRUE(log.Load(kTestFilename, &err));
164  ASSERT_NE(err.find("version"), string::npos);
165 }
166 
167 TEST_F(BuildLogTest, SpacesInOutputV4) {
168  FILE* f = fopen(kTestFilename, "wb");
169  fprintf(f, "# ninja log v4\n");
170  fprintf(f, "123\t456\t456\tout with space\tcommand\n");
171  fclose(f);
172 
173  string err;
174  BuildLog log;
175  EXPECT_TRUE(log.Load(kTestFilename, &err));
176  ASSERT_EQ("", err);
177 
178  BuildLog::LogEntry* e = log.LookupByOutput("out with space");
179  ASSERT_TRUE(e);
180  ASSERT_EQ(123, e->start_time);
181  ASSERT_EQ(456, e->end_time);
182  ASSERT_EQ(456, e->restat_mtime);
183  ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
184 }
185 
186 TEST_F(BuildLogTest, DuplicateVersionHeader) {
187  // Old versions of ninja accidentally wrote multiple version headers to the
188  // build log on Windows. This shouldn't crash, and the second version header
189  // should be ignored.
190  FILE* f = fopen(kTestFilename, "wb");
191  fprintf(f, "# ninja log v4\n");
192  fprintf(f, "123\t456\t456\tout\tcommand\n");
193  fprintf(f, "# ninja log v4\n");
194  fprintf(f, "456\t789\t789\tout2\tcommand2\n");
195  fclose(f);
196 
197  string err;
198  BuildLog log;
199  EXPECT_TRUE(log.Load(kTestFilename, &err));
200  ASSERT_EQ("", err);
201 
202  BuildLog::LogEntry* e = log.LookupByOutput("out");
203  ASSERT_TRUE(e);
204  ASSERT_EQ(123, e->start_time);
205  ASSERT_EQ(456, e->end_time);
206  ASSERT_EQ(456, e->restat_mtime);
207  ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
208 
209  e = log.LookupByOutput("out2");
210  ASSERT_TRUE(e);
211  ASSERT_EQ(456, e->start_time);
212  ASSERT_EQ(789, e->end_time);
213  ASSERT_EQ(789, e->restat_mtime);
214  ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
215 }
216 
217 TEST_F(BuildLogTest, VeryLongInputLine) {
218  // Ninja's build log buffer is currently 256kB. Lines longer than that are
219  // silently ignored, but don't affect parsing of other lines.
220  FILE* f = fopen(kTestFilename, "wb");
221  fprintf(f, "# ninja log v4\n");
222  fprintf(f, "123\t456\t456\tout\tcommand start");
223  for (size_t i = 0; i < (512 << 10) / strlen(" more_command"); ++i)
224  fputs(" more_command", f);
225  fprintf(f, "\n");
226  fprintf(f, "456\t789\t789\tout2\tcommand2\n");
227  fclose(f);
228 
229  string err;
230  BuildLog log;
231  EXPECT_TRUE(log.Load(kTestFilename, &err));
232  ASSERT_EQ("", err);
233 
234  BuildLog::LogEntry* e = log.LookupByOutput("out");
235  ASSERT_EQ(NULL, e);
236 
237  e = log.LookupByOutput("out2");
238  ASSERT_TRUE(e);
239  ASSERT_EQ(456, e->start_time);
240  ASSERT_EQ(789, e->end_time);
241  ASSERT_EQ(789, e->restat_mtime);
242  ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
243 }
244 
245 TEST_F(BuildLogTest, MultiTargetEdge) {
246  AssertParse(&state_,
247 "build out out.d: cat\n");
248 
249  BuildLog log;
250  log.RecordCommand(state_.edges_[0], 21, 22);
251 
252  ASSERT_EQ(2u, log.entries().size());
253  BuildLog::LogEntry* e1 = log.LookupByOutput("out");
254  ASSERT_TRUE(e1);
255  BuildLog::LogEntry* e2 = log.LookupByOutput("out.d");
256  ASSERT_TRUE(e2);
257  ASSERT_EQ("out", e1->output);
258  ASSERT_EQ("out.d", e2->output);
259  ASSERT_EQ(21, e1->start_time);
260  ASSERT_EQ(21, e2->start_time);
261  ASSERT_EQ(22, e2->end_time);
262  ASSERT_EQ(22, e2->end_time);
263 }
264 
265 struct BuildLogRecompactTest : public BuildLogTest {
266  virtual bool IsPathDead(StringPiece s) const { return s == "out2"; }
267 };
268 
269 TEST_F(BuildLogRecompactTest, Recompact) {
270  AssertParse(&state_,
271 "build out: cat in\n"
272 "build out2: cat in\n");
273 
274  BuildLog log1;
275  string err;
276  EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
277  ASSERT_EQ("", err);
278  // Record the same edge several times, to trigger recompaction
279  // the next time the log is opened.
280  for (int i = 0; i < 200; ++i)
281  log1.RecordCommand(state_.edges_[0], 15, 18 + i);
282  log1.RecordCommand(state_.edges_[1], 21, 22);
283  log1.Close();
284 
285  // Load...
286  BuildLog log2;
287  EXPECT_TRUE(log2.Load(kTestFilename, &err));
288  ASSERT_EQ("", err);
289  ASSERT_EQ(2u, log2.entries().size());
290  ASSERT_TRUE(log2.LookupByOutput("out"));
291  ASSERT_TRUE(log2.LookupByOutput("out2"));
292  // ...and force a recompaction.
293  EXPECT_TRUE(log2.OpenForWrite(kTestFilename, *this, &err));
294  log2.Close();
295 
296  // "out2" is dead, it should've been removed.
297  BuildLog log3;
298  EXPECT_TRUE(log2.Load(kTestFilename, &err));
299  ASSERT_EQ("", err);
300  ASSERT_EQ(1u, log2.entries().size());
301  ASSERT_TRUE(log2.LookupByOutput("out"));
302  ASSERT_FALSE(log2.LookupByOutput("out2"));
303 }
304 
305 } // anonymous namespace
const Entries & entries() const
Definition: build_log.h:85
TimeStamp restat_mtime
Definition: build_log.h:59
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
void AssertParse(State *state, const char *input)
Definition: test.cc:90
void Close()
Definition: build_log.cc:168
Store a log of every command ran for every build.
Definition: build_log.h:42
A base test fixture that includes a State object with a builtin "cat" rule.
Definition: test.h:30
virtual bool IsPathDead(StringPiece s) const =0
Return if a given output no longer part of the build manifest.
uint64_t command_hash
Definition: build_log.h:56
bool OpenForWrite(const string &path, const BuildLogUser &user, string *err)
Definition: build_log.cc:111
TEST_F(PlanTest, Basic)
Definition: build_test.cc:51
const char kTestFilename[]
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:282
bool Truncate(const string &path, size_t size, string *err)
Truncates a file to the given size.
Definition: util.cc:447
LogEntry * LookupByOutput(const string &path)
Lookup a previously-run command by its output path.
Definition: build_log.cc:341
bool RecordCommand(Edge *edge, int start_time, int end_time, TimeStamp restat_mtime=0)
Definition: build_log.cc:140
bool Load(const string &path, string *err)
Load the on-disk log.
Definition: build_log.cc:225
void AssertHash(const char *expected, uint64_t actual)
Definition: test.cc:97
Can answer questions about the manifest for the BuildLog.
Definition: build_log.h:29