Skip to content

Commit

Permalink
Update results files when adding tests
Browse files Browse the repository at this point in the history
Fixes #64
  • Loading branch information
LegalizeAdulthood committed Jan 17, 2024
1 parent 9df6da0 commit 740d90b
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 82 deletions.
12 changes: 5 additions & 7 deletions Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ which tests were executed against the updated version.
of the code being refactored.
1. After all the test cases have been added, use the support tool
[add-tests](#executable-add-tests) to adjust the temporary test markers with
appropriate test markers for the refactoring, numbering new test cases for you.
appropriate test markers for the refactoring, numbering new test cases for you and
adding the test cases to all the results files that support this refactoring.
1. Build the test suite. You should get no errors, but warnings about missing diffs
for your new test cases.
1. Commit the test cases with git to have a record of the source code
Expand All @@ -118,8 +119,6 @@ which tests were executed against the updated version.
1. Use the support tool [test-results](#executable-test-results) to validate your changes
to the results file for the tool you're testing.
1. Run `ctest --preset default` to validate your changes to the results file for all the tools.
Add the newly created test ids to the results files for any other tools that implement
this refactoring with the results column blank.
1. Use the [tool-summary](#executable-tool-summary) support tool to regenerate the summary
reports.
1. Issue a pull request with your changes, which should include:
Expand Down Expand Up @@ -158,7 +157,8 @@ which tests were executed against the updated version.
an expected value with an actual value.
1. After all the test cases have been added, use the support tool
[add-tests](#executable-add-tests) to adjust the temporary test markers with
appropriate test markers for the refactoring, numbering new test cases for you.
appropriate test markers for the refactoring, numbering new test cases for you and
adding the test cases to all the results files that support this refactoring.
1. Build the test suite. You should get no errors, but warnings about missing diffs
for your new test cases.
1. Commit the test case with git to have a record of the source code
Expand All @@ -176,8 +176,6 @@ which tests were executed against the updated version.
1. Use the support tool [test-results](#executable-test-results) to validate your changes
to the results file for the tool you're testing.
1. Run `ctest --preset default` to validate your changes to the results file for all the tools.
Add the newly created test ids to the results files for any other tools that implement
this refactoring with the results column blank.
1. Use the [tool-summary](#executable-tool-summary) support tool to regenerate the summary
reports.
1. Issue a pull request with your changes.
Expand Down Expand Up @@ -308,7 +306,7 @@ tool are updated.

## Executable add-tests

Example: `add-tests RefactorTest R RefactorTest/NewRename.cpp`
Example: `add-tests RefactorTest R RefactorTest/NewRename.cpp results`

The executable add-tests scans the test case directory for existing test cases
and replaces temporary test markers in a source file with consecutive markers
Expand Down
87 changes: 60 additions & 27 deletions Tools/AddTests/AddTests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <TestCases.h>
#include <ToolResults.h>

#include <algorithm>
#include <filesystem>
Expand All @@ -17,6 +18,37 @@ std::string g_testPrefix;
int g_numTestCases{};
std::string g_sourceFile;
std::vector<std::string> g_sourceLines;
std::vector<std::string> g_newLabels;

bool readTestCases(std::string_view testCaseDirectory)
{
std::vector<std::string> errors = testCases::scanTestDirectory(testCaseDirectory);
if (!errors.empty())
{
std::cerr << "Test cases contain errors:\n";
for (const std::string &error : errors)
{
std::cerr << error << '\n';
}
return false;
}
return true;
}

bool setTestCasePrefix(std::string_view prefix)
{
const std::vector<testCases::Test> &tests = testCases::getTests();
auto it =
std::find_if(tests.begin(), tests.end(), [&](const testCases::Test &test) { return test.prefix == prefix; });
if (it == tests.end())
{
std::cerr << "Unknown test prefix '" << prefix << "'\n";
return false;
}
g_testPrefix = it->prefix;
g_numTestCases = static_cast<int>(testCases::getNumTestCases(it->prefix));
return true;
}

bool readSourceFile(std::string_view sourceFile)
{
Expand All @@ -34,12 +66,12 @@ bool readSourceFile(std::string_view sourceFile)
g_sourceLines.emplace_back(std::move(line));
}
}
std::filesystem::copy_file(sourceFile, std::string{sourceFile} + ".bak");
copy_file(sourceFile, std::string{sourceFile} + ".bak", std::filesystem::copy_options::overwrite_existing);
g_sourceFile = sourceFile;
return true;
}

void writeTestMarkers()
void writeSourceFile()
{
std::string marker{"#GOINK#: "};
int testNum{g_numTestCases};
Expand All @@ -51,62 +83,63 @@ void writeTestMarkers()
{
++testNum;
auto markerEnd = line.find_first_of(' ', goink + marker.length());
line = line.substr(0, goink) + "#TEST#: " + g_testPrefix + std::to_string(testNum) + line.substr(markerEnd);
g_newLabels.emplace_back(g_testPrefix + std::to_string(testNum));
line = line.substr(0, goink) + "#TEST#: " + g_newLabels.back() + line.substr(markerEnd);
}
str << line << '\n';
}
}

bool readTestCases(std::string_view testCaseDirectory)
void updateResultsFile(std::filesystem::path file)
{
std::vector<std::string> errors = testCases::scanTestDirectory(testCaseDirectory);
if (!errors.empty())
testCases::ToolResults results(file);
if (results.addTests(g_testPrefix, g_newLabels))
{
std::cerr << "Test cases contain errors:\n";
for (const std::string &error : errors)
{
std::cerr << error << '\n';
}
return false;
results.writeResults();
}
return true;
}

bool setTestCasePrefix(std::string_view prefix)
void updateResultsDir(std::filesystem::path dir)
{
const std::vector<testCases::Test> &tests = testCases::getTests();
auto it =
std::find_if(tests.begin(), tests.end(), [&](const testCases::Test &test) { return test.prefix == prefix; });
if (it == tests.end())
const auto endsWith = [](const std::string &text, const std::string &suffix)
{ return text.length() > suffix.length() && text.substr(text.length() - suffix.length()) == suffix; };
const auto isResultsFile = [&](const std::filesystem::path &path)
{ return endsWith(path.filename().string(), "Results.md"); };

for (auto entry : std::filesystem::directory_iterator(dir))
{
std::cerr << "Unknown test prefix '" << prefix << "'\n";
return false;
const std::filesystem::path path = entry.path();
if (is_directory(path))
{
updateResultsDir(path);
}
else if (isResultsFile(path))
{
updateResultsFile(path);
}
}
g_testPrefix = it->prefix;
g_numTestCases = static_cast<int>(testCases::getNumTestCases(it->prefix));
return true;
}

int usage(std::string_view program)
{
std::cout << "Usage: " << program << " <RefactorTest> <TestPrefix> <SourceFile.cpp>\n";
std::cout << "Usage: " << program << " <RefactorTest> <TestPrefix> <SourceFile.cpp> <ResultsDir>\n";
return 1;
}

int main(std::vector<std::string_view> args)
{
try
{
if (args.size() < 4)
if (args.size() < 5)
{
return usage(args[0]);
}

if (!readTestCases(args[1]) || !setTestCasePrefix(args[2]) || !readSourceFile(args[3]))
{
return 1;
}
writeTestMarkers();
writeSourceFile();
updateResultsDir(args[4]);
return 0;
}
catch (const std::exception &bang)
Expand Down
Loading

0 comments on commit 740d90b

Please sign in to comment.