Add unit tests for Commit parsing
[libgit2.git] / tests / t0020-dirent.c
blob2e6776cae6c71d745beb9e102a4a18429f3c4cb6
1 #include "test_lib.h"
2 #include "fileops.h"
5 typedef struct name_data {
6 int count; /* return count */
7 char *name; /* filename */
8 } name_data;
10 typedef struct walk_data {
11 char *sub; /* sub-directory name */
12 name_data *names; /* name state data */
13 } walk_data;
16 static char path_buffer[GIT_PATH_MAX];
17 static char *top_dir = "dir-walk";
18 static walk_data *state_loc;
21 static int error(const char *fmt, ...)
23 va_list ap;
25 va_start(ap, fmt);
26 vfprintf(stderr, fmt, ap);
27 va_end(ap);
28 fprintf(stderr, "\n");
29 return -1;
32 static int setup(walk_data *d)
34 name_data *n;
36 if (gitfo_mkdir(top_dir, 0755) < 0)
37 return error("can't mkdir(\"%s\")", top_dir);
39 if (gitfo_chdir(top_dir) < 0)
40 return error("can't chdir(\"%s\")", top_dir);
42 if (strcmp(d->sub, ".") != 0)
43 if (gitfo_mkdir(d->sub, 0755) < 0)
44 return error("can't mkdir(\"%s\")", d->sub);
46 strcpy(path_buffer, d->sub);
47 state_loc = d;
49 for (n = d->names; n->name; n++) {
50 git_file fd = gitfo_creat(n->name, 0600);
51 must_be_true(fd >= 0);
52 gitfo_close(fd);
53 n->count = 0;
56 return 0;
59 static int knockdown(walk_data *d)
61 name_data *n;
63 for (n = d->names; n->name; n++) {
64 if (gitfo_unlink(n->name) < 0)
65 return error("can't unlink(\"%s\")", n->name);
68 if (strcmp(d->sub, ".") != 0)
69 if (gitfo_rmdir(d->sub) < 0)
70 return error("can't rmdir(\"%s\")", d->sub);
72 if (gitfo_chdir("..") < 0)
73 return error("can't chdir(\"..\")");
75 if (gitfo_rmdir(top_dir) < 0)
76 return error("can't rmdir(\"%s\")", top_dir);
78 return 0;
81 static int check_counts(walk_data *d)
83 int ret = 0;
84 name_data *n;
86 for (n = d->names; n->name; n++) {
87 if (n->count != 1)
88 ret = error("count (%d, %s)", n->count, n->name);
90 return ret;
93 static int one_entry(void *state, char *path)
95 walk_data *d = (walk_data *) state;
96 name_data *n;
98 must_be_true(state == state_loc);
99 must_be_true(path == path_buffer);
100 for (n = d->names; n->name; n++) {
101 if (!strcmp(n->name, path)) {
102 n->count++;
103 return 0;
106 test_die("unexpected path \"%s\"", path);
110 static name_data dot_names[] = {
111 { 0, "./a" },
112 { 0, "./asdf" },
113 { 0, "./pack-foo.pack" },
114 { 0, NULL }
116 static walk_data dot = {
117 ".",
118 dot_names
121 BEGIN_TEST(dot)
123 must_pass(setup(&dot));
125 must_pass(gitfo_dirent(path_buffer,
126 sizeof(path_buffer),
127 one_entry,
128 &dot));
130 must_pass(check_counts(&dot));
132 must_pass(knockdown(&dot));
133 END_TEST
135 static name_data sub_names[] = {
136 { 0, "sub/a" },
137 { 0, "sub/asdf" },
138 { 0, "sub/pack-foo.pack" },
139 { 0, NULL }
141 static walk_data sub = {
142 "sub",
143 sub_names
146 BEGIN_TEST(sub)
148 must_pass(setup(&sub));
150 must_pass(gitfo_dirent(path_buffer,
151 sizeof(path_buffer),
152 one_entry,
153 &sub));
155 must_pass(check_counts(&sub));
157 must_pass(knockdown(&sub));
158 END_TEST
160 static walk_data sub_slash = {
161 "sub/",
162 sub_names
165 BEGIN_TEST(sub_slash)
167 must_pass(setup(&sub_slash));
169 must_pass(gitfo_dirent(path_buffer,
170 sizeof(path_buffer),
171 one_entry,
172 &sub_slash));
174 must_pass(check_counts(&sub_slash));
176 must_pass(knockdown(&sub_slash));
177 END_TEST
179 static name_data empty_names[] = {
180 { 0, NULL }
182 static walk_data empty = {
183 "empty",
184 empty_names
187 static int dont_call_me(void *GIT_UNUSED(state), char *GIT_UNUSED(path))
189 GIT_UNUSED_ARG(state)
190 GIT_UNUSED_ARG(path)
191 test_die("dont_call_me: unexpected callback!");
194 BEGIN_TEST(empty)
196 must_pass(setup(&empty));
198 must_pass(gitfo_dirent(path_buffer,
199 sizeof(path_buffer),
200 one_entry,
201 &empty));
203 must_pass(check_counts(&empty));
205 /* make sure callback not called */
206 must_pass(gitfo_dirent(path_buffer,
207 sizeof(path_buffer),
208 dont_call_me,
209 &empty));
211 must_pass(knockdown(&empty));
212 END_TEST
214 static name_data odd_names[] = {
215 { 0, "odd/.a" },
216 { 0, "odd/..c" },
217 /* the following don't work on cygwin/win32 */
218 /* { 0, "odd/.b." }, */
219 /* { 0, "odd/..d.." }, */
220 { 0, NULL }
222 static walk_data odd = {
223 "odd",
224 odd_names
227 BEGIN_TEST(odd)
229 must_pass(setup(&odd));
231 must_pass(gitfo_dirent(path_buffer,
232 sizeof(path_buffer),
233 one_entry,
234 &odd));
236 must_pass(check_counts(&odd));
238 must_pass(knockdown(&odd));
239 END_TEST