Ninja
includes_normalize_test.cc
Go to the documentation of this file.
1 // Copyright 2012 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 "includes_normalize.h"
16 
17 #include <algorithm>
18 
19 #include <direct.h>
20 
21 #include "string_piece_util.h"
22 #include "test.h"
23 #include "util.h"
24 
25 namespace {
26 
27 string GetCurDir() {
28  char buf[_MAX_PATH];
29  _getcwd(buf, sizeof(buf));
30  vector<StringPiece> parts = SplitStringPiece(buf, '\\');
31  return parts[parts.size() - 1].AsString();
32 }
33 
34 string NormalizeAndCheckNoError(const string& input) {
35  string result, err;
36  IncludesNormalize normalizer(".");
37  EXPECT_TRUE(normalizer.Normalize(input, &result, &err));
38  EXPECT_EQ("", err);
39  return result;
40 }
41 
42 string NormalizeRelativeAndCheckNoError(const string& input,
43  const string& relative_to) {
44  string result, err;
45  IncludesNormalize normalizer(relative_to);
46  EXPECT_TRUE(normalizer.Normalize(input, &result, &err));
47  EXPECT_EQ("", err);
48  return result;
49 }
50 
51 } // namespace
52 
54  EXPECT_EQ("b", NormalizeAndCheckNoError("a\\..\\b"));
55  EXPECT_EQ("b", NormalizeAndCheckNoError("a\\../b"));
56  EXPECT_EQ("a/b", NormalizeAndCheckNoError("a\\.\\b"));
57  EXPECT_EQ("a/b", NormalizeAndCheckNoError("a\\./b"));
58 }
59 
60 TEST(IncludesNormalize, WithRelative) {
61  string err;
62  string currentdir = GetCurDir();
63  EXPECT_EQ("c", NormalizeRelativeAndCheckNoError("a/b/c", "a/b"));
64  EXPECT_EQ("a",
65  NormalizeAndCheckNoError(IncludesNormalize::AbsPath("a", &err)));
66  EXPECT_EQ("", err);
67  EXPECT_EQ(string("../") + currentdir + string("/a"),
68  NormalizeRelativeAndCheckNoError("a", "../b"));
69  EXPECT_EQ(string("../") + currentdir + string("/a/b"),
70  NormalizeRelativeAndCheckNoError("a/b", "../c"));
71  EXPECT_EQ("../../a", NormalizeRelativeAndCheckNoError("a", "b/c"));
72  EXPECT_EQ(".", NormalizeRelativeAndCheckNoError("a", "a"));
73 }
74 
76  EXPECT_EQ("b", NormalizeAndCheckNoError("Abc\\..\\b"));
77  EXPECT_EQ("BdEf", NormalizeAndCheckNoError("Abc\\..\\BdEf"));
78  EXPECT_EQ("A/b", NormalizeAndCheckNoError("A\\.\\b"));
79  EXPECT_EQ("a/b", NormalizeAndCheckNoError("a\\./b"));
80  EXPECT_EQ("A/B", NormalizeAndCheckNoError("A\\.\\B"));
81  EXPECT_EQ("A/B", NormalizeAndCheckNoError("A\\./B"));
82 }
83 
84 TEST(IncludesNormalize, DifferentDrive) {
85  EXPECT_EQ("stuff.h",
86  NormalizeRelativeAndCheckNoError("p:\\vs08\\stuff.h", "p:\\vs08"));
87  EXPECT_EQ("stuff.h",
88  NormalizeRelativeAndCheckNoError("P:\\Vs08\\stuff.h", "p:\\vs08"));
89  EXPECT_EQ("p:/vs08/stuff.h",
90  NormalizeRelativeAndCheckNoError("p:\\vs08\\stuff.h", "c:\\vs08"));
91  EXPECT_EQ("P:/vs08/stufF.h", NormalizeRelativeAndCheckNoError(
92  "P:\\vs08\\stufF.h", "D:\\stuff/things"));
93  EXPECT_EQ("P:/vs08/stuff.h", NormalizeRelativeAndCheckNoError(
94  "P:/vs08\\stuff.h", "D:\\stuff/things"));
95  EXPECT_EQ("P:/wee/stuff.h",
96  NormalizeRelativeAndCheckNoError("P:/vs08\\../wee\\stuff.h",
97  "D:\\stuff/things"));
98 }
99 
100 TEST(IncludesNormalize, LongInvalidPath) {
101  const char kLongInputString[] =
102  "C:\\Program Files (x86)\\Microsoft Visual Studio "
103  "12.0\\VC\\INCLUDEwarning #31001: The dll for reading and writing the "
104  "pdb (for example, mspdb110.dll) could not be found on your path. This "
105  "is usually a configuration error. Compilation will continue using /Z7 "
106  "instead of /Zi, but expect a similar error when you link your program.";
107  // Too long, won't be canonicalized. Ensure doesn't crash.
108  string result, err;
109  IncludesNormalize normalizer(".");
110  EXPECT_FALSE(
111  normalizer.Normalize(kLongInputString, &result, &err));
112  EXPECT_EQ("path too long", err);
113 
114 
115  // Construct max size path having cwd prefix.
116  // kExactlyMaxPath = "$cwd\\a\\aaaa...aaaa\0";
117  char kExactlyMaxPath[_MAX_PATH + 1];
118  ASSERT_NE(_getcwd(kExactlyMaxPath, sizeof kExactlyMaxPath), NULL);
119 
120  int cwd_len = strlen(kExactlyMaxPath);
121  ASSERT_LE(cwd_len + 3 + 1, _MAX_PATH)
122  kExactlyMaxPath[cwd_len] = '\\';
123  kExactlyMaxPath[cwd_len + 1] = 'a';
124  kExactlyMaxPath[cwd_len + 2] = '\\';
125 
126  kExactlyMaxPath[cwd_len + 3] = 'a';
127 
128  for (int i = cwd_len + 4; i < _MAX_PATH; ++i) {
129  if (i > cwd_len + 4 && i < _MAX_PATH - 1 && i % 10 == 0)
130  kExactlyMaxPath[i] = '\\';
131  else
132  kExactlyMaxPath[i] = 'a';
133  }
134 
135  kExactlyMaxPath[_MAX_PATH] = '\0';
136  EXPECT_EQ(strlen(kExactlyMaxPath), _MAX_PATH);
137 
138  string forward_slashes(kExactlyMaxPath);
139  replace(forward_slashes.begin(), forward_slashes.end(), '\\', '/');
140  // Make sure a path that's exactly _MAX_PATH long is canonicalized.
141  EXPECT_EQ(forward_slashes.substr(cwd_len + 1),
142  NormalizeAndCheckNoError(kExactlyMaxPath));
143 }
144 
145 TEST(IncludesNormalize, ShortRelativeButTooLongAbsolutePath) {
146  string result, err;
147  IncludesNormalize normalizer(".");
148  // A short path should work
149  EXPECT_TRUE(normalizer.Normalize("a", &result, &err));
150  EXPECT_EQ("", err);
151 
152  // Construct max size path having cwd prefix.
153  // kExactlyMaxPath = "aaaa\\aaaa...aaaa\0";
154  char kExactlyMaxPath[_MAX_PATH + 1];
155  for (int i = 0; i < _MAX_PATH; ++i) {
156  if (i < _MAX_PATH - 1 && i % 10 == 4)
157  kExactlyMaxPath[i] = '\\';
158  else
159  kExactlyMaxPath[i] = 'a';
160  }
161  kExactlyMaxPath[_MAX_PATH] = '\0';
162  EXPECT_EQ(strlen(kExactlyMaxPath), _MAX_PATH);
163 
164  // Make sure a path that's exactly _MAX_PATH long fails with a proper error.
165  EXPECT_FALSE(normalizer.Normalize(kExactlyMaxPath, &result, &err));
166  EXPECT_TRUE(err.find("GetFullPathName") != string::npos);
167 }
vector< StringPiece > SplitStringPiece(StringPiece input, char sep)
#define EXPECT_TRUE(a)
Definition: test.h:76
#define EXPECT_FALSE(a)
Definition: test.h:78
bool Normalize(const string &input, string *result, string *err) const
Normalize by fixing slashes style, fixing redundant .
#define ASSERT_LE(a, b)
Definition: test.h:91
#define EXPECT_EQ(a, b)
Definition: test.h:64
static string AbsPath(StringPiece s, string *err)
Utility functions for normalizing include paths on Windows.
#define ASSERT_NE(a, b)
Definition: test.h:83
TEST(IncludesNormalize, Simple)