Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// |
Michael J. Spencer | 9884778 | 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 | |
Rafael Espindola | 3bc8e71 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Path.h" |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Errc.h" |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 13 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 14 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 16 | #include "gtest/gtest.h" |
| 17 | |
Rafael Espindola | a813d60 | 2014-06-11 03:58:34 +0000 | [diff] [blame] | 18 | #ifdef LLVM_ON_WIN32 |
| 19 | #include <winerror.h> |
| 20 | #endif |
| 21 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 23 | using namespace llvm::sys; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 24 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 25 | #define ASSERT_NO_ERROR(x) \ |
| 26 | if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
| 27 | SmallString<128> MessageStorage; \ |
| 28 | raw_svector_ostream Message(MessageStorage); \ |
| 29 | Message << #x ": did not return errc::success.\n" \ |
| 30 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 31 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 32 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
| 33 | } else { \ |
| 34 | } |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 35 | |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 38 | TEST(is_separator, Works) { |
| 39 | EXPECT_TRUE(path::is_separator('/')); |
| 40 | EXPECT_FALSE(path::is_separator('\0')); |
| 41 | EXPECT_FALSE(path::is_separator('-')); |
| 42 | EXPECT_FALSE(path::is_separator(' ')); |
| 43 | |
| 44 | #ifdef LLVM_ON_WIN32 |
| 45 | EXPECT_TRUE(path::is_separator('\\')); |
| 46 | #else |
| 47 | EXPECT_FALSE(path::is_separator('\\')); |
| 48 | #endif |
| 49 | } |
| 50 | |
Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 51 | TEST(Support, Path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 52 | SmallVector<StringRef, 40> paths; |
| 53 | paths.push_back(""); |
| 54 | paths.push_back("."); |
| 55 | paths.push_back(".."); |
| 56 | paths.push_back("foo"); |
| 57 | paths.push_back("/"); |
| 58 | paths.push_back("/foo"); |
| 59 | paths.push_back("foo/"); |
| 60 | paths.push_back("/foo/"); |
| 61 | paths.push_back("foo/bar"); |
| 62 | paths.push_back("/foo/bar"); |
| 63 | paths.push_back("//net"); |
| 64 | paths.push_back("//net/foo"); |
| 65 | paths.push_back("///foo///"); |
| 66 | paths.push_back("///foo///bar"); |
| 67 | paths.push_back("/."); |
| 68 | paths.push_back("./"); |
| 69 | paths.push_back("/.."); |
| 70 | paths.push_back("../"); |
| 71 | paths.push_back("foo/."); |
| 72 | paths.push_back("foo/.."); |
| 73 | paths.push_back("foo/./"); |
| 74 | paths.push_back("foo/./bar"); |
| 75 | paths.push_back("foo/.."); |
| 76 | paths.push_back("foo/../"); |
| 77 | paths.push_back("foo/../bar"); |
| 78 | paths.push_back("c:"); |
| 79 | paths.push_back("c:/"); |
| 80 | paths.push_back("c:foo"); |
| 81 | paths.push_back("c:/foo"); |
| 82 | paths.push_back("c:foo/"); |
| 83 | paths.push_back("c:/foo/"); |
| 84 | paths.push_back("c:/foo/bar"); |
| 85 | paths.push_back("prn:"); |
| 86 | paths.push_back("c:\\"); |
| 87 | paths.push_back("c:foo"); |
| 88 | paths.push_back("c:\\foo"); |
| 89 | paths.push_back("c:foo\\"); |
| 90 | paths.push_back("c:\\foo\\"); |
| 91 | paths.push_back("c:\\foo/"); |
| 92 | paths.push_back("c:/foo\\bar"); |
| 93 | |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 94 | SmallVector<StringRef, 5> ComponentStack; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 95 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 96 | e = paths.end(); |
| 97 | i != e; |
| 98 | ++i) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 99 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 100 | ce = sys::path::end(*i); |
| 101 | ci != ce; |
| 102 | ++ci) { |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 103 | ASSERT_FALSE(ci->empty()); |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 104 | ComponentStack.push_back(*ci); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 105 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 106 | |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 107 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 108 | ce = sys::path::rend(*i); |
| 109 | ci != ce; |
| 110 | ++ci) { |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 111 | ASSERT_TRUE(*ci == ComponentStack.back()); |
| 112 | ComponentStack.pop_back(); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 113 | } |
Justin Bogner | 0c274ae | 2014-07-16 08:18:58 +0000 | [diff] [blame] | 114 | ASSERT_TRUE(ComponentStack.empty()); |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 115 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 116 | path::has_root_path(*i); |
| 117 | path::root_path(*i); |
| 118 | path::has_root_name(*i); |
| 119 | path::root_name(*i); |
| 120 | path::has_root_directory(*i); |
| 121 | path::root_directory(*i); |
| 122 | path::has_parent_path(*i); |
| 123 | path::parent_path(*i); |
| 124 | path::has_filename(*i); |
| 125 | path::filename(*i); |
| 126 | path::has_stem(*i); |
| 127 | path::stem(*i); |
| 128 | path::has_extension(*i); |
| 129 | path::extension(*i); |
| 130 | path::is_absolute(*i); |
| 131 | path::is_relative(*i); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 132 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 133 | SmallString<128> temp_store; |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 134 | temp_store = *i; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 135 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 136 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 137 | path::remove_filename(temp_store); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 138 | |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 139 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 140 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 141 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 142 | stem = path::stem(filename); |
| 143 | ext = path::extension(filename); |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame^] | 144 | EXPECT_EQ(*sys::path::rbegin(filename), (stem + ext).str()); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 145 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 146 | path::native(*i, temp_store); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 147 | } |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 148 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 149 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 150 | TEST(Support, RelativePathIterator) { |
| 151 | SmallString<64> Path(StringRef("c/d/e/foo.txt")); |
| 152 | typedef SmallVector<StringRef, 4> PathComponents; |
| 153 | PathComponents ExpectedPathComponents; |
| 154 | PathComponents ActualPathComponents; |
| 155 | |
| 156 | StringRef(Path).split(ExpectedPathComponents, "/"); |
| 157 | |
| 158 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 159 | ++I) { |
| 160 | ActualPathComponents.push_back(*I); |
| 161 | } |
| 162 | |
| 163 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 164 | |
| 165 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 166 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | TEST(Support, AbsolutePathIterator) { |
| 171 | SmallString<64> Path(StringRef("/c/d/e/foo.txt")); |
| 172 | typedef SmallVector<StringRef, 4> PathComponents; |
| 173 | PathComponents ExpectedPathComponents; |
| 174 | PathComponents ActualPathComponents; |
| 175 | |
| 176 | StringRef(Path).split(ExpectedPathComponents, "/"); |
| 177 | |
| 178 | // The root path will also be a component when iterating |
| 179 | ExpectedPathComponents[0] = "/"; |
| 180 | |
| 181 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 182 | ++I) { |
| 183 | ActualPathComponents.push_back(*I); |
| 184 | } |
| 185 | |
| 186 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 187 | |
| 188 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 189 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #ifdef LLVM_ON_WIN32 |
| 194 | TEST(Support, AbsolutePathIteratorWin32) { |
| 195 | SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); |
| 196 | typedef SmallVector<StringRef, 4> PathComponents; |
| 197 | PathComponents ExpectedPathComponents; |
| 198 | PathComponents ActualPathComponents; |
| 199 | |
| 200 | StringRef(Path).split(ExpectedPathComponents, "\\"); |
| 201 | |
| 202 | // The root path (which comes after the drive name) will also be a component |
| 203 | // when iterating. |
| 204 | ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); |
| 205 | |
| 206 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 207 | ++I) { |
| 208 | ActualPathComponents.push_back(*I); |
| 209 | } |
| 210 | |
| 211 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 212 | |
| 213 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 214 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 215 | } |
| 216 | } |
| 217 | #endif // LLVM_ON_WIN32 |
| 218 | |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 219 | TEST(Support, AbsolutePathIteratorEnd) { |
| 220 | // Trailing slashes are converted to '.' unless they are part of the root path. |
| 221 | SmallVector<StringRef, 4> Paths; |
| 222 | Paths.push_back("/foo/"); |
| 223 | Paths.push_back("/foo//"); |
| 224 | Paths.push_back("//net//"); |
| 225 | #ifdef LLVM_ON_WIN32 |
| 226 | Paths.push_back("c:\\\\"); |
| 227 | #endif |
| 228 | |
| 229 | for (StringRef Path : Paths) { |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame^] | 230 | StringRef LastComponent = *path::rbegin(Path); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 231 | EXPECT_EQ(".", LastComponent); |
| 232 | } |
| 233 | |
| 234 | SmallVector<StringRef, 3> RootPaths; |
| 235 | RootPaths.push_back("/"); |
| 236 | RootPaths.push_back("//net/"); |
| 237 | #ifdef LLVM_ON_WIN32 |
| 238 | RootPaths.push_back("c:\\"); |
| 239 | #endif |
| 240 | |
| 241 | for (StringRef Path : RootPaths) { |
Justin Bogner | 487e764 | 2014-08-04 17:36:41 +0000 | [diff] [blame^] | 242 | StringRef LastComponent = *path::rbegin(Path); |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 243 | EXPECT_EQ(1u, LastComponent.size()); |
| 244 | EXPECT_TRUE(path::is_separator(LastComponent[0])); |
| 245 | } |
| 246 | } |
| 247 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 248 | TEST(Support, HomeDirectory) { |
| 249 | #ifdef LLVM_ON_UNIX |
| 250 | // This test only makes sense on Unix if $HOME is set. |
| 251 | if (::getenv("HOME")) { |
| 252 | #endif |
| 253 | SmallString<128> HomeDir; |
| 254 | EXPECT_TRUE(path::home_directory(HomeDir)); |
| 255 | EXPECT_FALSE(HomeDir.empty()); |
| 256 | #ifdef LLVM_ON_UNIX |
| 257 | } |
| 258 | #endif |
| 259 | } |
| 260 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 261 | class FileSystemTest : public testing::Test { |
| 262 | protected: |
| 263 | /// Unique temporary directory in which all created filesystem entities must |
| 264 | /// be placed. It is recursively removed at the end of each test. |
| 265 | SmallString<128> TestDirectory; |
| 266 | |
| 267 | virtual void SetUp() { |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 268 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 269 | fs::createUniqueDirectory("file-system-test", TestDirectory)); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 270 | // We don't care about this specific file. |
Michael J. Spencer | 42368cc | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 271 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 272 | errs().flush(); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | virtual void TearDown() { |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 276 | ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 277 | } |
| 278 | }; |
| 279 | |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 280 | TEST_F(FileSystemTest, Unique) { |
| 281 | // Create a temp file. |
| 282 | int FileDescriptor; |
| 283 | SmallString<64> TempPath; |
| 284 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 285 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 286 | |
| 287 | // The same file should return an identical unique id. |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 288 | fs::UniqueID F1, F2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 289 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); |
| 290 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 291 | ASSERT_EQ(F1, F2); |
| 292 | |
| 293 | // Different files should return different unique ids. |
| 294 | int FileDescriptor2; |
| 295 | SmallString<64> TempPath2; |
| 296 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 297 | fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); |
| 298 | |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 299 | fs::UniqueID D; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 300 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 301 | ASSERT_NE(D, F1); |
| 302 | ::close(FileDescriptor2); |
| 303 | |
| 304 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 305 | |
| 306 | // Two paths representing the same file on disk should still provide the |
| 307 | // same unique id. We can test this by making a hard link. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 308 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 309 | fs::UniqueID D2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 310 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 311 | ASSERT_EQ(D2, F1); |
| 312 | |
| 313 | ::close(FileDescriptor); |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 314 | |
| 315 | SmallString<128> Dir1; |
| 316 | ASSERT_NO_ERROR( |
| 317 | fs::createUniqueDirectory("dir1", Dir1)); |
| 318 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); |
| 319 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); |
| 320 | ASSERT_EQ(F1, F2); |
| 321 | |
| 322 | SmallString<128> Dir2; |
| 323 | ASSERT_NO_ERROR( |
| 324 | fs::createUniqueDirectory("dir2", Dir2)); |
| 325 | ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); |
| 326 | ASSERT_NE(F1, F2); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 329 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 330 | // Create a temp file. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 331 | int FileDescriptor; |
| 332 | SmallString<64> TempPath; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 333 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 334 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 335 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 336 | // Make sure it exists. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 337 | bool TempFileExists; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 338 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 339 | EXPECT_TRUE(TempFileExists); |
| 340 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 341 | // Create another temp tile. |
| 342 | int FD2; |
| 343 | SmallString<64> TempPath2; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 344 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 345 | ASSERT_TRUE(TempPath2.endswith(".temp")); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 346 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 347 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 348 | fs::file_status A, B; |
| 349 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 350 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 351 | EXPECT_FALSE(fs::equivalent(A, B)); |
| 352 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 353 | ::close(FD2); |
Rafael Espindola | 213c4cb | 2013-07-18 03:29:51 +0000 | [diff] [blame] | 354 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 355 | // Remove Temp2. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 356 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 357 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 358 | ASSERT_EQ(fs::remove(Twine(TempPath2), false), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 359 | errc::no_such_file_or_directory); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 360 | |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 361 | std::error_code EC = fs::status(TempPath2.c_str(), B); |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 362 | EXPECT_EQ(EC, errc::no_such_file_or_directory); |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 363 | EXPECT_EQ(B.type(), fs::file_type::file_not_found); |
| 364 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 365 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 366 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 367 | EXPECT_FALSE(TempFileExists); |
| 368 | |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 369 | SmallString<64> TempPath3; |
| 370 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); |
| 371 | ASSERT_FALSE(TempPath3.endswith(".")); |
| 372 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 373 | // Create a hard link to Temp1. |
Rafael Espindola | 83f858e | 2014-03-11 18:40:24 +0000 | [diff] [blame] | 374 | ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 375 | bool equal; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 376 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 377 | EXPECT_TRUE(equal); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 378 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 379 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 380 | EXPECT_TRUE(fs::equivalent(A, B)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 381 | |
| 382 | // Remove Temp1. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 383 | ::close(FileDescriptor); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 384 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 385 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 386 | // Remove the hard link. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 387 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 388 | |
| 389 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 390 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 23e6bdf | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 391 | EXPECT_FALSE(TempFileExists); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 392 | |
| 393 | #ifdef LLVM_ON_WIN32 |
| 394 | // Path name > 260 chars should get an error. |
| 395 | const char *Path270 = |
| 396 | "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" |
| 397 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 398 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 399 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 400 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Rafael Espindola | 2eb1ae4 | 2013-07-05 22:32:33 +0000 | [diff] [blame] | 401 | EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 402 | errc::no_such_file_or_directory); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 403 | #endif |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 404 | } |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 405 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 406 | TEST_F(FileSystemTest, CreateDir) { |
| 407 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 408 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 409 | ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), |
Rafael Espindola | 2a826e4 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 410 | errc::file_exists); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 411 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); |
| 412 | } |
| 413 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 414 | TEST_F(FileSystemTest, DirectoryIteration) { |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 415 | std::error_code ec; |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 416 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 417 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 418 | |
| 419 | // Create a known hierarchy to recurse over. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 420 | ASSERT_NO_ERROR( |
| 421 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 422 | ASSERT_NO_ERROR( |
| 423 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 424 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + |
| 425 | "/recursive/dontlookhere/da1")); |
| 426 | ASSERT_NO_ERROR( |
| 427 | fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 428 | ASSERT_NO_ERROR( |
| 429 | fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 430 | typedef std::vector<std::string> v_t; |
| 431 | v_t visited; |
| 432 | for (fs::recursive_directory_iterator i(Twine(TestDirectory) |
| 433 | + "/recursive", ec), e; i != e; i.increment(ec)){ |
| 434 | ASSERT_NO_ERROR(ec); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 435 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 436 | i.pop(); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 437 | // FIXME: recursive_directory_iterator should be more robust. |
| 438 | if (i == e) break; |
| 439 | } |
Michael J. Spencer | 9b54a3e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 440 | if (path::filename(i->path()) == "dontlookhere") |
| 441 | i.no_push(); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 442 | visited.push_back(path::filename(i->path())); |
| 443 | } |
| 444 | v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); |
| 445 | v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); |
| 446 | v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); |
| 447 | v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), |
| 448 | "dontlookhere"); |
| 449 | v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); |
| 450 | v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); |
| 451 | v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); |
| 452 | v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); |
| 453 | v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1"); |
| 454 | |
| 455 | // Make sure that each path was visited correctly. |
| 456 | ASSERT_NE(a0, visited.end()); |
| 457 | ASSERT_NE(aa1, visited.end()); |
| 458 | ASSERT_NE(ab1, visited.end()); |
| 459 | ASSERT_NE(dontlookhere, visited.end()); |
| 460 | ASSERT_EQ(da1, visited.end()); // Not visited. |
| 461 | ASSERT_NE(z0, visited.end()); |
| 462 | ASSERT_NE(za1, visited.end()); |
| 463 | ASSERT_NE(pop, visited.end()); |
| 464 | ASSERT_EQ(p1, visited.end()); // Not visited. |
| 465 | |
| 466 | // Make sure that parents were visited before children. No other ordering |
| 467 | // guarantees can be made across siblings. |
| 468 | ASSERT_LT(a0, aa1); |
| 469 | ASSERT_LT(a0, ab1); |
| 470 | ASSERT_LT(z0, za1); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 471 | |
| 472 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 473 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 474 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); |
| 475 | ASSERT_NO_ERROR( |
| 476 | fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); |
| 477 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); |
| 478 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); |
| 479 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); |
| 480 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 481 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); |
| 482 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 483 | } |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 484 | |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 485 | const char archive[] = "!<arch>\x0A"; |
| 486 | const char bitcode[] = "\xde\xc0\x17\x0b"; |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 487 | const char coff_object[] = "\x00\x00......"; |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 488 | const char coff_import_library[] = "\x00\x00\xff\xff...."; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 489 | const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, |
| 490 | 0, 0, 0, 0, 0, 0, 0, 0, 1 }; |
| 491 | const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; |
| 492 | const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01"; |
| 493 | const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02"; |
| 494 | const char macho_fixed_virtual_memory_shared_lib[] = |
| 495 | "\xfe\xed\xfa\xce..........\x00\x03"; |
| 496 | const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04"; |
| 497 | const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05"; |
| 498 | const char macho_dynamically_linked_shared_lib[] = |
| 499 | "\xfe\xed\xfa\xce..........\x00\x06"; |
| 500 | const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07"; |
| 501 | const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08"; |
| 502 | const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a"; |
| 503 | const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 504 | |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 505 | TEST_F(FileSystemTest, Magic) { |
| 506 | struct type { |
| 507 | const char *filename; |
| 508 | const char *magic_str; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 509 | size_t magic_str_len; |
| 510 | fs::file_magic magic; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 511 | } types[] = { |
| 512 | #define DEFINE(magic) \ |
| 513 | { #magic, magic, sizeof(magic), fs::file_magic::magic } |
| 514 | DEFINE(archive), |
| 515 | DEFINE(bitcode), |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 516 | DEFINE(coff_object), |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 517 | DEFINE(coff_import_library), |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 518 | DEFINE(elf_relocatable), |
| 519 | DEFINE(macho_universal_binary), |
| 520 | DEFINE(macho_object), |
| 521 | DEFINE(macho_executable), |
| 522 | DEFINE(macho_fixed_virtual_memory_shared_lib), |
| 523 | DEFINE(macho_core), |
| 524 | DEFINE(macho_preload_executable), |
| 525 | DEFINE(macho_dynamically_linked_shared_lib), |
| 526 | DEFINE(macho_dynamic_linker), |
| 527 | DEFINE(macho_bundle), |
| 528 | DEFINE(macho_dsym_companion), |
| 529 | DEFINE(windows_resource) |
| 530 | #undef DEFINE |
| 531 | }; |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 532 | |
| 533 | // Create some files filled with magic. |
| 534 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 535 | ++i) { |
| 536 | SmallString<128> file_pathname(TestDirectory); |
| 537 | path::append(file_pathname, i->filename); |
| 538 | std::string ErrMsg; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 539 | raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_None); |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 540 | ASSERT_FALSE(file.has_error()); |
| 541 | StringRef magic(i->magic_str, i->magic_str_len); |
| 542 | file << magic; |
Michael J. Spencer | b587180 | 2011-01-15 18:52:49 +0000 | [diff] [blame] | 543 | file.close(); |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 544 | EXPECT_EQ(i->magic, fs::identify_magic(magic)); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 545 | ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 546 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 549 | #ifdef LLVM_ON_WIN32 |
| 550 | TEST_F(FileSystemTest, CarriageReturn) { |
| 551 | SmallString<128> FilePathname(TestDirectory); |
| 552 | std::string ErrMsg; |
| 553 | path::append(FilePathname, "test"); |
| 554 | |
| 555 | { |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 556 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Text); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 557 | EXPECT_EQ(ErrMsg, ""); |
| 558 | File << '\n'; |
| 559 | } |
| 560 | { |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 561 | auto Buf = MemoryBuffer::getFile(FilePathname.c_str()); |
Aaron Ballman | 586ee60 | 2014-07-06 20:20:02 +0000 | [diff] [blame] | 562 | EXPECT_TRUE((bool)Buf); |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 563 | EXPECT_EQ(Buf.get()->getBuffer(), "\r\n"); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | { |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 567 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_None); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 568 | EXPECT_EQ(ErrMsg, ""); |
| 569 | File << '\n'; |
| 570 | } |
| 571 | { |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 572 | auto Buf = MemoryBuffer::getFile(FilePathname.c_str()); |
Aaron Ballman | 586ee60 | 2014-07-06 20:20:02 +0000 | [diff] [blame] | 573 | EXPECT_TRUE((bool)Buf); |
Aaron Ballman | e56fe8a | 2014-07-06 19:34:52 +0000 | [diff] [blame] | 574 | EXPECT_EQ(Buf.get()->getBuffer(), "\n"); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 575 | } |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 576 | ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 577 | } |
| 578 | #endif |
| 579 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 580 | TEST_F(FileSystemTest, FileMapping) { |
| 581 | // Create a temp file. |
| 582 | int FileDescriptor; |
| 583 | SmallString<64> TempPath; |
| 584 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 585 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 586 | // Map in temp file and add some content |
Rafael Espindola | c049c65 | 2014-06-13 03:20:08 +0000 | [diff] [blame] | 587 | std::error_code EC; |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 588 | StringRef Val("hello there"); |
| 589 | { |
| 590 | fs::mapped_file_region mfr(FileDescriptor, |
Michael J. Spencer | 01ac34e | 2013-03-14 00:33:37 +0000 | [diff] [blame] | 591 | true, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 592 | fs::mapped_file_region::readwrite, |
| 593 | 4096, |
| 594 | 0, |
| 595 | EC); |
| 596 | ASSERT_NO_ERROR(EC); |
| 597 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 598 | // Explicitly add a 0. |
| 599 | mfr.data()[Val.size()] = 0; |
| 600 | // Unmap temp file |
| 601 | } |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 602 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 603 | // Map it back in read-only |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 604 | fs::mapped_file_region mfr(Twine(TempPath), |
| 605 | fs::mapped_file_region::readonly, |
| 606 | 0, |
| 607 | 0, |
| 608 | EC); |
| 609 | ASSERT_NO_ERROR(EC); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 610 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 611 | // Verify content |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 612 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 613 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 614 | // Unmap temp file |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 615 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 616 | fs::mapped_file_region m(Twine(TempPath), |
| 617 | fs::mapped_file_region::readonly, |
| 618 | 0, |
| 619 | 0, |
| 620 | EC); |
| 621 | ASSERT_NO_ERROR(EC); |
| 622 | const char *Data = m.const_data(); |
Chandler Carruth | 002da5d | 2014-03-02 04:08:41 +0000 | [diff] [blame] | 623 | fs::mapped_file_region mfrrv(std::move(m)); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 624 | EXPECT_EQ(mfrrv.const_data(), Data); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 625 | } |
Saleem Abdulrasool | afc50b3ed | 2014-03-11 22:05:42 +0000 | [diff] [blame] | 626 | |
| 627 | TEST(Support, NormalizePath) { |
| 628 | #if defined(LLVM_ON_WIN32) |
| 629 | #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ |
| 630 | EXPECT_EQ(path__, windows__); |
| 631 | #else |
| 632 | #define EXPECT_PATH_IS(path__, windows__, not_windows__) \ |
| 633 | EXPECT_EQ(path__, not_windows__); |
| 634 | #endif |
| 635 | |
| 636 | SmallString<64> Path1("a"); |
| 637 | SmallString<64> Path2("a/b"); |
| 638 | SmallString<64> Path3("a\\b"); |
| 639 | SmallString<64> Path4("a\\\\b"); |
| 640 | SmallString<64> Path5("\\a"); |
| 641 | SmallString<64> Path6("a\\"); |
| 642 | |
| 643 | ASSERT_NO_ERROR(fs::normalize_separators(Path1)); |
| 644 | EXPECT_PATH_IS(Path1, "a", "a"); |
| 645 | |
| 646 | ASSERT_NO_ERROR(fs::normalize_separators(Path2)); |
| 647 | EXPECT_PATH_IS(Path2, "a/b", "a/b"); |
| 648 | |
| 649 | ASSERT_NO_ERROR(fs::normalize_separators(Path3)); |
| 650 | EXPECT_PATH_IS(Path3, "a\\b", "a/b"); |
| 651 | |
| 652 | ASSERT_NO_ERROR(fs::normalize_separators(Path4)); |
| 653 | EXPECT_PATH_IS(Path4, "a\\\\b", "a\\\\b"); |
| 654 | |
| 655 | ASSERT_NO_ERROR(fs::normalize_separators(Path5)); |
| 656 | EXPECT_PATH_IS(Path5, "\\a", "/a"); |
| 657 | |
| 658 | ASSERT_NO_ERROR(fs::normalize_separators(Path6)); |
| 659 | EXPECT_PATH_IS(Path6, "a\\", "a/"); |
| 660 | |
| 661 | #undef EXPECT_PATH_IS |
| 662 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 663 | } // anonymous namespace |