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"
13 #include "gtest/gtest.h"
16 using namespace llvm::sys;
21 SmallVector<StringRef, 40> paths;
24 paths.push_back("..");
25 paths.push_back("foo");
27 paths.push_back("/foo");
28 paths.push_back("foo/");
29 paths.push_back("/foo/");
30 paths.push_back("foo/bar");
31 paths.push_back("/foo/bar");
32 paths.push_back("//net");
33 paths.push_back("//net/foo");
34 paths.push_back("///foo///");
35 paths.push_back("///foo///bar");
36 paths.push_back("/.");
37 paths.push_back("./");
38 paths.push_back("/..");
39 paths.push_back("../");
40 paths.push_back("foo/.");
41 paths.push_back("foo/..");
42 paths.push_back("foo/./");
43 paths.push_back("foo/./bar");
44 paths.push_back("foo/..");
45 paths.push_back("foo/../");
46 paths.push_back("foo/../bar");
47 paths.push_back("c:");
48 paths.push_back("c:/");
49 paths.push_back("c:foo");
50 paths.push_back("c:/foo");
51 paths.push_back("c:foo/");
52 paths.push_back("c:/foo/");
53 paths.push_back("c:/foo/bar");
54 paths.push_back("prn:");
55 paths.push_back("c:\\");
56 paths.push_back("c:foo");
57 paths.push_back("c:\\foo");
58 paths.push_back("c:foo\\");
59 paths.push_back("c:\\foo\\");
60 paths.push_back("c:\\foo/");
61 paths.push_back("c:/foo\\bar");
63 for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(),
67 for (sys::path::const_iterator ci = sys::path::begin(*i),
68 ce = sys::path::end(*i);
71 ASSERT_FALSE(ci->empty());
74 #if 0 // Valgrind is whining about this.
75 outs() << " Reverse Iteration: [";
76 for (sys::path::reverse_iterator ci = sys::path::rbegin(*i),
77 ce = sys::path::rend(*i);
87 ASSERT_FALSE(path::has_root_path(*i, bres));
88 ASSERT_FALSE(path::root_path(*i, sfres));
89 ASSERT_FALSE(path::has_root_name(*i, bres));
90 ASSERT_FALSE(path::root_name(*i, sfres));
91 ASSERT_FALSE(path::has_root_directory(*i, bres));
92 ASSERT_FALSE(path::root_directory(*i, sfres));
93 ASSERT_FALSE(path::has_parent_path(*i, bres));
94 ASSERT_FALSE(path::parent_path(*i, sfres));
95 ASSERT_FALSE(path::has_filename(*i, bres));
96 ASSERT_FALSE(path::filename(*i, sfres));
97 ASSERT_FALSE(path::has_stem(*i, bres));
98 ASSERT_FALSE(path::stem(*i, sfres));
99 ASSERT_FALSE(path::has_extension(*i, bres));
100 ASSERT_FALSE(path::extension(*i, sfres));
101 ASSERT_FALSE(path::is_absolute(*i, bres));
102 ASSERT_FALSE(path::is_relative(*i, bres));
104 SmallString<16> temp_store;
106 ASSERT_FALSE(path::make_absolute(temp_store));
108 ASSERT_FALSE(path::remove_filename(temp_store));
111 ASSERT_FALSE(path::replace_extension(temp_store, "ext"));
112 StringRef filename(temp_store.begin(), temp_store.size()), stem, ext;
113 ASSERT_FALSE(path::stem(filename, stem));
114 ASSERT_FALSE(path::extension(filename, ext));
115 EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str());
117 ASSERT_FALSE(path::native(*i, temp_store));
122 // Create a temp file.
124 SmallString<64> TempPath;
125 ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FileDescriptor, TempPath));
127 // Make sure it exists.
129 ASSERT_FALSE(sys::fs::exists(Twine(TempPath), TempFileExists));
130 EXPECT_TRUE(TempFileExists);
132 // Create another temp tile.
134 SmallString<64> TempPath2;
135 ASSERT_FALSE(fs::unique_file("%%-%%-%%-%%.temp", FD2, TempPath2));
136 ASSERT_NE(TempPath.str(), TempPath2.str());
138 // Try to copy the first to the second.
139 EXPECT_EQ(fs::copy_file(Twine(TempPath), Twine(TempPath2)), errc::file_exists);
142 // Try again with the proper options.
143 ASSERT_FALSE(fs::copy_file(Twine(TempPath), Twine(TempPath2),
144 fs::copy_option::overwrite_if_exists));
146 ASSERT_FALSE(fs::remove(Twine(TempPath2), TempFileExists));
147 EXPECT_TRUE(TempFileExists);
149 // Make sure Temp2 doesn't exist.
150 ASSERT_FALSE(fs::exists(Twine(TempPath2), TempFileExists));
151 EXPECT_FALSE(TempFileExists);
153 // Create a hard link to Temp1.
154 ASSERT_FALSE(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
156 ASSERT_FALSE(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
160 ::close(FileDescriptor);
161 ASSERT_FALSE(fs::remove(Twine(TempPath), TempFileExists));
162 EXPECT_TRUE(TempFileExists);
164 // Remove the hard link.
165 ASSERT_FALSE(fs::remove(Twine(TempPath2), TempFileExists));
166 EXPECT_TRUE(TempFileExists);
168 // Make sure Temp1 doesn't exist.
169 ASSERT_FALSE(fs::exists(Twine(TempPath), TempFileExists));
170 EXPECT_FALSE(TempFileExists);
173 } // anonymous namespace