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);
128 class FileSystemTest : public testing::Test {
130 /// Unique temporary directory in which all created filesystem entities must
131 /// be placed. It is recursively removed at the end of each test.
132 SmallString<128> TestDirectory;
134 virtual void SetUp() {
137 fs::unique_file("%%-%%-%%-%%/test-directory.anchor", fd, TestDirectory));
138 // We don't care about this specific file.
142 virtual void TearDown() {
144 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));*/
148 TEST_F(FileSystemTest, TempFiles) {
149 // Create a temp file.
151 SmallString<64> TempPath;
153 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
155 // Make sure it exists.
157 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
158 EXPECT_TRUE(TempFileExists);
160 // Create another temp tile.
162 SmallString<64> TempPath2;
163 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
164 ASSERT_NE(TempPath.str(), TempPath2.str());
166 // Try to copy the first to the second.
168 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
171 // Try again with the proper options.
172 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
173 fs::copy_option::overwrite_if_exists));
175 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
176 EXPECT_TRUE(TempFileExists);
178 // Make sure Temp2 doesn't exist.
179 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
180 EXPECT_FALSE(TempFileExists);
182 // Create a hard link to Temp1.
183 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
185 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
189 ::close(FileDescriptor);
190 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
191 EXPECT_TRUE(TempFileExists);
193 // Remove the hard link.
194 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
195 EXPECT_TRUE(TempFileExists);
197 // Make sure Temp1 doesn't exist.
198 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
199 EXPECT_FALSE(TempFileExists);
202 TEST_F(FileSystemTest, DirectoryIteration) {
204 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
206 errs() << ec.message() << '\n';
208 report_fatal_error("Directory iteration failed!");
213 } // anonymous namespace