Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 157 lines (132 sloc) 4.059 kb
d52238f @martine add copyrights
authored
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
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
15 #include "build_log.h"
16
17 #include "test.h"
18
41a4e26 @philipcraig Tests now build on a native Windows build (tested with VS2010)
philipcraig authored
19 #ifdef WIN32
820ecab @philipcraig mingw ninja_test links and passes. (as do linux and native VS2010)
philipcraig authored
20 #include <fcntl.h>
21 #include <share.h>
41a4e26 @philipcraig Tests now build on a native Windows build (tested with VS2010)
philipcraig authored
22 #endif
23
7682a31 @qhuo build_log_test.cc: add 3 #include for stat(2) on Linux
qhuo authored
24 #ifdef linux
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #endif
29
667f48b @martine remove tempnam
authored
30 static const char kTestFilename[] = "BuildLogTest-tempfile";
31
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
32 struct BuildLogTest : public StateTestWithBuiltinRules {
33 virtual void SetUp() {
34 }
35 virtual void TearDown() {
667f48b @martine remove tempnam
authored
36 unlink(kTestFilename);
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
37 }
38 };
39
40 TEST_F(BuildLogTest, WriteRead) {
41 AssertParse(&state_,
42 "build out: cat mid\n"
43 "build mid: cat in\n");
44
45 BuildLog log1;
46 string err;
667f48b @martine remove tempnam
authored
47 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
48 ASSERT_EQ("", err);
fa8830b @martine add a versioning header to build log, switch to v2
authored
49 log1.RecordCommand(state_.edges_[0], 15, 18);
50 log1.RecordCommand(state_.edges_[1], 20, 25);
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
51 log1.Close();
52
53 BuildLog log2;
667f48b @martine remove tempnam
authored
54 EXPECT_TRUE(log2.Load(kTestFilename, &err));
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
55 ASSERT_EQ("", err);
56
2f289b8 @martine windows: successfully link tests
authored
57 ASSERT_EQ(2u, log1.log_.size());
58 ASSERT_EQ(2u, log2.log_.size());
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
59 BuildLog::LogEntry* e1 = log1.LookupByOutput("out");
60 ASSERT_TRUE(e1);
61 BuildLog::LogEntry* e2 = log2.LookupByOutput("out");
62 ASSERT_TRUE(e2);
63 ASSERT_TRUE(*e1 == *e2);
fa8830b @martine add a versioning header to build log, switch to v2
authored
64 ASSERT_EQ(15, e1->start_time);
9a6fd1a @martine add a class for logging builds (commands + timing)
authored
65 ASSERT_EQ("out", e1->output);
66 }
681de18 @martine later entry in build log should overwrite earlier one
authored
67
68 TEST_F(BuildLogTest, DoubleEntry) {
667f48b @martine remove tempnam
authored
69 FILE* f = fopen(kTestFilename, "wb");
23ab282 @martine make my last change actually work
authored
70 fprintf(f, "# ninja log v3\n");
71 fprintf(f, "0 1 2 out command abc\n");
72 fprintf(f, "3 4 5 out command def\n");
681de18 @martine later entry in build log should overwrite earlier one
authored
73 fclose(f);
74
75 string err;
76 BuildLog log;
667f48b @martine remove tempnam
authored
77 EXPECT_TRUE(log.Load(kTestFilename, &err));
681de18 @martine later entry in build log should overwrite earlier one
authored
78 ASSERT_EQ("", err);
79
80 BuildLog::LogEntry* e = log.LookupByOutput("out");
81 ASSERT_TRUE(e);
82 ASSERT_EQ("command def", e->command);
83 }
eccebff @martine don't crash on truncated log files
authored
84
85 TEST_F(BuildLogTest, Truncate) {
86 AssertParse(&state_,
87 "build out: cat mid\n"
88 "build mid: cat in\n");
89
90 BuildLog log1;
91 string err;
92 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
93 ASSERT_EQ("", err);
94 log1.RecordCommand(state_.edges_[0], 15, 18);
95 log1.RecordCommand(state_.edges_[1], 20, 25);
96 log1.Close();
97
98 struct stat statbuf;
99 ASSERT_EQ(0, stat(kTestFilename, &statbuf));
100 ASSERT_GT(statbuf.st_size, 0);
101
102 // For all possible truncations of the input file, assert that we don't
103 // crash or report an error when parsing.
104 for (off_t size = statbuf.st_size; size > 0; --size) {
41a4e26 @philipcraig Tests now build on a native Windows build (tested with VS2010)
philipcraig authored
105 #ifndef WIN32
eccebff @martine don't crash on truncated log files
authored
106 ASSERT_EQ(0, truncate(kTestFilename, size));
41a4e26 @philipcraig Tests now build on a native Windows build (tested with VS2010)
philipcraig authored
107 #else
108 int fh;
820ecab @philipcraig mingw ninja_test links and passes. (as do linux and native VS2010)
philipcraig authored
109 fh = _sopen(kTestFilename, _O_RDWR | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE);
41a4e26 @philipcraig Tests now build on a native Windows build (tested with VS2010)
philipcraig authored
110 ASSERT_EQ(0, _chsize(fh, size));
111 _close(fh);
112 #endif
eccebff @martine don't crash on truncated log files
authored
113
114 BuildLog log2;
115 EXPECT_TRUE(log2.Load(kTestFilename, &err));
116 ASSERT_EQ("", err);
117 }
118 }
0efbbbf @pcc Implement restat rules
pcc authored
119
23ab282 @martine make my last change actually work
authored
120 TEST_F(BuildLogTest, UpgradeV3) {
0efbbbf @pcc Implement restat rules
pcc authored
121 FILE* f = fopen(kTestFilename, "wb");
23ab282 @martine make my last change actually work
authored
122 fprintf(f, "# ninja log v3\n");
123 fprintf(f, "123 456 0 out command\n");
0efbbbf @pcc Implement restat rules
pcc authored
124 fclose(f);
125
126 string err;
127 BuildLog log;
128 EXPECT_TRUE(log.Load(kTestFilename, &err));
129 ASSERT_EQ("", err);
130
131 BuildLog::LogEntry* e = log.LookupByOutput("out");
132 ASSERT_TRUE(e);
133 ASSERT_EQ(123, e->start_time);
134 ASSERT_EQ(456, e->end_time);
135 ASSERT_EQ(0, e->restat_mtime);
136 ASSERT_EQ("command", e->command);
137 }
83a50c3 @nico Switch build log to use tabs as field separators, to support outputs wit...
nico authored
138
139 TEST_F(BuildLogTest, SpacesInOutputV4) {
140 FILE* f = fopen(kTestFilename, "wb");
141 fprintf(f, "# ninja log v4\n");
142 fprintf(f, "123\t456\t456\tout with space\tcommand\n");
143 fclose(f);
144
145 string err;
146 BuildLog log;
147 EXPECT_TRUE(log.Load(kTestFilename, &err));
148 ASSERT_EQ("", err);
149
150 BuildLog::LogEntry* e = log.LookupByOutput("out with space");
151 ASSERT_TRUE(e);
152 ASSERT_EQ(123, e->start_time);
153 ASSERT_EQ(456, e->end_time);
154 ASSERT_EQ(456, e->restat_mtime);
155 ASSERT_EQ("command", e->command);
156 }
Something went wrong with that request. Please try again.