src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
18 / 18
100.0%
Branches:
14 / 20
70.0%
Functions:
3 / 3
100.0%

utils/system_utils.cpp
Line Branch Exec Source
1 #include "system_utils.hpp"
2 #include <format>
3
4 /**
5 * Initializes a SystemUtils instance with a reference to the UserInterface.
6 *
7 * @param ui Pointer to UserInterface for displaying messages and error output.
8 * Must remain valid for the lifetime of this SystemUtils instance.
9 *
10 * @note The SystemUtils class does not take ownership of the ui pointer;
11 * the caller is responsible for managing the UserInterface lifetime.
12 */
13 10 SystemUtils::SystemUtils(std::shared_ptr<UserInterface> ui) : ui(ui) {
14 10 }
15
16 /**
17 * Reports a failure and ends the program with EXIT_FAILURE.
18 *
19 * In interactive mode, the message is shown through the UserInterface and the
20 * session is ended with EXIT_FAILURE. Otherwise, the message is printed
21 * non-interactively and the process exits immediately with EXIT_FAILURE.
22 *
23 * @param message The error message to display. The messages passed from
24 * catch_error each end with a newline.
25 *
26 * @note In interactive mode, this relies on UserInterface::end_session() not
27 * returning. If it does return, report_failure returns to its caller.
28 */
29 8 void SystemUtils::report_failure(const std::string& message) {
30
2/2
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
8 if (this->ui->backend_running()) {
31 5 this->ui->print_error(message);
32 } else {
33 3 UserInterface::print_error_noninteractive(message);
34 }
35 8 this->ui->end_session(EXIT_FAILURE);
36 8 }
37
38 /**
39 * Evaluates the wait status from a system() call and reports any failure.
40 *
41 * This method interprets the status with the POSIX macros WIFEXITED and
42 * WEXITSTATUS. A status of -1 means system() itself failed, and only the
43 * "system() failed." message is reported. A status where the process did not
44 * exit normally is reported as "Command terminated abnormally." A normal exit
45 * with a non-zero code is reported with that code. A successful command
46 * (exit code 0) produces no output and returns normally.
47 *
48 * Each failure is passed to report_failure(), which shows the message through
49 * the UserInterface in interactive mode or prints it non-interactively
50 * otherwise, then ends the program with EXIT_FAILURE.
51 *
52 * @param status The wait status returned from a system() call, as interpreted
53 * by the POSIX macros WIFEXITED and WEXITSTATUS.
54 *
55 * @throws Does not throw. Ends the program with EXIT_FAILURE on any detected
56 * error, through report_failure().
57 *
58 * @note Non-interactive failures terminate the process immediately. Interactive
59 * failures return to the caller only if UserInterface::end_session() returns.
60 */
61 10 void SystemUtils::catch_error(int status) {
62
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (status == -1) {
63
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 this->report_failure("system() failed.\n");
64 5 return;
65 }
66
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 if (!WIFEXITED(status)) {
67
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 this->report_failure("Command terminated abnormally.\n");
68 3 return;
69 }
70 5 int exit_code = WEXITSTATUS(status);
71
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (exit_code != 0) {
72
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 this->report_failure(std::format("Command failed with exit code {}.\n", exit_code));
73 }
74 }
75