bootstrap/test/test_config.h

105 lines
2.7 KiB
C
Raw Normal View History

2023-11-23 18:02:40 +00:00
#ifndef _BOOTSTRAP_TEST_CONFIG
#define _BOOTSTRAP_TEST_CONFIG
2023-11-23 11:09:32 +00:00
#include <unistd.h>
2023-11-23 11:09:32 +00:00
#include "config.h"
#include "path.h"
2023-11-23 11:09:32 +00:00
#include "sput.h"
struct TestConfigFixture {
char *cwd;
char *root_dir;
char *target;
};
static struct TestConfigFixture *test_config_setup() {
char *cwd = getcwd(0, 0);
const char *segments[] = {cwd, "test", "specs"};
char *root_dir =
join_path_segments(sizeof(segments) / sizeof(char *), segments);
struct TestConfigFixture *fixture = malloc(sizeof(struct TestConfigFixture));
fixture->cwd = getcwd(0, 0);
fixture->root_dir = root_dir;
fixture->target = "minimal_spec_json";
return fixture;
}
static void test_config_teardown(struct TestConfigFixture *fixture) {
free(fixture->cwd);
free(fixture->root_dir);
free(fixture);
}
static void test_config_new_invalid_args() {
struct TestConfigFixture *fixture = test_config_setup();
2023-11-23 14:40:17 +00:00
struct Error *error = 0;
2023-11-23 15:07:22 +00:00
struct Config *config = 0;
error = config_new(0, fixture->root_dir, fixture->target, &config);
sput_fail_unless(error->code == ERROR_CONFIG_ENV_CWD_INVALID, "cwd == 0");
error_free(error);
error = config_new(fixture->cwd, 0, fixture->target, &config);
sput_fail_unless(
error->code == ERROR_CONFIG_ENV_ROOT_DIR_INVALID, "root_dir == 0"
);
error_free(error);
test_config_teardown(fixture);
2023-11-23 15:07:22 +00:00
}
2023-11-23 14:40:17 +00:00
static void test_config_new_spec_not_found() {
struct TestConfigFixture *fixture = test_config_setup();
struct Error *error = 0;
2023-11-23 15:07:22 +00:00
struct Config *config = 0;
error = config_new(fixture->cwd, fixture->root_dir, "not_found", &config);
sput_fail_unless(
error->code == ERROR_CONFIG_TARGET_NOT_FOUND, "target not found"
);
error_free(error);
test_config_teardown(fixture);
2023-11-23 11:09:32 +00:00
}
static void test_config_new_spec_not_dir() {
struct TestConfigFixture *fixture = test_config_setup();
struct Error *error = 0;
2023-11-23 14:40:17 +00:00
struct Config *config = 0;
error = config_new(fixture->cwd, fixture->root_dir, "not_dir", &config);
sput_fail_unless(
error->code == ERROR_CONFIG_TARGET_NOT_DIR, "target not dir"
);
error_free(error);
test_config_teardown(fixture);
2023-11-23 11:09:32 +00:00
}
static void test_config_new_success() {
struct TestConfigFixture *fixture = test_config_setup();
struct Error *error = 0;
2023-11-23 14:40:17 +00:00
struct Config *config = 0;
error = config_new(fixture->cwd, fixture->root_dir, fixture->target, &config);
sput_fail_unless(error == 0, "config_new() success");
sput_fail_unless(strcmp(config->cwd, fixture->cwd) == 0, "config_new() cwd");
sput_fail_unless(
strcmp(config->root_dir, fixture->root_dir) == 0, "config_new() root_dir"
);
sput_fail_unless(
strcmp(config->target, fixture->target) == 0, "config_new() target"
);
2023-11-23 14:40:17 +00:00
config_free(config);
test_config_teardown(fixture);
2023-11-23 11:09:32 +00:00
}
2023-11-23 18:02:40 +00:00
#endif /* _BOOTSTRAP_TEST_CONFIG */