Skip to content

Commit

Permalink
Simplify message parsing logic and improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
marcransome committed Feb 13, 2025
1 parent e259e40 commit dcae428
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,17 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
if ((message_args = poptGetArgs(context)) != NULL) {
flog_config_set_message_from_args(config, message_args);
} else if (is_regular_file_or_pipe(fileno(stdin), &stream_error)) {
if (stream_error == FLOG_ERROR_NONE) {
flog_config_set_message_from_stream(config, stdin);
} else {
flog_config_free(config);
poptFreeContext(context);
*error = stream_error;
return NULL;
}
flog_config_set_message_from_stream(config, stdin);
} else {
flog_config_free(config);
poptFreeContext(context);
*error = FLOG_ERROR_MSG;

if (stream_error != FLOG_ERROR_NONE) {
*error = stream_error;
} else {
*error = FLOG_ERROR_MSG;
}

return NULL;
}

Expand Down
24 changes: 24 additions & 0 deletions test/test_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,29 @@ flog_config_new_with_long_append_opt_and_long_path_fails(void **state) {
free(path);
}

static void
flog_config_new_with_message_from_unsupported_stream_fails(void **state) {
UNUSED(state);

FlogError error = TEST_ERROR;
MOCK_ARGS(
TEST_PROGRAM_NAME
)

const char *message = "0123456789ABCDEF";

FILE *saved_stdin = stdin;
stdin = fmemopen(message, strlen(message), "r");

FlogConfig *config = flog_config_new(mock_argc, mock_argv, &error);

fclose(stdin);
stdin = saved_stdin;

assert_null(config);
assert_int_equal(error, FLOG_ERROR_STAT);
}

static void
flog_config_new_with_message_succeeds(void **state) {
UNUSED(state);
Expand Down Expand Up @@ -1467,6 +1490,7 @@ int main(void) {
cmocka_unit_test(flog_config_new_with_long_level_opt_and_unknown_value_fails),
cmocka_unit_test(flog_config_new_with_short_append_opt_and_long_path_fails),
cmocka_unit_test(flog_config_new_with_long_append_opt_and_long_path_fails),
cmocka_unit_test(flog_config_new_with_message_from_unsupported_stream_fails),

// flog_config_new() success tests
cmocka_unit_test(flog_config_new_with_message_succeeds),
Expand Down

0 comments on commit dcae428

Please sign in to comment.