ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
Testing

Files

file  common.c
 Common test utilities implementation.
file  globals.c
 ๐Ÿ”— Global symbol stubs for test executables to satisfy linker dependencies
file  logging.c
 ๐Ÿงช Test utilities for logging: stdout/stderr redirection and capture helpers
file  common.h
 Common test utilities and environment detection.
file  test_env.h
 Test environment detection utilities.

Macros

#define TESTS_COMMON_VERSION_BUFFER_SIZE   256

Functions

int test_logging_disable (bool disable_stdout, bool disable_stderr)
 Disable stdout/stderr output for quiet test execution.
int test_logging_restore (void)
 Restore stdout/stderr output after test logging disable.
bool test_logging_is_disabled (void)
 Check if logging is currently disabled.
const char * test_get_binary_path (void)
 Get the path to the ascii-chat binary for integration tests.

Detailed Description

This header provides only environment and executable-path helpers for tests. Test files include their own Catch2 and project headers directly.

CORE FEATURES:

  • Test environment detection (CI, Docker, WSL)
  • Platform-specific environment checks (POSIX/Windows)
  • Headless environment detection for webcam tests

TEST ENVIRONMENT:

This header includes only the C library headers required by its inline environment helpers. Catch2, logging, and project APIs are intentionally included by each test that uses them.

HEADLESS ENVIRONMENT DETECTION:

Tests that require hardware (like webcam) can use test_is_in_headless_environment() to skip when running in CI/Docker/WSL.

Note
This header must be included before any test code that uses the test environment detection functions.
Include this header only when a test needs one of these helpers.
Author
Zachary Fogg me@zf.nosp@m.o.gg
Date
August 2025

This header provides test environment detection functions that can be used by both test code and production code (to adjust behavior like timeouts).

Unlike common.h, this header has NO Catch2 dependency, so it can be safely included by any code that needs to detect if it's running in a test environment.

Note
This is the canonical location for test environment detection. Do NOT duplicate this logic in other files.

Macro Definition Documentation

◆ TESTS_COMMON_VERSION_BUFFER_SIZE

#define TESTS_COMMON_VERSION_BUFFER_SIZE   256

Function Documentation

◆ test_get_binary_path()

const char * test_get_binary_path ( void )

#include <include/ascii-chat/tests/common.h>

Get the path to the ascii-chat binary for integration tests.

This function finds the ascii-chat binary by trying multiple candidate paths. It handles both direct test invocation from the repo root and ctest invocation from the build directory.

Search order:

  1. BUILD_DIR environment variable (if set)
  2. ./build_docker/bin/ascii-chat (Docker from repo root)
  3. ./build/bin/ascii-chat (local from repo root)
  4. ./bin/ascii-chat (from build directory - ctest)
  5. /app/build_docker/bin/ascii-chat (Docker absolute)
Returns
Path to the ascii-chat binary, or a fallback path if not found
Note
The returned string is static - do not free it.
On Windows, returns paths with .exe extension.
Example
Test(my_suite, integration_test) {
const char *binary = test_get_binary_path();
pid_t pid = fork();
if (pid == 0) {
execl(binary, "ascii-chat", "server", "--help", NULL);
exit(127);
}
// ...
}
const char * test_get_binary_path(void)
Get the path to the ascii-chat binary for integration tests.

Definition at line 19 of file tests/common.c.

19 {
20 static char binary_path[TESTS_COMMON_BINARY_PATH_BUFFER_SIZE];
21 static bool initialized = false;
22
23 if (initialized) {
24 return binary_path;
25 }
26
27#ifdef _WIN32
28 // Windows: try several paths
29 const char *candidates[] = {
30 "./build/bin/ascii-chat.exe",
31 "./bin/ascii-chat.exe",
32 "ascii-chat.exe",
33 };
34 const char *fallback = "./build/bin/ascii-chat.exe";
35#else
36 // Check if we're in Docker (/.dockerenv file exists)
37 bool in_docker = (access("/.dockerenv", F_OK) == 0);
38 const char *build_dir = SAFE_GETENV("BUILD_DIR");
39
40 // Try BUILD_DIR first if set
41 if (build_dir) {
42 safe_snprintf(binary_path, sizeof(binary_path), "%s/bin/ascii-chat", build_dir);
43 if (access(binary_path, X_OK) == 0) {
44 initialized = true;
45 return binary_path;
46 }
47 }
48
49 // Try several paths in order of preference
50 const char *candidates[] = {
51 // Relative paths from repo root
52 in_docker ? "./build_docker/bin/ascii-chat" : "./build/bin/ascii-chat",
53 // Relative paths from build directory (when ctest runs from there)
54 "./bin/ascii-chat",
55 // Absolute paths for Docker
56 in_docker ? "/app/build_docker/bin/ascii-chat" : NULL,
57 };
58 const char *fallback = in_docker ? "./build_docker/bin/ascii-chat" : "./build/bin/ascii-chat";
59#endif
60
61 // Try each candidate path
62 for (size_t i = 0; i < sizeof(candidates) / sizeof(candidates[0]); i++) {
63 if (candidates[i] && access(candidates[i], X_OK) == 0) {
64 safe_snprintf(binary_path, sizeof(binary_path), "%s", candidates[i]);
65 initialized = true;
66 return binary_path;
67 }
68 }
69
70 // Fallback to default (will likely fail but gives useful error)
71 safe_snprintf(binary_path, sizeof(binary_path), "%s", fallback);
72 initialized = true;
73 return binary_path;
74}
#define SAFE_GETENV(name)
bool initialized
Definition mmap.c:41
#define TESTS_COMMON_BINARY_PATH_BUFFER_SIZE
Definition tests/common.c:8

