xref: /llvm-project/lldb/unittests/Utility/FileSpecTest.cpp (revision e27561fc7de0231f2efdb750f2092c3ac807c1a3)
180814287SRaphael Isemann //===-- FileSpecTest.cpp --------------------------------------------------===//
20e0906c2SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60e0906c2SPavel Labath //
70e0906c2SPavel Labath //===----------------------------------------------------------------------===//
80e0906c2SPavel Labath 
90e0906c2SPavel Labath #include "gtest/gtest.h"
100e0906c2SPavel Labath 
110e0906c2SPavel Labath #include "lldb/Utility/FileSpec.h"
120e0906c2SPavel Labath 
130e0906c2SPavel Labath using namespace lldb_private;
140e0906c2SPavel Labath 
PosixSpec(llvm::StringRef path)15d1a561d4SPavel Labath static FileSpec PosixSpec(llvm::StringRef path) {
16d1a561d4SPavel Labath   return FileSpec(path, FileSpec::Style::posix);
17d1a561d4SPavel Labath }
18d1a561d4SPavel Labath 
WindowsSpec(llvm::StringRef path)19d1a561d4SPavel Labath static FileSpec WindowsSpec(llvm::StringRef path) {
20d1a561d4SPavel Labath   return FileSpec(path, FileSpec::Style::windows);
21d1a561d4SPavel Labath }
22d1a561d4SPavel Labath 
TEST(FileSpecTest,FileAndDirectoryComponents)230e0906c2SPavel Labath TEST(FileSpecTest, FileAndDirectoryComponents) {
248f3be7a3SJonas Devlieghere   FileSpec fs_posix("/foo/bar", FileSpec::Style::posix);
25529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar", fs_posix.GetPath().c_str());
260e0906c2SPavel Labath   EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
270e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
280e0906c2SPavel Labath 
298f3be7a3SJonas Devlieghere   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
30529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar", fs_windows.GetPath().c_str());
31529a3d87SGreg Clayton   // EXPECT_STREQ("F:\\", fs_windows.GetDirectory().GetPath().c_str()); // It returns
320e0906c2SPavel Labath   // "F:/"
330e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_windows.GetFilename().GetCString());
340e0906c2SPavel Labath 
358f3be7a3SJonas Devlieghere   FileSpec fs_posix_root("/", FileSpec::Style::posix);
36529a3d87SGreg Clayton   EXPECT_STREQ("/", fs_posix_root.GetPath().c_str());
370e0906c2SPavel Labath   EXPECT_EQ(nullptr, fs_posix_root.GetDirectory().GetCString());
380e0906c2SPavel Labath   EXPECT_STREQ("/", fs_posix_root.GetFilename().GetCString());
390e0906c2SPavel Labath 
408f3be7a3SJonas Devlieghere   FileSpec fs_net_drive("//net", FileSpec::Style::posix);
41529a3d87SGreg Clayton   EXPECT_STREQ("//net", fs_net_drive.GetPath().c_str());
42ad8d48f9SJonas Devlieghere   EXPECT_EQ(nullptr, fs_net_drive.GetDirectory().GetCString());
43ad8d48f9SJonas Devlieghere   EXPECT_STREQ("//net", fs_net_drive.GetFilename().GetCString());
44ad8d48f9SJonas Devlieghere 
458f3be7a3SJonas Devlieghere   FileSpec fs_net_root("//net/", FileSpec::Style::posix);
46529a3d87SGreg Clayton   EXPECT_STREQ("//net/", fs_net_root.GetPath().c_str());
47ad8d48f9SJonas Devlieghere   EXPECT_STREQ("//net", fs_net_root.GetDirectory().GetCString());
48ad8d48f9SJonas Devlieghere   EXPECT_STREQ("/", fs_net_root.GetFilename().GetCString());
49ad8d48f9SJonas Devlieghere 
508f3be7a3SJonas Devlieghere   FileSpec fs_windows_drive("F:", FileSpec::Style::windows);
51529a3d87SGreg Clayton   EXPECT_STREQ("F:", fs_windows_drive.GetPath().c_str());
520e0906c2SPavel Labath   EXPECT_EQ(nullptr, fs_windows_drive.GetDirectory().GetCString());
530e0906c2SPavel Labath   EXPECT_STREQ("F:", fs_windows_drive.GetFilename().GetCString());
540e0906c2SPavel Labath 
558f3be7a3SJonas Devlieghere   FileSpec fs_windows_root("F:\\", FileSpec::Style::windows);
56529a3d87SGreg Clayton   EXPECT_STREQ("F:\\", fs_windows_root.GetPath().c_str());
570e0906c2SPavel Labath   EXPECT_STREQ("F:", fs_windows_root.GetDirectory().GetCString());
580e0906c2SPavel Labath   // EXPECT_STREQ("\\", fs_windows_root.GetFilename().GetCString()); // It
590e0906c2SPavel Labath   // returns "/"
600e0906c2SPavel Labath 
618f3be7a3SJonas Devlieghere   FileSpec fs_posix_long("/foo/bar/baz", FileSpec::Style::posix);
62529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar/baz", fs_posix_long.GetPath().c_str());
630e0906c2SPavel Labath   EXPECT_STREQ("/foo/bar", fs_posix_long.GetDirectory().GetCString());
640e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_posix_long.GetFilename().GetCString());
650e0906c2SPavel Labath 
668f3be7a3SJonas Devlieghere   FileSpec fs_windows_long("F:\\bar\\baz", FileSpec::Style::windows);
67529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar\\baz", fs_windows_long.GetPath().c_str());
680e0906c2SPavel Labath   // EXPECT_STREQ("F:\\bar", fs_windows_long.GetDirectory().GetCString()); // It
690e0906c2SPavel Labath   // returns "F:/bar"
700e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_windows_long.GetFilename().GetCString());
710e0906c2SPavel Labath 
728f3be7a3SJonas Devlieghere   FileSpec fs_posix_trailing_slash("/foo/bar/", FileSpec::Style::posix);
73529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar", fs_posix_trailing_slash.GetPath().c_str());
74776cd7adSGreg Clayton   EXPECT_STREQ("/foo", fs_posix_trailing_slash.GetDirectory().GetCString());
75776cd7adSGreg Clayton   EXPECT_STREQ("bar", fs_posix_trailing_slash.GetFilename().GetCString());
760e0906c2SPavel Labath 
778f3be7a3SJonas Devlieghere   FileSpec fs_windows_trailing_slash("F:\\bar\\", FileSpec::Style::windows);
78529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar", fs_windows_trailing_slash.GetPath().c_str());
79776cd7adSGreg Clayton   EXPECT_STREQ("bar", fs_windows_trailing_slash.GetFilename().GetCString());
800e0906c2SPavel Labath }
810e0906c2SPavel Labath 
TEST(FileSpecTest,AppendPathComponent)820e0906c2SPavel Labath TEST(FileSpecTest, AppendPathComponent) {
838f3be7a3SJonas Devlieghere   FileSpec fs_posix("/foo", FileSpec::Style::posix);
840e0906c2SPavel Labath   fs_posix.AppendPathComponent("bar");
85529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar", fs_posix.GetPath().c_str());
860e0906c2SPavel Labath   EXPECT_STREQ("/foo", fs_posix.GetDirectory().GetCString());
870e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_posix.GetFilename().GetCString());
880e0906c2SPavel Labath 
898f3be7a3SJonas Devlieghere   FileSpec fs_posix_2("/foo", FileSpec::Style::posix);
900e0906c2SPavel Labath   fs_posix_2.AppendPathComponent("//bar/baz");
91529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar/baz", fs_posix_2.GetPath().c_str());
920e0906c2SPavel Labath   EXPECT_STREQ("/foo/bar", fs_posix_2.GetDirectory().GetCString());
930e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_posix_2.GetFilename().GetCString());
940e0906c2SPavel Labath 
958f3be7a3SJonas Devlieghere   FileSpec fs_windows("F:\\bar", FileSpec::Style::windows);
960e0906c2SPavel Labath   fs_windows.AppendPathComponent("baz");
97529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetPath().c_str());
980e0906c2SPavel Labath   // EXPECT_STREQ("F:\\bar", fs_windows.GetDirectory().GetCString()); // It
990e0906c2SPavel Labath   // returns "F:/bar"
1000e0906c2SPavel Labath   EXPECT_STREQ("baz", fs_windows.GetFilename().GetCString());
1010e0906c2SPavel Labath 
1028f3be7a3SJonas Devlieghere   FileSpec fs_posix_root("/", FileSpec::Style::posix);
1030e0906c2SPavel Labath   fs_posix_root.AppendPathComponent("bar");
104529a3d87SGreg Clayton   EXPECT_STREQ("/bar", fs_posix_root.GetPath().c_str());
1050e0906c2SPavel Labath   EXPECT_STREQ("/", fs_posix_root.GetDirectory().GetCString());
1060e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_posix_root.GetFilename().GetCString());
1070e0906c2SPavel Labath 
1088f3be7a3SJonas Devlieghere   FileSpec fs_windows_root("F:\\", FileSpec::Style::windows);
1090e0906c2SPavel Labath   fs_windows_root.AppendPathComponent("bar");
110529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar", fs_windows_root.GetPath().c_str());
1110e0906c2SPavel Labath   // EXPECT_STREQ("F:\\", fs_windows_root.GetDirectory().GetCString()); // It
1120e0906c2SPavel Labath   // returns "F:/"
1130e0906c2SPavel Labath   EXPECT_STREQ("bar", fs_windows_root.GetFilename().GetCString());
1140e0906c2SPavel Labath }
1150e0906c2SPavel Labath 
TEST(FileSpecTest,CopyByAppendingPathComponent)1160e0906c2SPavel Labath TEST(FileSpecTest, CopyByAppendingPathComponent) {
117d1a561d4SPavel Labath   FileSpec fs = PosixSpec("/foo").CopyByAppendingPathComponent("bar");
118529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar", fs.GetPath().c_str());
1190e0906c2SPavel Labath   EXPECT_STREQ("/foo", fs.GetDirectory().GetCString());
1200e0906c2SPavel Labath   EXPECT_STREQ("bar", fs.GetFilename().GetCString());
1210e0906c2SPavel Labath }
1220e0906c2SPavel Labath 
TEST(FileSpecTest,PrependPathComponent)1230e0906c2SPavel Labath TEST(FileSpecTest, PrependPathComponent) {
1248f3be7a3SJonas Devlieghere   FileSpec fs_posix("foo", FileSpec::Style::posix);
1250e0906c2SPavel Labath   fs_posix.PrependPathComponent("/bar");
126529a3d87SGreg Clayton   EXPECT_STREQ("/bar/foo", fs_posix.GetPath().c_str());
1270e0906c2SPavel Labath 
1288f3be7a3SJonas Devlieghere   FileSpec fs_posix_2("foo/bar", FileSpec::Style::posix);
1290e0906c2SPavel Labath   fs_posix_2.PrependPathComponent("/baz");
130529a3d87SGreg Clayton   EXPECT_STREQ("/baz/foo/bar", fs_posix_2.GetPath().c_str());
1310e0906c2SPavel Labath 
1328f3be7a3SJonas Devlieghere   FileSpec fs_windows("baz", FileSpec::Style::windows);
1330e0906c2SPavel Labath   fs_windows.PrependPathComponent("F:\\bar");
134529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetPath().c_str());
1350e0906c2SPavel Labath 
1368f3be7a3SJonas Devlieghere   FileSpec fs_posix_root("bar", FileSpec::Style::posix);
1370e0906c2SPavel Labath   fs_posix_root.PrependPathComponent("/");
138529a3d87SGreg Clayton   EXPECT_STREQ("/bar", fs_posix_root.GetPath().c_str());
1390e0906c2SPavel Labath 
1408f3be7a3SJonas Devlieghere   FileSpec fs_windows_root("bar", FileSpec::Style::windows);
1410e0906c2SPavel Labath   fs_windows_root.PrependPathComponent("F:\\");
142529a3d87SGreg Clayton   EXPECT_STREQ("F:\\bar", fs_windows_root.GetPath().c_str());
1430e0906c2SPavel Labath }
1440e0906c2SPavel Labath 
TEST(FileSpecTest,EqualSeparator)1450e0906c2SPavel Labath TEST(FileSpecTest, EqualSeparator) {
146d1a561d4SPavel Labath   EXPECT_EQ(WindowsSpec("C:\\foo\\bar"), WindowsSpec("C:/foo/bar"));
1470e0906c2SPavel Labath }
1480e0906c2SPavel Labath 
TEST(FileSpecTest,EqualDotsWindows)1490e0906c2SPavel Labath TEST(FileSpecTest, EqualDotsWindows) {
1500e0906c2SPavel Labath   std::pair<const char *, const char *> tests[] = {
1510e0906c2SPavel Labath       {R"(C:\foo\bar\baz)", R"(C:\foo\foo\..\bar\baz)"},
1520e0906c2SPavel Labath       {R"(C:\bar\baz)", R"(C:\foo\..\bar\baz)"},
1530e0906c2SPavel Labath       {R"(C:\bar\baz)", R"(C:/foo/../bar/baz)"},
1540e0906c2SPavel Labath       {R"(C:/bar/baz)", R"(C:\foo\..\bar\baz)"},
1550e0906c2SPavel Labath       {R"(C:\bar)", R"(C:\foo\..\bar)"},
1560e0906c2SPavel Labath       {R"(C:\foo\bar)", R"(C:\foo\.\bar)"},
1570e0906c2SPavel Labath       {R"(C:\foo\bar)", R"(C:\foo\bar\.)"},
1580e0906c2SPavel Labath   };
1590e0906c2SPavel Labath 
1600e0906c2SPavel Labath   for (const auto &test : tests) {
161d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine(test.first) + " <=> " + test.second);
162d1a561d4SPavel Labath     EXPECT_EQ(WindowsSpec(test.first), WindowsSpec(test.second));
1630e0906c2SPavel Labath   }
1640e0906c2SPavel Labath }
1650e0906c2SPavel Labath 
TEST(FileSpecTest,EqualDotsPosix)1660e0906c2SPavel Labath TEST(FileSpecTest, EqualDotsPosix) {
1670e0906c2SPavel Labath   std::pair<const char *, const char *> tests[] = {
1680e0906c2SPavel Labath       {R"(/foo/bar/baz)", R"(/foo/foo/../bar/baz)"},
1690e0906c2SPavel Labath       {R"(/bar/baz)", R"(/foo/../bar/baz)"},
1700e0906c2SPavel Labath       {R"(/bar)", R"(/foo/../bar)"},
1710e0906c2SPavel Labath       {R"(/foo/bar)", R"(/foo/./bar)"},
1720e0906c2SPavel Labath       {R"(/foo/bar)", R"(/foo/bar/.)"},
1730e0906c2SPavel Labath   };
1740e0906c2SPavel Labath 
1750e0906c2SPavel Labath   for (const auto &test : tests) {
176d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine(test.first) + " <=> " + test.second);
177d1a561d4SPavel Labath     EXPECT_EQ(PosixSpec(test.first), PosixSpec(test.second));
1780e0906c2SPavel Labath   }
1790e0906c2SPavel Labath }
1800e0906c2SPavel Labath 
TEST(FileSpecTest,EqualDotsPosixRoot)1810e0906c2SPavel Labath TEST(FileSpecTest, EqualDotsPosixRoot) {
1820e0906c2SPavel Labath   std::pair<const char *, const char *> tests[] = {
1830e0906c2SPavel Labath       {R"(/)", R"(/..)"},
1840e0906c2SPavel Labath       {R"(/)", R"(/.)"},
1850e0906c2SPavel Labath       {R"(/)", R"(/foo/..)"},
1860e0906c2SPavel Labath   };
1870e0906c2SPavel Labath 
1880e0906c2SPavel Labath   for (const auto &test : tests) {
189d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine(test.first) + " <=> " + test.second);
190d1a561d4SPavel Labath     EXPECT_EQ(PosixSpec(test.first), PosixSpec(test.second));
1910e0906c2SPavel Labath   }
1920e0906c2SPavel Labath }
1930e0906c2SPavel Labath 
TEST(FileSpecTest,GuessPathStyle)194841bea93SPavel Labath TEST(FileSpecTest, GuessPathStyle) {
195841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::posix, FileSpec::GuessPathStyle("/foo/bar.txt"));
196841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::posix, FileSpec::GuessPathStyle("//net/bar.txt"));
197841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::windows,
198841bea93SPavel Labath             FileSpec::GuessPathStyle(R"(C:\foo.txt)"));
199841bea93SPavel Labath   EXPECT_EQ(FileSpec::Style::windows,
200b548f584SMartin Storsjö             FileSpec::GuessPathStyle(R"(C:/foo.txt)"));
201b548f584SMartin Storsjö   EXPECT_EQ(FileSpec::Style::windows,
202841bea93SPavel Labath             FileSpec::GuessPathStyle(R"(\\net\foo.txt)"));
203f72ae5cbSJaroslav Sevcik   EXPECT_EQ(FileSpec::Style::windows, FileSpec::GuessPathStyle(R"(Z:\)"));
204b548f584SMartin Storsjö   EXPECT_EQ(FileSpec::Style::windows, FileSpec::GuessPathStyle(R"(Z:/)"));
205d2a6114fSKazu Hirata   EXPECT_EQ(std::nullopt, FileSpec::GuessPathStyle("foo.txt"));
206d2a6114fSKazu Hirata   EXPECT_EQ(std::nullopt, FileSpec::GuessPathStyle("foo/bar.txt"));
207d2a6114fSKazu Hirata   EXPECT_EQ(std::nullopt, FileSpec::GuessPathStyle("Z:"));
208841bea93SPavel Labath }
209841bea93SPavel Labath 
TEST(FileSpecTest,GetPath)210d1a561d4SPavel Labath TEST(FileSpecTest, GetPath) {
2110e0906c2SPavel Labath   std::pair<const char *, const char *> posix_tests[] = {
2120e0906c2SPavel Labath       {"/foo/.././bar", "/bar"},
2130e0906c2SPavel Labath       {"/foo/./../bar", "/bar"},
2140e0906c2SPavel Labath       {"/foo/../bar", "/bar"},
2150e0906c2SPavel Labath       {"/foo/./bar", "/foo/bar"},
2160e0906c2SPavel Labath       {"/foo/..", "/"},
2170e0906c2SPavel Labath       {"/foo/.", "/foo"},
2180e0906c2SPavel Labath       {"/foo//bar", "/foo/bar"},
2190e0906c2SPavel Labath       {"/foo//bar/baz", "/foo/bar/baz"},
2200e0906c2SPavel Labath       {"/foo//bar/./baz", "/foo/bar/baz"},
2210e0906c2SPavel Labath       {"/./foo", "/foo"},
2220e0906c2SPavel Labath       {"/", "/"},
223e7306b10SPavel Labath       {"//", "/"},
2240e0906c2SPavel Labath       {"//net", "//net"},
2250e0906c2SPavel Labath       {"/..", "/"},
2260e0906c2SPavel Labath       {"/.", "/"},
2270e0906c2SPavel Labath       {"..", ".."},
22839d50b72SGreg Clayton       {".", "."},
2290e0906c2SPavel Labath       {"../..", "../.."},
23039d50b72SGreg Clayton       {"foo/..", "."},
2310e0906c2SPavel Labath       {"foo/../bar", "bar"},
2320e0906c2SPavel Labath       {"../foo/..", ".."},
2330e0906c2SPavel Labath       {"./foo", "foo"},
234776cd7adSGreg Clayton       {"././foo", "foo"},
235776cd7adSGreg Clayton       {"../foo", "../foo"},
236776cd7adSGreg Clayton       {"../../foo", "../../foo"},
2370e0906c2SPavel Labath   };
2380e0906c2SPavel Labath   for (auto test : posix_tests) {
239e7306b10SPavel Labath     SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
240d1a561d4SPavel Labath     EXPECT_EQ(test.second, PosixSpec(test.first).GetPath());
2410e0906c2SPavel Labath   }
2420e0906c2SPavel Labath 
2430e0906c2SPavel Labath   std::pair<const char *, const char *> windows_tests[] = {
2440e0906c2SPavel Labath       {R"(c:\bar\..\bar)", R"(c:\bar)"},
2450e0906c2SPavel Labath       {R"(c:\bar\.\bar)", R"(c:\bar\bar)"},
2460e0906c2SPavel Labath       {R"(c:\bar\..)", R"(c:\)"},
2470e0906c2SPavel Labath       {R"(c:\bar\.)", R"(c:\bar)"},
2480e0906c2SPavel Labath       {R"(c:\.\bar)", R"(c:\bar)"},
2490e0906c2SPavel Labath       {R"(\)", R"(\)"},
250e7306b10SPavel Labath       {R"(\\)", R"(\)"},
251e7306b10SPavel Labath       {R"(\\net)", R"(\\net)"},
2520e0906c2SPavel Labath       {R"(c:\..)", R"(c:\)"},
2530e0906c2SPavel Labath       {R"(c:\.)", R"(c:\)"},
25458c7bf24SReid Kleckner       {R"(\..)", R"(\)"},
2550e0906c2SPavel Labath       //      {R"(c:..)", R"(c:..)"},
2560e0906c2SPavel Labath       {R"(..)", R"(..)"},
25739d50b72SGreg Clayton       {R"(.)", R"(.)"},
25858c7bf24SReid Kleckner       {R"(c:..\..)", R"(c:)"},
2590e0906c2SPavel Labath       {R"(..\..)", R"(..\..)"},
26039d50b72SGreg Clayton       {R"(foo\..)", R"(.)"},
2610e0906c2SPavel Labath       {R"(foo\..\bar)", R"(bar)"},
2620e0906c2SPavel Labath       {R"(..\foo\..)", R"(..)"},
2630e0906c2SPavel Labath       {R"(.\foo)", R"(foo)"},
264776cd7adSGreg Clayton       {R"(.\.\foo)", R"(foo)"},
265776cd7adSGreg Clayton       {R"(..\foo)", R"(..\foo)"},
266776cd7adSGreg Clayton       {R"(..\..\foo)", R"(..\..\foo)"},
2670e0906c2SPavel Labath   };
2680e0906c2SPavel Labath   for (auto test : windows_tests) {
269d1a561d4SPavel Labath     SCOPED_TRACE(llvm::Twine("test.first = ") + test.first);
270d1a561d4SPavel Labath     EXPECT_EQ(test.second, WindowsSpec(test.first).GetPath());
2710e0906c2SPavel Labath   }
2720e0906c2SPavel Labath }
2730e0906c2SPavel Labath 
TEST(FileSpecTest,FormatFileSpec)2740e0906c2SPavel Labath TEST(FileSpecTest, FormatFileSpec) {
2752cb7cf8eSPavel Labath   auto win = FileSpec::Style::windows;
2760e0906c2SPavel Labath 
2770e0906c2SPavel Labath   FileSpec F;
2780e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0}", F).str());
2790e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
2800e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0:F}", F).str());
2810e0906c2SPavel Labath 
2828f3be7a3SJonas Devlieghere   F = FileSpec("C:\\foo\\bar.txt", win);
2830e0906c2SPavel Labath   EXPECT_EQ("C:\\foo\\bar.txt", llvm::formatv("{0}", F).str());
2840e0906c2SPavel Labath   EXPECT_EQ("C:\\foo\\", llvm::formatv("{0:D}", F).str());
2850e0906c2SPavel Labath   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
2860e0906c2SPavel Labath 
2878f3be7a3SJonas Devlieghere   F = FileSpec("foo\\bar.txt", win);
2880e0906c2SPavel Labath   EXPECT_EQ("foo\\bar.txt", llvm::formatv("{0}", F).str());
2890e0906c2SPavel Labath   EXPECT_EQ("foo\\", llvm::formatv("{0:D}", F).str());
2900e0906c2SPavel Labath   EXPECT_EQ("bar.txt", llvm::formatv("{0:F}", F).str());
2910e0906c2SPavel Labath 
2928f3be7a3SJonas Devlieghere   F = FileSpec("foo", win);
2930e0906c2SPavel Labath   EXPECT_EQ("foo", llvm::formatv("{0}", F).str());
2940e0906c2SPavel Labath   EXPECT_EQ("foo", llvm::formatv("{0:F}", F).str());
2950e0906c2SPavel Labath   EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str());
2960e0906c2SPavel Labath }
297776cd7adSGreg Clayton 
TEST(FileSpecTest,IsRelative)29886188d8aSGreg Clayton TEST(FileSpecTest, IsRelative) {
29986188d8aSGreg Clayton   llvm::StringRef not_relative[] = {
30086188d8aSGreg Clayton     "/",
30186188d8aSGreg Clayton     "/a",
30286188d8aSGreg Clayton     "/a/",
30386188d8aSGreg Clayton     "/a/b",
30486188d8aSGreg Clayton     "/a/b/",
30586188d8aSGreg Clayton     "//",
30686188d8aSGreg Clayton     "//a/",
30786188d8aSGreg Clayton     "//a/b",
30886188d8aSGreg Clayton     "//a/b/",
30986188d8aSGreg Clayton     "~",
31086188d8aSGreg Clayton     "~/",
31186188d8aSGreg Clayton     "~/a",
31286188d8aSGreg Clayton     "~/a/",
3130f3ed7a4SRaphael Isemann     "~/a/b",
31486188d8aSGreg Clayton     "~/a/b/",
31586188d8aSGreg Clayton     "/foo/.",
31686188d8aSGreg Clayton     "/foo/..",
31786188d8aSGreg Clayton     "/foo/../",
31886188d8aSGreg Clayton     "/foo/../.",
31986188d8aSGreg Clayton   };
32086188d8aSGreg Clayton   for (const auto &path: not_relative) {
321d1a561d4SPavel Labath     SCOPED_TRACE(path);
322d1a561d4SPavel Labath     EXPECT_FALSE(PosixSpec(path).IsRelative());
32386188d8aSGreg Clayton   }
32486188d8aSGreg Clayton   llvm::StringRef is_relative[] = {
32586188d8aSGreg Clayton     ".",
32686188d8aSGreg Clayton     "./",
32786188d8aSGreg Clayton     ".///",
32886188d8aSGreg Clayton     "a",
32986188d8aSGreg Clayton     "./a",
33086188d8aSGreg Clayton     "./a/",
33186188d8aSGreg Clayton     "./a/",
33286188d8aSGreg Clayton     "./a/b",
33386188d8aSGreg Clayton     "./a/b/",
33486188d8aSGreg Clayton     "../foo",
33586188d8aSGreg Clayton     "foo/bar.c",
33686188d8aSGreg Clayton     "./foo/bar.c"
33786188d8aSGreg Clayton   };
33886188d8aSGreg Clayton   for (const auto &path: is_relative) {
339d1a561d4SPavel Labath     SCOPED_TRACE(path);
340d1a561d4SPavel Labath     EXPECT_TRUE(PosixSpec(path).IsRelative());
34186188d8aSGreg Clayton   }
34286188d8aSGreg Clayton }
34386188d8aSGreg Clayton 
TEST(FileSpecTest,RemoveLastPathComponent)344df8e291eSJonas Devlieghere TEST(FileSpecTest, RemoveLastPathComponent) {
3458f3be7a3SJonas Devlieghere   FileSpec fs_posix("/foo/bar/baz", FileSpec::Style::posix);
346529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar/baz", fs_posix.GetPath().c_str());
347df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
348529a3d87SGreg Clayton   EXPECT_STREQ("/foo/bar", fs_posix.GetPath().c_str());
349df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
350529a3d87SGreg Clayton   EXPECT_STREQ("/foo", fs_posix.GetPath().c_str());
351df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix.RemoveLastPathComponent());
352529a3d87SGreg Clayton   EXPECT_STREQ("/", fs_posix.GetPath().c_str());
353df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix.RemoveLastPathComponent());
354529a3d87SGreg Clayton   EXPECT_STREQ("/", fs_posix.GetPath().c_str());
355df8e291eSJonas Devlieghere 
3568f3be7a3SJonas Devlieghere   FileSpec fs_posix_relative("./foo/bar/baz", FileSpec::Style::posix);
357529a3d87SGreg Clayton   EXPECT_STREQ("foo/bar/baz", fs_posix_relative.GetPath().c_str());
358df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
359529a3d87SGreg Clayton   EXPECT_STREQ("foo/bar", fs_posix_relative.GetPath().c_str());
360df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_posix_relative.RemoveLastPathComponent());
361529a3d87SGreg Clayton   EXPECT_STREQ("foo", fs_posix_relative.GetPath().c_str());
362df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
363529a3d87SGreg Clayton   EXPECT_STREQ("foo", fs_posix_relative.GetPath().c_str());
364df8e291eSJonas Devlieghere 
3658f3be7a3SJonas Devlieghere   FileSpec fs_posix_relative2("./", FileSpec::Style::posix);
366529a3d87SGreg Clayton   EXPECT_STREQ(".", fs_posix_relative2.GetPath().c_str());
367df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix_relative2.RemoveLastPathComponent());
368529a3d87SGreg Clayton   EXPECT_STREQ(".", fs_posix_relative2.GetPath().c_str());
369df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_posix_relative.RemoveLastPathComponent());
370529a3d87SGreg Clayton   EXPECT_STREQ(".", fs_posix_relative2.GetPath().c_str());
371df8e291eSJonas Devlieghere 
3728f3be7a3SJonas Devlieghere   FileSpec fs_windows("C:\\foo\\bar\\baz", FileSpec::Style::windows);
373529a3d87SGreg Clayton   EXPECT_STREQ("C:\\foo\\bar\\baz", fs_windows.GetPath().c_str());
374df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
375529a3d87SGreg Clayton   EXPECT_STREQ("C:\\foo\\bar", fs_windows.GetPath().c_str());
376df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
377529a3d87SGreg Clayton   EXPECT_STREQ("C:\\foo", fs_windows.GetPath().c_str());
378df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
379529a3d87SGreg Clayton   EXPECT_STREQ("C:\\", fs_windows.GetPath().c_str());
380df8e291eSJonas Devlieghere   EXPECT_TRUE(fs_windows.RemoveLastPathComponent());
381529a3d87SGreg Clayton   EXPECT_STREQ("C:", fs_windows.GetPath().c_str());
382df8e291eSJonas Devlieghere   EXPECT_FALSE(fs_windows.RemoveLastPathComponent());
383529a3d87SGreg Clayton   EXPECT_STREQ("C:", fs_windows.GetPath().c_str());
384df8e291eSJonas Devlieghere }
385bf716eb8SPavel Labath 
TEST(FileSpecTest,Equal)386bf716eb8SPavel Labath TEST(FileSpecTest, Equal) {
387bf716eb8SPavel Labath   auto Eq = [](const char *a, const char *b, bool full) {
388bf716eb8SPavel Labath     return FileSpec::Equal(PosixSpec(a), PosixSpec(b), full);
389bf716eb8SPavel Labath   };
390bf716eb8SPavel Labath   EXPECT_TRUE(Eq("/foo/bar", "/foo/bar", true));
391bf716eb8SPavel Labath   EXPECT_TRUE(Eq("/foo/bar", "/foo/bar", false));
392bf716eb8SPavel Labath 
393bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/foo/bar", "/foo/baz", true));
394bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/foo/bar", "/foo/baz", false));
395bf716eb8SPavel Labath 
396bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/bar/foo", "/baz/foo", true));
397bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/bar/foo", "/baz/foo", false));
398bf716eb8SPavel Labath 
399bf716eb8SPavel Labath   EXPECT_FALSE(Eq("/bar/foo", "foo", true));
400bf716eb8SPavel Labath   EXPECT_TRUE(Eq("/bar/foo", "foo", false));
401bf716eb8SPavel Labath 
402bf716eb8SPavel Labath   EXPECT_FALSE(Eq("foo", "/bar/foo", true));
403bf716eb8SPavel Labath   EXPECT_TRUE(Eq("foo", "/bar/foo", false));
404bf716eb8SPavel Labath }
405532290e6SPavel Labath 
TEST(FileSpecTest,Match)406532290e6SPavel Labath TEST(FileSpecTest, Match) {
407532290e6SPavel Labath   auto Match = [](const char *pattern, const char *file) {
408532290e6SPavel Labath     return FileSpec::Match(PosixSpec(pattern), PosixSpec(file));
409532290e6SPavel Labath   };
410532290e6SPavel Labath   EXPECT_TRUE(Match("/foo/bar", "/foo/bar"));
411532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", "/oof/bar"));
412532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", "/foo/baz"));
413532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", "bar"));
414532290e6SPavel Labath   EXPECT_FALSE(Match("/foo/bar", ""));
415532290e6SPavel Labath 
416532290e6SPavel Labath   EXPECT_TRUE(Match("bar", "/foo/bar"));
417532290e6SPavel Labath   EXPECT_FALSE(Match("bar", "/foo/baz"));
418532290e6SPavel Labath   EXPECT_TRUE(Match("bar", "bar"));
419532290e6SPavel Labath   EXPECT_FALSE(Match("bar", "baz"));
420532290e6SPavel Labath   EXPECT_FALSE(Match("bar", ""));
421532290e6SPavel Labath 
422532290e6SPavel Labath   EXPECT_TRUE(Match("", "/foo/bar"));
423532290e6SPavel Labath   EXPECT_TRUE(Match("", ""));
424532290e6SPavel Labath 
425532290e6SPavel Labath }
426bc9b6b33SJonas Devlieghere 
TEST(FileSpecTest,TestAbsoluteCaching)427dc0f452eSGreg Clayton TEST(FileSpecTest, TestAbsoluteCaching) {
428dc0f452eSGreg Clayton   // Test that if we modify a path that we recalculate if a path is relative
429dc0f452eSGreg Clayton   // or absolute correctly. The test below calls set accessors and functions
430dc0f452eSGreg Clayton   // that change the path and verifies that the FileSpec::IsAbsolute() returns
431dc0f452eSGreg Clayton   // the correct value.
432dc0f452eSGreg Clayton   FileSpec file = PosixSpec("/tmp/a");
433dc0f452eSGreg Clayton   EXPECT_TRUE(file.IsAbsolute());
434dc0f452eSGreg Clayton   file.ClearDirectory();
435dc0f452eSGreg Clayton   EXPECT_FALSE(file.IsAbsolute());
436dc0f452eSGreg Clayton   file.SetDirectory("/tmp");
437dc0f452eSGreg Clayton   EXPECT_TRUE(file.IsAbsolute());
438dc0f452eSGreg Clayton   file.SetDirectory(".");
439dc0f452eSGreg Clayton   EXPECT_FALSE(file.IsAbsolute());
440dc0f452eSGreg Clayton   file.SetPath("/log.txt");
441dc0f452eSGreg Clayton   EXPECT_TRUE(file.IsAbsolute());
442dc0f452eSGreg Clayton   file.RemoveLastPathComponent();
443dc0f452eSGreg Clayton   EXPECT_TRUE(file.IsAbsolute());
444dc0f452eSGreg Clayton   file.ClearFilename();
445dc0f452eSGreg Clayton   EXPECT_FALSE(file.IsAbsolute());
446dc0f452eSGreg Clayton   file.AppendPathComponent("foo.txt");
447dc0f452eSGreg Clayton   EXPECT_FALSE(file.IsAbsolute());
448dc0f452eSGreg Clayton   file.PrependPathComponent("/tmp");
449dc0f452eSGreg Clayton   EXPECT_TRUE(file.IsAbsolute());
450dc0f452eSGreg Clayton }
451de5f96e9SAlex Langford 
TEST(FileSpecTest,TestFileNameExtensions)452de5f96e9SAlex Langford TEST(FileSpecTest, TestFileNameExtensions) {
453de5f96e9SAlex Langford   FileSpec dylib = PosixSpec("/tmp/foo.dylib");
454de5f96e9SAlex Langford   FileSpec exe = PosixSpec("/tmp/foo");
455de5f96e9SAlex Langford   FileSpec dSYM = PosixSpec("/tmp/foo.dSYM/");
456de5f96e9SAlex Langford   FileSpec just_dot = PosixSpec("/tmp/bar.");
457de5f96e9SAlex Langford 
458de5f96e9SAlex Langford   EXPECT_TRUE(dylib.GetFileNameExtension() == ".dylib");
4596fcdfc37SAlex Langford   EXPECT_TRUE(exe.GetFileNameExtension() == llvm::StringRef());
460de5f96e9SAlex Langford   EXPECT_TRUE(dSYM.GetFileNameExtension() == ".dSYM");
461de5f96e9SAlex Langford   EXPECT_TRUE(just_dot.GetFileNameExtension() == ".");
462de5f96e9SAlex Langford 
463de5f96e9SAlex Langford   FileSpec dll = WindowsSpec("C:\\tmp\\foo.dll");
464de5f96e9SAlex Langford   FileSpec win_noext = WindowsSpec("C:\\tmp\\foo");
465de5f96e9SAlex Langford 
466de5f96e9SAlex Langford   EXPECT_TRUE(dll.GetFileNameExtension() == ".dll");
4676fcdfc37SAlex Langford   EXPECT_TRUE(win_noext.GetFileNameExtension() == llvm::StringRef());
468de5f96e9SAlex Langford }
469de5f96e9SAlex Langford 
TEST(FileSpecTest,TestFileNameStrippingExtension)470de5f96e9SAlex Langford TEST(FileSpecTest, TestFileNameStrippingExtension) {
471de5f96e9SAlex Langford   FileSpec dylib = PosixSpec("/tmp/foo.dylib");
472de5f96e9SAlex Langford   FileSpec exe = PosixSpec("/tmp/foo");
473de5f96e9SAlex Langford   FileSpec just_dot = PosixSpec("/tmp/bar.");
474de5f96e9SAlex Langford 
475de5f96e9SAlex Langford   EXPECT_TRUE(dylib.GetFileNameStrippingExtension() == "foo");
476de5f96e9SAlex Langford   EXPECT_TRUE(exe.GetFileNameStrippingExtension() == "foo");
477de5f96e9SAlex Langford   EXPECT_TRUE(just_dot.GetFileNameStrippingExtension() == "bar");
478de5f96e9SAlex Langford 
479de5f96e9SAlex Langford   FileSpec dll = WindowsSpec("C:\\tmp\\foo.dll");
480de5f96e9SAlex Langford   FileSpec win_noext = WindowsSpec("C:\\tmp\\foo");
481de5f96e9SAlex Langford 
482de5f96e9SAlex Langford   EXPECT_TRUE(dll.GetFileNameStrippingExtension() == "foo");
483de5f96e9SAlex Langford   EXPECT_TRUE(win_noext.GetFileNameStrippingExtension() == "foo");
484de5f96e9SAlex Langford }
485de5f96e9SAlex Langford 
TEST(FileSpecTest,TestIsSourceImplementationFile)486de5f96e9SAlex Langford TEST(FileSpecTest, TestIsSourceImplementationFile) {
487de5f96e9SAlex Langford   FileSpec c_src = PosixSpec("/tmp/foo.c");
488de5f96e9SAlex Langford   FileSpec txt_file = PosixSpec("/tmp/foo.txt");
489de5f96e9SAlex Langford   FileSpec executable = PosixSpec("/tmp/foo");
490de5f96e9SAlex Langford   FileSpec just_dot = PosixSpec("/tmp/bar.");
491de5f96e9SAlex Langford 
492de5f96e9SAlex Langford   EXPECT_TRUE(c_src.IsSourceImplementationFile());
493de5f96e9SAlex Langford   EXPECT_FALSE(txt_file.IsSourceImplementationFile());
494de5f96e9SAlex Langford   EXPECT_FALSE(executable.IsSourceImplementationFile());
495de5f96e9SAlex Langford   EXPECT_FALSE(just_dot.IsSourceImplementationFile());
496de5f96e9SAlex Langford 
497de5f96e9SAlex Langford   FileSpec cpp_src = WindowsSpec("C:\\tmp\\foo.cpp");
498de5f96e9SAlex Langford   FileSpec dll = WindowsSpec("C:\\tmp\\foo.dll");
499de5f96e9SAlex Langford   FileSpec win_noext = WindowsSpec("C:\\tmp\\foo");
500de5f96e9SAlex Langford   FileSpec exe = WindowsSpec("C:\\tmp\\foo.exe");
501de5f96e9SAlex Langford 
502de5f96e9SAlex Langford   EXPECT_TRUE(cpp_src.IsSourceImplementationFile());
503de5f96e9SAlex Langford   EXPECT_FALSE(dll.IsSourceImplementationFile());
504de5f96e9SAlex Langford   EXPECT_FALSE(win_noext.IsSourceImplementationFile());
505de5f96e9SAlex Langford   EXPECT_FALSE(exe.IsSourceImplementationFile());
506de5f96e9SAlex Langford }
507*57154a63SAlex Langford 
TEST(FileSpecTest,TestGetComponents)508*57154a63SAlex Langford TEST(FileSpecTest, TestGetComponents) {
509*57154a63SAlex Langford   std::pair<llvm::StringRef, std::vector<llvm::StringRef>> PosixTests[] = {
510*57154a63SAlex Langford       {"/", {}},
511*57154a63SAlex Langford       {"/foo", {"foo"}},
512*57154a63SAlex Langford       {"/foo/", {"foo"}},
513*57154a63SAlex Langford       {"/foo/bar", {"foo", "bar"}},
514*57154a63SAlex Langford       {"/llvm-project/lldb/unittests/Utility/FileSpecTest.cpp",
515*57154a63SAlex Langford        {"llvm-project", "lldb", "unittests", "Utility", "FileSpecTest.cpp"}},
516*57154a63SAlex Langford   };
517*57154a63SAlex Langford 
518*57154a63SAlex Langford   for (const auto &pair : PosixTests) {
519*57154a63SAlex Langford     FileSpec file_spec = PosixSpec(pair.first);
520*57154a63SAlex Langford     EXPECT_EQ(file_spec.GetComponents(), pair.second);
521*57154a63SAlex Langford   }
522*57154a63SAlex Langford 
523*57154a63SAlex Langford   std::pair<llvm::StringRef, std::vector<llvm::StringRef>> WindowsTests[] = {
524*57154a63SAlex Langford       {"C:\\", {"C:"}},
525*57154a63SAlex Langford       {"C:\\Windows\\", {"C:", "Windows"}},
526*57154a63SAlex Langford       {"C:\\Windows\\System32", {"C:", "Windows", "System32"}},
527*57154a63SAlex Langford       {"C:\\llvm-project\\lldb\\unittests\\Utility\\FileSpecTest.cpp",
528*57154a63SAlex Langford        {"C:", "llvm-project", "lldb", "unittests", "Utility",
529*57154a63SAlex Langford         "FileSpecTest.cpp"}},
530*57154a63SAlex Langford   };
531*57154a63SAlex Langford 
532*57154a63SAlex Langford   for (const auto &pair : WindowsTests) {
533*57154a63SAlex Langford     FileSpec file_spec = WindowsSpec(pair.first);
534*57154a63SAlex Langford     EXPECT_EQ(file_spec.GetComponents(), pair.second);
535*57154a63SAlex Langford   }
536*57154a63SAlex Langford }
537