[unittests] Add a helper function for getting an input file

Summary:
Fetching an input file required about five lines of code, and this was
repeated in multiple unit tests, with slight variations. Add a helper
function for doing that into the lldbUtilityMocks module (which I rename
to lldbUtilityHelpers to commemorate the fact it includes more than
mocks)

Reviewers: zturner, eugene

Subscribers: emaste, mgorny, lldb-commits

Differential Revision: https://reviews.llvm.org/D34683

llvm-svn: 306668
This commit is contained in:
Pavel Labath 2017-06-29 13:02:11 +00:00
parent 476f855ec8
commit 7ed3e22f00
22 changed files with 89 additions and 100 deletions

View File

@ -4,7 +4,7 @@ add_lldb_unittest(InterpreterTests
LINK_LIBS
lldbInterpreter
lldbUtilityMocks
lldbUtilityHelpers
)
target_link_libraries(InterpreterTests

View File

@ -12,13 +12,12 @@
#include "lldb/Utility/StringList.h"
#include "lldb/Utility/TildeExpressionResolver.h"
#include "unittests/Utility/Helpers/MockTildeExpressionResolver.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "unittests/Utility/Mocks/MockTildeExpressionResolver.h"
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;
using namespace llvm;

View File

@ -6,6 +6,7 @@ add_lldb_unittest(ObjectFileELFTests
lldbPluginObjectFileELF
lldbPluginSymbolVendorELF
lldbCore
lldbUtilityHelpers
)
add_dependencies(ObjectFileELFTests yaml2obj)

View File

@ -9,20 +9,18 @@
//===----------------------------------------------------------------------===//
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
#include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/Section.h"
#include "lldb/Host/HostInfo.h"
#include "unittests/Utility/Helpers/TestUtilities.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
extern const char *TestMainArgv0;
using namespace lldb_private;
using namespace lldb;
@ -32,10 +30,6 @@ public:
HostInfo::Initialize();
ObjectFileELF::Initialize();
SymbolVendorELF::Initialize();
m_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0);
llvm::sys::path::append(m_inputs_folder, "Inputs");
llvm::sys::fs::make_absolute(m_inputs_folder);
}
void TearDown() override {
@ -45,7 +39,6 @@ public:
}
protected:
llvm::SmallString<128> m_inputs_folder;
};
#define ASSERT_NO_ERROR(x) \
@ -60,9 +53,8 @@ protected:
}
TEST_F(ObjectFileELFTest, SectionsResolveConsistently) {
llvm::SmallString<128> yaml = m_inputs_folder;
llvm::sys::path::append(yaml, "sections-resolve-consistently.yaml");
llvm::SmallString<128> obj = m_inputs_folder;
std::string yaml = GetInputFilePath("sections-resolve-consistently.yaml");
llvm::SmallString<128> obj;
ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
"sections-resolve-consistently-%%%%%%", "obj", obj));

View File

@ -7,6 +7,7 @@ add_lldb_unittest(LLDBMinidumpTests
lldbTarget
lldbPluginProcessUtility
lldbPluginProcessMinidump
lldbUtilityHelpers
LINK_COMPONENTS
Support
)

View File

@ -23,7 +23,7 @@
#include "lldb/Utility/DataBufferLLVM.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/FileSpec.h"
#include "unittests/Utility/Helpers/TestUtilities.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Support/FileSystem.h"
@ -35,24 +35,14 @@
// C++ includes
#include <memory>
extern const char *TestMainArgv0;
using namespace lldb_private;
using namespace minidump;
class MinidumpParserTest : public testing::Test {
public:
void SetUp() override {
llvm::StringRef dmp_folder = llvm::sys::path::parent_path(TestMainArgv0);
inputs_folder = dmp_folder;
llvm::sys::path::append(inputs_folder, "Inputs");
}
void SetUpData(const char *minidump_filename,
uint64_t load_size = UINT64_MAX) {
llvm::SmallString<128> filename = inputs_folder;
llvm::sys::path::append(filename, minidump_filename);
std::string filename = GetInputFilePath(minidump_filename);
auto BufferPtr = DataBufferLLVM::CreateSliceFromPath(filename, load_size, 0);
llvm::Optional<MinidumpParser> optional_parser =
@ -62,7 +52,6 @@ public:
ASSERT_GT(parser->GetData().size(), 0UL);
}
llvm::SmallString<128> inputs_folder;
std::unique_ptr<MinidumpParser> parser;
};

