Michael J. Spencer | 013d15a | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 2 | // |
| 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. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 10 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 11 | #include "llvm/Support/PathV2.h" |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 861ef4b | 2010-11-24 19:20:28 +0000 | [diff] [blame] | 13 | |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 14 | #include "gtest/gtest.h" |
| 15 | |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 16 | using namespace llvm; |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 17 | using namespace llvm::sys; |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 18 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 19 | #define ASSERT_NO_ERROR(x) \ |
| 20 | if (error_code ec = x) { \ |
| 21 | SmallString<128> Message; \ |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame^] | 22 | GTEST_FATAL_FAILURE_((Twine(#x ": did not return errc::success.\n") + \ |
| 23 | "error number: " + Twine(ec.value()) + "\n" + \ |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 24 | "error message: " + \ |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame^] | 25 | ec.message()).toNullTerminatedStringRef(Message).data()); \ |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 26 | } else {} |
| 27 | |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
Michael J. Spencer | 013d15a | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 30 | TEST(Support, Path) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 31 | SmallVector<StringRef, 40> paths; |
| 32 | paths.push_back(""); |
| 33 | paths.push_back("."); |
| 34 | paths.push_back(".."); |
| 35 | paths.push_back("foo"); |
| 36 | paths.push_back("/"); |
| 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"); |
| 72 | |
| 73 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 74 | e = paths.end(); |
| 75 | i != e; |
| 76 | ++i) { |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 77 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 78 | ce = sys::path::end(*i); |
| 79 | ci != ce; |
| 80 | ++ci) { |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 81 | ASSERT_FALSE(ci->empty()); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 82 | } |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 83 | |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 84 | #if 0 // Valgrind is whining about this. |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 85 | outs() << " Reverse Iteration: ["; |
| 86 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 87 | ce = sys::path::rend(*i); |
| 88 | ci != ce; |
| 89 | ++ci) { |
| 90 | outs() << *ci << ','; |
| 91 | } |
| 92 | outs() << "]\n"; |
Michael J. Spencer | 506e579 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 93 | #endif |
Michael J. Spencer | a42cf73 | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 94 | |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 95 | path::has_root_path(*i); |
| 96 | path::root_path(*i); |
| 97 | path::has_root_name(*i); |
| 98 | path::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); |
| 104 | path::filename(*i); |
| 105 | path::has_stem(*i); |
| 106 | path::stem(*i); |
| 107 | path::has_extension(*i); |
| 108 | path::extension(*i); |
| 109 | path::is_absolute(*i); |
| 110 | path::is_relative(*i); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 111 | |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 112 | SmallString<128> temp_store; |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 113 | temp_store = *i; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 114 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 115 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 116 | path::remove_filename(temp_store); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 117 | |
Michael J. Spencer | 371716c | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 118 | temp_store = *i; |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 119 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 120 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | 5029159 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 121 | stem = path::stem(filename); |
| 122 | ext = path::extension(filename); |
Michael J. Spencer | 1d38962 | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 123 | EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); |
| 124 | |
Michael J. Spencer | 936671b | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 125 | path::native(*i, temp_store); |
Michael J. Spencer | dffde99 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 126 | } |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 127 | } |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 128 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 129 | class FileSystemTest : public testing::Test { |
| 130 | protected: |
| 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; |
| 134 | |
| 135 | virtual void SetUp() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame^] | 136 | int fd; |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 137 | ASSERT_NO_ERROR( |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame^] | 138 | fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd, |
| 139 | TestDirectory)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 140 | // We don't care about this specific file. |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame^] | 141 | ::close(fd); |
| 142 | TestDirectory = path::parent_path(TestDirectory); |
| 143 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 144 | errs().flush(); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | virtual void TearDown() { |
Michael J. Spencer | f94f732 | 2011-01-05 16:39:46 +0000 | [diff] [blame^] | 148 | uint32_t removed; |
| 149 | ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed)); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 150 | } |
| 151 | }; |
| 152 | |
| 153 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 154 | // Create a temp file. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 155 | int FileDescriptor; |
| 156 | SmallString<64> TempPath; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 157 | ASSERT_NO_ERROR( |
| 158 | fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 159 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 160 | // Make sure it exists. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 161 | bool TempFileExists; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 162 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 163 | EXPECT_TRUE(TempFileExists); |
| 164 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 165 | // Create another temp tile. |
| 166 | int FD2; |
| 167 | SmallString<64> TempPath2; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 168 | ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 169 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 170 | |
| 171 | // Try to copy the first to the second. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 172 | EXPECT_EQ( |
| 173 | fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 174 | |
| 175 | ::close(FD2); |
| 176 | // Try again with the proper options. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 177 | ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2), |
| 178 | fs::copy_option::overwrite_if_exists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 179 | // Remove Temp2. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 180 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 181 | EXPECT_TRUE(TempFileExists); |
| 182 | |
| 183 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 184 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 185 | EXPECT_FALSE(TempFileExists); |
| 186 | |
| 187 | // Create a hard link to Temp1. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 188 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 189 | bool equal; |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 190 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 191 | EXPECT_TRUE(equal); |
| 192 | |
| 193 | // Remove Temp1. |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 194 | ::close(FileDescriptor); |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 195 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 106aa73 | 2010-12-03 17:53:43 +0000 | [diff] [blame] | 196 | EXPECT_TRUE(TempFileExists); |
Michael J. Spencer | 3cb84ef | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 197 | |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 198 | // Remove the hard link. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 199 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 9ad8221 | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 200 | EXPECT_TRUE(TempFileExists); |
| 201 | |
| 202 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | ba64b97 | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 203 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 6d6d16a | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 204 | EXPECT_FALSE(TempFileExists); |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 205 | } |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 206 | |
Michael J. Spencer | 2558516 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 207 | TEST_F(FileSystemTest, DirectoryIteration) { |
Michael J. Spencer | 753cbbb | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 208 | error_code ec; |
| 209 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) { |
| 210 | if (ec) { |
| 211 | errs() << ec.message() << '\n'; |
| 212 | errs().flush(); |
| 213 | report_fatal_error("Directory iteration failed!"); |
| 214 | } |
| 215 | } |
Michael J. Spencer | f2ca4cb | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // anonymous namespace |