1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/PathV2.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
18 using namespace llvm::sys;
20 #define ASSERT_NO_ERROR(x) \
21 if (error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
32 TEST(is_separator, Works) {
33 EXPECT_TRUE(path::is_separator('/'));
34 EXPECT_FALSE(path::is_separator('\0'));
35 EXPECT_FALSE(path::is_separator('-'));
36 EXPECT_FALSE(path::is_separator(' '));
39 EXPECT_TRUE(path::is_separator('\\'));
41 EXPECT_FALSE(path::is_separator('\\'));
46 SmallVector<StringRef, 40> paths;
49 paths.push_back("..");
50 paths.push_back("foo");
52 paths.push_back("/foo");
53 paths.push_back("foo/");
54 paths.push_back("/foo/");
55 paths.push_back("foo/bar");
56 paths.push_back("/foo/bar");
57 paths.push_back("//net");
58 paths.push_back("//net/foo");
59 paths.push_back("///foo///");
60 paths.push_back("///foo///bar");
61 paths.push_back("/.");
62 paths.push_back("./");
63 paths.push_back("/..");
64 paths.push_back("../");
65 paths.push_back("foo/.");
66 paths.push_back("foo/..");
67 paths.push_back("foo/./");
68 paths.push_back("foo/./bar");
69 paths.push_back("foo/..");
70 paths.push_back("foo/../");
71 paths.push_back("foo/../bar");
72 paths.push_back("c:");
73 paths.push_back("c:/");
74 paths.push_back("c:foo");
75 paths.push_back("c:/foo");
76 paths.push_back("c:foo/");
77 paths.push_back("c:/foo/");
78 paths.push_back("c:/foo/bar");
79 paths.push_back("prn:");
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/");
86 paths.push_back("c:/foo\\bar");
88 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
92 for (sys::path::const_iterator ci = sys::path::begin(*i),
93 ce = sys::path::end(*i);
96 ASSERT_FALSE(ci->empty());
99 #if 0 // Valgrind is whining about this.
100 outs() << " Reverse Iteration: [";
101 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
102 ce = sys::path::rend(*i);
105 outs() << *ci << ',';
110 path::has_root_path(*i);
112 path::has_root_name(*i);
114 path::has_root_directory(*i);
115 path::root_directory(*i);
116 path::has_parent_path(*i);
117 path::parent_path(*i);
118 path::has_filename(*i);
122 path::has_extension(*i);
124 path::is_absolute(*i);
125 path::is_relative(*i);
127 SmallString<128> temp_store;
129 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
131 path::remove_filename(temp_store);
134 path::replace_extension(temp_store, "ext");
135 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
136 stem = path::stem(filename);
137 ext = path::extension(filename);
138 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
140 path::native(*i, temp_store);
144 class FileSystemTest : public testing::Test {
146 /// Unique temporary directory in which all created filesystem entities must
147 /// be placed. It is recursively removed at the end of each test.
148 SmallString<128> TestDirectory;
150 virtual void SetUp() {
153 fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
155 // We don't care about this specific file.
157 TestDirectory = path::parent_path(TestDirectory);
158 errs() << "Test Directory: " << TestDirectory << '\n';
162 virtual void TearDown() {
164 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
168 TEST_F(FileSystemTest, TempFiles) {
169 // Create a temp file.
171 SmallString<64> TempPath;
173 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
175 // Make sure it exists.
177 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
178 EXPECT_TRUE(TempFileExists);
180 // Create another temp tile.
182 SmallString<64> TempPath2;
183 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
184 ASSERT_NE(TempPath.str(), TempPath2.str());
186 fs::file_status A, B;
187 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
188 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
189 EXPECT_FALSE(fs::equivalent(A, B));
191 // Try to copy the first to the second.
193 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
196 // Try again with the proper options.
197 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
198 fs::copy_option::overwrite_if_exists));
200 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
201 EXPECT_TRUE(TempFileExists);
203 // Make sure Temp2 doesn't exist.
204 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
205 EXPECT_FALSE(TempFileExists);
207 // Create a hard link to Temp1.
208 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
210 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
212 ASSERT_NO_ERROR(fs::status(Twine(TempPath), A));
213 ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B));
214 EXPECT_TRUE(fs::equivalent(A, B));
217 ::close(FileDescriptor);
218 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
219 EXPECT_TRUE(TempFileExists);
221 // Remove the hard link.
222 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
223 EXPECT_TRUE(TempFileExists);
225 // Make sure Temp1 doesn't exist.
226 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
227 EXPECT_FALSE(TempFileExists);
230 TEST_F(FileSystemTest, DirectoryIteration) {
232 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
235 // Create a known hierarchy to recurse over.
237 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
238 + "/recursive/a0/aa1", existed));
239 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
240 + "/recursive/a0/ab1", existed));
241 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
242 + "/recursive/dontlookhere/da1", existed));
243 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
244 + "/recursive/z0/za1", existed));
245 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
246 + "/recursive/pop/p1", existed));
247 typedef std::vector<std::string> v_t;
249 for (fs::recursive_directory_iterator i(Twine(TestDirectory)
250 + "/recursive", ec), e; i != e; i.increment(ec)){
252 if (path::filename(i->path()) == "p1") {
254 // FIXME: recursive_directory_iterator should be more robust.
257 if (path::filename(i->path()) == "dontlookhere")
259 visited.push_back(path::filename(i->path()));
261 v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0");
262 v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1");
263 v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1");
264 v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(),
266 v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1");
267 v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0");
268 v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1");
269 v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop");
270 v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1");
272 // Make sure that each path was visited correctly.
273 ASSERT_NE(a0, visited.end());
274 ASSERT_NE(aa1, visited.end());
275 ASSERT_NE(ab1, visited.end());
276 ASSERT_NE(dontlookhere, visited.end());
277 ASSERT_EQ(da1, visited.end()); // Not visited.
278 ASSERT_NE(z0, visited.end());
279 ASSERT_NE(za1, visited.end());
280 ASSERT_NE(pop, visited.end());
281 ASSERT_EQ(p1, visited.end()); // Not visited.
283 // Make sure that parents were visited before children. No other ordering
284 // guarantees can be made across siblings.
290 TEST_F(FileSystemTest, Magic) {
292 const char *filename;
293 const char *magic_str;
294 size_t magic_str_len;
295 } types [] = {{"magic.archive", "!<arch>\x0A", 8}};
297 // Create some files filled with magic.
298 for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e;
300 SmallString<128> file_pathname(TestDirectory);
301 path::append(file_pathname, i->filename);
303 raw_fd_ostream file(file_pathname.c_str(), ErrMsg,
304 raw_fd_ostream::F_Binary);
305 ASSERT_FALSE(file.has_error());
306 StringRef magic(i->magic_str, i->magic_str_len);
310 ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res));
315 TEST_F(FileSystemTest, Canonicalize) {
316 SmallString<128> file_pathname(TestDirectory);
317 path::append(file_pathname, "canonicalize", "a0", "aa1");
320 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
321 + "/canonicalize/a0/aa1", existed));
324 path::append(file_pathname, "file.txt");
326 raw_fd_ostream file(file_pathname.c_str(), ErrMsg);
331 ASSERT_NO_ERROR(fs::canonicalize(Twine(TestDirectory)
332 + "/cAnOnIcAlIzE/A0/aA1/fIlE.TxT", res));
333 // Only check if we actually found the file. As we won't find it on case
334 // sensitive file systems.
335 if (fs::exists(res.str())) {
336 ASSERT_TRUE(res.str().find("canonicalize") != StringRef::npos);
337 ASSERT_TRUE(res.str().find("a0") != StringRef::npos);
338 ASSERT_TRUE(res.str().find("aa1") != StringRef::npos);
339 ASSERT_TRUE(res.str().find("file.txt") != StringRef::npos);
343 } // anonymous namespace