Skip to content

Commit

Permalink
refactor(TestArgs): reuse public config args
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Oct 24, 2024
1 parent a1fb8ec commit 3a22947
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 79 deletions.
14 changes: 10 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,15 @@ if (MRDOCS_BUILD_TESTS)
include(CTest)
file(GLOB_RECURSE TEST_SUITE_FILES CONFIGURE_DEPENDS src/test_suite/*.cpp src/test_suite/*.hpp)
file(GLOB_RECURSE UNIT_TEST_SOURCES CONFIGURE_DEPENDS src/test/*.cpp src/test/*.hpp)
add_executable(mrdocs-test ${TEST_SUITE_FILES} ${UNIT_TEST_SOURCES})
add_executable(mrdocs-test ${TEST_SUITE_FILES} ${UNIT_TEST_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/src/tool/PublicToolArgs.cpp)
target_include_directories(mrdocs-test
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
PRIVATE
"${PROJECT_SOURCE_DIR}/include"
"${PROJECT_SOURCE_DIR}/src"
"${PROJECT_BINARY_DIR}/src"
)
target_link_libraries(mrdocs-test PUBLIC mrdocs-core)
if (MRDOCS_CLANG)
Expand All @@ -358,9 +362,10 @@ if (MRDOCS_BUILD_TESTS)
--action=test
"${PROJECT_SOURCE_DIR}/test-files/golden-tests"
"--addons=${CMAKE_SOURCE_DIR}/share/mrdocs/addons"
--generator=${testgenerator}
--generate=${testgenerator}
"--stdlib-includes=${LIBCXX_DIR}"
"--stdlib-includes=${STDLIB_INCLUDE_DIR}"
--report=2
)
foreach (action IN ITEMS test create update)
add_custom_target(
Expand All @@ -371,9 +376,10 @@ if (MRDOCS_BUILD_TESTS)
--action=${action}
"${PROJECT_SOURCE_DIR}/test-files/golden-tests"
"--addons=${CMAKE_SOURCE_DIR}/share/mrdocs/addons"
--generator=${testgenerator}
--generate=${testgenerator}
"--stdlib-includes=${LIBCXX_DIR}"
"--stdlib-includes=${STDLIB_INCLUDE_DIR}"
--report=2
DEPENDS mrdocs-test
)
endforeach ()
Expand Down
46 changes: 3 additions & 43 deletions src/test/TestArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
//

#include "TestArgs.hpp"
#include <fmt/format.h>
#include <cstddef>
#include <vector>

namespace clang {
Expand All @@ -22,12 +20,9 @@ TestArgs TestArgs::instance_;

TestArgs::
TestArgs()
: commonCat("COMMON")

, usageText(
R"(MrDocs Test Program
)")
: PublicToolArgs()

, usageText("MrDocs Test Program")
, extraHelp(
R"(
EXAMPLES:
Expand All @@ -36,29 +31,17 @@ R"(
mrdocs-test --action test friend.cpp
)")

//
// Common options
//

, reportLevel(
"report",
llvm::cl::desc("The minimum reporting level (0 to 4)."),
llvm::cl::init(2),
llvm::cl::cat(commonCat))

//
// Test options
//

, action(
"action",
llvm::cl::desc(R"(Which action should be performed:)"),
llvm::cl::init(test),
llvm::cl::values(
clEnumVal(test, "Compare output against expected."),
clEnumVal(create, "Create missing expected documentation files."),
clEnumVal(update, "Update all expected documentation files.")),
llvm::cl::cat(commonCat))
clEnumVal(update, "Update all expected documentation files.")))

, badOption(
"bad",
Expand All @@ -69,28 +52,6 @@ R"(
"unit",
llvm::cl::desc("Run all or selected unit test suites."),
llvm::cl::init(true))

, inputPaths(
"inputs",
llvm::cl::Sink,
llvm::cl::desc("A list of directories and/or .cpp files to test."),
llvm::cl::cat(commonCat))

, generator(
"generator",
llvm::cl::desc("The generator to use for tests."),
llvm::cl::init("xml"))

, addons(
"addons",
llvm::cl::desc("The directory with the addons."),
llvm::cl::cat(commonCat))

, stdlibIncludes(
"stdlib-includes",
llvm::cl::desc("A list of paths to std library headers."),
llvm::cl::cat(commonCat))

{
}

Expand All @@ -104,7 +65,6 @@ hideForeignOptions()

std::vector<llvm::cl::Option const*> ours({
&action,
std::addressof(inputPaths),
&badOption,
&unitOption
});
Expand Down
14 changes: 2 additions & 12 deletions src/test/TestArgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define MRDOCS_TEST_TESTARGS_HPP

#include <llvm/Support/CommandLine.h>
#include <tool/PublicToolArgs.hpp>
#include <string>

namespace clang {
Expand All @@ -26,31 +27,20 @@ enum Action : int

/** Command line options and test settings.
*/
class TestArgs
class TestArgs : public PublicToolArgs
{
TestArgs();

llvm::cl::OptionCategory commonCat;

public:
static TestArgs instance_;

char const* usageText;
llvm::cl::extrahelp extraHelp;

// Common options
llvm::cl::opt<unsigned> reportLevel;

// Test options
llvm::cl::opt<Action> action;
llvm::cl::opt<bool> badOption;
llvm::cl::opt<bool> unitOption;
llvm::cl::list<std::string> inputPaths;

// Options replication public settings
llvm::cl::opt<std::string> generator;
llvm::cl::opt<std::string> addons;
llvm::cl::list<std::string> stdlibIncludes;

// Hide all options that don't belong to us
void hideForeignOptions();
Expand Down
20 changes: 10 additions & 10 deletions src/test/TestMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
#include <llvm/Support/Signals.h>
#include <stdlib.h>

int main(int argc, char** argv);
int main(int argc, char const** argv);

namespace clang {
namespace mrdocs {

void DoTestAction()
void DoTestAction(char const** argv)
{
using namespace clang::mrdocs;

TestRunner runner(testArgs.generator);
for(auto const& inputPath : testArgs.inputPaths)
runner.checkPath(inputPath);
TestRunner runner(testArgs.generate);
for(auto const& inputPath : testArgs.inputs)
runner.checkPath(inputPath, argv);
auto const& results = runner.results;

std::stringstream os;
Expand Down Expand Up @@ -63,7 +63,7 @@ void DoTestAction()
report::print(os.str());
}

int test_main(int argc, char const* const* argv)
int test_main(int argc, char const** argv)
{
// VFALCO this heap checking is too strong for
// a clang tool's model of what is actually a leak.
Expand All @@ -80,10 +80,10 @@ int test_main(int argc, char const* const* argv)

// Apply reportLevel
report::setMinimumLevel(report::getLevel(
testArgs.reportLevel.getValue()));
testArgs.report.getValue()));

if(! testArgs.inputPaths.empty())
DoTestAction();
if(!testArgs.inputs.empty())
DoTestAction(argv);

if(testArgs.unitOption.getValue())
test_suite::unit_test_main(argc, argv);
Expand All @@ -106,7 +106,7 @@ static void reportUnhandledException(
} // mrdocs
} // clang

int main(int argc, char** argv)
int main(int argc, char const** argv)
{
try
{
Expand Down
12 changes: 3 additions & 9 deletions src/test/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ handleDir(
void
TestRunner::
checkPath(
std::string inputPath)
std::string inputPath,
char const** argv)
{
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;
Expand All @@ -274,14 +275,6 @@ checkPath(
// Set the reference directories for the test
dirs_.configDir = inputPath;
dirs_.cwd = dirs_.configDir;
if (testArgs.addons.getValue() != "")
{
dirs_.mrdocsRoot = files::getParentDir(files::normalizePath(testArgs.addons.getValue()), 3);
}
else
{
report::warn("No addons directory specified to mrdocs tests");
}

// See if inputPath references a file or directory
auto fileType = files::getFileType(inputPath);
Expand All @@ -291,6 +284,7 @@ checkPath(

// Check for a directory-wide config
Config::Settings dirSettings;
testArgs.apply(dirSettings, dirs_, argv);
dirSettings.sourceRoot = files::appendPath(inputPath, ".");
dirSettings.stdlibIncludes.insert(
dirSettings.stdlibIncludes.end(),
Expand Down
2 changes: 1 addition & 1 deletion src/test/TestRunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class TestRunner
This function checks the specified path
and blocks until completed.
*/
void checkPath(std::string inputPath);
void checkPath(std::string inputPath, char const** argv);
};

} // mrdocs
Expand Down

0 comments on commit 3a22947

Please sign in to comment.