From 70f76d77686b6e1521c9f3ca1162a9ad0f7f0317 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 28 Jan 2024 00:26:22 -0700 Subject: [PATCH] Extract Class Tool and derive most tools from it Drop-comment doesn't interact with test cases or test results. --- Tools/AddTestAlias/AddTestAlias.cpp | 27 +++-------- Tools/AddTests/AddTests.cpp | 62 +++++++++++------------- Tools/DeleteTests/DeleteTests.cpp | 54 ++++++++------------- Tools/RenumberTests/RenumberTests.cpp | 70 ++++++++++----------------- Tools/TestCases/CMakeLists.txt | 2 + Tools/TestCases/FileContents.cpp | 5 +- Tools/TestCases/FileContents.h | 4 +- Tools/TestCases/Tool.cpp | 37 ++++++++++++++ Tools/TestCases/Tool.h | 38 +++++++++++++++ Tools/TestDiffs/TestDiffs.cpp | 1 + Tools/ToolSummary/ToolSummary.cpp | 26 +++------- 11 files changed, 171 insertions(+), 155 deletions(-) create mode 100644 Tools/TestCases/Tool.cpp create mode 100644 Tools/TestCases/Tool.h diff --git a/Tools/AddTestAlias/AddTestAlias.cpp b/Tools/AddTestAlias/AddTestAlias.cpp index ffdc054..cc8ce44 100644 --- a/Tools/AddTestAlias/AddTestAlias.cpp +++ b/Tools/AddTestAlias/AddTestAlias.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -19,7 +20,7 @@ namespace { -class AddTestAlias +class AddTestAlias : public testCases::Tool { public: AddTestAlias(std::string_view testCaseDir, @@ -27,9 +28,9 @@ class AddTestAlias std::string_view prefix, int master, std::vector aliases) : - m_resultsDir(resultsDir), + Tool(testCaseDir, resultsDir), m_prefix(prefix), - m_test(readTestCases(testCaseDir)), + m_test(testCases::getTestForPrefix(prefix)), m_master(m_prefix + std::to_string(master)) { m_aliases.reserve(aliases.size()); @@ -43,30 +44,14 @@ class AddTestAlias void updateResults(); private: - const testCases::Test &readTestCases(std::string_view testCaseDir); void updateFile(const testCases::FileContents &file); - std::filesystem::path m_resultsDir; std::string m_prefix; const testCases::Test &m_test; std::string m_master; std::vector m_aliases; }; -const testCases::Test &AddTestAlias::readTestCases(std::string_view testCaseDir) -{ - const std::vector errors = testCases::Test::scanTestDirectory(testCaseDir); - if (!errors.empty()) - { - for (const std::string &error : errors) - { - std::cerr << "error: " << error << '\n'; - } - throw std::runtime_error("Test cases contain errors"); - } - return testCases::getTestForPrefix(m_prefix); -} - void AddTestAlias::updateFile(const testCases::FileContents &file) { auto updateLabel = [this](const std::string &line) @@ -99,11 +84,11 @@ void AddTestAlias::updateSourceFiles() void AddTestAlias::updateResults() { - for (const testCases::FileContents &diff : testCases::readCaseDiffs(m_resultsDir / "diffs", m_prefix)) + for (const testCases::FileContents &diff : getCaseDiffsForPrefix(m_prefix)) { updateFile(diff); } - if (std::filesystem::path fileDiff(m_resultsDir / "file-diffs" / (m_prefix + ".txt")); exists(fileDiff)) + if (std::filesystem::path fileDiff(getFileDiffForPrefix(m_prefix)); exists(fileDiff)) { updateFile(testCases::FileContents(fileDiff)); } diff --git a/Tools/AddTests/AddTests.cpp b/Tools/AddTests/AddTests.cpp index fd56587..86c7d19 100644 --- a/Tools/AddTests/AddTests.cpp +++ b/Tools/AddTests/AddTests.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -16,45 +17,32 @@ namespace { -const testCases::Test &getTestCase(const std::filesystem::path &testCaseDir, std::string_view prefix) -{ - const std::vector errors = testCases::Test::scanTestDirectory(testCaseDir); - if (!errors.empty()) - { - for (const std::string &error : errors) - { - std::cerr << "error: " << error << '\n'; - } - throw std::runtime_error("Test cases contain errors:"); - } - - return testCases::getTestForPrefix(prefix); -} - -class AddTests +class AddTests : public testCases::Tool { public: - AddTests(const std::filesystem::path &testCaseDir, + AddTests(std::string_view testCaseDir, std::string_view resultsDir, std::string_view prefix, - const std::filesystem::path &sourceFile, - const std::filesystem::path &resultsDir); + const std::filesystem::path &sourceFile); - bool readSourceFile(std::string_view sourceFile); void writeSourceFile(); - void updateResultsFile(const std::filesystem::path &file); + void updateResults(); + void writePlaceholderDiffs(); + +private: void updateResultsDir(const std::filesystem::path &dir); - void writePlaceholderDiffs(const std::filesystem::path &diffDir); + void updateResultsFile(const std::filesystem::path &file); const testCases::Test &m_test; testCases::FileContents m_sourceContents; std::vector m_newLabels; }; -AddTests::AddTests(const std::filesystem::path &testCaseDir, +AddTests::AddTests(std::string_view testCaseDir, + std::string_view resultsDir, std::string_view prefix, - const std::filesystem::path &sourceFile, - const std::filesystem::path &resultsDir) : - m_test(getTestCase(testCaseDir, prefix)), + const std::filesystem::path &sourceFile) : + Tool(testCaseDir, resultsDir), + m_test(testCases::getTestForPrefix(prefix)), m_sourceContents(sourceFile) { } @@ -79,6 +67,11 @@ void AddTests::writeSourceFile() m_sourceContents.transform(replaceMarkers); } +void AddTests::updateResults() +{ + updateResultsDir(getResultsDir()); +} + void AddTests::updateResultsFile(const std::filesystem::path &file) { testCases::ToolResults results(file); @@ -104,8 +97,9 @@ void AddTests::updateResultsDir(const std::filesystem::path &dir) } } -void AddTests::writePlaceholderDiffs(const std::filesystem::path &diffDir) +void AddTests::writePlaceholderDiffs() { + const std::filesystem::path diffDir(getDiffsDir()); for (const std::string &label : m_newLabels) { std::ofstream str(diffDir / (label + ".txt")); @@ -127,14 +121,14 @@ int toolMain(std::vector args) { return usage(args[0]); } - const std::filesystem::path testCaseDir{args[1]}; - if (!is_directory(testCaseDir)) + const std::string_view testCaseDir{args[1]}; + if (!is_directory(std::filesystem::path(testCaseDir))) { std::cerr << "Test case directory " << testCaseDir << " does not exist.\n"; return 1; } - const std::filesystem::path resultsDir{args[2]}; - if (!is_directory(resultsDir)) + const std::string_view resultsDir{args[2]}; + if (!is_directory(std::filesystem::path(resultsDir))) { std::cerr << "Results case directory " << resultsDir << " does not exist.\n"; return 1; @@ -147,9 +141,9 @@ int toolMain(std::vector args) return 1; } - AddTests tool(testCaseDir, testPrefix, sourceFile, resultsDir); + AddTests tool(testCaseDir, resultsDir, testPrefix, sourceFile); tool.writeSourceFile(); - tool.updateResultsDir(resultsDir); - tool.writePlaceholderDiffs(resultsDir / "diffs"); + tool.updateResults(); + tool.writePlaceholderDiffs(); return 0; } diff --git a/Tools/DeleteTests/DeleteTests.cpp b/Tools/DeleteTests/DeleteTests.cpp index bf6b9a3..e929cff 100644 --- a/Tools/DeleteTests/DeleteTests.cpp +++ b/Tools/DeleteTests/DeleteTests.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -19,56 +20,43 @@ namespace { -class DeleteTests +class DeleteTests : public testCases::Tool { public: DeleteTests(std::string_view testCaseDir, - std::string_view resultsDir, - std::string_view prefix, - std::vector deletions) : - m_resultsDir(resultsDir), - m_prefix(prefix), - m_test(readTestCases(testCaseDir)) - { - m_deletions.reserve(deletions.size()); - std::transform(deletions.begin(), - deletions.end(), - std::back_inserter(m_deletions), - [this](int alias) { return m_prefix + std::to_string(alias); }); - } + std::string_view resultsDir, + std::string_view prefix, + std::vector deletions); void updateSourceFiles(); void updateResults(); private: - const testCases::Test &readTestCases(std::string_view testCaseDir); - std::string removeLabel(const std::string &line); void updateFile(const testCases::FileContents &file); void updateToolResults(const std::filesystem::path &dir); void updateDiffs(); - std::filesystem::path m_resultsDir; std::string m_prefix; const testCases::Test &m_test; std::vector m_deletions; bool m_updatingDIffs{}; }; -const testCases::Test &DeleteTests::readTestCases(std::string_view testCaseDir) +DeleteTests::DeleteTests(std::string_view testCaseDir, + std::string_view resultsDir, + std::string_view prefix, + std::vector deletions) : + Tool(testCaseDir, resultsDir), + m_prefix(prefix), + m_test(testCases::getTestForPrefix(m_prefix)) { - const std::vector errors = testCases::Test::scanTestDirectory(testCaseDir); - if (!errors.empty()) - { - for (const std::string &error : errors) - { - std::cerr << "error: " << error << '\n'; - } - throw std::runtime_error("Test cases contain errors"); - } - return testCases::getTestForPrefix(m_prefix); + m_deletions.reserve(deletions.size()); + std::transform(deletions.begin(), + deletions.end(), + std::back_inserter(m_deletions), + [this](int alias) { return m_prefix + std::to_string(alias); }); } - void DeleteTests::updateFile(const testCases::FileContents &file) { const auto removeLabel = [this](const std::string &line) @@ -120,17 +108,17 @@ void DeleteTests::updateSourceFiles() void DeleteTests::updateDiffs() { - for (const testCases::FileContents &diff : testCases::readCaseDiffs(m_resultsDir / "diffs", m_prefix)) + for (const testCases::FileContents &diff : getCaseDiffsForPrefix(m_prefix)) { updateFile(diff); } - if (std::filesystem::path fileDiff(m_resultsDir / "file-diffs" / (m_prefix + ".txt")); exists(fileDiff)) + if (std::filesystem::path fileDiff(getFileDiffForPrefix(m_prefix)); exists(fileDiff)) { updateFile(testCases::FileContents(fileDiff)); } - for (const auto &entry : std::filesystem::directory_iterator(m_resultsDir / "diffs")) + for (const auto &entry : std::filesystem::directory_iterator(getDiffsDir())) { if (is_directory(entry)) { @@ -178,7 +166,7 @@ void DeleteTests::updateToolResults(const std::filesystem::path &dir) void DeleteTests::updateResults() { m_updatingDIffs = false; - updateToolResults(m_resultsDir); + updateToolResults(getResultsDir()); m_updatingDIffs = true; updateDiffs(); } diff --git a/Tools/RenumberTests/RenumberTests.cpp b/Tools/RenumberTests/RenumberTests.cpp index 808f3a4..beacd18 100644 --- a/Tools/RenumberTests/RenumberTests.cpp +++ b/Tools/RenumberTests/RenumberTests.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -22,10 +23,10 @@ struct CaseMapping std::string after; }; -class Processor +class RenumberTests : public testCases::Tool { public: - Processor(const std::filesystem::path &resultsDir, std::string_view prefix); + RenumberTests(std::string_view testCaseDir, std::string_view resultsDir, std::string_view prefix); void renumber(); void writeResults(); @@ -52,24 +53,23 @@ class Processor std::vector m_sourceFiles; std::vector m_caseDiffs; testCases::FileContents m_fileDiff; - std::filesystem::path m_resultsDir; std::vector m_missingDiffs; }; -Processor::Processor(const std::filesystem::path &resultsDir, std::string_view prefix) : - m_test(testCases::getTestForPrefix(prefix)), - m_resultsDir(resultsDir) +RenumberTests::RenumberTests(std::string_view testCaseDir, std::string_view resultsDir, std::string_view prefix) : + Tool(testCaseDir, resultsDir), + m_test(testCases::getTestForPrefix(prefix)) { m_sourceFiles.reserve(m_test.getPaths().size()); for (const std::filesystem::path &path : m_test.getPaths()) { m_sourceFiles.emplace_back(path); } - scanResultsDir(m_resultsDir); + scanResultsDir(resultsDir); readDiffs(); } -void Processor::scanResultsDir(const std::filesystem::path &dir) +void RenumberTests::scanResultsDir(const std::filesystem::path &dir) { for (const auto &entry : std::filesystem::directory_iterator(dir)) { @@ -88,7 +88,7 @@ void Processor::scanResultsDir(const std::filesystem::path &dir) } } -void Processor::readDiffs() +void RenumberTests::readDiffs() { if (!m_test.hasDiffs()) { @@ -99,21 +99,20 @@ void Processor::readDiffs() readFileDiff(); } -void Processor::readCaseDiffs() +void RenumberTests::readCaseDiffs() { - m_caseDiffs = testCases::readCaseDiffs(m_resultsDir / "diffs", m_test.getPrefix()); + m_caseDiffs = getCaseDiffsForPrefix(m_test.getPrefix()); } -void Processor::readFileDiff() +void RenumberTests::readFileDiff() { - if (const std::filesystem::path fileDiff(m_resultsDir / "file-diffs" / (m_test.getPrefix() + ".txt")); - exists(fileDiff)) + if (const std::filesystem::path fileDiff(getFileDiffForPrefix(m_test.getPrefix())); exists(fileDiff)) { m_fileDiff = testCases::FileContents(fileDiff); } } -void Processor::renumber() +void RenumberTests::renumber() { int testCaseNum{}; for (const testCases::FileContents &sourceFile : m_sourceFiles) @@ -144,7 +143,7 @@ void Processor::renumber() } } -std::string Processor::findMapping(std::string_view label) const +std::string RenumberTests::findMapping(std::string_view label) const { const auto it = std::find_if( m_mapping.begin(), m_mapping.end(), [&](const CaseMapping &mapping) { return mapping.before == label; }); @@ -155,7 +154,7 @@ std::string Processor::findMapping(std::string_view label) const return it->after; } -void Processor::writeTransformedFileContents(const testCases::FileContents &contents, const std::filesystem::path &dest) +void RenumberTests::writeTransformedFileContents(const testCases::FileContents &contents, const std::filesystem::path &dest) { auto renumberLabel = [this](const std::string &line) { @@ -176,7 +175,7 @@ void Processor::writeTransformedFileContents(const testCases::FileContents &cont contents.transform(renumberLabel, dest); } -void Processor::writeSourceFiles() +void RenumberTests::writeSourceFiles() { std::cout << "Updating " << m_sourceFiles.size() << " test case source files..." << std::flush; for (const testCases::FileContents &sourceFile : m_sourceFiles) @@ -186,7 +185,7 @@ void Processor::writeSourceFiles() std::cout << '\n'; } -const testCases::FileContents &Processor::getCaseDiffForLabel(const std::string &label) +const testCases::FileContents &RenumberTests::getCaseDiffForLabel(const std::string &label) { auto it = std::find_if(m_caseDiffs.begin(), m_caseDiffs.end(), @@ -198,7 +197,7 @@ const testCases::FileContents &Processor::getCaseDiffForLabel(const std::string return *it; } -void Processor::writeDiffs() +void RenumberTests::writeDiffs() { if (!m_test.hasDiffs()) { @@ -212,7 +211,7 @@ void Processor::writeDiffs() { if (std::find(m_missingDiffs.begin(), m_missingDiffs.end(), before) != m_missingDiffs.end()) { - if (const std::filesystem::path path = m_resultsDir / "diffs" / (findMapping(before) + ".txt"); + if (const std::filesystem::path path = getDiffsDir() / (findMapping(before) + ".txt"); exists(path)) { remove(path); @@ -235,7 +234,7 @@ void Processor::writeDiffs() writeTransformedFileContentsInPlace(m_fileDiff); } -void Processor::writeToolResults() +void RenumberTests::writeToolResults() { std::cout << "Updating " << m_toolResults.size() << " tool results..." << std::flush; std::vector before; @@ -256,28 +255,13 @@ void Processor::writeToolResults() std::cout << '\n'; } -void Processor::writeResults() +void RenumberTests::writeResults() { writeSourceFiles(); writeDiffs(); writeToolResults(); } -bool readTestCases(std::filesystem::path testCaseDirectory) -{ - std::vector errors = testCases::Test::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; -} - int usage(std::string_view program) { std::cout << "Usage: " << program << " \n"; @@ -292,14 +276,10 @@ int toolMain(std::vector args) { return usage(args[0]); } - const std::filesystem::path testCaseDir(args[1]); - if (!readTestCases(testCaseDir)) - { - return 1; - } - const std::filesystem::path resultsDir{args[2]}; + const std::string_view testCaseDir(args[1]); + const std::string_view resultsDir{args[2]}; const std::string_view prefix{args[3]}; - Processor processor(resultsDir, prefix); + RenumberTests processor(testCaseDir, resultsDir, prefix); processor.renumber(); processor.writeResults(); return 0; diff --git a/Tools/TestCases/CMakeLists.txt b/Tools/TestCases/CMakeLists.txt index 4558474..0465ca2 100644 --- a/Tools/TestCases/CMakeLists.txt +++ b/Tools/TestCases/CMakeLists.txt @@ -13,6 +13,8 @@ add_library(test-cases STATIC StringScanner.h TestCases.h TestCases.cpp + Tool.h + Tool.cpp ToolResults.h ToolResults.cpp ) diff --git a/Tools/TestCases/FileContents.cpp b/Tools/TestCases/FileContents.cpp index cde7edf..1241338 100644 --- a/Tools/TestCases/FileContents.cpp +++ b/Tools/TestCases/FileContents.cpp @@ -1,6 +1,7 @@ #include #include +#include namespace testCases { @@ -52,10 +53,10 @@ void FileContents::write() } } -std::vector readCaseDiffs(const std::filesystem::path &caseDiffsDir, const std::string &prefix) +std::vector readCaseDiffs(const std::filesystem::path &caseDiffsDir, std::string_view prefix) { std::vector caseDiffs; - const auto startsWith = [](const std::string &text, const std::string &prefix) + const auto startsWith = [](std::string_view text, std::string_view prefix) { return text.substr(0, prefix.length()) == prefix; }; const auto isTestCaseDiff = [startsWith, prefix](const std::filesystem::path &path) { return path.extension().string() == ".txt" && startsWith(path.stem().string(), prefix); }; diff --git a/Tools/TestCases/FileContents.h b/Tools/TestCases/FileContents.h index 98fc6a8..0ddc2e3 100644 --- a/Tools/TestCases/FileContents.h +++ b/Tools/TestCases/FileContents.h @@ -1,9 +1,9 @@ #pragma once #include -#include #include #include +#include #include namespace testCases @@ -36,6 +36,6 @@ struct FileContents std::vector m_lines; }; -std::vector readCaseDiffs(const std::filesystem::path &caseDiffsDir, const std::string &prefix); +std::vector readCaseDiffs(const std::filesystem::path &caseDiffsDir, std::string_view prefix); } // namespace testCases diff --git a/Tools/TestCases/Tool.cpp b/Tools/TestCases/Tool.cpp new file mode 100644 index 0000000..8e7b9c1 --- /dev/null +++ b/Tools/TestCases/Tool.cpp @@ -0,0 +1,37 @@ +#include + +#include "Tool.h" + +#include +#include + +namespace testCases +{ + +Tool::Tool(std::string_view testCaseDir, std::string_view resultsDir) : + m_testCaseDir(testCaseDir), + m_resultsDir(resultsDir), + m_diffsDir(m_resultsDir / "diffs"), + m_fileDiffsDir(m_resultsDir / "file-diffs") +{ + if (const std::vector errors = Test::scanTestDirectory(testCaseDir); !errors.empty()) + { + for (const std::string &message : errors) + { + std::cerr << "error: " << message << '\n'; + } + throw std::runtime_error("Test cases contain errors"); + } +} + +std::filesystem::path Tool::getFileDiffForPrefix(const std::string &prefix) const +{ + return m_fileDiffsDir / (prefix + ".txt"); +} + +std::vector Tool::getCaseDiffsForPrefix(std::string_view prefix) +{ + return readCaseDiffs(m_diffsDir, prefix); +} + +} // namespace testCases diff --git a/Tools/TestCases/Tool.h b/Tools/TestCases/Tool.h new file mode 100644 index 0000000..54e3c47 --- /dev/null +++ b/Tools/TestCases/Tool.h @@ -0,0 +1,38 @@ +#pragma once + +#include + +#include +#include + +namespace testCases +{ + +class Tool +{ +public: + Tool(std::string_view testCaseDir, std::string_view resultsDir); + + const std::filesystem::path &getTestCaseDir() const + { + return m_testCaseDir; + } + const std::filesystem::path &getResultsDir() const + { + return m_resultsDir; + } + const std::filesystem::path &getDiffsDir() const + { + return m_diffsDir; + } + std::filesystem::path getFileDiffForPrefix(const std::string &prefix) const; + std::vector getCaseDiffsForPrefix(std::string_view prefix); + +private: + std::filesystem::path m_testCaseDir; + std::filesystem::path m_resultsDir; + std::filesystem::path m_diffsDir; + std::filesystem::path m_fileDiffsDir; +}; + +} // namespace testCases diff --git a/Tools/TestDiffs/TestDiffs.cpp b/Tools/TestDiffs/TestDiffs.cpp index 35c7ed6..4643527 100644 --- a/Tools/TestDiffs/TestDiffs.cpp +++ b/Tools/TestDiffs/TestDiffs.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/Tools/ToolSummary/ToolSummary.cpp b/Tools/ToolSummary/ToolSummary.cpp index 684a92e..8657156 100644 --- a/Tools/ToolSummary/ToolSummary.cpp +++ b/Tools/ToolSummary/ToolSummary.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -25,10 +26,10 @@ std::map g_toolTitles = {{"AppleXcode", "Xcode"}, {"VisualAssistX", "Visual AssistX"}, {"VisualStudio", "Visual Studio"}}; -class ToolSummarizer +class ToolSummarizer : public testCases::Tool { public: - ToolSummarizer(const std::filesystem::path &testCaseDir, const std::filesystem::path &resultDir, bool annotate); + ToolSummarizer(std::string_view testCaseDir, std::string_view resultDir, bool annotate); void generateSummary(); void generateAnnotatedToolResults(); @@ -38,24 +39,13 @@ class ToolSummarizer void scanResultsDirectory(std::filesystem::path dir); std::vector m_toolResults; - std::filesystem::path m_resultDir; bool m_annotate; }; -ToolSummarizer::ToolSummarizer(const std::filesystem::path &testCaseDir, - const std::filesystem::path &resultDir, - bool annotate) : - m_resultDir(resultDir), +ToolSummarizer::ToolSummarizer(std::string_view testCaseDir, std::string_view resultDir, bool annotate) : + Tool(testCaseDir, resultDir), m_annotate(annotate) { - if (const std::vector errors = testCases::Test::scanTestDirectory(testCaseDir); !errors.empty()) - { - for (const std::string &message : errors) - { - std::cerr << "error: " << message << '\n'; - } - throw std::runtime_error("Test cases contain errors:"); - } scanResultsDirectory(resultDir); } @@ -148,7 +138,7 @@ void ToolSummarizer::generateAnnotatedToolResults() { for (testCases::ToolResults &toolResult : m_toolResults) { - toolResult.writeAnnotatedResults(m_resultDir / "annotated" / toolResult.getPath().filename()); + toolResult.writeAnnotatedResults(getResultsDir() / "annotated" / toolResult.getPath().filename()); } } @@ -189,8 +179,8 @@ int toolMain(std::vector args) return usage(args[0]); } - const std::filesystem::path testCaseDir{args[1]}; - const std::filesystem::path resultDir{args[2]}; + const std::string_view testCaseDir{args[1]}; + const std::string_view resultDir{args[2]}; ToolSummarizer(testCaseDir, resultDir, annotate).generateReport(); return 0; }