Skip to content

Commit

Permalink
return exit code from app::run and main
Browse files Browse the repository at this point in the history
We want to have exit codes from the main function in case of errors.
The exit codes are returned from app::run so we can unit test the
correct behavior.
  • Loading branch information
arnemertz committed Aug 2, 2021
1 parent 5632887 commit 41475f8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Checks: '*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm*'
Checks: '*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm*,-altera*'
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
5 changes: 4 additions & 1 deletion behave/steps/fix_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
fix_executable = '../cmake-build-debug/bin/fix'


def assert_equals(a, b):
assert(a == b), "{} should equal {}!".format(a, b)

def _start_fix_with_args(context, args):
context.fix = pexpect.spawn(fix_executable, args=args)

Expand Down Expand Up @@ -42,4 +45,4 @@ def check_commands(context):

@then(u'terminates with exit code {ec:d}')
def check_exit_code(context, ec):
assert (context.fix.wait() == ec)
assert_equals(context.fix.wait(), ec)
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ target_link_libraries(
project_warnings
fix_cli
)

6 changes: 5 additions & 1 deletion src/fix_cli/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ app::app(std::ostream &out)
out << USAGE;
}

void app::run(const std::vector<std::string_view>& /*args*/) { //NOLINT
auto app::run(const std::vector<std::string_view>& args) -> int { // NOLINT
if (args.empty()) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion src/fix_cli/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class app {
public:
explicit app(std::ostream &out);

void run(std::vector<std::string_view> const &);
auto run(std::vector<std::string_view> const &) -> int;
};

}
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

int main(int argc, char* argv[]) {
fix::cli::app app{std::cout};
const std::vector<std::string_view> args(argv, std::next(argv, argc));
app.run(args);
const std::vector<std::string_view> args(std::next(argv), std::next(argv, argc));
return app.run(args);
}
6 changes: 3 additions & 3 deletions test/fix_cli/app_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ TEST_CASE("Prints usage and commands...") {
fix::cli::app app{out};

SECTION("... when run without commands") {
app.run({});
CHECK(app.run({}) == EXIT_FAILURE);
}SECTION("... when run with --help option") {
app.run({"--help"});
CHECK(app.run({"--help"}) == EXIT_SUCCESS);
}SECTION("... when run with -h option") {
app.run({"-h"});
CHECK(app.run({"-h"}) == EXIT_SUCCESS);
}

CHECK(out.str() == USAGE);
Expand Down

0 comments on commit 41475f8

Please sign in to comment.