1 //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// 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 10 #include "llvm/Support/FileSystem.h" 11 #include "llvm/Support/PathV2.h" 12 #include "llvm/Support/ErrorHandling.h" 13 14 #include "gtest/gtest.h" 15 16 using namespace llvm; 17 using namespace llvm::sys; 18 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 message: " + \ 24 x.message()).toNullTerminatedStringRef(Message).data()); \ 25 } else {} 26 27 namespace { 28 29 TEST(Support, Path) { 30 SmallVector<StringRef, 40> paths; 31 paths.push_back(""); 32 paths.push_back("."); 33 paths.push_back(".."); 34 paths.push_back("foo"); 35 paths.push_back("/"); 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"); 71 72 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), 73 e = paths.end(); 74 i != e; 75 ++i) { 76 for (sys::path::const_iterator ci = sys::path::begin(*i), 77 ce = sys::path::end(*i); 78 ci != ce; 79 ++ci) { 80 ASSERT_FALSE(ci->empty()); 81 } 82 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); 87 ci != ce; 88 ++ci) { 89 outs() << *ci << ','; 90 } 91 outs() << "]\n"; 92 #endif 93 94 path::has_root_path(*i); 95 path::root_path(*i); 96 path::has_root_name(*i); 97 path::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); 103 path::filename(*i); 104 path::has_stem(*i); 105 path::stem(*i); 106 path::has_extension(*i); 107 path::extension(*i); 108 path::is_absolute(*i); 109 path::is_relative(*i); 110 111 SmallString<128> temp_store; 112 temp_store = *i; 113 ASSERT_NO_ERROR(fs::make_absolute(temp_store)); 114 temp_store = *i; 115 path::remove_filename(temp_store); 116 117 temp_store = *i; 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()); 123 124 path::native(*i, temp_store); 125 } 126 127 // Create a temp file. 128 int FileDescriptor; 129 SmallString<64> TempPath; 130 ASSERT_NO_ERROR( 131 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath)); 132 133 // Make sure it exists. 134 bool TempFileExists; 135 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); 136 EXPECT_TRUE(TempFileExists); 137 138 // Create another temp tile. 139 int FD2; 140 SmallString<64> TempPath2; 141 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2)); 142 ASSERT_NE(TempPath.str(), TempPath2.str()); 143 144 // Try to copy the first to the second. 145 EXPECT_EQ( 146 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists); 147 148 ::close(FD2); 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)); 152 // Remove Temp2. 153 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); 154 EXPECT_TRUE(TempFileExists); 155 156 // Make sure Temp2 doesn't exist. 157 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); 158 EXPECT_FALSE(TempFileExists); 159 160 // Create a hard link to Temp1. 161 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); 162 bool equal; 163 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); 164 EXPECT_TRUE(equal); 165 166 // Remove Temp1. 167 ::close(FileDescriptor); 168 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists)); 169 EXPECT_TRUE(TempFileExists); 170 171 // Remove the hard link. 172 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists)); 173 EXPECT_TRUE(TempFileExists); 174 175 // Make sure Temp1 doesn't exist. 176 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); 177 EXPECT_FALSE(TempFileExists); 178 179 error_code ec; 180 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) { 181 if (ec) { 182 errs() << ec.message() << '\n'; 183 errs().flush(); 184 report_fatal_error("Directory iteration failed!"); 185 } 186 } 187 } 188 189 } // anonymous namespace 190