Add noreturn declaration compatible with the MSVC compiler.
[libgit2.git] / tests / t0020-dirent.c
blob966f3c578c405f92510a54de59ef2263a8b217a9
1 #include <stdarg.h>
2 #include "test_lib.h"
3 #include "fileops.h"
6 typedef struct name_data {
7 int count; /* return count */
8 char *name; /* filename */
9 } name_data;
11 typedef struct walk_data {
12 char *sub; /* sub-directory name */
13 name_data *names; /* name state data */
14 } walk_data;
17 static char path_buffer[GIT_PATH_MAX];
18 static char *top_dir = "dir-walk";
19 static walk_data *state_loc;
22 static int error(const char *fmt, ...)
24 va_list ap;
26 va_start(ap, fmt);
27 vfprintf(stderr, fmt, ap);
28 va_end(ap);
29 fprintf(stderr, "\n");
30 return -1;
33 static int setup(walk_data *d)
35 name_data *n;
37 if (gitfo_mkdir(top_dir, 0755) < 0)
38 return error("can't mkdir(\"%s\")", top_dir);
40 if (gitfo_chdir(top_dir) < 0)
41 return error("can't chdir(\"%s\")", top_dir);
43 if (strcmp(d->sub, ".") != 0)
44 if (gitfo_mkdir(d->sub, 0755) < 0)
45 return error("can't mkdir(\"%s\")", d->sub);
47 strcpy(path_buffer, d->sub);
48 state_loc = d;
50 for (n = d->names; n->name; n++) {
51 git_file fd = gitfo_creat(n->name, 0600);
52 must_be_true(fd >= 0);
53 gitfo_close(fd);
54 n->count = 0;
57 return 0;
60 static int knockdown(walk_data *d)
62 name_data *n;
64 for (n = d->names; n->name; n++) {
65 if (gitfo_unlink(n->name) < 0)
66 return error("can't unlink(\"%s\")", n->name);
69 if (strcmp(d->sub, ".") != 0)
70 if (gitfo_rmdir(d->sub) < 0)
71 return error("can't rmdir(\"%s\")", d->sub);
73 if (gitfo_chdir("..") < 0)
74 return error("can't chdir(\"..\")");
76 if (gitfo_rmdir(top_dir) < 0)
77 return error("can't rmdir(\"%s\")", top_dir);
79 return 0;
82 static int check_counts(walk_data *d)
84 int ret = 0;
85 name_data *n;
87 for (n = d->names; n->name; n++) {
88 if (n->count != 1)
89 ret = error("count (%d, %s)", n->count, n->name);
91 return ret;
94 static int one_entry(void *state, char *path)
96 walk_data *d = (walk_data *) state;
97 name_data *n;
99 must_be_true(state == state_loc);
100 must_be_true(path == path_buffer);
101 for (n = d->names; n->name; n++) {
102 if (!strcmp(n->name, path)) {
103 n->count++;
104 return 0;
107 test_die("unexpected path \"%s\"", path);
111 static name_data dot_names[] = {
112 { 0, "./a" },
113 { 0, "./asdf" },
114 { 0, "./pack-foo.pack" },
115 { 0, NULL }
117 static walk_data dot = {
118 ".",
119 dot_names
122 BEGIN_TEST(dot)
124 must_pass(setup(&dot));
126 must_pass(gitfo_dirent(path_buffer,
127 sizeof(path_buffer),
128 one_entry,
129 &dot));
131 must_pass(check_counts(&dot));
133 must_pass(knockdown(&dot));
134 END_TEST
136 static name_data sub_names[] = {
137 { 0, "sub/a" },
138 { 0, "sub/asdf" },
139 { 0, "sub/pack-foo.pack" },
140 { 0, NULL }
142 static walk_data sub = {
143 "sub",
144 sub_names
147 BEGIN_TEST(sub)
149 must_pass(setup(&sub));
151 must_pass(gitfo_dirent(path_buffer,
152 sizeof(path_buffer),
153 one_entry,
154 &sub));
156 must_pass(check_counts(&sub));
158 must_pass(knockdown(&sub));
159 END_TEST
161 static walk_data sub_slash = {
162 "sub/",
163 sub_names
166 BEGIN_TEST(sub_slash)
168 must_pass(setup(&sub_slash));
170 must_pass(gitfo_dirent(path_buffer,
171 sizeof(path_buffer),
172 one_entry,
173 &sub_slash));
175 must_pass(check_counts(&sub_slash));
177 must_pass(knockdown(&sub_slash));
178 END_TEST
180 static name_data empty_names[] = {
181 { 0, NULL }
183 static walk_data empty = {
184 "empty",
185 empty_names
188 static int dont_call_me(void *state, char *path)
190 test_die("dont_call_me: unexpected callback!");
193 BEGIN_TEST(empty)
195 must_pass(setup(&empty));
197 must_pass(gitfo_dirent(path_buffer,
198 sizeof(path_buffer),
199 one_entry,
200 &empty));
202 must_pass(check_counts(&empty));
204 /* make sure callback not called */
205 must_pass(gitfo_dirent(path_buffer,
206 sizeof(path_buffer),
207 dont_call_me,
208 &empty));
210 must_pass(knockdown(&empty));
211 END_TEST
213 static name_data odd_names[] = {
214 { 0, "odd/.a" },
215 { 0, "odd/..c" },
216 /* the following don't work on cygwin/win32 */
217 /* { 0, "odd/.b." }, */
218 /* { 0, "odd/..d.." }, */
219 { 0, NULL }
221 static walk_data odd = {
222 "odd",
223 odd_names
226 BEGIN_TEST(odd)
228 must_pass(setup(&odd));
230 must_pass(gitfo_dirent(path_buffer,
231 sizeof(path_buffer),
232 one_entry,
233 &odd));
235 must_pass(check_counts(&odd));
237 must_pass(knockdown(&odd));
238 END_TEST