1 //===-- FindFileTest.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 "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"
22 using namespace llvm::sys::fs
;
23 using namespace lldb_private
;
28 llvm::StringRef remapped
;
29 Matches(const char *o
, const char *r
) : original(o
), remapped(r
) {}
30 Matches(const char *o
, llvm::sys::path::Style style
, const char *r
)
31 : original(o
, style
), remapped(r
) {}
34 class FindFileTest
: public testing::Test
{
36 void SetUp() override
{
37 FileSystem::Initialize();
38 HostInfo::Initialize();
40 void TearDown() override
{
41 HostInfo::Terminate();
42 FileSystem::Terminate();
47 static void TestFileFindings(const PathMappingList
&map
,
48 llvm::ArrayRef
<Matches
> matches
,
49 llvm::ArrayRef
<FileSpec
> fails
) {
50 for (const auto &fail
: fails
) {
51 SCOPED_TRACE(fail
.GetPath().c_str());
52 EXPECT_FALSE(map
.FindFile(fail
));
55 for (const auto &match
: matches
) {
56 SCOPED_TRACE(match
.original
.GetPath() + " -> " + match
.remapped
);
57 std::optional
<FileSpec
> remapped
;
59 EXPECT_TRUE(bool(remapped
= map
.FindFile(match
.original
)));
60 EXPECT_TRUE(FileSpec(*remapped
).GetPath() ==
61 ConstString(match
.remapped
).GetStringRef());
65 TEST_F(FindFileTest
, FindFileTests
) {
66 const auto *Info
= testing::UnitTest::GetInstance()->current_test_info();
67 llvm::SmallString
<128> DirName
, FileName
;
70 ASSERT_NO_ERROR(createUniqueDirectory(Info
->name(), DirName
));
72 sys::path::append(FileName
, Twine(DirName
), Twine("test"));
73 ASSERT_NO_ERROR(openFile(FileName
, fd
, CD_CreateAlways
, FA_Read
, OF_None
));
75 llvm::FileRemover
dir_remover(DirName
);
76 llvm::FileRemover
file_remover(FileName
);
79 map
.Append("/old", DirName
.str(), false);
80 map
.Append(R
"(C:\foo)", DirName
.str(), false);
83 {"/old", llvm::sys::path::Style::posix
, DirName
.c_str()},
84 {"/old/test", llvm::sys::path::Style::posix
, FileName
.c_str()},
85 {R
"(C:\foo)", llvm::sys::path::Style::windows
, DirName
.c_str()},
86 {R
"(C:\foo\test)", llvm::sys::path::Style::windows
, FileName
.c_str()}};
88 std::vector
<FileSpec
> fails
{
90 FileSpec("/foo", llvm::sys::path::Style::posix
),
91 FileSpec("/new", llvm::sys::path::Style::posix
),
92 FileSpec(R
"(C:\new)", llvm::sys::path::Style::windows
),
93 // path mapped, but file not exist
94 FileSpec("/old/test1", llvm::sys::path::Style::posix
),
95 FileSpec(R
"(C:\foo\test2)", llvm::sys::path::Style::windows
)};
97 TestFileFindings(map
, matches
, fails
);