99 lines · cpp
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-exception6//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 <optional>19#include <utility>20 21using namespace llvm;22using namespace llvm::sys::fs;23using namespace lldb_private;24 25namespace {26struct Matches {27 FileSpec original;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) {}32};33 34class FindFileTest : public testing::Test {35public:36 void SetUp() override {37 FileSystem::Initialize();38 HostInfo::Initialize();39 }40 void TearDown() override {41 HostInfo::Terminate();42 FileSystem::Terminate();43 }44};45} // namespace46 47static 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));53 }54 55 for (const auto &match : matches) {56 SCOPED_TRACE(match.original.GetPath() + " -> " + match.remapped);57 std::optional<FileSpec> remapped;58 59 EXPECT_TRUE(bool(remapped = map.FindFile(match.original)));60 EXPECT_TRUE(FileSpec(*remapped).GetPath() ==61 ConstString(match.remapped).GetStringRef());62 }63}64 65TEST_F(FindFileTest, FindFileTests) {66 const auto *Info = testing::UnitTest::GetInstance()->current_test_info();67 llvm::SmallString<128> DirName, FileName;68 int fd;69 70 ASSERT_NO_ERROR(createUniqueDirectory(Info->name(), DirName));71 72 sys::path::append(FileName, Twine(DirName), Twine("test"));73 ASSERT_NO_ERROR(openFile(FileName, fd, CD_CreateAlways, FA_Read, OF_None));74 75 llvm::FileRemover dir_remover(DirName);76 llvm::FileRemover file_remover(FileName);77 PathMappingList map;78 79 map.Append("/old", DirName.str(), false);80 map.Append(R"(C:\foo)", DirName.str(), false);81 82 Matches matches[] = {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()}};87 88 std::vector<FileSpec> fails{89 // path not mapped90 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 exist94 FileSpec("/old/test1", llvm::sys::path::Style::posix),95 FileSpec(R"(C:\foo\test2)", llvm::sys::path::Style::windows)};96 97 TestFileFindings(map, matches, fails);98}99