Skip to content

Commit

Permalink
added unit test for log_target_to_str
Browse files Browse the repository at this point in the history
Fixes: #805

It adds an unit test for log_target_to_str. Also, it fixes
mixed up stderr and stderr-full in the function under test.

Signed-off-by: Michael Engel <mengel@redhat.com>
  • Loading branch information
engelmi authored and mwperina committed Mar 7, 2024
1 parent d524dc3 commit f2d7946
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libbluechi/log/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ const char *log_target_to_str(LogFn logfn) {
log_target = BC_LOG_TARGET_JOURNALD;
}
if (bc_log_to_stderr_full_with_location == logfn) {
log_target = BC_LOG_TARGET_STDERR;
log_target = BC_LOG_TARGET_STDERR_FULL;
}
if (bc_log_to_stderr_with_location == logfn) {
log_target = BC_LOG_TARGET_STDERR_FULL;
log_target = BC_LOG_TARGET_STDERR;
}

return log_target;
Expand Down
38 changes: 38 additions & 0 deletions src/libbluechi/test/log/log_target_to_str_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "libbluechi/common/string-util.h"
#include "libbluechi/log/log.h"

bool test_log_target_to_str(LogFn fn, const char *expected_log_target) {
const char *result = log_target_to_str(fn);
if (result != NULL && expected_log_target != NULL && streq(result, expected_log_target)) {
return true;
}
if (result == NULL && expected_log_target == NULL) {
return true;
}
fprintf(stdout,
"FAILED: string_to_log_level - Expected '%s', but got '%s'\n",
expected_log_target,
result);
return false;
}

int main() {
bool result = true;

result = result && test_log_target_to_str(bc_log_to_journald_with_location, BC_LOG_TARGET_JOURNALD);
result = result &&
test_log_target_to_str(bc_log_to_stderr_full_with_location, BC_LOG_TARGET_STDERR_FULL);
result = result && test_log_target_to_str(bc_log_to_stderr_with_location, BC_LOG_TARGET_STDERR);
result = result && test_log_target_to_str(NULL, NULL);

if (result) {
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
1 change: 1 addition & 0 deletions src/libbluechi/test/log/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

common_src = [
'string_to_log_level_test',
'log_target_to_str_test',
'bc_log_init_test',
]

Expand Down

0 comments on commit f2d7946

Please sign in to comment.