UnitTests/Path: More ASSERT_NO_ERROR cleanup.
[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 #include "llvm/Support/raw_ostream.h"
14
15 #include "gtest/gtest.h"
16
17 using namespace llvm;
18 using namespace llvm::sys;
19
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()); \
28   } else {}
29
30 namespace {
31
32 TEST(Support, Path) {
33   SmallVector<StringRef, 40> paths;
34   paths.push_back("");
35   paths.push_back(".");
36   paths.push_back("..");
37   paths.push_back("foo");
38   paths.push_back("/");
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");
74
75   for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
76                                                   e = paths.end();
77                                                   i != e;
78                                                   ++i) {
79     for (sys::path::const_iterator ci = sys::path::begin(*i),
80                                    ce = sys::path::end(*i);
81                                    ci != ce;
82                                    ++ci) {
83       ASSERT_FALSE(ci->empty());
84     }
85
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);
90                                      ci != ce;
91                                      ++ci) {
92       outs() << *ci << ',';
93     }
94     outs() << "]\n";
95 #endif
96
97     path::has_root_path(*i);
98     path::root_path(*i);
99     path::has_root_name(*i);
100     path::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);
106     path::filename(*i);
107     path::has_stem(*i);
108     path::stem(*i);
109     path::has_extension(*i);
110     path::extension(*i);
111     path::is_absolute(*i);
112     path::is_relative(*i);
113
114     SmallString<128> temp_store;
115     temp_store = *i;
116     ASSERT_NO_ERROR(fs::make_absolute(temp_store));
117     temp_store = *i;
118     path::remove_filename(temp_store);
119
120     temp_store = *i;
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());
126
127     path::native(*i, temp_store);
128   }
129 }
130
131 class FileSystemTest : public testing::Test {
132 protected:
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;
136
137   virtual void SetUp() {
138     int fd;
139     ASSERT_NO_ERROR(
140       fs::unique_file("file-system-test-%%-%%-%%-%%/test-directory.anchor", fd,
141                       TestDirectory));
142     // We don't care about this specific file.
143     ::close(fd);
144     TestDirectory = path::parent_path(TestDirectory);
145     errs() << "Test Directory: " << TestDirectory << '\n';
146     errs().flush();
147   }
148
149   virtual void TearDown() {
150     uint32_t removed;
151     ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), removed));
152   }
153 };
154
155 TEST_F(FileSystemTest, TempFiles) {
156   // Create a temp file.
157   int FileDescriptor;
158   SmallString<64> TempPath;
159   ASSERT_NO_ERROR(
160     fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
161
162   // Make sure it exists.
163   bool TempFileExists;
164   ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists));
165   EXPECT_TRUE(TempFileExists);
166
167   // Create another temp tile.
168   int FD2;
169   SmallString<64> TempPath2;
170   ASSERT_NO_ERROR(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
171   ASSERT_NE(TempPath.str(), TempPath2.str());
172
173   // Try to copy the first to the second.
174   EXPECT_EQ(
175     fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
176
177   ::close(FD2);
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));
181   // Remove Temp2.
182   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
183   EXPECT_TRUE(TempFileExists);
184
185   // Make sure Temp2 doesn't exist.
186   ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists));
187   EXPECT_FALSE(TempFileExists);
188
189   // Create a hard link to Temp1.
190   ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
191   bool equal;
192   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
193   EXPECT_TRUE(equal);
194
195   // Remove Temp1.
196   ::close(FileDescriptor);
197   ASSERT_NO_ERROR(fs::remove(Twine(TempPath), TempFileExists));
198   EXPECT_TRUE(TempFileExists);
199
200   // Remove the hard link.
201   ASSERT_NO_ERROR(fs::remove(Twine(TempPath2), TempFileExists));
202   EXPECT_TRUE(TempFileExists);
203
204   // Make sure Temp1 doesn't exist.
205   ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists));
206   EXPECT_FALSE(TempFileExists);
207 }
208
209 TEST_F(FileSystemTest, DirectoryIteration) {
210   error_code ec;
211   for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec))
212     ASSERT_NO_ERROR(ec);
213 }
214   }
215 }
216
217 } // anonymous namespace