Ninja
msvc_helper_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 "msvc_helper.h"
16 
17 #include <gtest/gtest.h>
18 
19 #include "test.h"
20 #include "util.h"
21 
22 TEST(CLParserTest, ShowIncludes) {
23  ASSERT_EQ("", CLParser::FilterShowIncludes("", ""));
24 
25  ASSERT_EQ("", CLParser::FilterShowIncludes("Sample compiler output", ""));
26  ASSERT_EQ("c:\\Some Files\\foobar.h",
27  CLParser::FilterShowIncludes("Note: including file: "
28  "c:\\Some Files\\foobar.h", ""));
29  ASSERT_EQ("c:\\initspaces.h",
30  CLParser::FilterShowIncludes("Note: including file: "
31  "c:\\initspaces.h", ""));
32  ASSERT_EQ("c:\\initspaces.h",
33  CLParser::FilterShowIncludes("Non-default prefix: inc file: "
34  "c:\\initspaces.h",
35  "Non-default prefix: inc file:"));
36 }
37 
38 TEST(CLParserTest, FilterInputFilename) {
39  ASSERT_TRUE(CLParser::FilterInputFilename("foobar.cc"));
40  ASSERT_TRUE(CLParser::FilterInputFilename("foo bar.cc"));
41  ASSERT_TRUE(CLParser::FilterInputFilename("baz.c"));
42  ASSERT_TRUE(CLParser::FilterInputFilename("FOOBAR.CC"));
43 
44  ASSERT_FALSE(CLParser::FilterInputFilename(
45  "src\\cl_helper.cc(166) : fatal error C1075: end "
46  "of file found ..."));
47 }
48 
49 TEST(CLParserTest, ParseSimple) {
50  CLParser parser;
51  string output = parser.Parse(
52  "foo\r\n"
53  "Note: inc file prefix: foo.h\r\n"
54  "bar\r\n",
55  "Note: inc file prefix:");
56 
57  ASSERT_EQ("foo\nbar\n", output);
58  ASSERT_EQ(1u, parser.includes_.size());
59  ASSERT_EQ("foo.h", *parser.includes_.begin());
60 }
61 
62 TEST(CLParserTest, ParseFilenameFilter) {
63  CLParser parser;
64  string output = parser.Parse(
65  "foo.cc\r\n"
66  "cl: warning\r\n",
67  "");
68  ASSERT_EQ("cl: warning\n", output);
69 }
70 
71 TEST(CLParserTest, ParseSystemInclude) {
72  CLParser parser;
73  string output = parser.Parse(
74  "Note: including file: c:\\Program Files\\foo.h\r\n"
75  "Note: including file: d:\\Microsoft Visual Studio\\bar.h\r\n"
76  "Note: including file: path.h\r\n",
77  "");
78  // We should have dropped the first two includes because they look like
79  // system headers.
80  ASSERT_EQ("", output);
81  ASSERT_EQ(1u, parser.includes_.size());
82  ASSERT_EQ("path.h", *parser.includes_.begin());
83 }
84 
85 TEST(CLParserTest, DuplicatedHeader) {
86  CLParser parser;
87  string output = parser.Parse(
88  "Note: including file: foo.h\r\n"
89  "Note: including file: bar.h\r\n"
90  "Note: including file: foo.h\r\n",
91  "");
92  // We should have dropped one copy of foo.h.
93  ASSERT_EQ("", output);
94  ASSERT_EQ(2u, parser.includes_.size());
95 }
96 
97 TEST(CLParserTest, DuplicatedHeaderPathConverted) {
98  CLParser parser;
99  string output = parser.Parse(
100  "Note: including file: sub/foo.h\r\n"
101  "Note: including file: bar.h\r\n"
102  "Note: including file: sub\\foo.h\r\n",
103  "");
104  // We should have dropped one copy of foo.h.
105  ASSERT_EQ("", output);
106  ASSERT_EQ(2u, parser.includes_.size());
107 }
108 
109 TEST(CLParserTest, SpacesInFilename) {
110  ASSERT_EQ("sub\\some\\ sdk\\foo.h",
111  EscapeForDepfile("sub\\some sdk\\foo.h"));
112 }
113 
114 TEST(MSVCHelperTest, EnvBlock) {
115  char env_block[] = "foo=bar\0";
116  CLWrapper cl;
117  cl.SetEnvBlock(env_block);
118  string output;
119  cl.Run("cmd /c \"echo foo is %foo%", &output);
120  ASSERT_EQ("foo is bar\r\n", output);
121 }
122 
123 TEST(MSVCHelperTest, NoReadOfStderr) {
124  CLWrapper cl;
125  string output;
126  cl.Run("cmd /c \"echo to stdout&& echo to stderr 1>&2", &output);
127  ASSERT_EQ("to stdout\r\n", output);
128 }
static bool FilterInputFilename(string line)
Parse a line of cl.exe output and return true if it looks like it&#39;s printing an input filename...
static string FilterShowIncludes(const string &line, const string &deps_prefix)
Parse a line of cl.exe output and extract /showIncludes info.
int Run(const string &command, string *output)
Start a process and gather its raw output.
TEST(CLParserTest, ShowIncludes)
Visual Studio&#39;s cl.exe requires some massaging to work with Ninja; for example, it emits include info...
Definition: msvc_helper.h:26
set< string > includes_
Definition: msvc_helper.h:47
string Parse(const string &output, const string &deps_prefix)
Parse the full output of cl, returning the output (if any) that should printed.
Wraps a synchronous execution of a CL subprocess.
Definition: msvc_helper.h:51
string EscapeForDepfile(const string &path)
void SetEnvBlock(void *env_block)
Set the environment block (as suitable for CreateProcess) to be used by Run().
Definition: msvc_helper.h:56