1 //===-- FileSpecTest.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "lldb/Utility/FileSpec.h"
13 using namespace lldb_private
;
15 static FileSpec
PosixSpec(llvm::StringRef path
) {
16 return FileSpec(path
, FileSpec::Style::posix
);
19 static FileSpec
WindowsSpec(llvm::StringRef path
) {
20 return FileSpec(path
, FileSpec::Style::windows
);
23 TEST(FileSpecTest
, FileAndDirectoryComponents
) {
24 FileSpec
fs_posix("/foo/bar", FileSpec::Style::posix
);
25 EXPECT_STREQ("/foo/bar", fs_posix
.GetCString());
26 EXPECT_STREQ("/foo", fs_posix
.GetDirectory().GetCString());
27 EXPECT_STREQ("bar", fs_posix
.GetFilename().GetCString());
29 FileSpec
fs_windows("F:\\bar", FileSpec::Style::windows
);
30 EXPECT_STREQ("F:\\bar", fs_windows
.GetCString());
31 // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetCString()); // It returns
33 EXPECT_STREQ("bar", fs_windows
.GetFilename().GetCString());
35 FileSpec
fs_posix_root("/", FileSpec::Style::posix
);
36 EXPECT_STREQ("/", fs_posix_root
.GetCString());
37 EXPECT_EQ(nullptr, fs_posix_root
.GetDirectory().GetCString());
38 EXPECT_STREQ("/", fs_posix_root
.GetFilename().GetCString());
40 FileSpec
fs_net_drive("//net", FileSpec::Style::posix
);
41 EXPECT_STREQ("//net", fs_net_drive
.GetCString());
42 EXPECT_EQ(nullptr, fs_net_drive
.GetDirectory().GetCString());
43 EXPECT_STREQ("//net", fs_net_drive
.GetFilename().GetCString());
45 FileSpec
fs_net_root("//net/", FileSpec::Style::posix
);
46 EXPECT_STREQ("//net/", fs_net_root
.GetCString());
47 EXPECT_STREQ("//net", fs_net_root
.GetDirectory().GetCString());
48 EXPECT_STREQ("/", fs_net_root
.GetFilename().GetCString());
50 FileSpec
fs_windows_drive("F:", FileSpec::Style::windows
);
51 EXPECT_STREQ("F:", fs_windows_drive
.GetCString());
52 EXPECT_EQ(nullptr, fs_windows_drive
.GetDirectory().GetCString());
53 EXPECT_STREQ("F:", fs_windows_drive
.GetFilename().GetCString());
55 FileSpec
fs_windows_root("F:\\", FileSpec::Style::windows
);
56 EXPECT_STREQ("F:\\", fs_windows_root
.GetCString());
57 EXPECT_STREQ("F:", fs_windows_root
.GetDirectory().GetCString());
58 // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It
61 FileSpec
fs_posix_long("/foo/bar/baz", FileSpec::Style::posix
);
62 EXPECT_STREQ("/foo/bar/baz", fs_posix_long
.GetCString());
63 EXPECT_STREQ("/foo/bar", fs_posix_long
.GetDirectory().GetCString());
64 EXPECT_STREQ("baz", fs_posix_long
.GetFilename().GetCString());
66 FileSpec
fs_windows_long("F:\\bar\\baz", FileSpec::Style::windows
);
67 EXPECT_STREQ("F:\\bar\\baz", fs_windows_long
.GetCString());
68 // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It
70 EXPECT_STREQ("baz", fs_windows_long
.GetFilename().GetCString());
72 FileSpec
fs_posix_trailing_slash("/foo/bar/", FileSpec::Style::posix
);
73 EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash
.GetCString());
74 EXPECT_STREQ("/foo", fs_posix_trailing_slash
.GetDirectory().GetCString());
75 EXPECT_STREQ("bar", fs_posix_trailing_slash
.GetFilename().GetCString());
77 FileSpec
fs_windows_trailing_slash("F:\\bar\\", FileSpec::Style::windows
);
78 EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash
.GetCString());
79 EXPECT_STREQ("bar", fs_windows_trailing_slash
.GetFilename().GetCString());
82 TEST(FileSpecTest
, AppendPathComponent
) {
83 FileSpec
fs_posix("/foo", FileSpec::Style::posix
);
84 fs_posix
.AppendPathComponent("bar");
85 EXPECT_STREQ("/foo/bar", fs_posix
.GetCString());
86 EXPECT_STREQ("/foo", fs_posix
.GetDirectory().GetCString());
87 EXPECT_STREQ("bar", fs_posix
.GetFilename().GetCString());
89 FileSpec
fs_posix_2("/foo", FileSpec::Style::posix
);
90 fs_posix_2
.AppendPathComponent("//bar/baz");
91 EXPECT_STREQ("/foo/bar/baz", fs_posix_2
.GetCString());
92 EXPECT_STREQ("/foo/bar", fs_posix_2
.GetDirectory().GetCString());
93 EXPECT_STREQ("baz", fs_posix_2
.GetFilename().GetCString());
95 FileSpec
fs_windows("F:\\bar", FileSpec::Style::windows
);
96 fs_windows
.AppendPathComponent("baz");
97 EXPECT_STREQ("F:\\bar\\baz", fs_windows
.GetCString());
98 // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It
100 EXPECT_STREQ("baz", fs_windows
.GetFilename().GetCString());
102 FileSpec
fs_posix_root("/", FileSpec::Style::posix
);
103 fs_posix_root
.AppendPathComponent("bar");
104 EXPECT_STREQ("/bar", fs_posix_root
.GetCString());
105 EXPECT_STREQ("/", fs_posix_root
.GetDirectory().GetCString());
106 EXPECT_STREQ("bar", fs_posix_root
.GetFilename().GetCString());
108 FileSpec
fs_windows_root("F:\\", FileSpec::Style::windows
);
109 fs_windows_root
.AppendPathComponent("bar");
110 EXPECT_STREQ("F:\\bar", fs_windows_root
.GetCString());
111 // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It
113 EXPECT_STREQ("bar", fs_windows_root
.GetFilename().GetCString());
116 TEST(FileSpecTest
, CopyByAppendingPathComponent
) {
117 FileSpec fs
= PosixSpec("/foo").CopyByAppendingPathComponent("bar");
118 EXPECT_STREQ("/foo/bar", fs
.GetCString());
119 EXPECT_STREQ("/foo", fs
.GetDirectory().GetCString());
120 EXPECT_STREQ("bar", fs
.GetFilename().GetCString());
123 TEST(FileSpecTest
, PrependPathComponent
) {
124 FileSpec
fs_posix("foo", FileSpec::Style::posix
);
125 fs_posix
.PrependPathComponent("/bar");
126 EXPECT_STREQ("/bar/foo", fs_posix
.GetCString());
128 FileSpec
fs_posix_2("foo/bar", FileSpec::Style::posix
);
129 fs_posix_2
.PrependPathComponent("/baz");
130 EXPECT_STREQ("/baz/foo/bar", fs_posix_2
.GetCString());
132 FileSpec
fs_windows("baz", FileSpec::Style::windows
);
133 fs_windows
.PrependPathComponent("F:\\bar");
134 EXPECT_STREQ("F:\\bar\\baz", fs_windows
.GetCString());
136 FileSpec
fs_posix_root("bar", FileSpec::Style::posix
);
137 fs_posix_root
.PrependPathComponent("/");
138 EXPECT_STREQ("/bar", fs_posix_root
.GetCString());
140 FileSpec
fs_windows_root("bar", FileSpec::Style::windows
);
141 fs_windows_root
.PrependPathComponent("F:\\");
142 EXPECT_STREQ("F:\\bar", fs_windows_root
.GetCString());
145 TEST(FileSpecTest
, EqualSeparator
) {
146 EXPECT_EQ(WindowsSpec("C:\\foo\\bar"), WindowsSpec("C:/foo/bar"));
149 TEST(FileSpecTest
, EqualDotsWindows
) {
150 std::pair
<const char *, const char *> tests
[] = {
151 {R
"(C:\foo\bar\baz)", R
"(C:\foo\foo\..\bar\baz)"},
152 {R
"(C:\bar\baz)", R
"(C:\foo\..\bar\baz)"},
153 {R
"(C:\bar\baz)", R
"(C:/foo/../bar/baz)"},
154 {R
"(C:/bar/baz)", R
"(C:\foo\..\bar\baz)"},
155 {R
"(C:\bar)", R
"(C:\foo\..\bar)"},
156 {R
"(C:\foo\bar)", R
"(C:\foo\.\bar)"},
157 {R
"(C:\foo\bar)", R
"(C:\foo\bar\.)"},
160 for (const auto &test
: tests
) {
161 SCOPED_TRACE(llvm::Twine(test
.first
) + " <=> " + test
.second
);
162 EXPECT_EQ(WindowsSpec(test
.first
), WindowsSpec(test
.second
));
166 TEST(FileSpecTest
, EqualDotsPosix
) {
167 std::pair
<const char *, const char *> tests
[] = {
168 {R
"(/foo/bar/baz)", R
"(/foo/foo/../bar/baz)"},
169 {R
"(/bar/baz)", R
"(/foo/../bar/baz)"},
170 {R
"(/bar)", R
"(/foo/../bar)"},
171 {R
"(/foo/bar)", R
"(/foo/./bar)"},
172 {R
"(/foo/bar)", R
"(/foo/bar/.)"},
175 for (const auto &test
: tests
) {
176 SCOPED_TRACE(llvm::Twine(test
.first
) + " <=> " + test
.second
);
177 EXPECT_EQ(PosixSpec(test
.first
), PosixSpec(test
.second
));
181 TEST(FileSpecTest
, EqualDotsPosixRoot
) {
182 std::pair
<const char *, const char *> tests
[] = {
185 {R
"(/)", R
"(/foo/..)"},
188 for (const auto &test
: tests
) {
189 SCOPED_TRACE(llvm::Twine(test
.first
) + " <=> " + test
.second
);
190 EXPECT_EQ(PosixSpec(test
.first
), PosixSpec(test
.second
));
194 TEST(FileSpecTest
, GuessPathStyle
) {
195 EXPECT_EQ(FileSpec::Style::posix
, FileSpec::GuessPathStyle("/foo/bar.txt"));
196 EXPECT_EQ(FileSpec::Style::posix
, FileSpec::GuessPathStyle("//net/bar.txt"));
197 EXPECT_EQ(FileSpec::Style::windows
,
198 FileSpec::GuessPathStyle(R
"(C:\foo.txt)"));
199 EXPECT_EQ(FileSpec::Style::windows
,
200 FileSpec::GuessPathStyle(R
"(\\net\foo.txt)"));
201 EXPECT_EQ(FileSpec::Style::windows
, FileSpec::GuessPathStyle(R
"(Z:\)"));
202 EXPECT_EQ(llvm::None
, FileSpec::GuessPathStyle("foo.txt"));
203 EXPECT_EQ(llvm::None
, FileSpec::GuessPathStyle("foo/bar.txt"));
204 EXPECT_EQ(llvm::None
, FileSpec::GuessPathStyle("Z:"));
207 TEST(FileSpecTest
, GetPath
) {
208 std::pair
<const char *, const char *> posix_tests
[] = {
209 {"/foo/.././bar", "/bar"},
210 {"/foo/./../bar", "/bar"},
211 {"/foo/../bar", "/bar"},
212 {"/foo/./bar", "/foo/bar"},
215 {"/foo//bar", "/foo/bar"},
216 {"/foo//bar/baz", "/foo/bar/baz"},
217 {"/foo//bar/./baz", "/foo/bar/baz"},
228 {"foo/../bar", "bar"},
232 {"../foo", "../foo"},
233 {"../../foo", "../../foo"},
235 for (auto test
: posix_tests
) {
236 SCOPED_TRACE(llvm::Twine("test.first = ") + test
.first
);
237 EXPECT_EQ(test
.second
, PosixSpec(test
.first
).GetPath());
240 std::pair
<const char *, const char *> windows_tests
[] = {
241 {R
"(c:\bar\..\bar)", R
"(c:\bar)"},
242 {R
"(c:\bar\.\bar)", R
"(c:\bar\bar)"},
243 {R
"(c:\bar\..)", R
"(c:\)"},
244 {R
"(c:\bar\.)", R
"(c:\bar)"},
245 {R
"(c:\.\bar)", R
"(c:\bar)"},
248 {R
"(\\net)", R
"(\\net)"},
249 {R
"(c:\..)", R
"(c:\)"},
250 {R
"(c:\.)", R
"(c:\)"},
252 // {R"(c:..)", R"(c:..)"},
255 {R
"(c:..\..)", R
"(c:)"},
256 {R
"(..\..)", R
"(..\..)"},
257 {R
"(foo\..)", R
"(.)"},
258 {R
"(foo\..\bar)", R
"(bar)"},
259 {R
"(..\foo\..)", R
"(..)"},
260 {R
"(.\foo)", R
"(foo)"},
261 {R
"(.\.\foo)", R
"(foo)"},
262 {R
"(..\foo)", R
"(..\foo)"},
263 {R
"(..\..\foo)", R
"(..\..\foo)"},
265 for (auto test
: windows_tests
) {
266 SCOPED_TRACE(llvm::Twine("test.first = ") + test
.first
);
267 EXPECT_EQ(test
.second
, WindowsSpec(test
.first
).GetPath());
271 TEST(FileSpecTest
, FormatFileSpec
) {
272 auto win
= FileSpec::Style::windows
;
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());
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());
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());
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());
295 TEST(FileSpecTest
, IsRelative
) {
296 llvm::StringRef not_relative
[] = {
317 for (const auto &path
: not_relative
) {
319 EXPECT_FALSE(PosixSpec(path
).IsRelative());
321 llvm::StringRef is_relative
[] = {
335 for (const auto &path
: is_relative
) {
337 EXPECT_TRUE(PosixSpec(path
).IsRelative());
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());
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());
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());
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());
383 TEST(FileSpecTest
, Equal
) {
384 auto Eq
= [](const char *a
, const char *b
, bool full
) {
385 return FileSpec::Equal(PosixSpec(a
), PosixSpec(b
), full
);
387 EXPECT_TRUE(Eq("/foo/bar", "/foo/bar", true));
388 EXPECT_TRUE(Eq("/foo/bar", "/foo/bar", false));
390 EXPECT_FALSE(Eq("/foo/bar", "/foo/baz", true));
391 EXPECT_FALSE(Eq("/foo/bar", "/foo/baz", false));
393 EXPECT_FALSE(Eq("/bar/foo", "/baz/foo", true));
394 EXPECT_FALSE(Eq("/bar/foo", "/baz/foo", false));
396 EXPECT_FALSE(Eq("/bar/foo", "foo", true));
397 EXPECT_TRUE(Eq("/bar/foo", "foo", false));
399 EXPECT_FALSE(Eq("foo", "/bar/foo", true));
400 EXPECT_TRUE(Eq("foo", "/bar/foo", false));
403 TEST(FileSpecTest
, Match
) {
404 auto Match
= [](const char *pattern
, const char *file
) {
405 return FileSpec::Match(PosixSpec(pattern
), PosixSpec(file
));
407 EXPECT_TRUE(Match("/foo/bar", "/foo/bar"));
408 EXPECT_FALSE(Match("/foo/bar", "/oof/bar"));
409 EXPECT_FALSE(Match("/foo/bar", "/foo/baz"));
410 EXPECT_FALSE(Match("/foo/bar", "bar"));
411 EXPECT_FALSE(Match("/foo/bar", ""));
413 EXPECT_TRUE(Match("bar", "/foo/bar"));
414 EXPECT_FALSE(Match("bar", "/foo/baz"));
415 EXPECT_TRUE(Match("bar", "bar"));
416 EXPECT_FALSE(Match("bar", "baz"));
417 EXPECT_FALSE(Match("bar", ""));
419 EXPECT_TRUE(Match("", "/foo/bar"));
420 EXPECT_TRUE(Match("", ""));
424 TEST(FileSpecTest
, Yaml
) {
426 llvm::raw_string_ostream
os(buffer
);
429 FileSpec
fs_windows("F:\\bar", FileSpec::Style::windows
);
430 llvm::yaml::Output
yout(os
);
435 FileSpec deserialized
;
436 llvm::yaml::Input
yin(buffer
);
439 EXPECT_EQ(deserialized
.GetPathStyle(), fs_windows
.GetPathStyle());
440 EXPECT_EQ(deserialized
.GetFilename(), fs_windows
.GetFilename());
441 EXPECT_EQ(deserialized
.GetDirectory(), fs_windows
.GetDirectory());
442 EXPECT_EQ(deserialized
, fs_windows
);
445 TEST(FileSpecTest
, OperatorBool
) {
446 EXPECT_FALSE(FileSpec());
447 EXPECT_FALSE(FileSpec(""));
448 EXPECT_TRUE(FileSpec("/foo/bar"));