Skip to content
This repository
Newer
Older
100644 247 lines (210 sloc) 7.041 kb
d52238f2 »
2011-02-06 add copyrights
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
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
15 #include "build_log.h"
16
fadd5a3e » nico
2012-05-08 Don't write ninja log header to log on every build on Windows.
17 #include "util.h"
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
18 #include "test.h"
19
faac236d »
2012-01-22 windows: use _WIN32 define everywhere
20 #ifdef _WIN32
820ecab6 » philipcraig
2011-05-29 mingw ninja_test links and passes. (as do linux and native VS2010)
21 #include <fcntl.h>
22 #include <share.h>
de8d5080 » nico
2012-08-05 Include unistd.h in files that use unlink() on OS X.
23 #else
7682a312 » qhuo
2011-06-10 build_log_test.cc: add 3 #include for stat(2) on Linux
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #endif
28
1e9124b0 » nico
2012-05-08 Add a BuildLog loading perftest.
29 const char kTestFilename[] = "BuildLogTest-tempfile";
667f48b5 »
2011-01-09 remove tempnam
30
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
31 struct BuildLogTest : public StateTestWithBuiltinRules {
32 virtual void SetUp() {
e744221d » nico
2012-08-31 Make sure no stale test file exists before running build log tests.
33 // In case a crashing test left a stale file behind.
34 unlink(kTestFilename);
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
35 }
36 virtual void TearDown() {
667f48b5 »
2011-01-09 remove tempnam
37 unlink(kTestFilename);
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
38 }
39 };
40
41 TEST_F(BuildLogTest, WriteRead) {
42 AssertParse(&state_,
43 "build out: cat mid\n"
44 "build mid: cat in\n");
45
46 BuildLog log1;
47 string err;
667f48b5 »
2011-01-09 remove tempnam
48 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
49 ASSERT_EQ("", err);
fa8830b5 »
2011-05-09 add a versioning header to build log, switch to v2
50 log1.RecordCommand(state_.edges_[0], 15, 18);
51 log1.RecordCommand(state_.edges_[1], 20, 25);
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
52 log1.Close();
53
54 BuildLog log2;
667f48b5 »
2011-01-09 remove tempnam
55 EXPECT_TRUE(log2.Load(kTestFilename, &err));
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
56 ASSERT_EQ("", err);
57
f355f64f »
2012-04-04 Make |log_| variable private to BuildLog.
58 ASSERT_EQ(2u, log1.log().size());
59 ASSERT_EQ(2u, log2.log().size());
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
60 BuildLog::LogEntry* e1 = log1.LookupByOutput("out");
61 ASSERT_TRUE(e1);
62 BuildLog::LogEntry* e2 = log2.LookupByOutput("out");
63 ASSERT_TRUE(e2);
64 ASSERT_TRUE(*e1 == *e2);
fa8830b5 »
2011-05-09 add a versioning header to build log, switch to v2
65 ASSERT_EQ(15, e1->start_time);
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
66 ASSERT_EQ("out", e1->output);
67 }
681de180 »
2011-01-07 later entry in build log should overwrite earlier one
68
fadd5a3e » nico
2012-05-08 Don't write ninja log header to log on every build on Windows.
69 TEST_F(BuildLogTest, FirstWriteAddsSignature) {
70 const char kExpectedVersion[] = "# ninja log vX\n";
71 const size_t kVersionPos = strlen(kExpectedVersion) - 2; // Points at 'X'.
72
73 BuildLog log;
74 string contents, err;
75
76 EXPECT_TRUE(log.OpenForWrite(kTestFilename, &err));
77 ASSERT_EQ("", err);
78 log.Close();
79
80 ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
81 ASSERT_EQ("", err);
82 if (contents.size() >= kVersionPos)
83 contents[kVersionPos] = 'X';
84 EXPECT_EQ(kExpectedVersion, contents);
85
86 // Opening the file anew shouldn't add a second version string.
87 EXPECT_TRUE(log.OpenForWrite(kTestFilename, &err));
88 ASSERT_EQ("", err);
89 log.Close();
90
91 contents.clear();
92 ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err));
93 ASSERT_EQ("", err);
94 if (contents.size() >= kVersionPos)
95 contents[kVersionPos] = 'X';
96 EXPECT_EQ(kExpectedVersion, contents);
97 }
98
681de180 »
2011-01-07 later entry in build log should overwrite earlier one
99 TEST_F(BuildLogTest, DoubleEntry) {
667f48b5 »
2011-01-09 remove tempnam
100 FILE* f = fopen(kTestFilename, "wb");
91b986fe »
2012-06-18 drop support of log formats < 4
101 fprintf(f, "# ninja log v4\n");
102 fprintf(f, "0\t1\t2\tout\tcommand abc\n");
103 fprintf(f, "3\t4\t5\tout\tcommand def\n");
681de180 »
2011-01-07 later entry in build log should overwrite earlier one
104 fclose(f);
105
106 string err;
107 BuildLog log;
667f48b5 »
2011-01-09 remove tempnam
108 EXPECT_TRUE(log.Load(kTestFilename, &err));
681de180 »
2011-01-07 later entry in build log should overwrite earlier one
109 ASSERT_EQ("", err);
110
111 BuildLog::LogEntry* e = log.LookupByOutput("out");
112 ASSERT_TRUE(e);
5be83d0b » nico
2012-06-14 Only store command hashes in the build log.
113 ASSERT_NO_FATAL_FAILURE(AssertHash("command def", e->command_hash));
681de180 »
2011-01-07 later entry in build log should overwrite earlier one
114 }
eccebffc »
2011-05-11 don't crash on truncated log files
115
116 TEST_F(BuildLogTest, Truncate) {
117 AssertParse(&state_,
118 "build out: cat mid\n"
119 "build mid: cat in\n");
120
121 BuildLog log1;
122 string err;
123 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
124 ASSERT_EQ("", err);
125 log1.RecordCommand(state_.edges_[0], 15, 18);
126 log1.RecordCommand(state_.edges_[1], 20, 25);
127 log1.Close();
128
129 struct stat statbuf;
130 ASSERT_EQ(0, stat(kTestFilename, &statbuf));
131 ASSERT_GT(statbuf.st_size, 0);
132
133 // For all possible truncations of the input file, assert that we don't
34fdf02d »
2012-07-27 make the build log test match the indended build log logic
134 // crash when parsing.
eccebffc »
2011-05-11 don't crash on truncated log files
135 for (off_t size = statbuf.st_size; size > 0; --size) {
9726ee77 »
2012-08-17 fix test broken in 697350d
136 BuildLog log2;
137 string err;
138 EXPECT_TRUE(log2.OpenForWrite(kTestFilename, &err));
139 ASSERT_EQ("", err);
140 log2.RecordCommand(state_.edges_[0], 15, 18);
141 log2.RecordCommand(state_.edges_[1], 20, 25);
142 log2.Close();
143
faac236d »
2012-01-22 windows: use _WIN32 define everywhere
144 #ifndef _WIN32
eccebffc »
2011-05-11 don't crash on truncated log files
145 ASSERT_EQ(0, truncate(kTestFilename, size));
41a4e260 » philipcraig
2011-05-28 Tests now build on a native Windows build (tested with VS2010)
146 #else
147 int fh;
820ecab6 » philipcraig
2011-05-29 mingw ninja_test links and passes. (as do linux and native VS2010)
148 fh = _sopen(kTestFilename, _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE);
41a4e260 » philipcraig
2011-05-28 Tests now build on a native Windows build (tested with VS2010)
149 ASSERT_EQ(0, _chsize(fh, size));
150 _close(fh);
151 #endif
eccebffc »
2011-05-11 don't crash on truncated log files
152
9726ee77 »
2012-08-17 fix test broken in 697350d
153 BuildLog log3;
34fdf02d »
2012-07-27 make the build log test match the indended build log logic
154 err.clear();
9726ee77 »
2012-08-17 fix test broken in 697350d
155 ASSERT_TRUE(log3.Load(kTestFilename, &err) || !err.empty());
eccebffc »
2011-05-11 don't crash on truncated log files
156 }
157 }
0efbbbf6 » pcc
2011-09-19 Implement restat rules
158
91b986fe »
2012-06-18 drop support of log formats < 4
159 TEST_F(BuildLogTest, ObsoleteOldVersion) {
0efbbbf6 » pcc
2011-09-19 Implement restat rules
160 FILE* f = fopen(kTestFilename, "wb");
23ab2829 »
2012-01-04 make my last change actually work
161 fprintf(f, "# ninja log v3\n");
162 fprintf(f, "123 456 0 out command\n");
0efbbbf6 » pcc
2011-09-19 Implement restat rules
163 fclose(f);
164
165 string err;
166 BuildLog log;
697350d1 »
2012-08-17 make it more explicit that a bad build log causes us to rebuild
167 EXPECT_TRUE(log.Load(kTestFilename, &err));
91b986fe »
2012-06-18 drop support of log formats < 4
168 ASSERT_NE(err.find("version"), string::npos);
0efbbbf6 » pcc
2011-09-19 Implement restat rules
169 }
83a50c34 » nico
2012-01-04 Switch build log to use tabs as field separators, to support outputs …
170
171 TEST_F(BuildLogTest, SpacesInOutputV4) {
172 FILE* f = fopen(kTestFilename, "wb");
173 fprintf(f, "# ninja log v4\n");
174 fprintf(f, "123\t456\t456\tout with space\tcommand\n");
175 fclose(f);
176
177 string err;
178 BuildLog log;
179 EXPECT_TRUE(log.Load(kTestFilename, &err));
180 ASSERT_EQ("", err);
181
182 BuildLog::LogEntry* e = log.LookupByOutput("out with space");
183 ASSERT_TRUE(e);
184 ASSERT_EQ(123, e->start_time);
185 ASSERT_EQ(456, e->end_time);
186 ASSERT_EQ(456, e->restat_mtime);
5be83d0b » nico
2012-06-14 Only store command hashes in the build log.
187 ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
83a50c34 » nico
2012-01-04 Switch build log to use tabs as field separators, to support outputs …
188 }
5cd73252 » nico
2012-05-09 Add a BuildLog test that checks duplicate version headers don't crash.
189
190 TEST_F(BuildLogTest, DuplicateVersionHeader) {
191 // Old versions of ninja accidentally wrote multiple version headers to the
192 // build log on Windows. This shouldn't crash, and the second version header
193 // should be ignored.
194 FILE* f = fopen(kTestFilename, "wb");
195 fprintf(f, "# ninja log v4\n");
196 fprintf(f, "123\t456\t456\tout\tcommand\n");
197 fprintf(f, "# ninja log v4\n");
198 fprintf(f, "456\t789\t789\tout2\tcommand2\n");
199 fclose(f);
200
201 string err;
202 BuildLog log;
203 EXPECT_TRUE(log.Load(kTestFilename, &err));
204 ASSERT_EQ("", err);
205
206 BuildLog::LogEntry* e = log.LookupByOutput("out");
207 ASSERT_TRUE(e);
208 ASSERT_EQ(123, e->start_time);
209 ASSERT_EQ(456, e->end_time);
210 ASSERT_EQ(456, e->restat_mtime);
5be83d0b » nico
2012-06-14 Only store command hashes in the build log.
211 ASSERT_NO_FATAL_FAILURE(AssertHash("command", e->command_hash));
5cd73252 » nico
2012-05-09 Add a BuildLog test that checks duplicate version headers don't crash.
212
213 e = log.LookupByOutput("out2");
214 ASSERT_TRUE(e);
215 ASSERT_EQ(456, e->start_time);
216 ASSERT_EQ(789, e->end_time);
217 ASSERT_EQ(789, e->restat_mtime);
5be83d0b » nico
2012-06-14 Only store command hashes in the build log.
218 ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
5cd73252 » nico
2012-05-09 Add a BuildLog test that checks duplicate version headers don't crash.
219 }
4400a586 » nico
2012-05-09 Add a BuildLog test that checks that very long command liens don't cr…
220
221 TEST_F(BuildLogTest, VeryLongInputLine) {
222 // Ninja's build log buffer is currently 256kB. Lines longer than that are
223 // silently ignored, but don't affect parsing of other lines.
224 FILE* f = fopen(kTestFilename, "wb");
225 fprintf(f, "# ninja log v4\n");
226 fprintf(f, "123\t456\t456\tout\tcommand start");
227 for (size_t i = 0; i < (512 << 10) / strlen(" more_command"); ++i)
228 fputs(" more_command", f);
229 fprintf(f, "\n");
230 fprintf(f, "456\t789\t789\tout2\tcommand2\n");
231 fclose(f);
232
233 string err;
234 BuildLog log;
235 EXPECT_TRUE(log.Load(kTestFilename, &err));
236 ASSERT_EQ("", err);
237
238 BuildLog::LogEntry* e = log.LookupByOutput("out");
239 ASSERT_EQ(NULL, e);
240
241 e = log.LookupByOutput("out2");
242 ASSERT_TRUE(e);
243 ASSERT_EQ(456, e->start_time);
244 ASSERT_EQ(789, e->end_time);
245 ASSERT_EQ(789, e->restat_mtime);
5be83d0b » nico
2012-06-14 Only store command hashes in the build log.
246 ASSERT_NO_FATAL_FAILURE(AssertHash("command2", e->command_hash));
4400a586 » nico
2012-05-09 Add a BuildLog test that checks that very long command liens don't cr…
247 }
Something went wrong with that request. Please try again.