blob: fb4d2bcd7a66a69d5a4dfcb98eee9b031a92f2f7 [file] [log] [blame]
Michael J. Spencer013d15a2010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencerf2ca4cb2010-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
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000010#include "llvm/Support/FileSystem.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000011#include "llvm/Support/PathV2.h"
Michael J. Spencer753cbbb2010-12-06 04:28:42 +000012#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer861ef4b2010-11-24 19:20:28 +000013
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000014#include "gtest/gtest.h"
15
Michael J. Spencerdffde992010-11-29 22:28:51 +000016using namespace llvm;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000017using namespace llvm::sys;
Michael J. Spencerdffde992010-11-29 22:28:51 +000018
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000019namespace {
20
Michael J. Spencer013d15a2010-11-29 22:29:04 +000021TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000022 SmallVector<StringRef, 40> paths;
23 paths.push_back("");
24 paths.push_back(".");
25 paths.push_back("..");
26 paths.push_back("foo");
27 paths.push_back("/");
28 paths.push_back("/foo");
29 paths.push_back("foo/");
30 paths.push_back("/foo/");
31 paths.push_back("foo/bar");
32 paths.push_back("/foo/bar");
33 paths.push_back("//net");
34 paths.push_back("//net/foo");
35 paths.push_back("///foo///");
36 paths.push_back("///foo///bar");
37 paths.push_back("/.");
38 paths.push_back("./");
39 paths.push_back("/..");
40 paths.push_back("../");
41 paths.push_back("foo/.");
42 paths.push_back("foo/..");
43 paths.push_back("foo/./");
44 paths.push_back("foo/./bar");
45 paths.push_back("foo/..");
46 paths.push_back("foo/../");
47 paths.push_back("foo/../bar");
48 paths.push_back("c:");
49 paths.push_back("c:/");
50 paths.push_back("c:foo");
51 paths.push_back("c:/foo");
52 paths.push_back("c:foo/");
53 paths.push_back("c:/foo/");
54 paths.push_back("c:/foo/bar");
55 paths.push_back("prn:");
56 paths.push_back("c:\\");
57 paths.push_back("c:foo");
58 paths.push_back("c:\\foo");
59 paths.push_back("c:foo\\");
60 paths.push_back("c:\\foo\\");
61 paths.push_back("c:\\foo/");
62 paths.push_back("c:/foo\\bar");
63
64 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
65 e = paths.end();
66 i != e;
67 ++i) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000068 for (sys::path::const_iterator ci = sys::path::begin(*i),
69 ce = sys::path::end(*i);
70 ci != ce;
71 ++ci) {
Michael J. Spencer371716c2010-12-03 02:22:34 +000072 ASSERT_FALSE(ci->empty());
Michael J. Spencerdffde992010-11-29 22:28:51 +000073 }
Michael J. Spencerdffde992010-11-29 22:28:51 +000074
Michael J. Spencer506e5792010-12-01 22:28:42 +000075#if 0 // Valgrind is whining about this.
Michael J. Spencera42cf732010-11-30 23:28:07 +000076 outs() << " Reverse Iteration: [";
77 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
78 ce = sys::path::rend(*i);
79 ci != ce;
80 ++ci) {
81 outs() << *ci << ',';
82 }
83 outs() << "]\n";
Michael J. Spencer506e5792010-12-01 22:28:42 +000084#endif
Michael J. Spencera42cf732010-11-30 23:28:07 +000085
Michael J. Spencer50291592010-12-07 17:04:04 +000086 path::has_root_path(*i);
87 path::root_path(*i);
88 path::has_root_name(*i);
89 path::root_name(*i);
90 path::has_root_directory(*i);
91 path::root_directory(*i);
92 path::has_parent_path(*i);
93 path::parent_path(*i);
94 path::has_filename(*i);
95 path::filename(*i);
96 path::has_stem(*i);
97 path::stem(*i);
98 path::has_extension(*i);
99 path::extension(*i);
100 path::is_absolute(*i);
101 path::is_relative(*i);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000102
Michael J. Spencer1d389622010-12-01 06:03:33 +0000103 SmallString<16> temp_store;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000104 temp_store = *i;
Michael J. Spenceree271d82010-12-07 03:57:17 +0000105 ASSERT_FALSE(fs::make_absolute(temp_store));
Michael J. Spencer371716c2010-12-03 02:22:34 +0000106 temp_store = *i;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000107 path::remove_filename(temp_store);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000108
Michael J. Spencer371716c2010-12-03 02:22:34 +0000109 temp_store = *i;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000110 path::replace_extension(temp_store, "ext");
Michael J. Spencer1d389622010-12-01 06:03:33 +0000111 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencer50291592010-12-07 17:04:04 +0000112 stem = path::stem(filename);
113 ext = path::extension(filename);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000114 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
115
Michael J. Spencer936671b2010-12-07 03:57:37 +0000116 path::native(*i, temp_store);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000117
118 outs().flush();
119 }
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000120
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000121 // Create a temp file.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000122 int FileDescriptor;
123 SmallString<64> TempPath;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000124 ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000125
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000126 // Make sure it exists.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000127 bool TempFileExists;
128 ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
129 EXPECT_TRUE(TempFileExists);
130
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000131 // Create another temp tile.
132 int FD2;
133 SmallString<64> TempPath2;
134 ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
135 ASSERT_NE(TempPath.str(), TempPath2.str());
136
137 // Try to copy the first to the second.
138 EXPECT_EQ(fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
139
140 ::close(FD2);
141 // Try again with the proper options.
142 ASSERT_FALSE(fs::copy_file(Twine(TempPath), Twine(TempPath2),
143 fs::copy_option::overwrite_if_exists));
144 // Remove Temp2.
145 ASSERT_FALSE(fs::remove(Twine(TempPath2), TempFileExists));
146 EXPECT_TRUE(TempFileExists);
147
148 // Make sure Temp2 doesn't exist.
149 ASSERT_FALSE(fs::exists(Twine(TempPath2), TempFileExists));
150 EXPECT_FALSE(TempFileExists);
151
152 // Create a hard link to Temp1.
153 ASSERT_FALSE(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
154 bool equal;
155 ASSERT_FALSE(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
156 EXPECT_TRUE(equal);
157
158 // Remove Temp1.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000159 ::close(FileDescriptor);
Michael J. Spencer106aa732010-12-03 17:53:43 +0000160 ASSERT_FALSE(fs::remove(Twine(TempPath), TempFileExists));
161 EXPECT_TRUE(TempFileExists);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000162
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000163 // Remove the hard link.
164 ASSERT_FALSE(fs::remove(Twine(TempPath2), TempFileExists));
165 EXPECT_TRUE(TempFileExists);
166
167 // Make sure Temp1 doesn't exist.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000168 ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
Benjamin Kramer6d6d16a2010-12-03 12:33:32 +0000169 EXPECT_FALSE(TempFileExists);
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000170
171 // I've yet to do directory iteration on Unix.
172#ifdef LLVM_ON_WIN32
173 error_code ec;
174 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
175 if (ec) {
176 errs() << ec.message() << '\n';
177 errs().flush();
178 report_fatal_error("Directory iteration failed!");
179 }
180 }
181#endif
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000182}
183
184} // anonymous namespace