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, GuessPathStyle) {
193   EXPECT_EQ(FileSpec::Style::posix, FileSpec::GuessPathStyle("/foo/bar.txt"));
194   EXPECT_EQ(FileSpec::Style::posix, FileSpec::GuessPathStyle("//net/bar.txt"));
195   EXPECT_EQ(FileSpec::Style::windows,
196             FileSpec::GuessPathStyle(R"(C:\foo.txt)"));
197   EXPECT_EQ(FileSpec::Style::windows,
198             FileSpec::GuessPathStyle(R"(\\net\foo.txt)"));
199   EXPECT_EQ(llvm::None, FileSpec::GuessPathStyle("foo.txt"));
200   EXPECT_EQ(llvm::None, FileSpec::GuessPathStyle("foo/bar.txt"));
201 }
202 
203 TEST(FileSpecTest, GetNormalizedPath) {
204   std::pair<const char *, const char *> posix_tests[] = {
205       {"/foo/.././bar", "/bar"},
206       {"/foo/./../bar", "/bar"},
207       {"/foo/../bar", "/bar"},
208       {"/foo/./bar", "/foo/bar"},
209       {"/foo/..", "/"},
210       {"/foo/.", "/foo"},
211       {"/foo//bar", "/foo/bar"},
212       {"/foo//bar/baz", "/foo/bar/baz"},
213       {"/foo//bar/./baz", "/foo/bar/baz"},
214       {"/./foo", "/foo"},
215       {"/", "/"},
216       {"//", "/"},
217       {"//net", "//net"},
218       {"/..", "/"},
219       {"/.", "/"},
220       {"..", ".."},
221       {".", "."},
222       {"../..", "../.."},
223       {"foo/..", "."},
224       {"foo/../bar", "bar"},
225       {"../foo/..", ".."},
226       {"./foo", "foo"},
227       {"././foo", "foo"},
228       {"../foo", "../foo"},
229       {"../../foo", "../../foo"},
230   };
231   for (auto test : posix_tests) {
232     SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
233     EXPECT_EQ(test.second,
234               FileSpec(test.first, FileSpec::Style::posix).GetPath());
235   }
236 
237   std::pair<const char *, const char *> windows_tests[] = {
238       {R"(c:\bar\..\bar)", R"(c:\bar)"},
239       {R"(c:\bar\.\bar)", R"(c:\bar\bar)"},
240       {R"(c:\bar\..)", R"(c:\)"},
241       {R"(c:\bar\.)", R"(c:\bar)"},
242       {R"(c:\.\bar)", R"(c:\bar)"},
243       {R"(\)", R"(\)"},
244       {R"(\\)", R"(\)"},
245       {R"(\\net)", R"(\\net)"},
246       {R"(c:\..)", R"(c:\)"},
247       {R"(c:\.)", R"(c:\)"},
248       // TODO: fix llvm::sys::path::remove_dots() to return "\" below.
249       {R"(\..)", R"(\..)"},
250       //      {R"(c:..)", R"(c:..)"},
251       {R"(..)", R"(..)"},
252       {R"(.)", R"(.)"},
253       // TODO: fix llvm::sys::path::remove_dots() to return "c:\" below.
254       {R"(c:..\..)", R"(c:\..\..)"},
255       {R"(..\..)", R"(..\..)"},
256       {R"(foo\..)", R"(.)"},
257       {R"(foo\..\bar)", R"(bar)"},
258       {R"(..\foo\..)", R"(..)"},
259       {R"(.\foo)", R"(foo)"},
260       {R"(.\.\foo)", R"(foo)"},
261       {R"(..\foo)", R"(..\foo)"},
262       {R"(..\..\foo)", R"(..\..\foo)"},
263   };
264   for (auto test : windows_tests) {
265     EXPECT_EQ(test.second,
266               FileSpec(test.first, FileSpec::Style::windows).GetPath())
267         << "Original path: " << test.first;
268   }
269 }
270 
271 TEST(FileSpecTest, FormatFileSpec) {
272   auto win = FileSpec::Style::windows;
273 
274   FileSpec F;
275   EXPECT_EQ("(empty)", llvm::formatv("{0}", F).str());
276   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
277   EXPECT_EQ("(empty)", llvm::formatv("{0:F}", F).str());
278 
279   F = FileSpec("C:\\foo\\bar.txt", win);
280   EXPECT_EQ("C:\\foo\\bar.txt", llvm::formatv("{0}", F).str());
281   EXPECT_EQ("C:\\foo\\", llvm::formatv("{0:D}", F).str());
282   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
283 
284   F = FileSpec("foo\\bar.txt", win);
285   EXPECT_EQ("foo\\bar.txt", llvm::formatv("{0}", F).str());
286   EXPECT_EQ("foo\\", llvm::formatv("{0:D}", F).str());
287   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
288 
289   F = FileSpec("foo", win);
290   EXPECT_EQ("foo", llvm::formatv("{0}", F).str());
291   EXPECT_EQ("foo", llvm::formatv("{0:F}", F).str());
292   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
293 }
294 
295 TEST(FileSpecTest, IsRelative) {
296   llvm::StringRef not_relative[] = {
297     "/",
298     "/a",
299     "/a/",
300     "/a/b",
301     "/a/b/",
302     "//",
303     "//a/",
304     "//a/b",
305     "//a/b/",
306     "~",
307     "~/",
308     "~/a",
309     "~/a/",
310     "~/a/b"
311     "~/a/b/",
312     "/foo/.",
313     "/foo/..",
314     "/foo/../",
315     "/foo/../.",
316   };
317   for (const auto &path: not_relative) {
318     FileSpec spec(path, FileSpec::Style::posix);
319     EXPECT_FALSE(spec.IsRelative());
320   }
321   llvm::StringRef is_relative[] = {
322     ".",
323     "./",
324     ".///",
325     "a",
326     "./a",
327     "./a/",
328     "./a/",
329     "./a/b",
330     "./a/b/",
331     "../foo",
332     "foo/bar.c",
333     "./foo/bar.c"
334   };
335   for (const auto &path: is_relative) {
336     FileSpec spec(path, FileSpec::Style::posix);
337     EXPECT_TRUE(spec.IsRelative());
338   }
339 }
340 
341 TEST(FileSpecTest, RemoveLastPathComponent) {
342   FileSpec fs_posix("/foo/bar/baz", FileSpec::Style::posix);
343   EXPECT_STREQ("/foo/bar/baz", fs_posix.GetCString());
344   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
345   EXPECT_STREQ("/foo/bar", fs_posix.GetCString());
346   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
347   EXPECT_STREQ("/foo", fs_posix.GetCString());
348   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
349   EXPECT_STREQ("/", fs_posix.GetCString());
350   EXPECT_FALSE(fs_posix.RemoveLastPathComponent());
351   EXPECT_STREQ("/", fs_posix.GetCString());
352 
353   FileSpec fs_posix_relative("./foo/bar/baz", FileSpec::Style::posix);
354   EXPECT_STREQ("foo/bar/baz", fs_posix_relative.GetCString());
355   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
356   EXPECT_STREQ("foo/bar", fs_posix_relative.GetCString());
357   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
358   EXPECT_STREQ("foo", fs_posix_relative.GetCString());
359   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
360   EXPECT_STREQ("foo", fs_posix_relative.GetCString());
361 
362   FileSpec fs_posix_relative2("./", FileSpec::Style::posix);
363   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
364   EXPECT_FALSE(fs_posix_relative2.RemoveLastPathComponent());
365   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
366   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
367   EXPECT_STREQ(".", fs_posix_relative2.GetCString());
368 
369   FileSpec fs_windows("C:\\foo\\bar\\baz", FileSpec::Style::windows);
370   EXPECT_STREQ("C:\\foo\\bar\\baz", fs_windows.GetCString());
371   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
372   EXPECT_STREQ("C:\\foo\\bar", fs_windows.GetCString());
373   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
374   EXPECT_STREQ("C:\\foo", fs_windows.GetCString());
375   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
376   EXPECT_STREQ("C:\\", fs_windows.GetCString());
377   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
378   EXPECT_STREQ("C:", fs_windows.GetCString());
379   EXPECT_FALSE(fs_windows.RemoveLastPathComponent());
380   EXPECT_STREQ("C:", fs_windows.GetCString());
381 }
382