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") + \
23 "error number: " + Twine(ec.value()) + "\n" + \
25 ec.message()).toNullTerminatedStringRef(Message).data()); \
31 SmallVector<StringRef, 40> paths;
34 paths.push_back("..");
35 paths.push_back("foo");
37 paths.push_back("/foo");
38 paths.push_back("foo/");
39 paths.push_back("/foo/");
40 paths.push_back("foo/bar");
41 paths.push_back("/foo/bar");
42 paths.push_back("//net");
43 paths.push_back("//net/foo");
44 paths.push_back("///foo///");
45 paths.push_back("///foo///bar");
46 paths.push_back("/.");
47 paths.push_back("./");
48 paths.push_back("/..");
49 paths.push_back("../");
50 paths.push_back("foo/.");
51 paths.push_back("foo/..");
52 paths.push_back("foo/./");
53 paths.push_back("foo/./bar");
54 paths.push_back("foo/..");
55 paths.push_back("foo/../");
56 paths.push_back("foo/../bar");
57 paths.push_back("c:");
58 paths.push_back("c:/");
59 paths.push_back("c:foo");
60 paths.push_back("c:/foo");
61 paths.push_back("c:foo/");
62 paths.push_back("c:/foo/");
63 paths.push_back("c:/foo/bar");
64 paths.push_back("prn:");
65 paths.push_back("c:\\");
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/");
71 paths.push_back("c:/foo\\bar");
73 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
77 for (sys::path::const_iterator ci = sys::path::begin(*i),
78 ce = sys::path::end(*i);
81 ASSERT_FALSE(ci->empty());
84 #if 0 // Valgrind is whining about this.
85 outs() << " Reverse Iteration: [";
86 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
87 ce = sys::path::rend(*i);
95 path::has_root_path(*i);
97 path::has_root_name(*i);
99 path::has_root_directory(*i);
100 path::root_directory(*i);
101 path::has_parent_path(*i);
102 path::parent_path(*i);
103 path::has_filename(*i);
107 path::has_extension(*i);
109 path::is_absolute(*i);
110 path::is_relative(*i);
112 SmallString<128> temp_store;
114 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
116 path::remove_filename(temp_store);
119 path::replace_extension(temp_store, "ext");
120 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
121 stem = path::stem(filename);
122 ext = path::extension(filename);
123 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
125 path::native(*i, temp_store);
129 class FileSystemTest : public testing::Test {
131 /// Unique temporary directory in which all created filesystem entities must
132 /// be placed. It is recursively removed at the end of each test.
133 SmallString<128> TestDirectory;
135 virtual void SetUp() {
138 fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
140 // We don't care about this specific file.
142 TestDirectory = path::parent_path(TestDirectory);
143 errs() << "Test Directory: " << TestDirectory << '\n';
147 virtual void TearDown() {
149 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
153 TEST_F(FileSystemTest, TempFiles) {
154 // Create a temp file.
156 SmallString<64> TempPath;
158 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
160 // Make sure it exists.
162 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
163 EXPECT_TRUE(TempFileExists);
165 // Create another temp tile.
167 SmallString<64> TempPath2;
168 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
169 ASSERT_NE(TempPath.str(), TempPath2.str());
171 // Try to copy the first to the second.
173 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
176 // Try again with the proper options.
177 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
178 fs::copy_option::overwrite_if_exists));
180 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
181 EXPECT_TRUE(TempFileExists);
183 // Make sure Temp2 doesn't exist.
184 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
185 EXPECT_FALSE(TempFileExists);
187 // Create a hard link to Temp1.
188 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
190 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
194 ::close(FileDescriptor);
195 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
196 EXPECT_TRUE(TempFileExists);
198 // Remove the hard link.
199 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
200 EXPECT_TRUE(TempFileExists);
202 // Make sure Temp1 doesn't exist.
203 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
204 EXPECT_FALSE(TempFileExists);
207 TEST_F(FileSystemTest, DirectoryIteration) {
209 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
211 errs() << ec.message() << '\n';
213 report_fatal_error("Directory iteration failed!");
218 } // anonymous namespace