Skip to content

Commit

Permalink
Add OutputDirectoryOption in command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier authored and Olivier committed May 4, 2014
1 parent 5308470 commit 65dd8e9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
13 changes: 13 additions & 0 deletions CppCoverage/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ namespace CppCoverage
return verboseModeSelected_;
}

//-------------------------------------------------------------------------
void Options::SetOutputDirectoryOption(const boost::filesystem::path& outputDirectoryOption)
{
outputDirectoryOption_ = outputDirectoryOption;
}

//-------------------------------------------------------------------------
const boost::optional<boost::filesystem::path>&
Options::GetOutputDirectoryOption() const
{
return outputDirectoryOption_;
}

//-------------------------------------------------------------------------
std::wostream& operator<<(std::wostream& ostr, const Options& options)
{
Expand Down
8 changes: 8 additions & 0 deletions CppCoverage/Options.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <boost/optional.hpp>
#include <boost/filesystem.hpp>

#include "CppCoverageExport.hpp"
#include "CppCoverage/Patterns.hpp"
#include "CppCoverage/StartInfo.hpp"
Expand All @@ -25,6 +28,10 @@ namespace CppCoverage
void SetVerboseModeSelected();
bool IsVerboseModeSelected() const;

void SetOutputDirectoryOption(const boost::filesystem::path&);
const boost::optional<boost::filesystem::path>&
GetOutputDirectoryOption() const;

friend CPPCOVERAGE_DLL std::wostream& operator<<(std::wostream&, const Options&);

private:
Expand All @@ -36,5 +43,6 @@ namespace CppCoverage
CppCoverage::Patterns modules_;
CppCoverage::Patterns sources_;
bool verboseModeSelected_;
boost::optional<boost::filesystem::path> outputDirectoryOption_;
};
}
7 changes: 7 additions & 0 deletions CppCoverage/OptionsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>;

Expand All @@ -141,6 +142,7 @@ namespace CppCoverage
po::value<T_Strings>(),
"The pattern that source's paths should NOT match.")
(WorkingDirectoryOption.c_str(), po::value<std::string>(), "The program working directory.")
(OutputDirectoryOption.c_str(), po::value<std::string>(), "The coverage report directory.")
((VerboseOption + "," + VerboseShortOption).c_str(), "Show verbose log")
((HelpOption + "," + HelpShortOption).c_str(), "Show help message");
}
Expand Down Expand Up @@ -187,6 +189,11 @@ namespace CppCoverage
if (IsOptionSelected(variables, VerboseOption))
options.SetVerboseModeSelected();

const auto* outputDirectoryOption = GetOptionalValue<std::string>(variables, OptionsParser::OutputDirectoryOption);

if (outputDirectoryOption)
options.SetOutputDirectoryOption(*outputDirectoryOption);

return options;
}

Expand Down
1 change: 1 addition & 0 deletions CppCoverage/OptionsParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions CppCoverageTest/OptionsParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
9 changes: 7 additions & 2 deletions OpenCppCoverage/OpenCppCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
22 changes: 17 additions & 5 deletions OpenCppCoverageTest/OpenCppCoverageConsoleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,31 +21,42 @@ 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<std::wstring>& arguments)
{
Tools::TemporaryFolder tempFolder;
fs::path openCppCoverage = OpenCppCoverage::GetOutputBinaryPath();

std::vector<std::string> coverageArguments{
"--" + cov::OptionsParser::SelectedModulesOption,
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();

Expand Down

0 comments on commit 65dd8e9

Please sign in to comment.