blob: 210b3a04cb2148915f83008a9e0f0745990ea5b7 [file] [log] [blame]
Michael J. Spencer3ef91c52010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencer98847782010-11-24 19:20:05 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rafael Espindola3bc8e712013-06-11 22:21:28 +000010#include "llvm/Support/Path.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000011#include "llvm/Support/Errc.h"
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000012#include "llvm/Support/ErrorHandling.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000013#include "llvm/Support/FileSystem.h"
Rafael Espindola84ab9b32013-07-19 14:41:25 +000014#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f063602011-01-06 05:57:54 +000015#include "llvm/Support/raw_ostream.h"
Michael J. Spencer98847782010-11-24 19:20:05 +000016#include "gtest/gtest.h"
17
Rafael Espindolaa813d602014-06-11 03:58:34 +000018#ifdef LLVM_ON_WIN32
NAKAMURA Takumib94f0522015-06-16 06:46:16 +000019#include <windows.h>
Rafael Espindolaa813d602014-06-11 03:58:34 +000020#include <winerror.h>
21#endif
22
Michael J. Spencerebad2f92010-11-29 22:28:51 +000023using namespace llvm;
Michael J. Spencer45710402010-12-03 01:21:28 +000024using namespace llvm::sys;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000025
Rafael Espindolac049c652014-06-13 03:20:08 +000026#define ASSERT_NO_ERROR(x) \
27 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
28 SmallString<128> MessageStorage; \
29 raw_svector_ostream Message(MessageStorage); \
30 Message << #x ": did not return errc::success.\n" \
31 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
32 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
33 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
34 } else { \
35 }
Michael J. Spencer3b264ba2011-01-04 17:00:18 +000036
Michael J. Spencer98847782010-11-24 19:20:05 +000037namespace {
38
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000039TEST(is_separator, Works) {
40 EXPECT_TRUE(path::is_separator('/'));
41 EXPECT_FALSE(path::is_separator('\0'));
42 EXPECT_FALSE(path::is_separator('-'));
43 EXPECT_FALSE(path::is_separator(' '));
44
45#ifdef LLVM_ON_WIN32
46 EXPECT_TRUE(path::is_separator('\\'));
47#else
48 EXPECT_FALSE(path::is_separator('\\'));
49#endif
50}
51
Michael J. Spencer3ef91c52010-11-29 22:29:04 +000052TEST(Support, Path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000053 SmallVector<StringRef, 40> paths;
54 paths.push_back("");
55 paths.push_back(".");
56 paths.push_back("..");
57 paths.push_back("foo");
58 paths.push_back("/");
59 paths.push_back("/foo");
60 paths.push_back("foo/");
61 paths.push_back("/foo/");
62 paths.push_back("foo/bar");
63 paths.push_back("/foo/bar");
64 paths.push_back("//net");
65 paths.push_back("//net/foo");
66 paths.push_back("///foo///");
67 paths.push_back("///foo///bar");
68 paths.push_back("/.");
69 paths.push_back("./");
70 paths.push_back("/..");
71 paths.push_back("../");
72 paths.push_back("foo/.");
73 paths.push_back("foo/..");
74 paths.push_back("foo/./");
75 paths.push_back("foo/./bar");
76 paths.push_back("foo/..");
77 paths.push_back("foo/../");
78 paths.push_back("foo/../bar");
79 paths.push_back("c:");
80 paths.push_back("c:/");
81 paths.push_back("c:foo");
82 paths.push_back("c:/foo");
83 paths.push_back("c:foo/");
84 paths.push_back("c:/foo/");
85 paths.push_back("c:/foo/bar");
86 paths.push_back("prn:");
87 paths.push_back("c:\\");
88 paths.push_back("c:foo");
89 paths.push_back("c:\\foo");
90 paths.push_back("c:foo\\");
91 paths.push_back("c:\\foo\\");
92 paths.push_back("c:\\foo/");
93 paths.push_back("c:/foo\\bar");
94
Justin Bogner0c274ae2014-07-16 08:18:58 +000095 SmallVector<StringRef, 5> ComponentStack;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000096 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
97 e = paths.end();
98 i != e;
99 ++i) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000100 for (sys::path::const_iterator ci = sys::path::begin(*i),
101 ce = sys::path::end(*i);
102 ci != ce;
103 ++ci) {
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000104 ASSERT_FALSE(ci->empty());
Justin Bogner0c274ae2014-07-16 08:18:58 +0000105 ComponentStack.push_back(*ci);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000106 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000107
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000108 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
109 ce = sys::path::rend(*i);
110 ci != ce;
111 ++ci) {
Justin Bogner0c274ae2014-07-16 08:18:58 +0000112 ASSERT_TRUE(*ci == ComponentStack.back());
113 ComponentStack.pop_back();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000114 }
Justin Bogner0c274ae2014-07-16 08:18:58 +0000115 ASSERT_TRUE(ComponentStack.empty());
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000116
Michael J. Spencerf616b212010-12-07 17:04:04 +0000117 path::has_root_path(*i);
118 path::root_path(*i);
119 path::has_root_name(*i);
120 path::root_name(*i);
121 path::has_root_directory(*i);
122 path::root_directory(*i);
123 path::has_parent_path(*i);
124 path::parent_path(*i);
125 path::has_filename(*i);
126 path::filename(*i);
127 path::has_stem(*i);
128 path::stem(*i);
129 path::has_extension(*i);
130 path::extension(*i);
131 path::is_absolute(*i);
132 path::is_relative(*i);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000133
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000134 SmallString<128> temp_store;
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000135 temp_store = *i;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000136 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000137 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000138 path::remove_filename(temp_store);
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000139
Michael J. Spencerb5ca6442010-12-03 02:22:34 +0000140 temp_store = *i;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000141 path::replace_extension(temp_store, "ext");
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000142 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000143 stem = path::stem(filename);
144 ext = path::extension(filename);
Justin Bogner487e7642014-08-04 17:36:41 +0000145 EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str());
Michael J. Spencer4d0c6fd2010-12-01 06:03:33 +0000146
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000147 path::native(*i, temp_store);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000148 }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000149}
Michael J. Spencer45710402010-12-03 01:21:28 +0000150
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000151TEST(Support, RelativePathIterator) {
152 SmallString<64> Path(StringRef("c/d/e/foo.txt"));
153 typedef SmallVector<StringRef, 4> PathComponents;
154 PathComponents ExpectedPathComponents;
155 PathComponents ActualPathComponents;
156
157 StringRef(Path).split(ExpectedPathComponents, "/");
158
159 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
160 ++I) {
161 ActualPathComponents.push_back(*I);
162 }
163
164 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
165
166 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
167 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
168 }
169}
170
Ben Langmuir2547f932015-03-10 00:04:29 +0000171TEST(Support, RelativePathDotIterator) {
172 SmallString<64> Path(StringRef(".c/.d/../."));
173 typedef SmallVector<StringRef, 4> PathComponents;
174 PathComponents ExpectedPathComponents;
175 PathComponents ActualPathComponents;
176
177 StringRef(Path).split(ExpectedPathComponents, "/");
178
179 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
180 ++I) {
181 ActualPathComponents.push_back(*I);
182 }
183
184 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
185
186 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
187 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
188 }
189}
190
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000191TEST(Support, AbsolutePathIterator) {
192 SmallString<64> Path(StringRef("/c/d/e/foo.txt"));
193 typedef SmallVector<StringRef, 4> PathComponents;
194 PathComponents ExpectedPathComponents;
195 PathComponents ActualPathComponents;
196
197 StringRef(Path).split(ExpectedPathComponents, "/");
198
199 // The root path will also be a component when iterating
200 ExpectedPathComponents[0] = "/";
201
202 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
203 ++I) {
204 ActualPathComponents.push_back(*I);
205 }
206
207 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
208
209 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
210 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
211 }
212}
213
Ben Langmuir2547f932015-03-10 00:04:29 +0000214TEST(Support, AbsolutePathDotIterator) {
215 SmallString<64> Path(StringRef("/.c/.d/../."));
216 typedef SmallVector<StringRef, 4> PathComponents;
217 PathComponents ExpectedPathComponents;
218 PathComponents ActualPathComponents;
219
220 StringRef(Path).split(ExpectedPathComponents, "/");
221
222 // The root path will also be a component when iterating
223 ExpectedPathComponents[0] = "/";
224
225 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
226 ++I) {
227 ActualPathComponents.push_back(*I);
228 }
229
230 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
231
232 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
233 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
234 }
235}
236
Tareq A. Siraj73537ea2013-08-12 17:10:49 +0000237#ifdef LLVM_ON_WIN32
238TEST(Support, AbsolutePathIteratorWin32) {
239 SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt"));
240 typedef SmallVector<StringRef, 4> PathComponents;
241 PathComponents ExpectedPathComponents;
242 PathComponents ActualPathComponents;
243
244 StringRef(Path).split(ExpectedPathComponents, "\\");
245
246 // The root path (which comes after the drive name) will also be a component
247 // when iterating.
248 ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\");
249
250 for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E;
251 ++I) {
252 ActualPathComponents.push_back(*I);
253 }
254
255 ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size());
256
257 for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) {
258 EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str());
259 }
260}
261#endif // LLVM_ON_WIN32
262
Ben Langmuir8d116392014-03-05 19:56:30 +0000263TEST(Support, AbsolutePathIteratorEnd) {
264 // Trailing slashes are converted to '.' unless they are part of the root path.
265 SmallVector<StringRef, 4> Paths;
266 Paths.push_back("/foo/");
267 Paths.push_back("/foo//");
268 Paths.push_back("//net//");
269#ifdef LLVM_ON_WIN32
270 Paths.push_back("c:\\\\");
271#endif
272
273 for (StringRef Path : Paths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000274 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000275 EXPECT_EQ(".", LastComponent);
276 }
277
278 SmallVector<StringRef, 3> RootPaths;
279 RootPaths.push_back("/");
280 RootPaths.push_back("//net/");
281#ifdef LLVM_ON_WIN32
282 RootPaths.push_back("c:\\");
283#endif
284
285 for (StringRef Path : RootPaths) {
Justin Bogner487e7642014-08-04 17:36:41 +0000286 StringRef LastComponent = *path::rbegin(Path);
Ben Langmuir8d116392014-03-05 19:56:30 +0000287 EXPECT_EQ(1u, LastComponent.size());
288 EXPECT_TRUE(path::is_separator(LastComponent[0]));
289 }
290}
291
Peter Collingbournef7d41012014-01-31 23:46:06 +0000292TEST(Support, HomeDirectory) {
293#ifdef LLVM_ON_UNIX
294 // This test only makes sense on Unix if $HOME is set.
295 if (::getenv("HOME")) {
296#endif
297 SmallString<128> HomeDir;
298 EXPECT_TRUE(path::home_directory(HomeDir));
299 EXPECT_FALSE(HomeDir.empty());
300#ifdef LLVM_ON_UNIX
301 }
302#endif
303}
304
Michael J. Spencer346a1332011-01-05 16:39:05 +0000305class FileSystemTest : public testing::Test {
306protected:
307 /// Unique temporary directory in which all created filesystem entities must
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000308 /// be placed. It is removed at the end of each test (must be empty).
Michael J. Spencer346a1332011-01-05 16:39:05 +0000309 SmallString<128> TestDirectory;
310
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000311 void SetUp() override {
Michael J. Spencer346a1332011-01-05 16:39:05 +0000312 ASSERT_NO_ERROR(
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000313 fs::createUniqueDirectory("file-system-test", TestDirectory));
Michael J. Spencer346a1332011-01-05 16:39:05 +0000314 // We don't care about this specific file.
Michael J. Spencer42368cc2011-01-05 16:39:46 +0000315 errs() << "Test Directory: " << TestDirectory << '\n';
316 errs().flush();
Michael J. Spencer346a1332011-01-05 16:39:05 +0000317 }
318
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000319 void TearDown() override { ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); }
Michael J. Spencer346a1332011-01-05 16:39:05 +0000320};
321
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000322TEST_F(FileSystemTest, Unique) {
323 // Create a temp file.
324 int FileDescriptor;
325 SmallString<64> TempPath;
326 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000327 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000328
329 // The same file should return an identical unique id.
Rafael Espindola7f822a92013-07-29 21:26:49 +0000330 fs::UniqueID F1, F2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000331 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1));
332 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000333 ASSERT_EQ(F1, F2);
334
335 // Different files should return different unique ids.
336 int FileDescriptor2;
337 SmallString<64> TempPath2;
338 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000339 fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2));
340
Rafael Espindola7f822a92013-07-29 21:26:49 +0000341 fs::UniqueID D;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000342 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000343 ASSERT_NE(D, F1);
344 ::close(FileDescriptor2);
345
346 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
347
348 // Two paths representing the same file on disk should still provide the
349 // same unique id. We can test this by making a hard link.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000350 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Rafael Espindola7f822a92013-07-29 21:26:49 +0000351 fs::UniqueID D2;
Rafael Espindola7cf7c512013-06-20 15:06:35 +0000352 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000353 ASSERT_EQ(D2, F1);
354
355 ::close(FileDescriptor);
Rafael Espindolaa5932af2013-07-30 20:25:53 +0000356
357 SmallString<128> Dir1;
358 ASSERT_NO_ERROR(
359 fs::createUniqueDirectory("dir1", Dir1));
360 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1));
361 ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2));
362 ASSERT_EQ(F1, F2);
363
364 SmallString<128> Dir2;
365 ASSERT_NO_ERROR(
366 fs::createUniqueDirectory("dir2", Dir2));
367 ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
368 ASSERT_NE(F1, F2);
Aaron Ballman9fbe5302013-06-19 21:03:50 +0000369}
370
Michael J. Spencer346a1332011-01-05 16:39:05 +0000371TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000372 // Create a temp file.
Michael J. Spencer45710402010-12-03 01:21:28 +0000373 int FileDescriptor;
374 SmallString<64> TempPath;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000375 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000376 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Michael J. Spencer45710402010-12-03 01:21:28 +0000377
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000378 // Make sure it exists.
Rafael Espindolac1599152014-09-11 19:11:02 +0000379 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000380
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000381 // Create another temp tile.
382 int FD2;
383 SmallString<64> TempPath2;
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000384 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
Rafael Espindolad3c89042013-07-25 15:00:17 +0000385 ASSERT_TRUE(TempPath2.endswith(".temp"));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000386 ASSERT_NE(TempPath.str(), TempPath2.str());
387
Michael J. Spencer203d7802011-12-12 06:04:28 +0000388 fs::file_status A, B;
389 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
390 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
391 EXPECT_FALSE(fs::equivalent(A, B));
392
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000393 ::close(FD2);
Rafael Espindola213c4cb2013-07-18 03:29:51 +0000394
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000395 // Remove Temp2.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000396 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
397 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
398 ASSERT_EQ(fs::remove(Twine(TempPath2), false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000399 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000400
Rafael Espindolac049c652014-06-13 03:20:08 +0000401 std::error_code EC = fs::status(TempPath2.c_str(), B);
Rafael Espindola2a826e42014-06-13 17:20:48 +0000402 EXPECT_EQ(EC, errc::no_such_file_or_directory);
Rafael Espindola107b74c2013-07-31 00:10:25 +0000403 EXPECT_EQ(B.type(), fs::file_type::file_not_found);
404
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000405 // Make sure Temp2 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000406 ASSERT_EQ(fs::access(Twine(TempPath2), sys::fs::AccessMode::Exist),
407 errc::no_such_file_or_directory);
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000408
Rafael Espindolad3c89042013-07-25 15:00:17 +0000409 SmallString<64> TempPath3;
410 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
411 ASSERT_FALSE(TempPath3.endswith("."));
412
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000413 // Create a hard link to Temp1.
Rafael Espindola83f858e2014-03-11 18:40:24 +0000414 ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000415 bool equal;
Michael J. Spencer3b264ba2011-01-04 17:00:18 +0000416 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000417 EXPECT_TRUE(equal);
Michael J. Spencer203d7802011-12-12 06:04:28 +0000418 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
419 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
420 EXPECT_TRUE(fs::equivalent(A, B));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000421
422 // Remove Temp1.
Michael J. Spencer45710402010-12-03 01:21:28 +0000423 ::close(FileDescriptor);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000424 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Michael J. Spencer45710402010-12-03 01:21:28 +0000425
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000426 // Remove the hard link.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000427 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2)));
Michael J. Spencer4fb115d2010-12-04 03:18:42 +0000428
429 // Make sure Temp1 doesn't exist.
Rafael Espindola281f23a2014-09-11 20:30:02 +0000430 ASSERT_EQ(fs::access(Twine(TempPath), sys::fs::AccessMode::Exist),
431 errc::no_such_file_or_directory);
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000432
433#ifdef LLVM_ON_WIN32
434 // Path name > 260 chars should get an error.
435 const char *Path270 =
436 "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8"
437 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
438 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
439 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
440 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000441 EXPECT_EQ(fs::createUniqueFile(Path270, FileDescriptor, TempPath),
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000442 errc::invalid_argument);
443 // Relative path < 247 chars, no problem.
444 const char *Path216 =
445 "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6"
446 "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4"
447 "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2"
448 "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0";
Paul Robinson1b6c7342014-11-13 00:36:34 +0000449 ASSERT_NO_ERROR(fs::createTemporaryFile(Path216, "", TempPath));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000450 ASSERT_NO_ERROR(fs::remove(Twine(TempPath)));
Aaron Ballmanfcdf9a82013-03-16 15:00:51 +0000451#endif
Michael J. Spencer346a1332011-01-05 16:39:05 +0000452}
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000453
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000454TEST_F(FileSystemTest, CreateDir) {
455 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
456 ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo"));
457 ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false),
Rafael Espindola2a826e42014-06-13 17:20:48 +0000458 errc::file_exists);
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000459 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000460
461#ifdef LLVM_ON_WIN32
462 // Prove that create_directories() can handle a pathname > 248 characters,
463 // which is the documented limit for CreateDirectory().
464 // (248 is MAX_PATH subtracting room for an 8.3 filename.)
465 // Generate a directory path guaranteed to fall into that range.
466 size_t TmpLen = TestDirectory.size();
467 const char *OneDir = "\\123456789";
468 size_t OneDirLen = strlen(OneDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000469 ASSERT_LT(OneDirLen, 12U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000470 size_t NLevels = ((248 - TmpLen) / OneDirLen) + 1;
471 SmallString<260> LongDir(TestDirectory);
472 for (size_t I = 0; I < NLevels; ++I)
473 LongDir.append(OneDir);
474 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
475 ASSERT_NO_ERROR(fs::create_directories(Twine(LongDir)));
476 ASSERT_EQ(fs::create_directories(Twine(LongDir), false),
477 errc::file_exists);
478 // Tidy up, "recursively" removing the directories.
479 StringRef ThisDir(LongDir);
480 for (size_t J = 0; J < NLevels; ++J) {
481 ASSERT_NO_ERROR(fs::remove(ThisDir));
482 ThisDir = path::parent_path(ThisDir);
483 }
484
485 // Similarly for a relative pathname. Need to set the current directory to
486 // TestDirectory so that the one we create ends up in the right place.
487 char PreviousDir[260];
488 size_t PreviousDirLen = ::GetCurrentDirectoryA(260, PreviousDir);
Aaron Ballman493456e2014-11-13 13:39:49 +0000489 ASSERT_GT(PreviousDirLen, 0U);
490 ASSERT_LT(PreviousDirLen, 260U);
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000491 ASSERT_NE(::SetCurrentDirectoryA(TestDirectory.c_str()), 0);
492 LongDir.clear();
493 // Generate a relative directory name with absolute length > 248.
494 size_t LongDirLen = 249 - TestDirectory.size();
495 LongDir.assign(LongDirLen, 'a');
496 ASSERT_NO_ERROR(fs::create_directory(Twine(LongDir)));
497 // While we're here, prove that .. and . handling works in these long paths.
498 const char *DotDotDirs = "\\..\\.\\b";
499 LongDir.append(DotDotDirs);
Paul Robinson1b6c7342014-11-13 00:36:34 +0000500 ASSERT_NO_ERROR(fs::create_directory("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000501 ASSERT_EQ(fs::create_directory(Twine(LongDir), false), errc::file_exists);
502 // And clean up.
Paul Robinson1b6c7342014-11-13 00:36:34 +0000503 ASSERT_NO_ERROR(fs::remove("b"));
Paul Robinsond9c4a9a2014-11-13 00:12:14 +0000504 ASSERT_NO_ERROR(fs::remove(
505 Twine(LongDir.substr(0, LongDir.size() - strlen(DotDotDirs)))));
506 ASSERT_NE(::SetCurrentDirectoryA(PreviousDir), 0);
507#endif
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000508}
509
Michael J. Spencer346a1332011-01-05 16:39:05 +0000510TEST_F(FileSystemTest, DirectoryIteration) {
Rafael Espindolac049c652014-06-13 03:20:08 +0000511 std::error_code ec;
Michael J. Spencer1f063602011-01-06 05:57:54 +0000512 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
513 ASSERT_NO_ERROR(ec);
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000514
515 // Create a known hierarchy to recurse over.
Rafael Espindola5c20ac02014-02-23 13:56:14 +0000516 ASSERT_NO_ERROR(
517 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1"));
518 ASSERT_NO_ERROR(
519 fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1"));
520 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) +
521 "/recursive/dontlookhere/da1"));
522 ASSERT_NO_ERROR(
523 fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1"));
524 ASSERT_NO_ERROR(
525 fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1"));
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000526 typedef std::vector<std::string> v_t;
527 v_t visited;
528 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
529 + "/recursive", ec), e; i != e; i.increment(ec)){
530 ASSERT_NO_ERROR(ec);
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000531 if (path::filename(i->path()) == "p1") {
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000532 i.pop();
NAKAMURA Takumi0cfb3672011-12-09 23:20:03 +0000533 // FIXME: recursive_directory_iterator should be more robust.
534 if (i == e) break;
535 }
Michael J. Spencer9b54a3e2011-12-09 01:14:41 +0000536 if (path::filename(i->path()) == "dontlookhere")
537 i.no_push();
Michael J. Spencer0a7625d2011-12-08 22:50:09 +0000538 visited.push_back(path::filename(i->path()));
539 }
540 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
541 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
542 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
543 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
544 "dontlookhere");
545 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
546 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
547 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
548 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
549 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
550
551 // Make sure that each path was visited correctly.
552 ASSERT_NE(a0, visited.end());
553 ASSERT_NE(aa1, visited.end());
554 ASSERT_NE(ab1, visited.end());
555 ASSERT_NE(dontlookhere, visited.end());
556 ASSERT_EQ(da1, visited.end()); // Not visited.
557 ASSERT_NE(z0, visited.end());
558 ASSERT_NE(za1, visited.end());
559 ASSERT_NE(pop, visited.end());
560 ASSERT_EQ(p1, visited.end()); // Not visited.
561
562 // Make sure that parents were visited before children. No other ordering
563 // guarantees can be made across siblings.
564 ASSERT_LT(a0, aa1);
565 ASSERT_LT(a0, ab1);
566 ASSERT_LT(z0, za1);
Rafael Espindola78dcc032014-01-10 20:36:42 +0000567
568 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1"));
569 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1"));
570 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0"));
571 ASSERT_NO_ERROR(
572 fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1"));
573 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere"));
574 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1"));
575 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop"));
576 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1"));
577 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0"));
578 ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive"));
Michael J. Spencer1f063602011-01-06 05:57:54 +0000579}
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000580
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000581const char archive[] = "!<arch>\x0A";
582const char bitcode[] = "\xde\xc0\x17\x0b";
Rui Ueyama829c4392013-11-14 22:09:08 +0000583const char coff_object[] = "\x00\x00......";
Rui Ueyama5c69ff52014-09-11 22:34:32 +0000584const char coff_bigobj[] = "\x00\x00\xff\xff\x00\x02......"
Rui Ueyama2acb0582014-09-11 21:09:57 +0000585 "\xc7\xa1\xba\xd1\xee\xba\xa9\x4b\xaf\x20\xfa\xf6\x6a\xa4\xdc\xb8";
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000586const char coff_import_library[] = "\x00\x00\xff\xff....";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000587const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0,
588 0, 0, 0, 0, 0, 0, 0, 0, 1 };
589const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00";
590const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01";
591const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02";
592const char macho_fixed_virtual_memory_shared_lib[] =
593 "\xfe\xed\xfa\xce..........\x00\x03";
594const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04";
595const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05";
596const char macho_dynamically_linked_shared_lib[] =
597 "\xfe\xed\xfa\xce..........\x00\x06";
598const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07";
599const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08";
600const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a";
Justin Bognera7ad4b32015-02-25 22:59:20 +0000601const char macho_kext_bundle[] = "\xfe\xed\xfa\xce..........\x00\x0b";
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000602const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff";
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000603const char macho_dynamically_linked_shared_lib_stub[] =
604 "\xfe\xed\xfa\xce..........\x00\x09";
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000605
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000606TEST_F(FileSystemTest, Magic) {
607 struct type {
608 const char *filename;
609 const char *magic_str;
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000610 size_t magic_str_len;
611 fs::file_magic magic;
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000612 } types[] = {
613#define DEFINE(magic) \
614 { #magic, magic, sizeof(magic), fs::file_magic::magic }
615 DEFINE(archive),
616 DEFINE(bitcode),
Rui Ueyama829c4392013-11-14 22:09:08 +0000617 DEFINE(coff_object),
Rui Ueyama2acb0582014-09-11 21:09:57 +0000618 { "coff_bigobj", coff_bigobj, sizeof(coff_bigobj), fs::file_magic::coff_object },
Rui Ueyamae448f9e2013-11-15 21:22:02 +0000619 DEFINE(coff_import_library),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000620 DEFINE(elf_relocatable),
621 DEFINE(macho_universal_binary),
622 DEFINE(macho_object),
623 DEFINE(macho_executable),
624 DEFINE(macho_fixed_virtual_memory_shared_lib),
625 DEFINE(macho_core),
626 DEFINE(macho_preload_executable),
627 DEFINE(macho_dynamically_linked_shared_lib),
628 DEFINE(macho_dynamic_linker),
629 DEFINE(macho_bundle),
Nick Kledzik2d2b2542014-09-17 00:53:44 +0000630 DEFINE(macho_dynamically_linked_shared_lib_stub),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000631 DEFINE(macho_dsym_companion),
Justin Bognera7ad4b32015-02-25 22:59:20 +0000632 DEFINE(macho_kext_bundle),
Rui Ueyama5e3de7a2013-11-13 21:55:41 +0000633 DEFINE(windows_resource)
634#undef DEFINE
635 };
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000636
637 // Create some files filled with magic.
638 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
639 ++i) {
640 SmallString<128> file_pathname(TestDirectory);
641 path::append(file_pathname, i->filename);
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000642 std::error_code EC;
643 raw_fd_ostream file(file_pathname, EC, sys::fs::F_None);
Michael J. Spencer1d3c4a72011-01-06 05:58:02 +0000644 ASSERT_FALSE(file.has_error());
645 StringRef magic(i->magic_str, i->magic_str_len);
646 file << magic;
Michael J. Spencerb5871802011-01-15 18:52:49 +0000647 file.close();
Michael J. Spencerb8055cb2013-04-05 20:10:04 +0000648 EXPECT_EQ(i->magic, fs::identify_magic(magic));
Rafael Espindola78dcc032014-01-10 20:36:42 +0000649 ASSERT_NO_ERROR(fs::remove(Twine(file_pathname)));
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +0000650 }
Michael J. Spencer98847782010-11-24 19:20:05 +0000651}
652
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000653#ifdef LLVM_ON_WIN32
654TEST_F(FileSystemTest, CarriageReturn) {
655 SmallString<128> FilePathname(TestDirectory);
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000656 std::error_code EC;
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000657 path::append(FilePathname, "test");
658
659 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000660 raw_fd_ostream File(FilePathname, EC, sys::fs::F_Text);
661 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000662 File << '\n';
663 }
664 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000665 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000666 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000667 EXPECT_EQ(Buf.get()->getBuffer(), "\r\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000668 }
669
670 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000671 raw_fd_ostream File(FilePathname, EC, sys::fs::F_None);
672 ASSERT_NO_ERROR(EC);
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000673 File << '\n';
674 }
675 {
Reid Klecknerd83c63b2014-08-26 00:24:23 +0000676 auto Buf = MemoryBuffer::getFile(FilePathname.str());
Aaron Ballman586ee602014-07-06 20:20:02 +0000677 EXPECT_TRUE((bool)Buf);
Aaron Ballmane56fe8a2014-07-06 19:34:52 +0000678 EXPECT_EQ(Buf.get()->getBuffer(), "\n");
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000679 }
Rafael Espindola78dcc032014-01-10 20:36:42 +0000680 ASSERT_NO_ERROR(fs::remove(Twine(FilePathname)));
Rafael Espindola84ab9b32013-07-19 14:41:25 +0000681}
682#endif
683
Rafael Espindola59aaa6c2014-12-12 17:55:12 +0000684TEST_F(FileSystemTest, Resize) {
685 int FD;
686 SmallString<64> TempPath;
687 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
688 ASSERT_NO_ERROR(fs::resize_file(FD, 123));
689 fs::file_status Status;
690 ASSERT_NO_ERROR(fs::status(FD, Status));
691 ASSERT_EQ(Status.getSize(), 123U);
692}
693
Nick Kledzik18497e92012-06-20 00:28:54 +0000694TEST_F(FileSystemTest, FileMapping) {
695 // Create a temp file.
696 int FileDescriptor;
697 SmallString<64> TempPath;
698 ASSERT_NO_ERROR(
Rafael Espindola155cf0f2013-07-05 20:14:52 +0000699 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000700 unsigned Size = 4096;
701 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
702
Nick Kledzik18497e92012-06-20 00:28:54 +0000703 // Map in temp file and add some content
Rafael Espindolac049c652014-06-13 03:20:08 +0000704 std::error_code EC;
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000705 StringRef Val("hello there");
706 {
707 fs::mapped_file_region mfr(FileDescriptor,
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000708 fs::mapped_file_region::readwrite, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000709 ASSERT_NO_ERROR(EC);
710 std::copy(Val.begin(), Val.end(), mfr.data());
711 // Explicitly add a 0.
712 mfr.data()[Val.size()] = 0;
713 // Unmap temp file
714 }
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000715
Nick Kledzik18497e92012-06-20 00:28:54 +0000716 // Map it back in read-only
Rafael Espindola71bc5072014-12-11 17:17:26 +0000717 int FD;
718 EC = fs::openFileForRead(Twine(TempPath), FD);
719 ASSERT_NO_ERROR(EC);
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000720 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000721 ASSERT_NO_ERROR(EC);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000722
Nick Kledzik18497e92012-06-20 00:28:54 +0000723 // Verify content
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000724 EXPECT_EQ(StringRef(mfr.const_data()), Val);
Rui Ueyama89d1bdb2013-11-13 20:31:21 +0000725
Nick Kledzik18497e92012-06-20 00:28:54 +0000726 // Unmap temp file
Rafael Espindolac69f13b2014-12-12 18:13:23 +0000727 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000728 ASSERT_NO_ERROR(EC);
Rafael Espindola71bc5072014-12-11 17:17:26 +0000729 ASSERT_EQ(close(FD), 0);
Michael J. Spenceref2284f2012-08-15 19:05:47 +0000730}
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000731
732TEST(Support, NormalizePath) {
733#if defined(LLVM_ON_WIN32)
734#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
735 EXPECT_EQ(path__, windows__);
736#else
737#define EXPECT_PATH_IS(path__, windows__, not_windows__) \
738 EXPECT_EQ(path__, not_windows__);
739#endif
740
741 SmallString<64> Path1("a");
742 SmallString<64> Path2("a/b");
743 SmallString<64> Path3("a\\b");
744 SmallString<64> Path4("a\\\\b");
745 SmallString<64> Path5("\\a");
746 SmallString<64> Path6("a\\");
747
Rafael Espindola7e774c22014-08-08 21:35:52 +0000748 path::native(Path1);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000749 EXPECT_PATH_IS(Path1, "a", "a");
750
Rafael Espindola7e774c22014-08-08 21:35:52 +0000751 path::native(Path2);
Rafael Espindolaffbabb72014-08-09 00:37:05 +0000752 EXPECT_PATH_IS(Path2, "a\\b", "a/b");
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000753
Rafael Espindola7e774c22014-08-08 21:35:52 +0000754 path::native(Path3);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000755 EXPECT_PATH_IS(Path3, "a\\b", "a/b");
756
Rafael Espindola7e774c22014-08-08 21:35:52 +0000757 path::native(Path4);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000758 EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b");
759
Rafael Espindola7e774c22014-08-08 21:35:52 +0000760 path::native(Path5);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000761 EXPECT_PATH_IS(Path5, "\\a", "/a");
762
Rafael Espindola7e774c22014-08-08 21:35:52 +0000763 path::native(Path6);
Saleem Abdulrasoolafc50b3ed2014-03-11 22:05:42 +0000764 EXPECT_PATH_IS(Path6, "a\\", "a/");
765
766#undef EXPECT_PATH_IS
767}
Michael J. Spencer98847782010-11-24 19:20:05 +0000768} // anonymous namespace