Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 137 lines (115 sloc) 3.509 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");
681de18 @martine later entry in build log should overwrite earlier one
authored
70 fprintf(f, "0 out command abc\n");
71 fprintf(f, "0 out command def\n");
72 fclose(f);
73
74 string err;
75 BuildLog log;
667f48b @martine remove tempnam
authored
76 EXPECT_TRUE(log.Load(kTestFilename, &err));
681de18 @martine later entry in build log should overwrite earlier one
authored
77 ASSERT_EQ("", err);
78
79 BuildLog::LogEntry* e = log.LookupByOutput("out");
80 ASSERT_TRUE(e);
81 ASSERT_EQ("command def", e->command);
82 }
eccebff @martine don't crash on truncated log files
authored
83
84 TEST_F(BuildLogTest, Truncate) {
85 AssertParse(&state_,
86 "build out: cat mid\n"
87 "build mid: cat in\n");
88
89 BuildLog log1;
90 string err;
91 EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
92 ASSERT_EQ("", err);
93 log1.RecordCommand(state_.edges_[0], 15, 18);
94 log1.RecordCommand(state_.edges_[1], 20, 25);
95 log1.Close();
96
97 struct stat statbuf;
98 ASSERT_EQ(0, stat(kTestFilename, &statbuf));
99 ASSERT_GT(statbuf.st_size, 0);
100
101 // For all possible truncations of the input file, assert that we don't
102 // crash or report an error when parsing.
103 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
104 #ifndef WIN32
eccebff @martine don't crash on truncated log files
authored
105 ASSERT_EQ(0, truncate(kTestFilename, size));
41a4e26 @philipcraig Tests now build on a native Windows build (tested with VS2010)
philipcraig authored
106 #else
107 int fh;
820ecab @philipcraig mingw ninja_test links and passes. (as do linux and native VS2010)
philipcraig authored
108 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
109 ASSERT_EQ(0, _chsize(fh, size));
110 _close(fh);
111 #endif
eccebff @martine don't crash on truncated log files
authored
112
113 BuildLog log2;
114 EXPECT_TRUE(log2.Load(kTestFilename, &err));
115 ASSERT_EQ("", err);
116 }
117 }
0efbbbf @pcc Implement restat rules
pcc authored
118
119 TEST_F(BuildLogTest, UpgradeV2) {
120 FILE* f = fopen(kTestFilename, "wb");
121 fprintf(f, "# ninja log v2\n");
122 fprintf(f, "123 456 out command\n");
123 fclose(f);
124
125 string err;
126 BuildLog log;
127 EXPECT_TRUE(log.Load(kTestFilename, &err));
128 ASSERT_EQ("", err);
129
130 BuildLog::LogEntry* e = log.LookupByOutput("out");
131 ASSERT_TRUE(e);
132 ASSERT_EQ(123, e->start_time);
133 ASSERT_EQ(456, e->end_time);
134 ASSERT_EQ(0, e->restat_mtime);
135 ASSERT_EQ("command", e->command);
136 }
Something went wrong with that request. Please try again.