From 36de940977a416587074736732013d4274be5756 Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Fri, 3 Nov 2023 15:46:24 +0100 Subject: [PATCH] Move output and archive deduction in ProblemGenerationExeOptions --- src/cpp/lpnamer/main/ProblemGeneration.cpp | 43 ++----------------- .../main/ProblemGenerationExeOptions.cpp | 38 ++++++++++++++++ .../lpnamer/main/include/ProblemGeneration.h | 15 ------- .../include/ProblemGenerationExeOptions.h | 7 +++ .../main/include/ProblemGenerationOptions.h | 15 +++++++ .../ProblemGenerationExeOptionsTest.cpp | 3 +- tests/cpp/lp_namer/ProblemGenerationTest.cpp | 10 +++++ 7 files changed, 76 insertions(+), 55 deletions(-) diff --git a/src/cpp/lpnamer/main/ProblemGeneration.cpp b/src/cpp/lpnamer/main/ProblemGeneration.cpp index 5ab3109ed4..539edad2be 100644 --- a/src/cpp/lpnamer/main/ProblemGeneration.cpp +++ b/src/cpp/lpnamer/main/ProblemGeneration.cpp @@ -47,7 +47,7 @@ std::filesystem::path ProblemGeneration::updateProblems() { const auto archive_path = options_.ArchivePath(); auto deduced_xpansion_output_dir = - deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path); + options_.deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path); const auto log_file_path = xpansion_output_dir / "lp" / "ProblemGenerationLog.txt"; @@ -63,9 +63,9 @@ std::filesystem::path ProblemGeneration::updateProblems() { auto unnamed_problems = options_.UnnamedProblems(); std::filesystem::path deduced_archive_path; try { - deduced_archive_path = - deduceArchivePathIfEmpty(deduced_xpansion_output_dir, archive_path); - } catch (const MismatchedParameters& m) { + deduced_archive_path = options_.deduceArchivePathIfEmpty( + deduced_xpansion_output_dir, archive_path); + } catch (const ProblemGenerationOptions::MismatchedParameters& m) { (*logger)(LogUtils::LOGLEVEL::ERR) << m.what(); throw; } @@ -75,38 +75,6 @@ std::filesystem::path ProblemGeneration::updateProblems() { logger, log_file_path, weights_file, unnamed_problems); return deduced_xpansion_output_dir; } -std::filesystem::path ProblemGeneration::deduceArchivePathIfEmpty( - const std::filesystem::path& xpansion_output_dir, - const std::filesystem::path& archive_path) { - if (archive_path.empty() && !xpansion_output_dir.empty()) { - if (xpansion_output_dir.string().find("-Xpansion") == std::string::npos) { - auto log_location = LOGLOCATION; - auto msg = - "Archive path is missing and output path does not contains" - " \"-Xpansion\" suffixe. Can't deduce archive file name."; - throw MismatchedParameters(msg, log_location); - } - auto deduced_archive_path = xpansion_output_dir; - auto dir_name = deduced_archive_path.stem().string(); - dir_name = dir_name.substr(0, dir_name.find("-Xpansion")); - deduced_archive_path = - deduced_archive_path.replace_filename(dir_name).replace_extension( - ".zip"); - return deduced_archive_path; - } - return archive_path; -} -std::filesystem::path ProblemGeneration::deduceXpansionDirIfEmpty( - std::filesystem::path xpansion_output_dir, - const std::filesystem::path& archive_path) { - if (xpansion_output_dir.empty() && !archive_path.empty()) { - auto deduced_dir = archive_path; - deduced_dir = deduced_dir.replace_filename( - deduced_dir.stem().replace_extension("").string() + "-Xpansion"); - return deduced_dir; - } - return xpansion_output_dir; -} struct Version { explicit Version(std::string_view version) { @@ -303,6 +271,3 @@ std::shared_ptr InstantiateZipReader( reader->Open(); return reader; } -ProblemGeneration::MismatchedParameters::MismatchedParameters( - const std::string& err_message, const std::string& log_location) - : XpansionError(err_message, log_location) {} diff --git a/src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp b/src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp index 425a30b5ba..d65732b864 100644 --- a/src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp +++ b/src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp @@ -24,3 +24,41 @@ void ProblemGenerationExeOptions::Parse(unsigned int argc, const char *const *argv) { OptionsParser::Parse(argc, argv); } + +std::filesystem::path ProblemGenerationExeOptions::deduceArchivePathIfEmpty( + const std::filesystem::path& xpansion_output_dir, + const std::filesystem::path& archive_path) const { + if (archive_path.empty() && !xpansion_output_dir.empty()) { + if (xpansion_output_dir.string().find("-Xpansion") == std::string::npos) { + auto log_location = LOGLOCATION; + auto msg = + "Archive path is missing and output path does not contains" + " \"-Xpansion\" suffixe. Can't deduce archive file name."; + throw MismatchedParameters(msg, log_location); + } + auto deduced_archive_path = xpansion_output_dir; + auto dir_name = deduced_archive_path.stem().string(); + dir_name = dir_name.substr(0, dir_name.find("-Xpansion")); + deduced_archive_path = + deduced_archive_path.replace_filename(dir_name).replace_extension( + ".zip"); + return deduced_archive_path; + } + return archive_path; +} + +std::filesystem::path ProblemGenerationExeOptions::deduceXpansionDirIfEmpty( + std::filesystem::path xpansion_output_dir, + const std::filesystem::path& archive_path) const { + if (xpansion_output_dir.empty() && !archive_path.empty()) { + auto deduced_dir = archive_path; + deduced_dir = deduced_dir.replace_filename( + deduced_dir.stem().replace_extension("").string() + "-Xpansion"); + return deduced_dir; + } + return xpansion_output_dir; +} + +ProblemGenerationExeOptions::MismatchedParameters::MismatchedParameters( + const std::string& err_message, const std::string& log_location) + : XpansionError(err_message, log_location) {} \ No newline at end of file diff --git a/src/cpp/lpnamer/main/include/ProblemGeneration.h b/src/cpp/lpnamer/main/include/ProblemGeneration.h index 867a96dbd9..f9ac5618f7 100644 --- a/src/cpp/lpnamer/main/include/ProblemGeneration.h +++ b/src/cpp/lpnamer/main/include/ProblemGeneration.h @@ -26,15 +26,6 @@ class ProblemGeneration { const std::filesystem::path& log_file_path, const std::filesystem::path& weights_file, bool unnamed_problems); - class MismatchedParameters - : public LogUtils::XpansionError { - using LogUtils::XpansionError::XpansionError; - - public: - explicit MismatchedParameters(const std::string& err_message, - const std::string& log_location); - }; - private: void ProcessWeights( const std::filesystem::path& xpansion_output_dir, @@ -45,10 +36,4 @@ class ProblemGeneration { const std::filesystem::path& antares_archive_path, const std::filesystem::path& xpansion_output_dir, ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger); - static std::filesystem::path deduceXpansionDirIfEmpty( - std::filesystem::path xpansion_output_dir, - const std::filesystem::path& archive_path); - static std::filesystem::path deduceArchivePathIfEmpty( - const std::filesystem::path& xpansion_output_dir, - const std::filesystem::path& archive_path); }; diff --git a/src/cpp/lpnamer/main/include/ProblemGenerationExeOptions.h b/src/cpp/lpnamer/main/include/ProblemGenerationExeOptions.h index a1f0c67117..9f05cdc219 100644 --- a/src/cpp/lpnamer/main/include/ProblemGenerationExeOptions.h +++ b/src/cpp/lpnamer/main/include/ProblemGenerationExeOptions.h @@ -34,5 +34,12 @@ class ProblemGenerationExeOptions : public OptionsParser, bool UnnamedProblems() const { return unnamed_problems_; } void Parse(unsigned int argc, const char *const *argv) override; + + [[nodiscard]] std::filesystem::path deduceXpansionDirIfEmpty( + std::filesystem::path xpansion_output_dir, + const std::filesystem::path& archive_path) const override; + [[nodiscard]] std::filesystem::path deduceArchivePathIfEmpty( + const std::filesystem::path& xpansion_output_dir, + const std::filesystem::path& archive_path) const override; }; #endif // ANTARES_XPANSION_SRC_CPP_LPNAMER_MAIN_INCLUDE_PROBLEMGENERATIONEXEOPTIONS_H diff --git a/src/cpp/lpnamer/main/include/ProblemGenerationOptions.h b/src/cpp/lpnamer/main/include/ProblemGenerationOptions.h index aa32d223a1..e861cf8779 100644 --- a/src/cpp/lpnamer/main/include/ProblemGenerationOptions.h +++ b/src/cpp/lpnamer/main/include/ProblemGenerationOptions.h @@ -17,4 +17,19 @@ class ProblemGenerationOptions { [[nodiscard]] virtual std::filesystem::path WeightsFile() const = 0; [[nodiscard]] virtual std::vector ActiveYears() const = 0; [[nodiscard]] virtual bool UnnamedProblems() const = 0; + [[nodiscard]] virtual std::filesystem::path deduceXpansionDirIfEmpty( + std::filesystem::path xpansion_output_dir, + const std::filesystem::path& archive_path) const = 0; + [[nodiscard]] virtual std::filesystem::path deduceArchivePathIfEmpty( + const std::filesystem::path& xpansion_output_dir, + const std::filesystem::path& archive_path) const = 0; + + class MismatchedParameters + : public LogUtils::XpansionError { + using LogUtils::XpansionError::XpansionError; + + public: + explicit MismatchedParameters(const std::string& err_message, + const std::string& log_location); + }; }; diff --git a/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp b/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp index 89bc2fd5b5..207caa1d75 100644 --- a/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp +++ b/tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp @@ -174,7 +174,8 @@ TEST_F( ProblemGenerationSpyAndMock pbg(problem_generation_options_parser_); - EXPECT_THROW(pbg.updateProblems(), ProblemGeneration::MismatchedParameters); + EXPECT_THROW(pbg.updateProblems(), + ProblemGenerationOptions::MismatchedParameters); EXPECT_TRUE(pbg.archive_path_.empty()); // Can't deduce EXPECT_TRUE(pbg.xpansion_output_dir_ diff --git a/tests/cpp/lp_namer/ProblemGenerationTest.cpp b/tests/cpp/lp_namer/ProblemGenerationTest.cpp index 6cd0afce61..b718bc9a0b 100644 --- a/tests/cpp/lp_namer/ProblemGenerationTest.cpp +++ b/tests/cpp/lp_namer/ProblemGenerationTest.cpp @@ -26,6 +26,16 @@ class InMemoryOption : public ProblemGenerationOptions { } std::vector ActiveYears() const override { return std::vector(); } bool UnnamedProblems() const override { return false; } + std::filesystem::path deduceXpansionDirIfEmpty( + std::filesystem::path xpansion_output_dir, + const std::filesystem::path& archive_path) const override { + return std::filesystem::path(); + } + std::filesystem::path deduceArchivePathIfEmpty( + const std::filesystem::path& xpansion_output_dir, + const std::filesystem::path& archive_path) const override { + return std::filesystem::path(); + } }; TEST(InitializationTest, FoldersAreEmpty) {