View File

@ -6,6 +6,7 @@ add_lldb_unittest(SymbolTests
LINK_LIBS
lldbHost
lldbSymbol
lldbUtilityHelpers
)
add_dependencies(SymbolTests yaml2obj)

View File

@ -16,14 +16,13 @@
#include "lldb/Host/HostInfo.h"
#include "lldb/Symbol/DWARFCallFrameInfo.h"
#include "lldb/Utility/StreamString.h"
#include "unittests/Utility/Helpers/TestUtilities.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
extern const char *TestMainArgv0;
using namespace lldb_private;
using namespace lldb;
@ -32,10 +31,6 @@ public:
void SetUp() override {
HostInfo::Initialize();
ObjectFileELF::Initialize();
m_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0);
llvm::sys::path::append(m_inputs_folder, "Inputs");
llvm::sys::fs::make_absolute(m_inputs_folder);
}
void TearDown() override {
@ -44,8 +39,6 @@ public:
}
protected:
llvm::SmallString<128> m_inputs_folder;
void TestBasic(DWARFCallFrameInfo::Type type, llvm::StringRef symbol);
};
@ -96,9 +89,8 @@ static UnwindPlan::Row GetExpectedRow2() {
void DWARFCallFrameInfoTest::TestBasic(DWARFCallFrameInfo::Type type,
llvm::StringRef symbol) {
llvm::SmallString<128> yaml = m_inputs_folder;
llvm::sys::path::append(yaml, "basic-call-frame-info.yaml");
llvm::SmallString<128> obj = m_inputs_folder;
std::string yaml = GetInputFilePath("basic-call-frame-info.yaml");
llvm::SmallString<128> obj;
ASSERT_NO_ERROR(llvm::sys::fs::createTemporaryFile(
"basic-call-frame-info-%%%%%%", "obj", obj));

View File

@ -8,6 +8,7 @@ add_lldb_unittest(SymbolFileDWARFTests
lldbPluginObjectFilePECOFF
lldbPluginSymbolFileDWARF
lldbPluginSymbolFilePDB
lldbUtilityHelpers
LINK_COMPONENTS
Support
DebugInfoPDB

View File

@ -15,6 +15,9 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
#include "lldb/Core/Address.h"
#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/Module.h"
@ -25,12 +28,7 @@
#include "lldb/Symbol/LineTable.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Utility/FileSpec.h"
#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
extern const char *TestMainArgv0;
#include "unittests/Utility/Helpers/TestUtilities.h"
using namespace lldb_private;
@ -46,12 +44,7 @@ public:
ClangASTContext::Initialize();
SymbolFilePDB::Initialize();
llvm::StringRef exe_folder = llvm::sys::path::parent_path(TestMainArgv0);
llvm::SmallString<128> inputs_folder = exe_folder;
llvm::sys::path::append(inputs_folder, "Inputs");
m_dwarf_test_exe = inputs_folder;
llvm::sys::path::append(m_dwarf_test_exe, "test-dwarf.exe");
m_dwarf_test_exe = GetInputFilePath("test-dwarf.exe");
}
void TearDown() override {
@ -63,12 +56,12 @@ public:
}
protected:
llvm::SmallString<128> m_dwarf_test_exe;
std::string m_dwarf_test_exe;
};
TEST_F(SymbolFileDWARFTests, TestAbilitiesForDWARF) {
// Test that when we have Dwarf debug info, SymbolFileDWARF is used.
FileSpec fspec(m_dwarf_test_exe.c_str(), false);
FileSpec fspec(m_dwarf_test_exe, false);
ArchSpec aspec("i686-pc-windows");
lldb::ModuleSP module = std::make_shared<Module>(fspec, aspec);

View File

@ -8,6 +8,7 @@ add_lldb_unittest(SymbolFilePDBTests
lldbPluginObjectFilePECOFF
lldbPluginSymbolFileDWARF
lldbPluginSymbolFilePDB
lldbUtiltyHelpers
LINK_COMPONENTS
Support
DebugInfoPDB

View File

@ -15,6 +15,9 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
#include "lldb/Core/Address.h"
#include "lldb/Core/ArchSpec.h"
#include "lldb/Core/Module.h"
@ -25,10 +28,7 @@
#include "lldb/Symbol/LineTable.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Utility/FileSpec.h"
#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "Plugins/SymbolFile/PDB/SymbolFilePDB.h"
#include "unittests/Utility/Helpers/TestUtilities.h"
#if defined(_MSC_VER)
#include "lldb/Host/windows/windows.h"
@ -37,8 +37,6 @@
#include <algorithm>
extern const char *TestMainArgv0;
using namespace lldb_private;
class SymbolFilePDBTests : public testing::Test {
@ -61,10 +59,8 @@ public:
llvm::SmallString<128> inputs_folder = exe_folder;
llvm::sys::path::append(inputs_folder, "Inputs");
m_pdb_test_exe = inputs_folder;
m_types_test_exe = inputs_folder;
llvm::sys::path::append(m_pdb_test_exe, "test-pdb.exe");
llvm::sys::path::append(m_types_test_exe, "test-pdb-types.exe");
m_pdb_test_exe = GetInputFile("test-pdb.exe");
m_types_test_exe = GetInputFile("test-pdb-types.exe");
}
void TearDown() override {
@ -80,8 +76,8 @@ public:
}
protected:
llvm::SmallString<128> m_pdb_test_exe;
llvm::SmallString<128> m_types_test_exe;
std::string m_pdb_test_exe;
std::string m_types_test_exe;
bool FileSpecMatchesAsBaseOrFull(const FileSpec &left,
const FileSpec &right) const {

View File

@ -8,6 +8,7 @@ add_lldb_unittest(TargetTests
lldbSymbol
lldbUtility
lldbPluginObjectFileELF
lldbUtilityHelpers
LINK_COMPONENTS
Support
)

View File

@ -10,8 +10,7 @@
#include "lldb/Host/HostInfo.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Target/ModuleCache.h"
extern const char *TestMainArgv0;
#include "unittests/Utility/Helpers/TestUtilities.h"
using namespace lldb_private;
using namespace lldb;
@ -26,7 +25,7 @@ public:
protected:
static FileSpec s_cache_dir;
static llvm::SmallString<128> s_test_executable;
static std::string s_test_executable;
void TryGetAndPut(const FileSpec &cache_dir, const char *hostname,
bool expect_download);
@ -34,7 +33,7 @@ protected:
}
FileSpec ModuleCacheTest::s_cache_dir;
llvm::SmallString<128> ModuleCacheTest::s_test_executable;
std::string ModuleCacheTest::s_test_executable;
static const char dummy_hostname[] = "dummy_hostname";
static const char dummy_remote_dir[] = "bin";
@ -71,10 +70,7 @@ void ModuleCacheTest::SetUpTestCase() {
FileSpec tmpdir_spec;
HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, s_cache_dir);
llvm::StringRef exe_folder = llvm::sys::path::parent_path(TestMainArgv0);
s_test_executable = exe_folder;
llvm::sys::path::append(s_test_executable, "Inputs", module_name);
s_test_executable = GetInputFilePath(module_name);
}
void ModuleCacheTest::TearDownTestCase() {

View File

@ -1,4 +1,4 @@
add_subdirectory(Mocks)
add_subdirectory(Helpers)
add_lldb_unittest(UtilityTests
ConstStringTest.cpp
@ -15,7 +15,7 @@ add_lldb_unittest(UtilityTests
LINK_LIBS
lldbUtility
lldbUtilityMocks
lldbUtilityHelpers
LINK_COMPONENTS
Support
)

View File

@ -1,5 +1,7 @@
add_lldb_library(lldbUtilityMocks
set(EXCLUDE_FROM_ALL ON)
add_lldb_library(lldbUtilityHelpers
MockTildeExpressionResolver.cpp
TestUtilities.cpp
LINK_LIBS
lldbUtility

View File

@ -32,6 +32,6 @@ public:
llvm::SmallVectorImpl<char> &Output) override;
bool ResolvePartial(llvm::StringRef Expr, llvm::StringSet<> &Output) override;
};
}
} // namespace lldb_private
#endif

View File

@ -0,0 +1,22 @@
//===- TestUtilities.cpp ----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "TestUtilities.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
extern const char *TestMainArgv0;
std::string lldb_private::GetInputFilePath(const llvm::Twine &name) {
llvm::SmallString<128> result = llvm::sys::path::parent_path(TestMainArgv0);
llvm::sys::fs::make_absolute(result);
llvm::sys::path::append(result, "Inputs", name);
return result.str();
}

View File

@ -0,0 +1,20 @@
//===- TestUtilities.h ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_UNITTESTS_UTILITY_HELPERS_TESTUTILITIES_H
#define LLDB_UNITTESTS_UTILITY_HELPERS_TESTUTILITIES_H
#include "llvm/ADT/Twine.h"
#include <string>
namespace lldb_private {
std::string GetInputFilePath(const llvm::Twine &name);
}
#endif

View File

@ -9,33 +9,16 @@
#include "gtest/gtest.h"
#include "Helpers/TestUtilities.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StructuredData.h"
#include "llvm/Support/Path.h"
extern const char *TestMainArgv0;
using namespace lldb;
using namespace lldb_private;
namespace {
class StructuredDataTest : public testing::Test {
public:
static void SetUpTestCase() {
s_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0);
llvm::sys::path::append(s_inputs_folder, "Inputs");
}
protected:
static llvm::SmallString<128> s_inputs_folder;
};
} // namespace
llvm::SmallString<128> StructuredDataTest::s_inputs_folder;
TEST_F(StructuredDataTest, StringDump) {
TEST(StructuredDataTest, StringDump) {
std::pair<llvm::StringRef, llvm::StringRef> TestCases[] = {
{R"(asdfg)", R"("asdfg")"},
{R"(as"df)", R"("as\"df")"},
@ -49,14 +32,13 @@ TEST_F(StructuredDataTest, StringDump) {
}
}
TEST_F(StructuredDataTest, ParseJSONFromFile) {
TEST(StructuredDataTest, ParseJSONFromFile) {
Status status;
auto object_sp = StructuredData::ParseJSONFromFile(
FileSpec("non-existing-file.json", false), status);
EXPECT_EQ(nullptr, object_sp);
llvm::SmallString<128> input = s_inputs_folder;
llvm::sys::path::append(input, "StructuredData-basic.json");
std::string input = GetInputFilePath("StructuredData-basic.json");
object_sp = StructuredData::ParseJSONFromFile(FileSpec(input, false), status);
ASSERT_NE(nullptr, object_sp);

View File

@ -1,6 +1,6 @@
#include "gtest/gtest.h"
#include "Mocks/MockTildeExpressionResolver.h"
#include "Helpers/MockTildeExpressionResolver.h"
#include "lldb/Utility/TildeExpressionResolver.h"
#include "llvm/ADT/SmallString.h"