From 65dd8e9b56f6a1c11ed98614b888c98adf967623 Mon Sep 17 00:00:00 2001 From: Olivier Date: Sun, 4 May 2014 22:04:54 +0200 Subject: [PATCH] Add OutputDirectoryOption in command line --- CppCoverage/Options.cpp | 13 +++++++++++ CppCoverage/Options.hpp | 8 +++++++ CppCoverage/OptionsParser.cpp | 7 ++++++ CppCoverage/OptionsParser.hpp | 1 + CppCoverageTest/OptionsParserTests.cpp | 16 ++++++++++++++ OpenCppCoverage/OpenCppCoverage.cpp | 9 ++++++-- .../OpenCppCoverageConsoleTest.cpp | 22 ++++++++++++++----- 7 files changed, 69 insertions(+), 7 deletions(-) diff --git a/CppCoverage/Options.cpp b/CppCoverage/Options.cpp index 208bef07..9b8bb048 100644 --- a/CppCoverage/Options.cpp +++ b/CppCoverage/Options.cpp @@ -47,6 +47,19 @@ namespace CppCoverage return verboseModeSelected_; } + //------------------------------------------------------------------------- + void Options::SetOutputDirectoryOption(const boost::filesystem::path& outputDirectoryOption) + { + outputDirectoryOption_ = outputDirectoryOption; + } + + //------------------------------------------------------------------------- + const boost::optional& + Options::GetOutputDirectoryOption() const + { + return outputDirectoryOption_; + } + //------------------------------------------------------------------------- std::wostream& operator<<(std::wostream& ostr, const Options& options) { diff --git a/CppCoverage/Options.hpp b/CppCoverage/Options.hpp index 3c030dec..91b78ae8 100644 --- a/CppCoverage/Options.hpp +++ b/CppCoverage/Options.hpp @@ -1,5 +1,8 @@ #pragma once +#include +#include + #include "CppCoverageExport.hpp" #include "CppCoverage/Patterns.hpp" #include "CppCoverage/StartInfo.hpp" @@ -25,6 +28,10 @@ namespace CppCoverage void SetVerboseModeSelected(); bool IsVerboseModeSelected() const; + void SetOutputDirectoryOption(const boost::filesystem::path&); + const boost::optional& + GetOutputDirectoryOption() const; + friend CPPCOVERAGE_DLL std::wostream& operator<<(std::wostream&, const Options&); private: @@ -36,5 +43,6 @@ namespace CppCoverage CppCoverage::Patterns modules_; CppCoverage::Patterns sources_; bool verboseModeSelected_; + boost::optional outputDirectoryOption_; }; } \ No newline at end of file diff --git a/CppCoverage/OptionsParser.cpp b/CppCoverage/OptionsParser.cpp index cc3fecaf..eed3d4bd 100644 --- a/CppCoverage/OptionsParser.cpp +++ b/CppCoverage/OptionsParser.cpp @@ -118,6 +118,7 @@ namespace CppCoverage const std::string OptionsParser::HelpOption = "help"; const std::string OptionsParser::HelpShortOption = "h"; const std::string OptionsParser::WorkingDirectoryOption = "working_dir"; + const std::string OptionsParser::OutputDirectoryOption = "output_dir"; using T_Strings = std::vector; @@ -141,6 +142,7 @@ namespace CppCoverage po::value(), "The pattern that source's paths should NOT match.") (WorkingDirectoryOption.c_str(), po::value(), "The program working directory.") + (OutputDirectoryOption.c_str(), po::value(), "The coverage report directory.") ((VerboseOption + "," + VerboseShortOption).c_str(), "Show verbose log") ((HelpOption + "," + HelpShortOption).c_str(), "Show help message"); } @@ -187,6 +189,11 @@ namespace CppCoverage if (IsOptionSelected(variables, VerboseOption)) options.SetVerboseModeSelected(); + const auto* outputDirectoryOption = GetOptionalValue(variables, OptionsParser::OutputDirectoryOption); + + if (outputDirectoryOption) + options.SetOutputDirectoryOption(*outputDirectoryOption); + return options; } diff --git a/CppCoverage/OptionsParser.hpp b/CppCoverage/OptionsParser.hpp index 7098cc4f..ddd6ae98 100644 --- a/CppCoverage/OptionsParser.hpp +++ b/CppCoverage/OptionsParser.hpp @@ -23,6 +23,7 @@ namespace CppCoverage static const std::string HelpOption; static const std::string HelpShortOption; static const std::string WorkingDirectoryOption; + static const std::string OutputDirectoryOption; public: OptionsParser(); diff --git a/CppCoverageTest/OptionsParserTests.cpp b/CppCoverageTest/OptionsParserTests.cpp index 5bc158e2..d72e4968 100644 --- a/CppCoverageTest/OptionsParserTests.cpp +++ b/CppCoverageTest/OptionsParserTests.cpp @@ -140,6 +140,22 @@ namespace CppCoverageTest ASSERT_EQ(Tools::ToWString(folder), *workingDirectory); } + //------------------------------------------------------------------------- + TEST(OptionsParserTest, OutputDirectory) + { + cov::OptionsParser parser; + const std::string folder = "Output"; + + auto options = Parse(parser, + { optionPrefix + cov::OptionsParser::OutputDirectoryOption, folder }); + ASSERT_TRUE(options); + + auto outputDirectoryOption = options->GetOutputDirectoryOption(); + + ASSERT_TRUE(outputDirectoryOption); + ASSERT_EQ(folder, outputDirectoryOption->string()); + } + //------------------------------------------------------------------------- TEST(OptionsParserTest, Program) { diff --git a/OpenCppCoverage/OpenCppCoverage.cpp b/OpenCppCoverage/OpenCppCoverage.cpp index ef7a9c67..2b368a24 100644 --- a/OpenCppCoverage/OpenCppCoverage.cpp +++ b/OpenCppCoverage/OpenCppCoverage.cpp @@ -57,8 +57,13 @@ namespace OpenCppCoverage } //----------------------------------------------------------------------------- - fs::path GetOutputPath() + fs::path GetOutputPath(const cov::Options& options) { + auto outputDirectoryOption = options.GetOutputDirectoryOption(); + + if (outputDirectoryOption) + return *outputDirectoryOption; + auto now = std::time(nullptr); auto localNow = std::localtime(&now); std::ostringstream ostr; @@ -90,7 +95,7 @@ namespace OpenCppCoverage auto now = std::time(nullptr); auto localNow = std::localtime(&now); - boost::filesystem::path output = GetOutputPath(); + boost::filesystem::path output = GetOutputPath(options); htmlExporter.Export(coverage, output); return coverage.GetExitCode(); } diff --git a/OpenCppCoverageTest/OpenCppCoverageConsoleTest.cpp b/OpenCppCoverageTest/OpenCppCoverageConsoleTest.cpp index 9996dad3..9fdf509e 100644 --- a/OpenCppCoverageTest/OpenCppCoverageConsoleTest.cpp +++ b/OpenCppCoverageTest/OpenCppCoverageConsoleTest.cpp @@ -10,8 +10,9 @@ #include "OpenCppCoverage/OpenCppCoverage.hpp" #include "CppCoverage/OptionsParser.hpp" -#include "tools/ScopedAction.hpp" +#include "Tools/ScopedAction.hpp" #include "Tools/Tool.hpp" +#include "Tools/TemporaryFolder.hpp" namespace fs = boost::filesystem; namespace cov = CppCoverage; @@ -20,11 +21,18 @@ namespace OpenCppCoverageTest { namespace { + //--------------------------------------------------------------------- + void CheckOutputDirectory(const Tools::TemporaryFolder& tempFolder) + { + ASSERT_FALSE(fs::is_empty(tempFolder.GetPath())); + } + //--------------------------------------------------------------------- int RunCoverageOnProgram( const fs::path& programToRun, const std::vector& arguments) { + Tools::TemporaryFolder tempFolder; fs::path openCppCoverage = OpenCppCoverage::GetOutputBinaryPath(); std::vector coverageArguments{ @@ -32,19 +40,23 @@ namespace OpenCppCoverageTest programToRun.string(), "--" + cov::OptionsParser::SelectedSourcesOption, SOLUTION_DIR, + "--" + cov::OptionsParser::OutputDirectoryOption, + tempFolder.GetPath().string(), programToRun.string() }; for (const auto& argument : arguments) coverageArguments.push_back(Tools::ToString(argument)); - auto handle = Poco::Process::launch(openCppCoverage.string(), coverageArguments, ".", nullptr, nullptr, nullptr); - - return handle.wait(); + auto handle = Poco::Process::launch(openCppCoverage.string(), coverageArguments, ".", nullptr, nullptr, nullptr); + int exitCode = handle.wait(); + + CheckOutputDirectory(tempFolder); + return exitCode; } } //------------------------------------------------------------------------- - TEST(OpenCppCoverageConsoleTest, Basic) // $$ use temp folder + TEST(OpenCppCoverageConsoleTest, Basic) { fs::path testCoverageConsole = TestCoverageConsole::GetOutputBinaryPath();