21static void utest_file_exists(
char const * tmp_base_path);
22static void utest_file_contains(
char const * tmp_base_path);
23static void utest_file_path_join(
void);
24static void utest_remove_directory(
char const * tmp_base_path);
26int main(
int argc,
char ** argv) {
29 PUT_ERR(
"wrong number of arguments\n");
33 utest_file_exists(argv[1]);
34 utest_file_contains(argv[1]);
35 utest_file_path_join();
36 utest_remove_directory(argv[1]);
41static char * utest_make_tmp_path(
42 char const * tmp_base_path,
char const * name_prefix) {
52static char * utest_make_tmp_dir(
53 char const * tmp_base_path,
char const * name_prefix) {
55 char * path = utest_make_tmp_path(tmp_base_path, name_prefix);
60 path != NULL,
"mkdtemp failed for tmp_base_path=\"%s\", name_prefix=\"%s\"",
61 tmp_base_path, name_prefix);
68static char * utest_write_tmp_file(
69 char const * tmp_base_path,
char const * content) {
71 char * path = utest_make_tmp_path(tmp_base_path,
"test_io_utils_");
73 YAC_ASSERT_F(path != NULL,
"failed to create temporary path");
75 int fd = mkstemp(path);
77 YAC_ASSERT_F(fd != -1,
"mkstemp failed for path \"%s\"", path);
79 size_t len = strlen(content);
81 int status =
write(fd, content, len);
82 YAC_ASSERT_F(status == (
int)len,
"short write for path \"%s\"", path);
89static void utest_file_exists(
char const * tmp_base_path) {
94 char * path = utest_make_tmp_path(tmp_base_path,
"no_such_file_");
97 int fd = mkstemp(path);
104 PUT_ERR(
"yac_file_exists: expected 0 for non-existent file\n");
110 char * path = utest_write_tmp_file(tmp_base_path,
"hello\n");
113 PUT_ERR(
"yac_file_exists: expected 1 for existing file\n");
120static void utest_file_contains(
char const * tmp_base_path) {
123 char * path = utest_write_tmp_file(
124 tmp_base_path,
"first line\nneedle_token here\nlast line\n");
127 PUT_ERR(
"yac_file_contains: expected 1 when needle is present\n");
134 char * path = utest_write_tmp_file(
135 tmp_base_path,
"first line\nsecond line\nthird line\n");
138 PUT_ERR(
"yac_file_contains: expected 0 when needle is absent\n");
145static void utest_file_path_join(
void) {
150 if (strcmp(path,
"base/file.txt"))
152 "yac_file_path_join: unexpected join result for base and file.txt\n");
160 if (strcmp(path,
"/tmp/abc"))
161 PUT_ERR(
"yac_file_path_join: unexpected join result for /tmp and abc\n");
169 if (strcmp(path,
"root//child"))
171 "yac_file_path_join: unexpected join result for root/ and child\n");
179 if (strcmp(path,
"base/file.txt"))
181 "yac_file_path_join: unexpected join result for base, file, .txt\n");
189 if (strcmp(path,
"base/file"))
191 "yac_file_path_join: unexpected join result for base, file, \"\"\n");
197static void utest_remove_directory(
char const * tmp_base_path) {
202 char * dir = utest_make_tmp_dir(tmp_base_path,
"test_io_utils_dir_");
208 "yac_remove_directory: empty dir still exists after removal\n");
214 char * dir = utest_make_tmp_dir(tmp_base_path,
"test_io_utils_dir_");
215 char * file_path = utest_write_tmp_file(dir,
"content\n");
221 "yac_remove_directory: empty dir still exists after removal\n");
228 char * dir = utest_make_tmp_dir(tmp_base_path,
"test_io_utils_dir_");
229 char * subdir_path = utest_make_tmp_dir(dir,
"subdir_");
230 char * file_path = utest_write_tmp_file(subdir_path,
"nested content\n");
236 "yac_remove_directory: dir with subdir still exists after removal\n");
253 char * dir = utest_make_tmp_dir(tmp_base_path,
"test_io_utils_dir_");
254 char * subdir1 = utest_make_tmp_dir(dir,
"subdir1_");
255 char * file1 = utest_write_tmp_file(subdir1,
"file1 content\n");
256 char * subsubdir1 = utest_make_tmp_dir(subdir1,
"subsubdir1_");
257 char * file2 = utest_write_tmp_file(subsubdir1,
"file2 content\n");
258 char * subdir2 = utest_make_tmp_dir(dir,
"subdir2_");
259 char * file3 = utest_write_tmp_file(subdir2,
"file3 content\n");
260 char * file4 = utest_write_tmp_file(subdir2,
"file4 content\n");
266 "yac_remove_directory: complex dir structure still exists after removal\n");
#define YAC_ASSERT(exp, msg)
char * yac_file_path_join(char const *path_prefix, char const *path_suffix, char const *extension)
Join two path components with a '/' and optional extension.
void yac_remove_directory(char const *path)
Remove a directory and its contents recursively.
int yac_file_contains(char const *path, char const *needle)
Check whether a file contains a specific string.
int yac_file_exists(const char *filename)
Check whether a file exists.
#define YAC_ASSERT_F(exp, format,...)