1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "build_log.h"
25 #include <sys/types.h>
32 const char kTestFilename[] = "BuildLogTest-tempfile";
34 struct BuildLogTest : public StateTestWithBuiltinRules, public BuildLogUser {
35 virtual void SetUp() {
36 // In case a crashing test left a stale file behind.
37 unlink(kTestFilename);
39 virtual void TearDown() {
40 unlink(kTestFilename);
42 virtual bool IsPathDead(StringPiece s) const { return false; }
45 TEST_F(BuildLogTest, WriteRead) {
47 "build out: cat mid\n"
48 "build mid: cat in\n");
52 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
54 log1.RecordCommand(state_.edges_[0], 15, 18);
55 log1.RecordCommand(state_.edges_[1], 20, 25);
59 EXPECT_TRUE(log2.Load(kTestFilename, &err));
62 ASSERT_EQ(2u, log1.entries().size());
63 ASSERT_EQ(2u, log2.entries().size());
64 BuildLog::LogEntry* e1 = log1.LookupByOutput("out");
66 BuildLog::LogEntry* e2 = log2.LookupByOutput("out");
68 ASSERT_TRUE(*e1 == *e2);
69 ASSERT_EQ(15, e1->start_time);
70 ASSERT_EQ("out", e1->output);
73 TEST_F(BuildLogTest, FirstWriteAddsSignature) {
74 const char kExpectedVersion[] = "# ninja log vX\n";
75 const size_t kVersionPos = strlen(kExpectedVersion) - 2; // Points at 'X'.
80 EXPECT_TRUE(log.OpenForWrite(kTestFilename, *this, &err));
84 ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
86 if (contents.size() >= kVersionPos)
87 contents[kVersionPos] = 'X';
88 EXPECT_EQ(kExpectedVersion, contents);
90 // Opening the file anew shouldn't add a second version string.
91 EXPECT_TRUE(log.OpenForWrite(kTestFilename, *this, &err));
96 ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
98 if (contents.size() >= kVersionPos)
99 contents[kVersionPos] = 'X';
100 EXPECT_EQ(kExpectedVersion, contents);
103 TEST_F(BuildLogTest, DoubleEntry) {
104 FILE* f = fopen(kTestFilename, "wb");
105 fprintf(f, "# ninja log v4\n");
106 fprintf(f, "0\t1\t2\tout\tcommand abc\n");
107 fprintf(f, "3\t4\t5\tout\tcommand def\n");
112 EXPECT_TRUE(log.Load(kTestFilename, &err));
115 BuildLog::LogEntry* e = log.LookupByOutput("out");
117 ASSERT_NO_FATAL_FAILURE(AssertHash("command def", e->command_hash));
120 TEST_F(BuildLogTest, Truncate) {
122 "build out: cat mid\n"
123 "build mid: cat in\n");
128 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
130 log1.RecordCommand(state_.edges_[0], 15, 18);
131 log1.RecordCommand(state_.edges_[1], 20, 25);
136 ASSERT_EQ(0, stat(kTestFilename, &statbuf));
137 ASSERT_GT(statbuf.st_size, 0);
139 // For all possible truncations of the input file, assert that we don't
140 // crash when parsing.
141 for (off_t size = statbuf.st_size; size > 0; --size) {
144 EXPECT_TRUE(log2.OpenForWrite(kTestFilename, *this, &err));
146 log2.RecordCommand(state_.edges_[0], 15, 18);
147 log2.RecordCommand(state_.edges_[1], 20, 25);
150 ASSERT_TRUE(Truncate(kTestFilename, size, &err));
154 ASSERT_TRUE(log3.Load(kTestFilename, &err) == LOAD_SUCCESS || !err.empty());
158 TEST_F(BuildLogTest, ObsoleteOldVersion) {
159 FILE* f = fopen(kTestFilename, "wb");
160 fprintf(f, "# ninja log v3\n");
161 fprintf(f, "123 456 0 out command\n");
166 EXPECT_TRUE(log.Load(kTestFilename, &err));
167 ASSERT_NE(err.find("version"), string::npos);
170 TEST_F(BuildLogTest, SpacesInOutputV4) {
171 FILE* f = fopen(kTestFilename, "wb");
172 fprintf(f, "# ninja log v4\n");
173 fprintf(f, "123\t456\t456\tout with space\tcommand\n");
178 EXPECT_TRUE(log.Load(kTestFilename, &err));
181 BuildLog::LogEntry* e = log.LookupByOutput("out with space");
183 ASSERT_EQ(123, e->start_time);
184 ASSERT_EQ(456, e->end_time);
185 ASSERT_EQ(456, e->mtime);
186 ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
189 TEST_F(BuildLogTest, DuplicateVersionHeader) {
190 // Old versions of ninja accidentally wrote multiple version headers to the
191 // build log on Windows. This shouldn't crash, and the second version header
192 // should be ignored.
193 FILE* f = fopen(kTestFilename, "wb");
194 fprintf(f, "# ninja log v4\n");
195 fprintf(f, "123\t456\t456\tout\tcommand\n");
196 fprintf(f, "# ninja log v4\n");
197 fprintf(f, "456\t789\t789\tout2\tcommand2\n");
202 EXPECT_TRUE(log.Load(kTestFilename, &err));
205 BuildLog::LogEntry* e = log.LookupByOutput("out");
207 ASSERT_EQ(123, e->start_time);
208 ASSERT_EQ(456, e->end_time);
209 ASSERT_EQ(456, e->mtime);
210 ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
212 e = log.LookupByOutput("out2");
214 ASSERT_EQ(456, e->start_time);
215 ASSERT_EQ(789, e->end_time);
216 ASSERT_EQ(789, e->mtime);
217 ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
220 struct TestDiskInterface : public DiskInterface {
221 virtual TimeStamp Stat(const string& path, string* err) const {
224 virtual bool WriteFile(const string& path, const string& contents) {
228 virtual bool MakeDir(const string& path) {
232 virtual Status ReadFile(const string& path, string* contents, string* err) {
236 virtual int RemoveFile(const string& path) {
242 TEST_F(BuildLogTest, Restat) {
243 FILE* f = fopen(kTestFilename, "wb");
244 fprintf(f, "# ninja log v4\n"
245 "1\t2\t3\tout\tcommand\n");
249 EXPECT_TRUE(log.Load(kTestFilename, &err));
251 BuildLog::LogEntry* e = log.LookupByOutput("out");
252 ASSERT_EQ(3, e->mtime);
254 TestDiskInterface testDiskInterface;
255 char out2[] = { 'o', 'u', 't', '2', 0 };
256 char* filter2[] = { out2 };
257 EXPECT_TRUE(log.Restat(kTestFilename, testDiskInterface, 1, filter2, &err));
259 e = log.LookupByOutput("out");
260 ASSERT_EQ(3, e->mtime); // unchanged, since the filter doesn't match
262 EXPECT_TRUE(log.Restat(kTestFilename, testDiskInterface, 0, NULL, &err));
264 e = log.LookupByOutput("out");
265 ASSERT_EQ(4, e->mtime);
268 TEST_F(BuildLogTest, VeryLongInputLine) {
269 // Ninja's build log buffer is currently 256kB. Lines longer than that are
270 // silently ignored, but don't affect parsing of other lines.
271 FILE* f = fopen(kTestFilename, "wb");
272 fprintf(f, "# ninja log v4\n");
273 fprintf(f, "123\t456\t456\tout\tcommand start");
274 for (size_t i = 0; i < (512 << 10) / strlen(" more_command"); ++i)
275 fputs(" more_command", f);
277 fprintf(f, "456\t789\t789\tout2\tcommand2\n");
282 EXPECT_TRUE(log.Load(kTestFilename, &err));
285 BuildLog::LogEntry* e = log.LookupByOutput("out");
288 e = log.LookupByOutput("out2");
290 ASSERT_EQ(456, e->start_time);
291 ASSERT_EQ(789, e->end_time);
292 ASSERT_EQ(789, e->mtime);
293 ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
296 TEST_F(BuildLogTest, MultiTargetEdge) {
298 "build out out.d: cat\n");
301 log.RecordCommand(state_.edges_[0], 21, 22);
303 ASSERT_EQ(2u, log.entries().size());
304 BuildLog::LogEntry* e1 = log.LookupByOutput("out");
306 BuildLog::LogEntry* e2 = log.LookupByOutput("out.d");
308 ASSERT_EQ("out", e1->output);
309 ASSERT_EQ("out.d", e2->output);
310 ASSERT_EQ(21, e1->start_time);
311 ASSERT_EQ(21, e2->start_time);
312 ASSERT_EQ(22, e2->end_time);
313 ASSERT_EQ(22, e2->end_time);
316 struct BuildLogRecompactTest : public BuildLogTest {
317 virtual bool IsPathDead(StringPiece s) const { return s == "out2"; }
320 TEST_F(BuildLogRecompactTest, Recompact) {
322 "build out: cat in\n"
323 "build out2: cat in\n");
327 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, *this, &err));
329 // Record the same edge several times, to trigger recompaction
330 // the next time the log is opened.
331 for (int i = 0; i < 200; ++i)
332 log1.RecordCommand(state_.edges_[0], 15, 18 + i);
333 log1.RecordCommand(state_.edges_[1], 21, 22);
338 EXPECT_TRUE(log2.Load(kTestFilename, &err));
340 ASSERT_EQ(2u, log2.entries().size());
341 ASSERT_TRUE(log2.LookupByOutput("out"));
342 ASSERT_TRUE(log2.LookupByOutput("out2"));
343 // ...and force a recompaction.
344 EXPECT_TRUE(log2.OpenForWrite(kTestFilename, *this, &err));
347 // "out2" is dead, it should've been removed.
349 EXPECT_TRUE(log2.Load(kTestFilename, &err));
351 ASSERT_EQ(1u, log2.entries().size());
352 ASSERT_TRUE(log2.LookupByOutput("out"));
353 ASSERT_FALSE(log2.LookupByOutput("out2"));
356 } // anonymous namespace