1 //===-- FileSpecTest.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "gtest/gtest.h"
10 
11 #include "lldb/Utility/FileSpec.h"
12 
13 using namespace lldb_private;
14 
15 TEST(FileSpecTest, FileAndDirectoryComponents) {
16   FileSpec fs_posix("/foo/bar", FileSpec::Style::posix);
17   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
18   EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
19   EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
20 
21   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
22   EXPECT_STREQ("F:\\bar", fs_windows.GetCString());
23   // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString()); // It returns
24   // "F:/"
25   EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());
26 
27   FileSpec fs_posix_root("/", FileSpec::Style::posix);
28   EXPECT_STREQ("/", fs_posix_root.GetCString());
29   EXPECT_EQ(nullptr, fs_posix_root.GetDirectory().GetCString());
30   EXPECT_STREQ("/", fs_posix_root.GetFilename().GetCString());
31 
32   FileSpec fs_net_drive("//net", FileSpec::Style::posix);
33   EXPECT_STREQ("//net", fs_net_drive.GetCString());
34   EXPECT_EQ(nullptr, fs_net_drive.GetDirectory().GetCString());
35   EXPECT_STREQ("//net", fs_net_drive.GetFilename().GetCString());
36 
37   FileSpec fs_net_root("//net/", FileSpec::Style::posix);
38   EXPECT_STREQ("//net/", fs_net_root.GetCString());
39   EXPECT_STREQ("//net", fs_net_root.GetDirectory().GetCString());
40   EXPECT_STREQ("/", fs_net_root.GetFilename().GetCString());
41 
42   FileSpec fs_windows_drive("F:", FileSpec::Style::windows);
43   EXPECT_STREQ("F:", fs_windows_drive.GetCString());
44   EXPECT_EQ(nullptr, fs_windows_drive.GetDirectory().GetCString());
45   EXPECT_STREQ("F:", fs_windows_drive.GetFilename().GetCString());
46 
47   FileSpec fs_windows_root("F:\\", FileSpec::Style::windows);
48   EXPECT_STREQ("F:\\", fs_windows_root.GetCString());
49   EXPECT_STREQ("F:", fs_windows_root.GetDirectory().GetCString());
50   // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It
51   // returns "/"
52 
53   FileSpec fs_posix_long("/foo/bar/baz", FileSpec::Style::posix);
54   EXPECT_STREQ("/foo/bar/baz", fs_posix_long.GetCString());
55   EXPECT_STREQ("/foo/bar", fs_posix_long.GetDirectory().GetCString());
56   EXPECT_STREQ("baz", fs_posix_long.GetFilename().GetCString());
57 
58   FileSpec fs_windows_long("F:\\bar\\baz", FileSpec::Style::windows);
59   EXPECT_STREQ("F:\\bar\\baz", fs_windows_long.GetCString());
60   // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It
61   // returns "F:/bar"
62   EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());
63 
64   FileSpec fs_posix_trailing_slash("/foo/bar/", FileSpec::Style::posix);
65   EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetCString());
66   EXPECT_STREQ("/foo", fs_posix_trailing_slash.GetDirectory().GetCString());
67   EXPECT_STREQ("bar", fs_posix_trailing_slash.GetFilename().GetCString());
68 
69   FileSpec fs_windows_trailing_slash("F:\\bar\\", FileSpec::Style::windows);
70   EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetCString());
71   EXPECT_STREQ("bar", fs_windows_trailing_slash.GetFilename().GetCString());
72 }
73 
74 TEST(FileSpecTest, AppendPathComponent) {
75   FileSpec fs_posix("/foo", FileSpec::Style::posix);
76   fs_posix.AppendPathComponent("bar");
77   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
78   EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
79   EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
80 
81   FileSpec fs_posix_2("/foo", FileSpec::Style::posix);
82   fs_posix_2.AppendPathComponent("//bar/baz");
83   EXPECT_STREQ("/foo/bar/baz", fs_posix_2.GetCString());
84   EXPECT_STREQ("/foo/bar", fs_posix_2.GetDirectory().GetCString());
85   EXPECT_STREQ("baz", fs_posix_2.GetFilename().GetCString());
86 
87   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
88   fs_windows.AppendPathComponent("baz");
89   EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
90   // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It
91   // returns "F:/bar"
92   EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString());
93 
94   FileSpec fs_posix_root("/", FileSpec::Style::posix);
95   fs_posix_root.AppendPathComponent("bar");
96   EXPECT_STREQ("/bar", fs_posix_root.GetCString());
97   EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
98   EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());
99 
100   FileSpec fs_windows_root("F:\\", FileSpec::Style::windows);
101   fs_windows_root.AppendPathComponent("bar");
102   EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
103   // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It
104   // returns "F:/"
105   EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
106 }
107 
108 TEST(FileSpecTest, CopyByAppendingPathComponent) {
109   FileSpec fs = FileSpec("/foo", FileSpec::Style::posix)
110                     .CopyByAppendingPathComponent("bar");
111   EXPECT_STREQ("/foo/bar", fs.GetCString());
112   EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
113   EXPECT_STREQ("bar", fs.GetFilename().GetCString());
114 }
115 
116 TEST(FileSpecTest, PrependPathComponent) {
117   FileSpec fs_posix("foo", FileSpec::Style::posix);
118   fs_posix.PrependPathComponent("/bar");
119   EXPECT_STREQ("/bar/foo", fs_posix.GetCString());
120 
121   FileSpec fs_posix_2("foo/bar", FileSpec::Style::posix);
122   fs_posix_2.PrependPathComponent("/baz");
123   EXPECT_STREQ("/baz/foo/bar", fs_posix_2.GetCString());
124 
125   FileSpec fs_windows("baz", FileSpec::Style::windows);
126   fs_windows.PrependPathComponent("F:\\bar");
127   EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString());
128 
129   FileSpec fs_posix_root("bar", FileSpec::Style::posix);
130   fs_posix_root.PrependPathComponent("/");
131   EXPECT_STREQ("/bar", fs_posix_root.GetCString());
132 
133   FileSpec fs_windows_root("bar", FileSpec::Style::windows);
134   fs_windows_root.PrependPathComponent("F:\\");
135   EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString());
136 }
137 
138 TEST(FileSpecTest, EqualSeparator) {
139   FileSpec backward("C:\\foo\\bar", FileSpec::Style::windows);
140   FileSpec forward("C:/foo/bar", FileSpec::Style::windows);
141   EXPECT_EQ(forward, backward);
142 }
143 
144 TEST(FileSpecTest, EqualDotsWindows) {
145   std::pair<const char *, const char *> tests[] = {
146       {R"(C:\foo\bar\baz)", R"(C:\foo\foo\..\bar\baz)"},
147       {R"(C:\bar\baz)", R"(C:\foo\..\bar\baz)"},
148       {R"(C:\bar\baz)", R"(C:/foo/../bar/baz)"},
149       {R"(C:/bar/baz)", R"(C:\foo\..\bar\baz)"},
150       {R"(C:\bar)", R"(C:\foo\..\bar)"},
151       {R"(C:\foo\bar)", R"(C:\foo\.\bar)"},
152       {R"(C:\foo\bar)", R"(C:\foo\bar\.)"},
153   };
154 
155   for (const auto &test : tests) {
156     FileSpec one(test.first, FileSpec::Style::windows);
157     FileSpec two(test.second, FileSpec::Style::windows);
158     EXPECT_EQ(one, two);
159   }
160 }
161 
162 TEST(FileSpecTest, EqualDotsPosix) {
163   std::pair<const char *, const char *> tests[] = {
164       {R"(/foo/bar/baz)", R"(/foo/foo/../bar/baz)"},
165       {R"(/bar/baz)", R"(/foo/../bar/baz)"},
166       {R"(/bar)", R"(/foo/../bar)"},
167       {R"(/foo/bar)", R"(/foo/./bar)"},
168       {R"(/foo/bar)", R"(/foo/bar/.)"},
169   };
170 
171   for (const auto &test : tests) {
172     FileSpec one(test.first, FileSpec::Style::posix);
173     FileSpec two(test.second, FileSpec::Style::posix);
174     EXPECT_EQ(one, two);
175   }
176 }
177 
178 TEST(FileSpecTest, EqualDotsPosixRoot) {
179   std::pair<const char *, const char *> tests[] = {
180       {R"(/)", R"(/..)"},
181       {R"(/)", R"(/.)"},
182       {R"(/)", R"(/foo/..)"},
183   };
184 
185   for (const auto &test : tests) {
186     FileSpec one(test.first, FileSpec::Style::posix);
187     FileSpec two(test.second, FileSpec::Style::posix);
188     EXPECT_EQ(one, two);
189   }
190 }
191 
192 TEST(FileSpecTest, GetNormalizedPath) {
193   std::pair<const char *, const char *> posix_tests[] = {
194       {"/foo/.././bar", "/bar"},
195       {"/foo/./../bar", "/bar"},
196       {"/foo/../bar", "/bar"},
197       {"/foo/./bar", "/foo/bar"},
198       {"/foo/..", "/"},
199       {"/foo/.", "/foo"},
200       {"/foo//bar", "/foo/bar"},
201       {"/foo//bar/baz", "/foo/bar/baz"},
202       {"/foo//bar/./baz", "/foo/bar/baz"},
203       {"/./foo", "/foo"},
204       {"/", "/"},
205       {"//", "/"},
206       {"//net", "//net"},
207       {"/..", "/"},
208       {"/.", "/"},
209       {"..", ".."},
210       {".", "."},
211       {"../..", "../.."},
212       {"foo/..", "."},
213       {"foo/../bar", "bar"},
214       {"../foo/..", ".."},
215       {"./foo", "foo"},
216       {"././foo", "foo"},
217       {"../foo", "../foo"},
218       {"../../foo", "../../foo"},
219   };
220   for (auto test : posix_tests) {
221     SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
222     EXPECT_EQ(test.second,
223               FileSpec(test.first, FileSpec::Style::posix).GetPath());
224   }
225 
226   std::pair<const char *, const char *> windows_tests[] = {
227       {R"(c:\bar\..\bar)", R"(c:\bar)"},
228       {R"(c:\bar\.\bar)", R"(c:\bar\bar)"},
229       {R"(c:\bar\..)", R"(c:\)"},
230       {R"(c:\bar\.)", R"(c:\bar)"},
231       {R"(c:\.\bar)", R"(c:\bar)"},
232       {R"(\)", R"(\)"},
233       {R"(\\)", R"(\)"},
234       {R"(\\net)", R"(\\net)"},
235       {R"(c:\..)", R"(c:\)"},
236       {R"(c:\.)", R"(c:\)"},
237       // TODO: fix llvm::sys::path::remove_dots() to return "\" below.
238       {R"(\..)", R"(\..)"},
239       //      {R"(c:..)", R"(c:..)"},
240       {R"(..)", R"(..)"},
241       {R"(.)", R"(.)"},
242       // TODO: fix llvm::sys::path::remove_dots() to return "c:\" below.
243       {R"(c:..\..)", R"(c:\..\..)"},
244       {R"(..\..)", R"(..\..)"},
245       {R"(foo\..)", R"(.)"},
246       {R"(foo\..\bar)", R"(bar)"},
247       {R"(..\foo\..)", R"(..)"},
248       {R"(.\foo)", R"(foo)"},
249       {R"(.\.\foo)", R"(foo)"},
250       {R"(..\foo)", R"(..\foo)"},
251       {R"(..\..\foo)", R"(..\..\foo)"},
252   };
253   for (auto test : windows_tests) {
254     EXPECT_EQ(test.second,
255               FileSpec(test.first, FileSpec::Style::windows).GetPath())
256         << "Original path: " << test.first;
257   }
258 }
259 
260 TEST(FileSpecTest, FormatFileSpec) {
261   auto win = FileSpec::Style::windows;
262 
263   FileSpec F;
264   EXPECT_EQ("(empty)", llvm::formatv("{0}", F).str());
265   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
266   EXPECT_EQ("(empty)", llvm::formatv("{0:F}", F).str());
267 
268   F = FileSpec("C:\\foo\\bar.txt", win);
269   EXPECT_EQ("C:\\foo\\bar.txt", llvm::formatv("{0}", F).str());
270   EXPECT_EQ("C:\\foo\\", llvm::formatv("{0:D}", F).str());
271   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
272 
273   F = FileSpec("foo\\bar.txt", win);
274   EXPECT_EQ("foo\\bar.txt", llvm::formatv("{0}", F).str());
275   EXPECT_EQ("foo\\", llvm::formatv("{0:D}", F).str());
276   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
277 
278   F = FileSpec("foo", win);
279   EXPECT_EQ("foo", llvm::formatv("{0}", F).str());
280   EXPECT_EQ("foo", llvm::formatv("{0:F}", F).str());
281   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
282 }
283 
284 TEST(FileSpecTest, IsRelative) {
285   llvm::StringRef not_relative[] = {
286     "/",
287     "/a",
288     "/a/",
289     "/a/b",
290     "/a/b/",
291     "//",
292     "//a/",
293     "//a/b",
294     "//a/b/",
295     "~",
296     "~/",
297     "~/a",
298     "~/a/",
299     "~/a/b"
300     "~/a/b/",
301     "/foo/.",
302     "/foo/..",
303     "/foo/../",
304     "/foo/../.",
305   };
306   for (const auto &path: not_relative) {
307     FileSpec spec(path, FileSpec::Style::posix);
308     EXPECT_FALSE(spec.IsRelative());
309   }
310   llvm::StringRef is_relative[] = {
311     ".",
312     "./",
313     ".///",
314     "a",
315     "./a",
316     "./a/",
317     "./a/",
318     "./a/b",
319     "./a/b/",
320     "../foo",
321     "foo/bar.c",
322     "./foo/bar.c"
323   };
324   for (const auto &path: is_relative) {
325     FileSpec spec(path, FileSpec::Style::posix);
326     EXPECT_TRUE(spec.IsRelative());
327   }
328 }
329 
330 TEST(FileSpecTest, RemoveLastPathComponent) {
331   FileSpec fs_posix("/foo/bar/baz", FileSpec::Style::posix);
332   EXPECT_STREQ("/foo/bar/baz", fs_posix.GetCString());
333   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
334   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
335   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
336   EXPECT_STREQ("/foo", fs_posix.GetCString());
337   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
338   EXPECT_STREQ("/", fs_posix.GetCString());
339   EXPECT_FALSE(fs_posix.RemoveLastPathComponent());
340   EXPECT_STREQ("/", fs_posix.GetCString());
341 
342   FileSpec fs_posix_relative("./foo/bar/baz", FileSpec::Style::posix);
343   EXPECT_STREQ("foo/bar/baz", fs_posix_relative.GetCString());
344   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
345   EXPECT_STREQ("foo/bar", fs_posix_relative.GetCString());
346   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
347   EXPECT_STREQ("foo", fs_posix_relative.GetCString());
348   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
349   EXPECT_STREQ("foo", fs_posix_relative.GetCString());
350 
351   FileSpec fs_posix_relative2("./", FileSpec::Style::posix);
352   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
353   EXPECT_FALSE(fs_posix_relative2.RemoveLastPathComponent());
354   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
355   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
356   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
357 
358   FileSpec fs_windows("C:\\foo\\bar\\baz", FileSpec::Style::windows);
359   EXPECT_STREQ("C:\\foo\\bar\\baz", fs_windows.GetCString());
360   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
361   EXPECT_STREQ("C:\\foo\\bar", fs_windows.GetCString());
362   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
363   EXPECT_STREQ("C:\\foo", fs_windows.GetCString());
364   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
365   EXPECT_STREQ("C:\\", fs_windows.GetCString());
366   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
367   EXPECT_STREQ("C:", fs_windows.GetCString());
368   EXPECT_FALSE(fs_windows.RemoveLastPathComponent());
369   EXPECT_STREQ("C:", fs_windows.GetCString());
370 }
371