UnitTests/Path: Fix typo, add error number, and enable the directory cleanup code.
[oota-llvm.git] / unittests / Support / Path.cpp
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 number: " + Twine(ec.value()) + "\n" + \
24                          "error message: " + \
25                      ec.message()).toNullTerminatedStringRef(Message).data()); \
26   } else {}
27
28 namespace {
29
30 TEST(Support, Path) {
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) {
77     for (sys::path::const_iterator ci = sys::path::begin(*i),
78                                    ce = sys::path::end(*i);
79                                    ci != ce;
80                                    ++ci) {
81       ASSERT_FALSE(ci->empty());
82     }
83
84 #if 0 // Valgrind is whining about this.
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";
93 #endif
94
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);
111
112     SmallString<128> temp_store;
113     temp_store = *i;
114     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
115     temp_store = *i;
116     path::remove_filename(temp_store);
117
118     temp_store = *i;
119     path::replace_extension(temp_store, "ext");
120     StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
121     stem = path::stem(filename);
122     ext  = path::extension(filename);
123     EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
124
125     path::native(*i, temp_store);
126   }
127 }
128
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() {
136     int fd;
137     ASSERT_NO_ERROR(
138       fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
139                       TestDirectory));
140     // We don't care about this specific file.
141     ::close(fd);
142     TestDirectory = path::parent_path(TestDirectory);
143     errs() << "Test Directory: " << TestDirectory << '\n';
144     errs().flush();
145   }
146
147   virtual void TearDown() {
148     uint32_t removed;
149     ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
150   }
151 };
152
153 TEST_F(FileSystemTest, TempFiles) {
154   // Create a temp file.
155   int FileDescriptor;
156   SmallString<64> TempPath;
157   ASSERT_NO_ERROR(
158     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
159
160   // Make sure it exists.
161   bool TempFileExists;
162   ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
163   EXPECT_TRUE(TempFileExists);
164
165   // Create another temp tile.
166   int FD2;
167   SmallString<64> TempPath2;
168   ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
169   ASSERT_NE(TempPath.str(), TempPath2.str());
170
171   // Try to copy the first to the second.
172   EXPECT_EQ(
173     fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
174
175   ::close(FD2);
176   // Try again with the proper options.
177   ASSERT_NO_ERROR(fs::copy_file(Twine(TempPath), Twine(TempPath2),
178                                 fs::copy_option::overwrite_if_exists));
179   // Remove Temp2.
180   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
181   EXPECT_TRUE(TempFileExists);
182
183   // Make sure Temp2 doesn't exist.
184   ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
185   EXPECT_FALSE(TempFileExists);
186
187   // Create a hard link to Temp1.
188   ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
189   bool equal;
190   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
191   EXPECT_TRUE(equal);
192
193   // Remove Temp1.
194   ::close(FileDescriptor);
195   ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
196   EXPECT_TRUE(TempFileExists);
197
198   // Remove the hard link.
199   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
200   EXPECT_TRUE(TempFileExists);
201
202   // Make sure Temp1 doesn't exist.
203   ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
204   EXPECT_FALSE(TempFileExists);
205 }
206
207 TEST_F(FileSystemTest, DirectoryIteration) {
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   }
216 }
217
218 } // anonymous namespace