References initialized, SAFE_GETENV, and TESTS_COMMON_BINARY_PATH_BUFFER_SIZE.

◆ test_logging_disable()

int test_logging_disable ( bool disable_stdout,
bool disable_stderr )

#include <lib/tests/logging.c>

Disable stdout/stderr output for quiet test execution.

Parameters
disable_stdoutIf true, redirect stdout to /dev/null
disable_stderrIf true, redirect stderr to /dev/null
Returns
0 on success, -1 on error

Definition at line 33 of file tests/logging.c.

33 {
34 // If already disabled, return success
35 if (logging_disabled) {
36 return 0;
37 }
38
39 // Open /dev/null for writing
40 dev_null_fd = platform_open("test-logging", "/dev/null", PLATFORM_O_WRONLY);
41 if (dev_null_fd == -1) {
42 return -1;
43 }
44
45 // Save original file descriptors if we need to redirect them
46 if (disable_stdout) {
47 original_stdout_fd = dup(STDOUT_FILENO);
48 if (original_stdout_fd == -1) {
49 close(dev_null_fd);
50 dev_null_fd = -1;
51 return -1;
52 }
53 dup2(dev_null_fd, STDOUT_FILENO);
54 // Reopen stdout to use the redirected file descriptor
55 (void)freopen("/dev/null", "w", stdout);
56 // Make stdout unbuffered to ensure immediate redirection
57 (void)setvbuf(stdout, NULL, _IONBF, 0);
58 }
59
60 if (disable_stderr) {
61 original_stderr_fd = dup(STDERR_FILENO);
62 if (original_stderr_fd == -1) {
63 // Clean up stdout if it was redirected
64 if (disable_stdout && original_stdout_fd != -1) {
65 dup2(original_stdout_fd, STDOUT_FILENO);
66 close(original_stdout_fd);
67 original_stdout_fd = -1;
68 }
69 close(dev_null_fd);
70 dev_null_fd = -1;
71 return -1;
72 }
73 dup2(dev_null_fd, STDERR_FILENO);
74 // Reopen stderr to use the redirected file descriptor
75 (void)freopen("/dev/null", "w", stderr);
76 // Make stderr unbuffered to ensure immediate redirection
77 (void)setvbuf(stderr, NULL, _IONBF, 0);
78 }
79
80 logging_disabled = true;
81 return 0;
82}
int platform_open(const char *name, const char *pathname, int flags,...)
Definition filesystem.c:65

References platform_open().

◆ test_logging_is_disabled()

bool test_logging_is_disabled ( void )

#include <lib/tests/logging.c>

Check if logging is currently disabled.

Returns
true if logging is disabled, false otherwise

Definition at line 132 of file tests/logging.c.

132 {
133 return logging_disabled;
134}

◆ test_logging_restore()

int test_logging_restore ( void )

#include <lib/tests/logging.c>

Restore stdout/stderr output after test logging disable.

Returns
0 on success, -1 on error

Definition at line 89 of file tests/logging.c.

89 {
90 // If not disabled, return success
91 if (!logging_disabled) {
92 return 0;
93 }
94
95 // Restore original stdout
96 if (original_stdout_fd != -1) {
97 dup2(original_stdout_fd, STDOUT_FILENO);
98 // Reopen stdout to use the restored file descriptor
99 (void)freopen("/dev/stdout", "w", stdout);
100 // Restore line buffering for stdout
101 (void)setvbuf(stdout, NULL, _IOLBF, 0);
102 close(original_stdout_fd);
103 original_stdout_fd = -1;
104 }
105
106 // Restore original stderr
107 if (original_stderr_fd != -1) {
108 dup2(original_stderr_fd, STDERR_FILENO);
109 // Reopen stderr to use the restored file descriptor
110 (void)freopen("/dev/stderr", "w", stderr);
111 // Restore unbuffered mode for stderr
112 (void)setvbuf(stderr, NULL, _IONBF, 0);
113 close(original_stderr_fd);
114 original_stderr_fd = -1;
115 }
116
117 // Close /dev/null file descriptor
118 if (dev_null_fd != -1) {
119 close(dev_null_fd);
120 dev_null_fd = -1;
121 }
122
123 logging_disabled = false;
124 return 0;
125}