Ninja
depfile_parser_test.cc
Go to the documentation of this file.
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 
15 #include "depfile_parser.h"
16 
17 #include <gtest/gtest.h>
18 
19 struct DepfileParserTest : public testing::Test {
20  bool Parse(const char* input, string* err);
21 
23  string input_;
24 };
25 
26 bool DepfileParserTest::Parse(const char* input, string* err) {
27  input_ = input;
28  return parser_.Parse(&input_, err);
29 }
30 
32  string err;
33  EXPECT_TRUE(Parse(
34 "build/ninja.o: ninja.cc ninja.h eval_env.h manifest_parser.h\n",
35  &err));
36  ASSERT_EQ("", err);
37  EXPECT_EQ("build/ninja.o", parser_.out_.AsString());
38  EXPECT_EQ(4u, parser_.ins_.size());
39 }
40 
41 TEST_F(DepfileParserTest, EarlyNewlineAndWhitespace) {
42  string err;
43  EXPECT_TRUE(Parse(
44 " \\\n"
45 " out: in\n",
46  &err));
47  ASSERT_EQ("", err);
48 }
49 
50 TEST_F(DepfileParserTest, Continuation) {
51  string err;
52  EXPECT_TRUE(Parse(
53 "foo.o: \\\n"
54 " bar.h baz.h\n",
55  &err));
56  ASSERT_EQ("", err);
57  EXPECT_EQ("foo.o", parser_.out_.AsString());
58  EXPECT_EQ(2u, parser_.ins_.size());
59 }
60 
61 TEST_F(DepfileParserTest, CarriageReturnContinuation) {
62  string err;
63  EXPECT_TRUE(Parse(
64 "foo.o: \\\r\n"
65 " bar.h baz.h\r\n",
66  &err));
67  ASSERT_EQ("", err);
68  EXPECT_EQ("foo.o", parser_.out_.AsString());
69  EXPECT_EQ(2u, parser_.ins_.size());
70 }
71 
72 TEST_F(DepfileParserTest, BackSlashes) {
73  string err;
74  EXPECT_TRUE(Parse(
75 "Project\\Dir\\Build\\Release8\\Foo\\Foo.res : \\\n"
76 " Dir\\Library\\Foo.rc \\\n"
77 " Dir\\Library\\Version\\Bar.h \\\n"
78 " Dir\\Library\\Foo.ico \\\n"
79 " Project\\Thing\\Bar.tlb \\\n",
80  &err));
81  ASSERT_EQ("", err);
82  EXPECT_EQ("Project\\Dir\\Build\\Release8\\Foo\\Foo.res",
83  parser_.out_.AsString());
84  EXPECT_EQ(4u, parser_.ins_.size());
85 }
86 
88  string err;
89  EXPECT_TRUE(Parse(
90 "a\\ bc\\ def: a\\ b c d",
91  &err));
92  ASSERT_EQ("", err);
93  EXPECT_EQ("a bc def",
94  parser_.out_.AsString());
95  ASSERT_EQ(3u, parser_.ins_.size());
96  EXPECT_EQ("a b",
97  parser_.ins_[0].AsString());
98  EXPECT_EQ("c",
99  parser_.ins_[1].AsString());
100  EXPECT_EQ("d",
101  parser_.ins_[2].AsString());
102 }
103 
105  // Put backslashes before a variety of characters, see which ones make
106  // it through.
107  string err;
108  EXPECT_TRUE(Parse(
109 "\\!\\@\\#$$\\%\\^\\&\\\\",
110  &err));
111  ASSERT_EQ("", err);
112  EXPECT_EQ("\\!\\@#$\\%\\^\\&\\",
113  parser_.out_.AsString());
114  ASSERT_EQ(0u, parser_.ins_.size());
115 }
116 
117 TEST_F(DepfileParserTest, SpecialChars) {
118  // See filenames like istreambuf.iterator_op!= in
119  // https://github.com/google/libcxx/tree/master/test/iterators/stream.iterators/istreambuf.iterator/
120  string err;
121  EXPECT_TRUE(Parse(
122 "C:/Program\\ Files\\ (x86)/Microsoft\\ crtdefs.h: \n"
123 " en@quot.header~ t+t-x!=1 \n"
124 " openldap/slapd.d/cn=config/cn=schema/cn={0}core.ldif",
125  &err));
126  ASSERT_EQ("", err);
127  EXPECT_EQ("C:/Program Files (x86)/Microsoft crtdefs.h",
128  parser_.out_.AsString());
129  ASSERT_EQ(3u, parser_.ins_.size());
130  EXPECT_EQ("en@quot.header~",
131  parser_.ins_[0].AsString());
132  EXPECT_EQ("t+t-x!=1",
133  parser_.ins_[1].AsString());
134  EXPECT_EQ("openldap/slapd.d/cn=config/cn=schema/cn={0}core.ldif",
135  parser_.ins_[2].AsString());
136 }
137 
138 TEST_F(DepfileParserTest, UnifyMultipleOutputs) {
139  // check that multiple duplicate targets are properly unified
140  string err;
141  EXPECT_TRUE(Parse("foo foo: x y z", &err));
142  ASSERT_EQ(parser_.out_.AsString(), "foo");
143  ASSERT_EQ(parser_.ins_.size(), 3u);
144  EXPECT_EQ("x", parser_.ins_[0].AsString());
145  EXPECT_EQ("y", parser_.ins_[1].AsString());
146  EXPECT_EQ("z", parser_.ins_[2].AsString());
147 }
148 
149 TEST_F(DepfileParserTest, RejectMultipleDifferentOutputs) {
150  // check that multiple different outputs are rejected by the parser
151  string err;
152  EXPECT_FALSE(Parse("foo bar: x y z", &err));
153  ASSERT_EQ("depfile has multiple output paths", err);
154 }
Parser for the dependency information emitted by gcc's -M flags.
bool Parse(string *content, string *err)
Parse an input file.
bool Parse(const char *input, string *err)
TEST_F(DepfileParserTest, Basic)