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"
14 #include "gtest/gtest.h"
17 using namespace llvm::sys;
19 #define ASSERT_NO_ERROR(x) \
20 if (error_code ec = x) { \
21 SmallString<128> Message; \
22 GTEST_FATAL_FAILURE_((Twine(#x) + ": did not return errc::success.\n" + \
24 x.message()).toNullTerminatedStringRef(Message).data()); \
30 SmallVector<StringRef, 40> paths;
33 paths.push_back("..");
34 paths.push_back("foo");
36 paths.push_back("/foo");
37 paths.push_back("foo/");
38 paths.push_back("/foo/");
39 paths.push_back("foo/bar");
40 paths.push_back("/foo/bar");
41 paths.push_back("//net");
42 paths.push_back("//net/foo");
43 paths.push_back("///foo///");
44 paths.push_back("///foo///bar");
45 paths.push_back("/.");
46 paths.push_back("./");
47 paths.push_back("/..");
48 paths.push_back("../");
49 paths.push_back("foo/.");
50 paths.push_back("foo/..");
51 paths.push_back("foo/./");
52 paths.push_back("foo/./bar");
53 paths.push_back("foo/..");
54 paths.push_back("foo/../");
55 paths.push_back("foo/../bar");
56 paths.push_back("c:");
57 paths.push_back("c:/");
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 paths.push_back("prn:");
64 paths.push_back("c:\\");
65 paths.push_back("c:foo");
66 paths.push_back("c:\\foo");
67 paths.push_back("c:foo\\");
68 paths.push_back("c:\\foo\\");
69 paths.push_back("c:\\foo/");
70 paths.push_back("c:/foo\\bar");
72 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
76 for (sys::path::const_iterator ci = sys::path::begin(*i),
77 ce = sys::path::end(*i);
80 ASSERT_FALSE(ci->empty());
83 #if 0 // Valgrind is whining about this.
84 outs() << " Reverse Iteration: [";
85 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
86 ce = sys::path::rend(*i);
94 path::has_root_path(*i);
96 path::has_root_name(*i);
98 path::has_root_directory(*i);
99 path::root_directory(*i);
100 path::has_parent_path(*i);
101 path::parent_path(*i);
102 path::has_filename(*i);
106 path::has_extension(*i);
108 path::is_absolute(*i);
109 path::is_relative(*i);
111 SmallString<128> temp_store;
113 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
115 path::remove_filename(temp_store);
118 path::replace_extension(temp_store, "ext");
119 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
120 stem = path::stem(filename);
121 ext = path::extension(filename);
122 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
124 path::native(*i, temp_store);
127 // Create a temp file.
129 SmallString<64> TempPath;
131 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
133 // Make sure it exists.
135 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
136 EXPECT_TRUE(TempFileExists);
138 // Create another temp tile.
140 SmallString<64> TempPath2;
141 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
142 ASSERT_NE(TempPath.str(), TempPath2.str());
144 // Try to copy the first to the second.
146 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
149 // Try again with the proper options.
150 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
151 fs::copy_option::overwrite_if_exists));
153 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
154 EXPECT_TRUE(TempFileExists);
156 // Make sure Temp2 doesn't exist.
157 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
158 EXPECT_FALSE(TempFileExists);
160 // Create a hard link to Temp1.
161 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
163 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
167 ::close(FileDescriptor);
168 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
169 EXPECT_TRUE(TempFileExists);
171 // Remove the hard link.
172 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
173 EXPECT_TRUE(TempFileExists);
175 // Make sure Temp1 doesn't exist.
176 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
177 EXPECT_FALSE(TempFileExists);
180 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
182 errs() << ec.message() << '\n';
184 report_fatal_error("Directory iteration failed!");
189 } // anonymous namespace