config: 添加用户配置加载流程和默认配置

This commit is contained in:
2026-03-24 02:40:57 +08:00
parent 67f0c169f5
commit 3e09dec613
12 changed files with 772 additions and 18 deletions

View File

@@ -0,0 +1,74 @@
set(CONFIG_FIXTURE_TARGET "zdwm-config-fixture")
set(CONFIG_FIXTURE_NO_SETUP_TARGET "zdwm-config-fixture-no-setup")
set(CONFIG_LOADER_TEST_APP_NAME "zdwm-config-loader-tests")
set(RUNTIME_CONFIG_TEST_APP_NAME "zdwm-runtime-config-tests")
add_library(${CONFIG_FIXTURE_TARGET} SHARED
${CMAKE_CURRENT_SOURCE_DIR}/fixture_valid_config.c
)
target_include_directories(${CONFIG_FIXTURE_TARGET}
PRIVATE ${INCLUDE_DIR}
)
add_library(${CONFIG_FIXTURE_NO_SETUP_TARGET} SHARED
${CMAKE_CURRENT_SOURCE_DIR}/fixture_without_setup.c
)
add_executable(${CONFIG_LOADER_TEST_APP_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/config_loader_test.c
${SOURCE_DIR}/base/log.c
${SOURCE_DIR}/config/loader.c
)
target_include_directories(${CONFIG_LOADER_TEST_APP_NAME} SYSTEM
PRIVATE ${INCLUDE_DIR}
)
target_include_directories(${CONFIG_LOADER_TEST_APP_NAME}
PRIVATE ${SOURCE_DIR}
PRIVATE ${BUILD_DIR}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${CONFIG_LOADER_TEST_APP_NAME}
PRIVATE ${CMAKE_DL_LIBS}
)
add_dependencies(${CONFIG_LOADER_TEST_APP_NAME}
${CONFIG_FIXTURE_TARGET}
${CONFIG_FIXTURE_NO_SETUP_TARGET}
)
add_executable(${RUNTIME_CONFIG_TEST_APP_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/runtime_config_test.c
${SOURCE_DIR}/base/log.c
${SOURCE_DIR}/config/defaults.c
${SOURCE_DIR}/config/loader.c
${SOURCE_DIR}/config/runtime_config.c
${SOURCE_DIR}/core/layout.c
${SOURCE_DIR}/core/runtime.c
${SOURCE_DIR}/core/state.c
)
target_include_directories(${RUNTIME_CONFIG_TEST_APP_NAME} SYSTEM
PRIVATE ${INCLUDE_DIR}
)
target_include_directories(${RUNTIME_CONFIG_TEST_APP_NAME}
PRIVATE ${SOURCE_DIR}
PRIVATE ${BUILD_DIR}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${RUNTIME_CONFIG_TEST_APP_NAME}
PRIVATE ${CMAKE_DL_LIBS}
)
add_dependencies(${RUNTIME_CONFIG_TEST_APP_NAME}
${CONFIG_FIXTURE_TARGET}
${CONFIG_FIXTURE_NO_SETUP_TARGET}
)
add_test(NAME ${CONFIG_LOADER_TEST_APP_NAME}
COMMAND $<TARGET_FILE:${CONFIG_LOADER_TEST_APP_NAME}>
$<TARGET_FILE:${CONFIG_FIXTURE_TARGET}>
$<TARGET_FILE:${CONFIG_FIXTURE_NO_SETUP_TARGET}>
)
add_test(NAME ${RUNTIME_CONFIG_TEST_APP_NAME}
COMMAND $<TARGET_FILE:${RUNTIME_CONFIG_TEST_APP_NAME}>
$<TARGET_FILE:${CONFIG_FIXTURE_TARGET}>
$<TARGET_FILE:${CONFIG_FIXTURE_NO_SETUP_TARGET}>
)

View File

@@ -0,0 +1,173 @@
#include "config/loader.h"
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "helpers.h"
static void test_config_loader_loads_explicit_override(const char *fixture_path) {
config_test_clear_env();
config_loader_t loader = {0};
assert(config_loader_load(&loader, fixture_path));
assert(loader.path);
assert(strcmp(loader.path, fixture_path) == 0);
assert(loader.handle);
assert(loader.setup);
config_loader_cleanup(&loader);
}
static void test_config_loader_override_has_priority_over_env(
const char *fixture_path) {
config_test_clear_env();
assert(setenv("ZDWM_CONFIG_LIB", "/tmp/definitely-missing-config.so", 1) == 0);
config_loader_t loader = {0};
assert(config_loader_load(&loader, fixture_path));
assert(loader.path);
assert(strcmp(loader.path, fixture_path) == 0);
assert(loader.handle);
assert(loader.setup);
config_loader_cleanup(&loader);
}
static void test_config_loader_uses_zdwm_config_lib_env(
const char *fixture_path) {
config_test_clear_env();
assert(setenv("ZDWM_CONFIG_LIB", fixture_path, 1) == 0);
config_loader_t loader = {0};
assert(config_loader_load(&loader, nullptr));
assert(loader.path);
assert(strcmp(loader.path, fixture_path) == 0);
assert(loader.handle);
assert(loader.setup);
config_loader_cleanup(&loader);
}
static void test_config_loader_uses_xdg_config_home(const char *fixture_path) {
config_test_clear_env();
char temp_root[PATH_MAX] = {0};
char config_dir[PATH_MAX] = {0};
char config_path[PATH_MAX] = {0};
config_test_make_temp_dir(temp_root, sizeof(temp_root),
"zdwm-config-loader-xdg");
config_test_join_path(config_dir, sizeof(config_dir), temp_root, "zdwm");
config_test_join_path(config_path, sizeof(config_path), config_dir,
"config.so");
config_test_mkdir(config_dir);
config_test_symlink(fixture_path, config_path);
assert(setenv("XDG_CONFIG_HOME", temp_root, 1) == 0);
config_loader_t loader = {0};
assert(config_loader_load(&loader, nullptr));
assert(loader.path);
assert(strcmp(loader.path, config_path) == 0);
assert(loader.handle);
assert(loader.setup);
config_loader_cleanup(&loader);
config_test_unlink(config_path);
config_test_rmdir(config_dir);
config_test_rmdir(temp_root);
}
static void test_config_loader_uses_home_fallback(const char *fixture_path) {
config_test_clear_env();
char temp_root[PATH_MAX] = {0};
char config_root[PATH_MAX] = {0};
char zdwm_dir[PATH_MAX] = {0};
char config_path[PATH_MAX] = {0};
config_test_make_temp_dir(temp_root, sizeof(temp_root),
"zdwm-config-loader-home");
config_test_join_path(config_root, sizeof(config_root), temp_root, ".config");
config_test_join_path(zdwm_dir, sizeof(zdwm_dir), config_root, "zdwm");
config_test_join_path(config_path, sizeof(config_path), zdwm_dir,
"config.so");
config_test_mkdir(config_root);
config_test_mkdir(zdwm_dir);
config_test_symlink(fixture_path, config_path);
assert(setenv("HOME", temp_root, 1) == 0);
config_loader_t loader = {0};
assert(config_loader_load(&loader, nullptr));
assert(loader.path);
assert(strcmp(loader.path, config_path) == 0);
assert(loader.handle);
assert(loader.setup);
config_loader_cleanup(&loader);
config_test_unlink(config_path);
config_test_rmdir(zdwm_dir);
config_test_rmdir(config_root);
config_test_rmdir(temp_root);
}
static void test_config_loader_allows_missing_implicit_library(void) {
config_test_clear_env();
char temp_root[PATH_MAX] = {0};
char expected_path[PATH_MAX] = {0};
config_test_make_temp_dir(temp_root, sizeof(temp_root),
"zdwm-config-loader-missing");
config_test_join_path(expected_path, sizeof(expected_path), temp_root,
"zdwm/config.so");
assert(setenv("XDG_CONFIG_HOME", temp_root, 1) == 0);
config_loader_t loader = {0};
assert(config_loader_load(&loader, nullptr));
assert(loader.path);
assert(strcmp(loader.path, expected_path) == 0);
assert(loader.handle == nullptr);
assert(loader.setup == nullptr);
config_loader_cleanup(&loader);
config_test_rmdir(temp_root);
}
static void test_config_loader_rejects_missing_explicit_library(void) {
config_test_clear_env();
config_loader_t loader = {0};
assert(!config_loader_load(&loader, "/tmp/definitely-missing-config.so"));
assert(loader.path == nullptr);
assert(loader.handle == nullptr);
assert(loader.setup == nullptr);
}
static void test_config_loader_rejects_library_without_setup(
const char *fixture_path) {
config_test_clear_env();
config_loader_t loader = {0};
assert(!config_loader_load(&loader, fixture_path));
assert(loader.path == nullptr);
assert(loader.handle == nullptr);
assert(loader.setup == nullptr);
}
int main(int argc, char **argv) {
assert(argc == 3);
test_config_loader_loads_explicit_override(argv[1]);
test_config_loader_override_has_priority_over_env(argv[1]);
test_config_loader_uses_zdwm_config_lib_env(argv[1]);
test_config_loader_uses_xdg_config_home(argv[1]);
test_config_loader_uses_home_fallback(argv[1]);
test_config_loader_allows_missing_implicit_library();
test_config_loader_rejects_missing_explicit_library();
test_config_loader_rejects_library_without_setup(argv[2]);
return 0;
}

View File

@@ -0,0 +1,28 @@
#include <zdwm/config.h>
static bool fixture_layout(const zdwm_layout_ctx_t *ctx,
zdwm_layout_result_t *out) {
if (!ctx || !out) return false;
zdwm_layout_result_reset(out);
if (!ctx->window_ids || ctx->window_count == 0) return true;
zdwm_layout_result_push(out, (zdwm_layout_item_t){
.window_id = ctx->window_ids[0],
.rect = ctx->workarea,
});
return true;
}
bool setup(const zdwm_api_t *api, zdwm_config_builder_t *builder,
const zdwm_output_info_t *outputs, size_t output_count) {
if (!api || !builder || !outputs || output_count == 0) return false;
zdwm_layout_id_t layout_id =
api->register_layout(builder, "test-only", "[T]", "fixture layout",
fixture_layout);
if (layout_id == ZDWM_LAYOUT_ID_INVALID) return false;
return api->define_workspace(builder, 0, "from-so", &layout_id, 1,
layout_id) != ZDWM_WORKSPACE_ID_INVALID;
}

View File

@@ -0,0 +1 @@
int zdwm_config_fixture_without_setup_symbol(void) { return 1; }

48
tests/config/helpers.h Normal file
View File

@@ -0,0 +1,48 @@
#pragma once
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static inline void config_test_clear_env(void) {
assert(unsetenv("ZDWM_CONFIG_LIB") == 0);
assert(unsetenv("XDG_CONFIG_HOME") == 0);
assert(unsetenv("HOME") == 0);
}
static inline void config_test_make_temp_dir(char *path, size_t path_size,
const char *prefix) {
int len = snprintf(path, path_size, "/tmp/%s-XXXXXX", prefix);
assert(len > 0);
assert((size_t)len < path_size);
assert(mkdtemp(path) == path);
}
static inline void config_test_join_path(char *out, size_t out_size,
const char *base,
const char *suffix) {
int len = snprintf(out, out_size, "%s/%s", base, suffix);
assert(len > 0);
assert((size_t)len < out_size);
}
static inline void config_test_mkdir(const char *path) {
assert(mkdir(path, 0700) == 0);
}
static inline void config_test_symlink(const char *target, const char *path) {
assert(symlink(target, path) == 0);
}
static inline void config_test_unlink(const char *path) {
assert(unlink(path) == 0);
}
static inline void config_test_rmdir(const char *path) {
assert(rmdir(path) == 0);
}

View File

@@ -0,0 +1,213 @@
#include "config/runtime_config.h"
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "core/layout.h"
#include "core/runtime.h"
#include "helpers.h"
struct backend_t {
int unused;
};
void backend_destroy(backend_t *backend) { free(backend); }
static backend_t *test_backend_create(void) {
return calloc(1, sizeof(backend_t));
}
static void assert_default_layouts(const runtime_init_desc_t *desc) {
assert(desc);
assert(layout_registry_count(&desc->layouts) == 3);
const layout_slot_t *tile = layout_registry_at(&desc->layouts, 0);
const layout_slot_t *monocle = layout_registry_at(&desc->layouts, 1);
const layout_slot_t *floating = layout_registry_at(&desc->layouts, 2);
assert(tile);
assert(monocle);
assert(floating);
assert(strcmp(tile->name, "tile") == 0);
assert(strcmp(monocle->name, "monocle") == 0);
assert(strcmp(floating->name, "floating") == 0);
}
static void test_runtime_config_loads_user_library(const char *fixture_path) {
config_test_clear_env();
output_info_t outputs[] = {
{
.name = "HDMI-A-1",
.geometry = {.x = 0, .y = 0, .width = 1920, .height = 1080},
},
};
runtime_init_desc_t desc = {
.outputs = outputs,
.output_count = 1,
};
assert(runtime_config_load(fixture_path, &desc));
assert(desc.config_module_handle != nullptr);
assert(layout_registry_count(&desc.layouts) == 1);
assert(desc.workspace_count == 1);
assert(strcmp(desc.workspaces[0].name, "from-so") == 0);
assert(desc.workspaces[0].output_index == 0);
assert(desc.workspaces[0].layout_count == 1);
assert(desc.workspaces[0].initial_layout_id == 0);
const layout_slot_t *layout = layout_registry_at(&desc.layouts, 0);
assert(layout);
assert(strcmp(layout->name, "test-only") == 0);
assert(strcmp(layout->symbol, "[T]") == 0);
runtime_config_cleanup(&desc);
}
static void test_runtime_config_keeps_module_loaded_after_runtime_init(
const char *fixture_path) {
config_test_clear_env();
output_info_t outputs[] = {
{
.name = "HDMI-A-1",
.geometry = {.x = 0, .y = 0, .width = 1920, .height = 1080},
},
};
window_id_t window_ids[] = {42};
runtime_init_desc_t desc = {
.backend = test_backend_create(),
.outputs = outputs,
.output_count = 1,
};
assert(desc.backend);
assert(runtime_config_load(fixture_path, &desc));
runtime_t runtime = {0};
assert(runtime_init(&runtime, &desc));
assert(desc.config_module_handle == nullptr);
runtime_init_desc_cleanup(&desc);
assert(runtime.config_module_handle != nullptr);
layout_fn layout = layout_get(&runtime.layouts, 0);
assert(layout != nullptr);
layout_result_t result = {0};
layout_result_init(&result);
layout_ctx_t ctx = {
.workspace_id = 0,
.focused_window_id = window_ids[0],
.workarea = {.x = 11, .y = 22, .width = 333, .height = 444},
.window_ids = window_ids,
.window_count = 1,
};
assert(layout(&ctx, &result));
assert(result.item_count == 1);
assert(result.items[0].window_id == window_ids[0]);
assert(result.items[0].rect.x == ctx.workarea.x);
assert(result.items[0].rect.y == ctx.workarea.y);
assert(result.items[0].rect.width == ctx.workarea.width);
assert(result.items[0].rect.height == ctx.workarea.height);
layout_result_cleanup(&result);
runtime_shutdown(&runtime);
assert(runtime.config_module_handle == nullptr);
}
static void test_runtime_config_falls_back_to_defaults_when_implicit_library_is_missing(
void) {
config_test_clear_env();
output_info_t outputs[] = {
{
.name = "eDP-1",
.geometry = {.x = 0, .y = 0, .width = 1920, .height = 1080},
},
{
.name = "DP-1",
.geometry = {.x = 1920, .y = 0, .width = 2560, .height = 1440},
},
};
char temp_root[PATH_MAX] = {0};
config_test_make_temp_dir(temp_root, sizeof(temp_root),
"zdwm-runtime-config-missing");
assert(setenv("XDG_CONFIG_HOME", temp_root, 1) == 0);
runtime_init_desc_t desc = {
.outputs = outputs,
.output_count = 2,
};
assert(runtime_config_load(nullptr, &desc));
assert(desc.config_module_handle == nullptr);
assert_default_layouts(&desc);
assert(desc.workspace_count == 2);
assert(strcmp(desc.workspaces[0].name, "main") == 0);
assert(strcmp(desc.workspaces[1].name, "main") == 0);
assert(desc.workspaces[0].output_index == 0);
assert(desc.workspaces[1].output_index == 1);
runtime_config_cleanup(&desc);
config_test_rmdir(temp_root);
}
static void test_runtime_config_rejects_missing_explicit_library(void) {
config_test_clear_env();
output_info_t outputs[] = {
{
.name = "HDMI-A-1",
.geometry = {.x = 0, .y = 0, .width = 1920, .height = 1080},
},
};
runtime_init_desc_t desc = {
.outputs = outputs,
.output_count = 1,
};
assert(!runtime_config_load("/tmp/definitely-missing-config.so", &desc));
assert(layout_registry_count(&desc.layouts) == 0);
assert(desc.workspace_count == 0);
assert(desc.workspaces == nullptr);
assert(desc.config_module_handle == nullptr);
}
static void test_runtime_config_rejects_library_without_setup(
const char *fixture_path) {
config_test_clear_env();
output_info_t outputs[] = {
{
.name = "HDMI-A-1",
.geometry = {.x = 0, .y = 0, .width = 1920, .height = 1080},
},
};
runtime_init_desc_t desc = {
.outputs = outputs,
.output_count = 1,
};
assert(!runtime_config_load(fixture_path, &desc));
assert(layout_registry_count(&desc.layouts) == 0);
assert(desc.workspace_count == 0);
assert(desc.workspaces == nullptr);
assert(desc.config_module_handle == nullptr);
}
int main(int argc, char **argv) {
assert(argc == 3);
test_runtime_config_loads_user_library(argv[1]);
test_runtime_config_keeps_module_loaded_after_runtime_init(argv[1]);
test_runtime_config_falls_back_to_defaults_when_implicit_library_is_missing();
test_runtime_config_rejects_missing_explicit_library();
test_runtime_config_rejects_library_without_setup(argv[2]);
return 0;
}