Skip to content
This repository
Newer
Older
100644 187 lines (157 sloc) 4.961 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>
41a4e260 » philipcraig
2011-05-28 Tests now build on a native Windows build (tested with VS2010)
23 #endif
24
7682a312 » qhuo
2011-06-10 build_log_test.cc: add 3 #include for stat(2) on Linux
25 #ifdef linux
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #endif
30
667f48b5 »
2011-01-09 remove tempnam
31 static const char kTestFilename[] = "BuildLogTest-tempfile";
32
9a6fd1a9 »
2010-12-16 add a class for logging builds (commands + timing)
33 struct BuildLogTest : public StateTestWithBuiltinRules {
34 virtual void SetUp() {
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");
23ab2829 »
2012-01-04 make my last change actually work
101 fprintf(f, "# ninja log v3\n");
102 fprintf(f, "0 1 2 out command abc\n");
103 fprintf(f, "3 4 5 out command 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);
113 ASSERT_EQ("command def", e->command);
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
134 // crash or report an error when parsing.
135 for (off_t size = statbuf.st_size; size > 0; --size) {
faac236d »
2012-01-22 windows: use _WIN32 define everywhere
136 #ifndef _WIN32
eccebffc »
2011-05-11 don't crash on truncated log files
137 ASSERT_EQ(0, truncate(kTestFilename, size));
41a4e260 » philipcraig
2011-05-28 Tests now build on a native Windows build (tested with VS2010)
138 #else
139 int fh;
820ecab6 » philipcraig
2011-05-29 mingw ninja_test links and passes. (as do linux and native VS2010)
140 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)
141 ASSERT_EQ(0, _chsize(fh, size));
142 _close(fh);
143 #endif
eccebffc »
2011-05-11 don't crash on truncated log files
144
145 BuildLog log2;
146 EXPECT_TRUE(log2.Load(kTestFilename, &err));
147 ASSERT_EQ("", err);
148 }
149 }
0efbbbf6 » pcc
2011-09-19 Implement restat rules
150
23ab2829 »
2012-01-04 make my last change actually work
151 TEST_F(BuildLogTest, UpgradeV3) {
0efbbbf6 » pcc
2011-09-19 Implement restat rules
152 FILE* f = fopen(kTestFilename, "wb");
23ab2829 »
2012-01-04 make my last change actually work
153 fprintf(f, "# ninja log v3\n");
154 fprintf(f, "123 456 0 out command\n");
0efbbbf6 » pcc
2011-09-19 Implement restat rules
155 fclose(f);
156
157 string err;
158 BuildLog log;
159 EXPECT_TRUE(log.Load(kTestFilename, &err));
160 ASSERT_EQ("", err);
161
162 BuildLog::LogEntry* e = log.LookupByOutput("out");
163 ASSERT_TRUE(e);
164 ASSERT_EQ(123, e->start_time);
165 ASSERT_EQ(456, e->end_time);
166 ASSERT_EQ(0, e->restat_mtime);
167 ASSERT_EQ("command", e->command);
168 }
83a50c34 » nico
2012-01-04 Switch build log to use tabs as field separators, to support outputs …
169
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");
174 fclose(f);
175
176 string err;
177 BuildLog log;
178 EXPECT_TRUE(log.Load(kTestFilename, &err));
179 ASSERT_EQ("", err);
180
181 BuildLog::LogEntry* e = log.LookupByOutput("out with space");
182 ASSERT_TRUE(e);
183 ASSERT_EQ(123, e->start_time);
184 ASSERT_EQ(456, e->end_time);
185 ASSERT_EQ(456, e->restat_mtime);
186 ASSERT_EQ("command", e->command);
187 }
Something went wrong with that request. Please try again.