Skip to content

Commit

Permalink
Handle not deductible archive file name
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Nov 3, 2023
1 parent 9f58e2d commit eb223c8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 15 deletions.
56 changes: 41 additions & 15 deletions src/cpp/lpnamer/main/ProblemGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,16 @@ void CreateDirectories(const std::filesystem::path& output_path) {
ProblemGeneration::ProblemGeneration(ProblemGenerationOptions& options)
: options_(options) {}
std::filesystem::path ProblemGeneration::updateProblems() {
auto xpansion_output_dir = options_.XpansionOutputDir();
auto archive_path = options_.ArchivePath();
const auto xpansion_output_dir = options_.XpansionOutputDir();
const auto archive_path = options_.ArchivePath();

xpansion_output_dir =
auto deduced_xpansion_output_dir =
deduceXpansionDirIfEmpty(xpansion_output_dir, archive_path);
if (archive_path.empty() && !xpansion_output_dir.empty()) {
archive_path = xpansion_output_dir;
auto dir_name = archive_path.stem().string();
dir_name = dir_name.substr(0, dir_name.find("-Xpansion"));
archive_path =
archive_path.replace_filename(dir_name).replace_extension(".zip");
}

const auto log_file_path =
xpansion_output_dir / "lp" / "ProblemGenerationLog.txt";

CreateDirectories(xpansion_output_dir);
CreateDirectories(deduced_xpansion_output_dir);
auto logger = ProblemGenerationLog::BuildLogger(log_file_path, std::cout,
"Problem Generation");

Expand All @@ -68,10 +61,40 @@ std::filesystem::path ProblemGeneration::updateProblems() {
options_.AdditionalConstraintsFilename();
auto weights_file = options_.WeightsFile();
auto unnamed_problems = options_.UnnamedProblems();
RunProblemGeneration(xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, archive_path, logger,
log_file_path, weights_file, unnamed_problems);
return xpansion_output_dir;
std::filesystem::path deduced_archive_path;
try {
deduced_archive_path =
deduceArchivePathIfEmpty(deduced_xpansion_output_dir, archive_path);
} catch (const MismatchedParameters& m) {
(*logger)(LogUtils::LOGLEVEL::ERR) << m.what();
throw;
}

RunProblemGeneration(deduced_xpansion_output_dir, master_formulation,
additionalConstraintFilename_l, deduced_archive_path,
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,
Expand Down Expand Up @@ -280,3 +303,6 @@ std::shared_ptr<ArchiveReader> InstantiateZipReader(
reader->Open();
return reader;
}
ProblemGeneration::MismatchedParameters::MismatchedParameters(
const std::string& err_message, const std::string& log_location)
: XpansionError(err_message, log_location) {}
12 changes: 12 additions & 0 deletions src/cpp/lpnamer/main/include/ProblemGeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class ProblemGeneration {
const std::filesystem::path& log_file_path,
const std::filesystem::path& weights_file, bool unnamed_problems);

class MismatchedParameters
: public LogUtils::XpansionError<std::runtime_error> {
using LogUtils::XpansionError<std::runtime_error>::XpansionError;

public:
explicit MismatchedParameters(const std::string& err_message,
const std::string& log_location);
};

private:
void ProcessWeights(
const std::filesystem::path& xpansion_output_dir,
Expand All @@ -39,4 +48,7 @@ class ProblemGeneration {
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);
};
42 changes: 42 additions & 0 deletions tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,46 @@ TEST_F(ProblemGenerationExeOptionsTest,
EXPECT_EQ(pbg.xpansion_output_dir_, output_path);
EXPECT_TRUE(std::filesystem::exists(output_path));
EXPECT_TRUE(std::filesystem::exists(output_path / "lp"));
}

bool Constains(std::ifstream& file, const std::string& string) {
std::string line;

while (std::getline(file, line)) {
if (line.find(string) != std::string::npos) {
return true;
}
}
return false;
}

TEST_F(
ProblemGenerationExeOptionsTest,
OutputAndArchiveParameters_cantDeduceArchiveFromOutputDirWithoutRegularSuffixe) {
auto test_root =
std::filesystem::temp_directory_path() / std::tmpnam(nullptr);
auto archive = test_root / "study.zip";
auto output_path = test_root / "study-out";

const char argv0[] = "lp.exe ";
const char argv1[] = "--output";
auto argv2 = output_path.string();

std::vector<const char*> ppargv = {argv0, argv1, argv2.c_str()};
problem_generation_options_parser_.Parse(3, ppargv.data());

ProblemGenerationSpyAndMock pbg(problem_generation_options_parser_);

EXPECT_THROW(pbg.updateProblems(), ProblemGeneration::MismatchedParameters);

EXPECT_TRUE(pbg.archive_path_.empty()); // Can't deduce
EXPECT_TRUE(pbg.xpansion_output_dir_
.empty()); // Exception occurres before RunProblemGeneration
// We expect to have created directories to log properly
EXPECT_TRUE(std::filesystem::exists(output_path));
EXPECT_TRUE(std::filesystem::exists(output_path / "lp"));

std::ifstream logfile(output_path / "lp" / "ProblemGenerationLog.txt");
EXPECT_TRUE(Constains(
logfile, "Archive path is missing and output path does not contains"));
}

0 comments on commit eb223c8

Please sign in to comment.