blob: 132d43950b3199a53f27e3f2f6804e2f687f4422 [file] [log] [blame]
Michael J. Spencer013d15a2010-11-29 22:29:04 +00001//===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===//
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +00002//
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. Spencer3cb84ef2010-12-03 01:21:28 +000010#include "llvm/Support/FileSystem.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000011#include "llvm/Support/PathV2.h"
Michael J. Spencer753cbbb2010-12-06 04:28:42 +000012#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer861ef4b2010-11-24 19:20:28 +000013
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000014#include "gtest/gtest.h"
15
Michael J. Spencerdffde992010-11-29 22:28:51 +000016using namespace llvm;
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +000017using namespace llvm::sys;
Michael J. Spencerdffde992010-11-29 22:28:51 +000018
Michael J. Spencerba64b972011-01-04 17:00:18 +000019#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
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +000027namespace {
28
Michael J. Spencer013d15a2010-11-29 22:29:04 +000029TEST(Support, Path) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000030 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) {
Michael J. Spencerdffde992010-11-29 22:28:51 +000076 for (sys::path::const_iterator ci = sys::path::begin(*i),
77 ce = sys::path::end(*i);
78 ci != ce;
79 ++ci) {
Michael J. Spencer371716c2010-12-03 02:22:34 +000080 ASSERT_FALSE(ci->empty());
Michael J. Spencerdffde992010-11-29 22:28:51 +000081 }
Michael J. Spencerdffde992010-11-29 22:28:51 +000082
Michael J. Spencer506e5792010-12-01 22:28:42 +000083#if 0 // Valgrind is whining about this.
Michael J. Spencera42cf732010-11-30 23:28:07 +000084 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";
Michael J. Spencer506e5792010-12-01 22:28:42 +000092#endif
Michael J. Spencera42cf732010-11-30 23:28:07 +000093
Michael J. Spencer50291592010-12-07 17:04:04 +000094 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);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000110
Michael J. Spencerba64b972011-01-04 17:00:18 +0000111 SmallString<128> temp_store;
Michael J. Spencer371716c2010-12-03 02:22:34 +0000112 temp_store = *i;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000113 ASSERT_NO_ERROR(fs::make_absolute(temp_store));
Michael J. Spencer371716c2010-12-03 02:22:34 +0000114 temp_store = *i;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000115 path::remove_filename(temp_store);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000116
Michael J. Spencer371716c2010-12-03 02:22:34 +0000117 temp_store = *i;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000118 path::replace_extension(temp_store, "ext");
Michael J. Spencer1d389622010-12-01 06:03:33 +0000119 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
Michael J. Spencer50291592010-12-07 17:04:04 +0000120 stem = path::stem(filename);
121 ext = path::extension(filename);
Michael J. Spencer1d389622010-12-01 06:03:33 +0000122 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
123
Michael J. Spencer936671b2010-12-07 03:57:37 +0000124 path::native(*i, temp_store);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000125 }
Michael J. Spencer25585162011-01-05 16:39:05 +0000126}
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000127
Michael J. Spencer25585162011-01-05 16:39:05 +0000128class FileSystemTest : public testing::Test {
129protected:
130 /// Unique temporary directory in which all created filesystem entities must
131 /// be placed. It is recursively removed at the end of each test.
132 SmallString<128> TestDirectory;
133
134 virtual void SetUp() {
135 /*int fd;
136 ASSERT_NO_ERROR(
137 fs::unique_file("%%-%%-%%-%%/test-directory.anchor", fd, TestDirectory));
138 // We don't care about this specific file.
139 ::close(fd);*/
140 }
141
142 virtual void TearDown() {
143 /*uint32_t removed;
144 ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));*/
145 }
146};
147
148TEST_F(FileSystemTest, TempFiles) {
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000149 // Create a temp file.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000150 int FileDescriptor;
151 SmallString<64> TempPath;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000152 ASSERT_NO_ERROR(
153 fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000154
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000155 // Make sure it exists.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000156 bool TempFileExists;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000157 ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000158 EXPECT_TRUE(TempFileExists);
159
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000160 // Create another temp tile.
161 int FD2;
162 SmallString<64> TempPath2;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000163 ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000164 ASSERT_NE(TempPath.str(), TempPath2.str());
165
166 // Try to copy the first to the second.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000167 EXPECT_EQ(
168 fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000169
170 ::close(FD2);
171 // Try again with the proper options.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000172 ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
173 fs::copy_option::overwrite_if_exists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000174 // Remove Temp2.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000175 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000176 EXPECT_TRUE(TempFileExists);
177
178 // Make sure Temp2 doesn't exist.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000179 ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000180 EXPECT_FALSE(TempFileExists);
181
182 // Create a hard link to Temp1.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000183 ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000184 bool equal;
Michael J. Spencerba64b972011-01-04 17:00:18 +0000185 ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000186 EXPECT_TRUE(equal);
187
188 // Remove Temp1.
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000189 ::close(FileDescriptor);
Michael J. Spencerba64b972011-01-04 17:00:18 +0000190 ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
Michael J. Spencer106aa732010-12-03 17:53:43 +0000191 EXPECT_TRUE(TempFileExists);
Michael J. Spencer3cb84ef2010-12-03 01:21:28 +0000192
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000193 // Remove the hard link.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000194 ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
Michael J. Spencer9ad82212010-12-04 03:18:42 +0000195 EXPECT_TRUE(TempFileExists);
196
197 // Make sure Temp1 doesn't exist.
Michael J. Spencerba64b972011-01-04 17:00:18 +0000198 ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
Benjamin Kramer6d6d16a2010-12-03 12:33:32 +0000199 EXPECT_FALSE(TempFileExists);
Michael J. Spencer25585162011-01-05 16:39:05 +0000200}
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000201
Michael J. Spencer25585162011-01-05 16:39:05 +0000202TEST_F(FileSystemTest, DirectoryIteration) {
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000203 error_code ec;
204 for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) {
205 if (ec) {
206 errs() << ec.message() << '\n';
207 errs().flush();
208 report_fatal_error("Directory iteration failed!");
209 }
210 }
Michael J. Spencerf2ca4cb2010-11-24 19:20:05 +0000211}
212
213} // anonymous namespace