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"
13 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
18 using namespace llvm::sys
;
20 #define ASSERT_NO_ERROR(x) \
21 if (error_code ASSERT_NO_ERROR_ec = x) { \
22 SmallString<128> MessageStorage; \
23 raw_svector_ostream Message(MessageStorage); \
24 Message << #x ": did not return errc::success.\n" \
25 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
26 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
27 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
33 SmallVector
<StringRef
, 40> paths
;
36 paths
.push_back("..");
37 paths
.push_back("foo");
39 paths
.push_back("/foo");
40 paths
.push_back("foo/");
41 paths
.push_back("/foo/");
42 paths
.push_back("foo/bar");
43 paths
.push_back("/foo/bar");
44 paths
.push_back("//net");
45 paths
.push_back("//net/foo");
46 paths
.push_back("///foo///");
47 paths
.push_back("///foo///bar");
48 paths
.push_back("/.");
49 paths
.push_back("./");
50 paths
.push_back("/..");
51 paths
.push_back("../");
52 paths
.push_back("foo/.");
53 paths
.push_back("foo/..");
54 paths
.push_back("foo/./");
55 paths
.push_back("foo/./bar");
56 paths
.push_back("foo/..");
57 paths
.push_back("foo/../");
58 paths
.push_back("foo/../bar");
59 paths
.push_back("c:");
60 paths
.push_back("c:/");
61 paths
.push_back("c:foo");
62 paths
.push_back("c:/foo");
63 paths
.push_back("c:foo/");
64 paths
.push_back("c:/foo/");
65 paths
.push_back("c:/foo/bar");
66 paths
.push_back("prn:");
67 paths
.push_back("c:\\");
68 paths
.push_back("c:foo");
69 paths
.push_back("c:\\foo");
70 paths
.push_back("c:foo\\");
71 paths
.push_back("c:\\foo\\");
72 paths
.push_back("c:\\foo/");
73 paths
.push_back("c:/foo\\bar");
75 for (SmallVector
<StringRef
, 40>::const_iterator i
= paths
.begin(),
79 for (sys::path::const_iterator ci
= sys::path::begin(*i
),
80 ce
= sys::path::end(*i
);
83 ASSERT_FALSE(ci
->empty());
86 #if 0 // Valgrind is whining about this.
87 outs() << " Reverse Iteration: [";
88 for (sys::path::reverse_iterator ci
= sys::path::rbegin(*i
),
89 ce
= sys::path::rend(*i
);
97 path::has_root_path(*i
);
99 path::has_root_name(*i
);
101 path::has_root_directory(*i
);
102 path::root_directory(*i
);
103 path::has_parent_path(*i
);
104 path::parent_path(*i
);
105 path::has_filename(*i
);
109 path::has_extension(*i
);
111 path::is_absolute(*i
);
112 path::is_relative(*i
);
114 SmallString
<128> temp_store
;
116 ASSERT_NO_ERROR(fs::make_absolute(temp_store
));
118 path::remove_filename(temp_store
);
121 path::replace_extension(temp_store
, "ext");
122 StringRef
filename(temp_store
.begin(), temp_store
.size()), stem
, ext
;
123 stem
= path::stem(filename
);
124 ext
= path::extension(filename
);
125 EXPECT_EQ(*(--sys::path::end(filename
)), (stem
+ ext
).str());
127 path::native(*i
, temp_store
);
131 class FileSystemTest
: public testing::Test
{
133 /// Unique temporary directory in which all created filesystem entities must
134 /// be placed. It is recursively removed at the end of each test.
135 SmallString
<128> TestDirectory
;
137 virtual void SetUp() {
140 fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd
,
142 // We don't care about this specific file.
144 TestDirectory
= path::parent_path(TestDirectory
);
145 errs() << "Test Directory: " << TestDirectory
<< '\n';
149 virtual void TearDown() {
151 ASSERT_NO_ERROR(fs::remove_all(TestDirectory
.str(), removed
));
155 TEST_F(FileSystemTest
, TempFiles
) {
156 // Create a temp file.
158 SmallString
<64> TempPath
;
160 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor
, TempPath
));
162 // Make sure it exists.
164 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath
), TempFileExists
));
165 EXPECT_TRUE(TempFileExists
);
167 // Create another temp tile.
169 SmallString
<64> TempPath2
;
170 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2
, TempPath2
));
171 ASSERT_NE(TempPath
.str(), TempPath2
.str());
173 // Try to copy the first to the second.
175 fs::copy_file(Twine(TempPath
), Twine(TempPath2
)), errc::file_exists
);
178 // Try again with the proper options.
179 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath
), Twine(TempPath2
),
180 fs::copy_option::overwrite_if_exists
));
182 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2
), TempFileExists
));
183 EXPECT_TRUE(TempFileExists
);
185 // Make sure Temp2 doesn't exist.
186 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2
), TempFileExists
));
187 EXPECT_FALSE(TempFileExists
);
189 // Create a hard link to Temp1.
190 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath
), Twine(TempPath2
)));
192 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath
), Twine(TempPath2
), equal
));
196 ::close(FileDescriptor
);
197 ASSERT_NO_ERROR(fs::remove(Twine(TempPath
), TempFileExists
));
198 EXPECT_TRUE(TempFileExists
);
200 // Remove the hard link.
201 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2
), TempFileExists
));
202 EXPECT_TRUE(TempFileExists
);
204 // Make sure Temp1 doesn't exist.
205 ASSERT_NO_ERROR(fs::exists(Twine(TempPath
), TempFileExists
));
206 EXPECT_FALSE(TempFileExists
);
209 TEST_F(FileSystemTest
, DirectoryIteration
) {
211 for (fs::directory_iterator
i(".", ec
), e
; i
!= e
; i
.increment(ec
))
215 TEST_F(FileSystemTest
, Magic
) {
217 const char *filename
;
218 const char *magic_str
;
219 size_t magic_str_len
;
220 } types
[] = {{"magic.archive", "!<arch>\x0A", 8}};
222 // Create some files filled with magic.
223 for (type
*i
= types
, *e
= types
+ (sizeof(types
) / sizeof(type
)); i
!= e
;
225 SmallString
<128> file_pathname(TestDirectory
);
226 path::append(file_pathname
, i
->filename
);
228 raw_fd_ostream
file(file_pathname
.c_str(), ErrMsg
,
229 raw_fd_ostream::F_Binary
);
230 ASSERT_FALSE(file
.has_error());
231 StringRef
magic(i
->magic_str
, i
->magic_str_len
);
235 ASSERT_NO_ERROR(fs::has_magic(file_pathname
.c_str(), magic
, res
));
240 } // anonymous namespace