Skip to content

Commit

Permalink
Improve PatchReporter code
Browse files Browse the repository at this point in the history
  • Loading branch information
m42e authored and AlexDenisov committed Nov 9, 2021
1 parent b98c3e3 commit 9f2accc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
34 changes: 16 additions & 18 deletions lib/Reporters/PatchesReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ void mull::PatchesReporter::reportResults(const Result &result) {

const ExecutionResult mutationExecutionResult = mutationResult->getExecutionResult();

std::stringstream filenamebuilder;
auto mutant = *mutationResult->getMutant();
auto& sourceLocation = mutant.getSourceLocation();
auto& sourceEndLocation = mutant.getEndLocation();
const auto mutant = *mutationResult->getMutant();
const auto& sourceLocation = mutant.getSourceLocation();
const auto& sourceEndLocation = mutant.getEndLocation();
const std::string sourceBasename = std::regex_replace(sourceLocation.filePath.substr(sourceLocation.directory.size()+1), std::regex("([/]|\\.(?!patch))"), "_");;
auto mutator = factory.getMutator(mutant.getMutatorIdentifier());
const auto mutator = factory.getMutator(mutant.getMutatorIdentifier());
const std::string sourceLine = sourceCodeReader.getSourceLine(sourceLocation);
const std::string sourcePath = std::regex_replace(sourceLocation.filePath, basePathRegex, "");

auto prefix = [&mutationExecutionResult](){
const std::string prefix = [&mutationExecutionResult](){
switch(mutationExecutionResult.status){
case ExecutionStatus::Passed:
return "survived-";
Expand All @@ -83,30 +82,29 @@ void mull::PatchesReporter::reportResults(const Result &result) {
default:
return "killed-";
}
};
filenamebuilder << patchesPath << "/" << prefix()
<< sourceBasename << "-" << mutant.getMutatorIdentifier()
<< "-L" << sourceLocation.line << "-C" << sourceLocation.column
<< ".patch";
}();

const std::string filename = filenamebuilder.str();
const std::string filename =[&](){
std::stringstream filenamebuilder;
filenamebuilder << patchesPath << "/" << prefix
<< sourceBasename << "-" << mutant.getMutatorIdentifier()
<< "-L" << sourceLocation.line << "-C" << sourceLocation.column
<< ".patch";
return filenamebuilder.str();
}();

diagnostics.debug(std::string("Writing Patchfile: ") + filename.c_str());
std::ofstream myfile{filename};
if(!myfile.good())
diagnostics.warning(std::string("Writing File failed") + filename.c_str());
myfile << "--- a" << sourcePath << " 0" << "\n"
<< "+++ b" << sourcePath << " 0" << "\n"
<< "@@ -" << sourceLocation.line << ",1 +" << sourceLocation.line << ",1 @@\n"
<< "-" << sourceLine
<< "+" << sourceLine.substr(0, sourceLocation.column-1)
<< mutator->getReplacement() << sourceLine.substr(sourceEndLocation.column-1) ;
if(!myfile.good())
diagnostics.warning(std::string("Writing File failed"));
myfile.flush();
if(!myfile.good())
diagnostics.warning(std::string("Writing File failed") + filename.c_str());
diagnostics.warning(std::string("Writing Patchfile failed") + filename.c_str());
myfile.close();
diagnostics.debug(std::string("Writing Patchfile: ") + filename.c_str());
}

diagnostics.info(std::string("Patchfiles can be found at '") + patchesPath + "'");
Expand Down
6 changes: 1 addition & 5 deletions lib/Reporters/SourceCodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ std::string SourceCodeReader::getSourceLineWithCaret(const SourceLocation &sourc
}

std::string SourceCodeReader::getSourceLine(const SourceLocation &sourceLocation) {
std::stringstream ss;

auto line = sourceManager.getLine(sourceLocation);
assert(sourceLocation.column < line.size());

ss << line;
return ss.str();
return line;
}
2 changes: 1 addition & 1 deletion tools/CLIOptions/CLIOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static std::vector<ReporterDefinition> reporterOptions({
{ "Elements",
"Generates mutation-testing-elements compatible JSON file",
ReporterKind::Elements },
{ "Patches", "Saves results into Patchfiles", ReporterKind::Patches },
{ "Patches", "Generates patch file for each mutation", ReporterKind::Patches },
});

ReportersCLIOptions::ReportersCLIOptions(Diagnostics &diagnostics, list<ReporterKind> &parameter)
Expand Down
1 change: 1 addition & 0 deletions tools/mull-runner/mull-runner-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void dumpCLIInterface(mull::Diagnostics &diagnostics) {

&ReportName,
&ReportDirectory,
&ReportPatchBaseDirectory,
reporters,
&IDEReporterShowKilled,
&DebugEnabled,
Expand Down
2 changes: 0 additions & 2 deletions tools/mull-runner/mull-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ int main(int argc, char **argv) {
configuration.captureMutantOutput = false;
}


tool::ReporterParameters params{ .reporterName = tool::ReportName.getValue(),
.reporterDirectory = tool::ReportDirectory.getValue(),
.patchBasePathDir = tool::ReportPatchBaseDirectory.getValue(),
.compilationDatabaseAvailable = false,
.IDEReporterShowKilled = tool::IDEReporterShowKilled };

std::vector<std::unique_ptr<mull::Reporter>> reporters = reportersOption.reporters(params);

std::string executable = inputFile;
Expand Down

0 comments on commit 9f2accc

Please sign in to comment.