1 //===-- FindFileTest.cpp -------------------------------------------------===//
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 "TestingSupport/TestUtilities.h"
10 #include "lldb/Host/FileSystem.h"
11 #include "lldb/Host/HostInfo.h"
12 #include "lldb/Target/PathMappingList.h"
13 #include "lldb/Utility/FileSpec.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/FileUtilities.h"
17 #include "gtest/gtest.h"
18 #include <utility>
19 
20 using namespace llvm;
21 using namespace llvm::sys::fs;
22 using namespace lldb_private;
23 
24 namespace {
25 struct Matches {
26   FileSpec original;
27   llvm::StringRef remapped;
Matches__anoncb2dd9360111::Matches28   Matches(const char *o, const char *r) : original(o), remapped(r) {}
Matches__anoncb2dd9360111::Matches29   Matches(const char *o, llvm::sys::path::Style style, const char *r)
30       : original(o, style), remapped(r) {}
31 };
32 
33 class FindFileTest : public testing::Test {
34 public:
SetUp()35   void SetUp() override {
36     FileSystem::Initialize();
37     HostInfo::Initialize();
38   }
TearDown()39   void TearDown() override {
40     HostInfo::Terminate();
41     FileSystem::Terminate();
42   }
43 };
44 } // namespace
45 
TestFileFindings(const PathMappingList & map,llvm::ArrayRef<Matches> matches,llvm::ArrayRef<FileSpec> fails)46 static void TestFileFindings(const PathMappingList &map,
47                              llvm::ArrayRef<Matches> matches,
48                              llvm::ArrayRef<FileSpec> fails) {
49   for (const auto &fail : fails) {
50     SCOPED_TRACE(fail.GetCString());
51     EXPECT_FALSE(map.FindFile(fail));
52   }
53 
54   for (const auto &match : matches) {
55     SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped);
56     llvm::Optional<FileSpec> remapped;
57 
58     EXPECT_TRUE(bool(remapped = map.FindFile(match.original)));
59     EXPECT_TRUE(FileSpec(remapped.value()).GetPath() ==
60                 ConstString(match.remapped).GetStringRef());
61   }
62 }
63 
TEST_F(FindFileTest,FindFileTests)64 TEST_F(FindFileTest, FindFileTests) {
65   const auto *Info = testing::UnitTest::GetInstance()->current_test_info();
66   llvm::SmallString<128> DirName, FileName;
67   int fd;
68 
69   ASSERT_NO_ERROR(createUniqueDirectory(Info->name(), DirName));
70 
71   sys::path::append(FileName, Twine(DirName), Twine("test"));
72   ASSERT_NO_ERROR(openFile(FileName, fd, CD_CreateAlways, FA_Read, OF_None));
73 
74   llvm::FileRemover dir_remover(DirName);
75   llvm::FileRemover file_remover(FileName);
76   PathMappingList map;
77 
78   map.Append("/old", DirName.str(), false);
79   map.Append(R"(C:\foo)", DirName.str(), false);
80 
81   Matches matches[] = {
82       {"/old", llvm::sys::path::Style::posix, DirName.c_str()},
83       {"/old/test", llvm::sys::path::Style::posix, FileName.c_str()},
84       {R"(C:\foo)", llvm::sys::path::Style::windows, DirName.c_str()},
85       {R"(C:\foo\test)", llvm::sys::path::Style::windows, FileName.c_str()}};
86 
87   std::vector<FileSpec> fails{
88       // path not mapped
89       FileSpec("/foo", llvm::sys::path::Style::posix),
90       FileSpec("/new", llvm::sys::path::Style::posix),
91       FileSpec(R"(C:\new)", llvm::sys::path::Style::windows),
92       // path mapped, but file not exist
93       FileSpec("/old/test1", llvm::sys::path::Style::posix),
94       FileSpec(R"(C:\foo\test2)", llvm::sys::path::Style::windows)};
95 
96   TestFileFindings(map, matches, fails);
97 }
98