Skip to content

Commit

Permalink
Merge pull request #244 from marcransome/config-test-message-from-stream
Browse files Browse the repository at this point in the history
Add config test for reading message from stream
  • Loading branch information
marcransome authored Sep 29, 2024
2 parents 3aae92a + 4a4c0b8 commit 04c9fa3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ struct FlogConfigData {

FlogConfig *
flog_config_new(int argc, char *argv[], FlogError *error) {
assert(argc > 1);
assert(argv != NULL);
assert(error != NULL);

Expand Down
1 change: 0 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ typedef struct FlogConfigData FlogConfig;
* \param[out] error A pointer to a FlogError object that will be used to represent
* an error condition on failure
*
* \pre \c argc is greater than \c 1
* \pre \c argv is \e not \c NULL
* \pre \c error is \e not \c NULL
*
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "flog.h"
#include "common.h"

int
main(int argc, char *argv[]) {

if (argc == 1 ) {
if (argc == 1 && isatty(fileno(stdin))) {
flog_usage();
return 1;
}
Expand Down
47 changes: 32 additions & 15 deletions test/test_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,21 @@

#define UNUSED(x) (void)(x)

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

FlogError error; // should pass non-null assertion (&error)
int mock_argc = 0; // should fail >1 assertion
char *mock_argv[] = {}; // should pass non-null assertion

expect_assert_failure(flog_config_new(mock_argc, mock_argv, &error));
}

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

FlogError error; // should pass non-null assertion (&error)
int mock_argc = 2; // should pass >1 assertion
int mock_argc = 0;
char **mock_argv = NULL; // should fail non-null assertion
FlogError error; // should pass non-null assertion (&error)

expect_assert_failure(flog_config_new(mock_argc, mock_argv, &error));
}

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

int mock_argc = 2; // should pass >1 assertion
int mock_argc = 0;
char *mock_argv[] = {}; // should pass non-null assertion

expect_assert_failure(flog_config_new(mock_argc, mock_argv, NULL));
Expand Down Expand Up @@ -681,6 +670,34 @@ flog_config_new_with_long_append_opt_and_path_succeeds(void **state) {
flog_config_free(config);
}

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

FlogError error = TEST_ERROR;
MOCK_ARGS(
TEST_PROGRAM_NAME
)

char *message = "0123456789ABCDEF";

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

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

// Ensure stdin stream is restored before making assertions in order to avoid impacting
// other tests if an assertion fails and leaves stdin pointing to the in-memory buffer
fclose(stdin);
stdin = saved_stdin;

assert_non_null(config);
assert_int_equal(error, FLOG_ERROR_NONE);
assert_string_equal(flog_config_get_message(config), message);

flog_config_free(config);
}

static void
flog_config_new_with_long_version_opt_succeeds(void **state) {
UNUSED(state);
Expand Down Expand Up @@ -1325,7 +1342,6 @@ int main(void) {

const struct CMUnitTest tests[] = {
// flog_config_new() precondition tests
cmocka_unit_test(flog_config_new_with_zero_arg_count_fails),
cmocka_unit_test(flog_config_new_with_null_arg_values_fails),

// flog_config_new() failure tests
Expand Down Expand Up @@ -1359,6 +1375,7 @@ int main(void) {
cmocka_unit_test(flog_config_new_with_long_level_opt_and_fault_value_succeeds),
cmocka_unit_test(flog_config_new_with_long_private_opt_and_message_succeeds),
cmocka_unit_test(flog_config_new_with_long_append_opt_and_path_succeeds),
cmocka_unit_test(flog_config_new_with_message_from_stream_succeeds),
cmocka_unit_test(flog_config_new_with_short_version_opt_succeeds),
cmocka_unit_test(flog_config_new_with_long_version_opt_succeeds),
cmocka_unit_test(flog_config_new_with_short_help_opt_succeeds),
Expand Down

0 comments on commit 04c9fa3

Please sign in to comment.