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 }
33 
34 TEST(CLParserTest, FilterInputFilename) {
35  ASSERT_TRUE(CLParser::FilterInputFilename("foobar.cc"));
36  ASSERT_TRUE(CLParser::FilterInputFilename("foo bar.cc"));
37  ASSERT_TRUE(CLParser::FilterInputFilename("baz.c"));
38  ASSERT_TRUE(CLParser::FilterInputFilename("FOOBAR.CC"));
39 
40  ASSERT_FALSE(CLParser::FilterInputFilename(
41  "src\\cl_helper.cc(166) : fatal error C1075: end "
42  "of file found ..."));
43 }
44 
45 TEST(CLParserTest, ParseSimple) {
46  CLParser parser;
47  string output = parser.Parse(
48  "foo\r\n"
49  "Note: including file: foo.h\r\n"
50  "bar\r\n");
51 
52  ASSERT_EQ("foo\nbar\n", output);
53  ASSERT_EQ(1u, parser.includes_.size());
54  ASSERT_EQ("foo.h", *parser.includes_.begin());
55 }
56 
57 TEST(CLParserTest, ParseFilenameFilter) {
58  CLParser parser;
59  string output = parser.Parse(
60  "foo.cc\r\n"
61  "cl: warning\r\n");
62  ASSERT_EQ("cl: warning\n", output);
63 }
64 
65 TEST(CLParserTest, ParseSystemInclude) {
66  CLParser parser;
67  string output = parser.Parse(
68  "Note: including file: c:\\Program Files\\foo.h\r\n"
69  "Note: including file: d:\\Microsoft Visual Studio\\bar.h\r\n"
70  "Note: including file: path.h\r\n");
71  // We should have dropped the first two includes because they look like
72  // system headers.
73  ASSERT_EQ("", output);
74  ASSERT_EQ(1u, parser.includes_.size());
75  ASSERT_EQ("path.h", *parser.includes_.begin());
76 }
77 
78 TEST(CLParserTest, DuplicatedHeader) {
79  CLParser parser;
80  string output = parser.Parse(
81  "Note: including file: foo.h\r\n"
82  "Note: including file: bar.h\r\n"
83  "Note: including file: foo.h\r\n");
84  // We should have dropped one copy of foo.h.
85  ASSERT_EQ("", output);
86  ASSERT_EQ(2u, parser.includes_.size());
87 }
88 
89 TEST(CLParserTest, DuplicatedHeaderPathConverted) {
90  CLParser parser;
91  string output = parser.Parse(
92  "Note: including file: sub/foo.h\r\n"
93  "Note: including file: bar.h\r\n"
94  "Note: including file: sub\\foo.h\r\n");
95  // We should have dropped one copy of foo.h.
96  ASSERT_EQ("", output);
97  ASSERT_EQ(2u, parser.includes_.size());
98 }
99 
100 TEST(CLParserTest, SpacesInFilename) {
101  ASSERT_EQ("sub\\some\\ sdk\\foo.h",
102  EscapeForDepfile("sub\\some sdk\\foo.h"));
103 }
104 
105 TEST(MSVCHelperTest, EnvBlock) {
106  char env_block[] = "foo=bar\0";
107  CLWrapper cl;
108  cl.SetEnvBlock(env_block);
109  string output;
110  cl.Run("cmd /c \"echo foo is %foo%", &output);
111  ASSERT_EQ("foo is bar\r\n", output);
112 }
static string FilterShowIncludes(const string &line)
Parse a line of cl.exe output and extract /showIncludes info.
static bool FilterInputFilename(string line)
Parse a line of cl.exe output and return true if it looks like it's printing an input filename...
string Parse(const string &output)
Parse the full output of cl, returning the output (if any) that should printed.
int Run(const string &command, string *output)
Start a process and gather its raw output.
TEST(CLParserTest, ShowIncludes)
Visual Studio'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:46
Wraps a synchronous execution of a CL subprocess.
Definition: msvc_helper.h:50
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:55