Skip to content

Commit

Permalink
Missing both output and archive path is an error
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Nov 3, 2023
1 parent 52784d3 commit 3fda4bb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/cpp/lpnamer/main/ProblemGenerationExeOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ ProblemGenerationExeOptions::ProblemGenerationExeOptions()
void ProblemGenerationExeOptions::Parse(unsigned int argc,
const char *const *argv) {
OptionsParser::Parse(argc, argv);
if (XpansionOutputDir().empty() && ArchivePath().empty()) {
auto log_location = LOGLOCATION;
auto msg = "Both output directory and archive path are empty";
throw ProblemGenerationOptions::MissingParameters(msg, log_location);
}
}

std::filesystem::path ProblemGenerationExeOptions::deduceArchivePathIfEmpty(
Expand Down Expand Up @@ -57,8 +62,4 @@ std::filesystem::path ProblemGenerationExeOptions::deduceXpansionDirIfEmpty(
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) {}
}
14 changes: 12 additions & 2 deletions src/cpp/lpnamer/main/include/ProblemGenerationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class ProblemGenerationOptions {

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

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

public:
explicit MissingParameters(const std::string& err_message,
const std::string& log_location)
: XpansionError(err_message, log_location) {}
};
};
24 changes: 13 additions & 11 deletions tests/cpp/lp_namer/ProblemGenerationExeOptionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ class ProblemGenerationSpyAndMock : public ProblemGeneration {
bool unnamed_problems_;
};

TEST_F(ProblemGenerationExeOptionsTest, WithoutArchiveOption) {
char argv = 'c';
char* pargv = &argv;
char** ppargv = &pargv;
try {
problem_generation_options_parser_.Parse(1, ppargv);
} catch (const std::exception& e) {
EXPECT_EQ(e.what(),
std::string("the option '--archive' is required but missing"));
}
}
TEST_F(ProblemGenerationExeOptionsTest, WithoutOutputOption) {
const char argv0[] = "lp_namer.exe";
const char argv1[] = "--archive";
Expand Down Expand Up @@ -187,4 +176,17 @@ TEST_F(
std::ifstream logfile(output_path / "lp" / "ProblemGenerationLog.txt");
EXPECT_TRUE(Constains(
logfile, "Archive path is missing and output path does not contains"));
}

TEST_F(ProblemGenerationExeOptionsTest,
OutputAndArchiveParameters_ErrorIfBothArchiveAndOutputAreMissing) {
auto test_root =
std::filesystem::temp_directory_path() / std::tmpnam(nullptr);

const char argv0[] = "lp.exe ";

std::vector<const char*> ppargv = {argv0};

EXPECT_THROW(problem_generation_options_parser_.Parse(1, ppargv.data()),
ProblemGenerationOptions::MissingParameters);
}

0 comments on commit 3fda4bb

Please sign in to comment.