t0020-dirent.c: allow test to be run standalone This test assumed that it was invoked in an empty directory, which is true when run from the Makefile, and so would fail if run standalone. In order to allow the test to work when run from any directory, create a sub directory "dir-walk" and chdir() into this directory while running the tests. Also, add some additional tests. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
diff --git a/src/fileops.h b/src/fileops.h
index f2a98a8..dca7499 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -44,6 +44,7 @@ extern void gitfo_free_buf(gitfo_buf *obj);
#define gitfo_unlink(p) unlink(p)
#define gitfo_rmdir(p) rmdir(p)
+#define gitfo_chdir(p) chdir(p)
#ifdef GIT_WIN32
#define gitfo_mkdir(p,m) mkdir(p)
diff --git a/tests/t0020-dirent.c b/tests/t0020-dirent.c
index 4c65453..966f3c5 100644
--- a/tests/t0020-dirent.c
+++ b/tests/t0020-dirent.c
@@ -1,48 +1,239 @@
+#include <stdarg.h>
#include "test_lib.h"
#include "fileops.h"
+
+typedef struct name_data {
+ int count; /* return count */
+ char *name; /* filename */
+} name_data;
+
+typedef struct walk_data {
+ char *sub; /* sub-directory name */
+ name_data *names; /* name state data */
+} walk_data;
+
+
static char path_buffer[GIT_PATH_MAX];
-static int state_loc;
-static const char* names[] = {
- "./a",
- "./asdf",
- "./pack-foo.pack",
- NULL
-};
+static char *top_dir = "dir-walk";
+static walk_data *state_loc;
+
+
+static int error(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vfprintf(stderr, fmt, ap);
+ va_end(ap);
+ fprintf(stderr, "\n");
+ return -1;
+}
+
+static int setup(walk_data *d)
+{
+ name_data *n;
+
+ if (gitfo_mkdir(top_dir, 0755) < 0)
+ return error("can't mkdir(\"%s\")", top_dir);
+
+ if (gitfo_chdir(top_dir) < 0)
+ return error("can't chdir(\"%s\")", top_dir);
+
+ if (strcmp(d->sub, ".") != 0)
+ if (gitfo_mkdir(d->sub, 0755) < 0)
+ return error("can't mkdir(\"%s\")", d->sub);
+
+ strcpy(path_buffer, d->sub);
+ state_loc = d;
+
+ for (n = d->names; n->name; n++) {
+ git_file fd = gitfo_creat(n->name, 0600);
+ must_be_true(fd >= 0);
+ gitfo_close(fd);
+ n->count = 0;
+ }
+
+ return 0;
+}
+
+static int knockdown(walk_data *d)
+{
+ name_data *n;
+
+ for (n = d->names; n->name; n++) {
+ if (gitfo_unlink(n->name) < 0)
+ return error("can't unlink(\"%s\")", n->name);
+ }
+
+ if (strcmp(d->sub, ".") != 0)
+ if (gitfo_rmdir(d->sub) < 0)
+ return error("can't rmdir(\"%s\")", d->sub);
+
+ if (gitfo_chdir("..") < 0)
+ return error("can't chdir(\"..\")");
+
+ if (gitfo_rmdir(top_dir) < 0)
+ return error("can't rmdir(\"%s\")", top_dir);
+
+ return 0;
+}
+
+static int check_counts(walk_data *d)
+{
+ int ret = 0;
+ name_data *n;
+
+ for (n = d->names; n->name; n++) {
+ if (n->count != 1)
+ ret = error("count (%d, %s)", n->count, n->name);
+ }
+ return ret;
+}
static int one_entry(void *state, char *path)
{
- const char **c;
+ walk_data *d = (walk_data *) state;
+ name_data *n;
- must_be_true(state == &state_loc);
+ must_be_true(state == state_loc);
must_be_true(path == path_buffer);
- for (c = names; *c; c++) {
- if (!strcmp(*c, path)) {
- *c = "";
+ for (n = d->names; n->name; n++) {
+ if (!strcmp(n->name, path)) {
+ n->count++;
return 0;
}
}
test_die("unexpected path \"%s\"", path);
}
-BEGIN_TEST(setup)
- const char **c;
- for (c = names; *c; c++) {
- git_file fd = gitfo_creat(*c, 0600);
- must_be_true(fd >= 0);
- gitfo_close(fd);
- }
+
+static name_data dot_names[] = {
+ { 0, "./a" },
+ { 0, "./asdf" },
+ { 0, "./pack-foo.pack" },
+ { 0, NULL }
+};
+static walk_data dot = {
+ ".",
+ dot_names
+};
+
+BEGIN_TEST(dot)
+
+ must_pass(setup(&dot));
+
+ must_pass(gitfo_dirent(path_buffer,
+ sizeof(path_buffer),
+ one_entry,
+ &dot));
+
+ must_pass(check_counts(&dot));
+
+ must_pass(knockdown(&dot));
+END_TEST
+
+static name_data sub_names[] = {
+ { 0, "sub/a" },
+ { 0, "sub/asdf" },
+ { 0, "sub/pack-foo.pack" },
+ { 0, NULL }
+};
+static walk_data sub = {
+ "sub",
+ sub_names
+};
+
+BEGIN_TEST(sub)
+
+ must_pass(setup(&sub));
+
+ must_pass(gitfo_dirent(path_buffer,
+ sizeof(path_buffer),
+ one_entry,
+ &sub));
+
+ must_pass(check_counts(&sub));
+
+ must_pass(knockdown(&sub));
+END_TEST
+
+static walk_data sub_slash = {
+ "sub/",
+ sub_names
+};
+
+BEGIN_TEST(sub_slash)
+
+ must_pass(setup(&sub_slash));
+
+ must_pass(gitfo_dirent(path_buffer,
+ sizeof(path_buffer),
+ one_entry,
+ &sub_slash));
+
+ must_pass(check_counts(&sub_slash));
+
+ must_pass(knockdown(&sub_slash));
END_TEST
-BEGIN_TEST(direent_walk)
- const char **c;
+static name_data empty_names[] = {
+ { 0, NULL }
+};
+static walk_data empty = {
+ "empty",
+ empty_names
+};
+
+static int dont_call_me(void *state, char *path)
+{
+ test_die("dont_call_me: unexpected callback!");
+}
+
+BEGIN_TEST(empty)
+
+ must_pass(setup(&empty));
+
+ must_pass(gitfo_dirent(path_buffer,
+ sizeof(path_buffer),
+ one_entry,
+ &empty));
+
+ must_pass(check_counts(&empty));
+
+ /* make sure callback not called */
+ must_pass(gitfo_dirent(path_buffer,
+ sizeof(path_buffer),
+ dont_call_me,
+ &empty));
+
+ must_pass(knockdown(&empty));
+END_TEST
+
+static name_data odd_names[] = {
+ { 0, "odd/.a" },
+ { 0, "odd/..c" },
+ /* the following don't work on cygwin/win32 */
+ /* { 0, "odd/.b." }, */
+ /* { 0, "odd/..d.." }, */
+ { 0, NULL }
+};
+static walk_data odd = {
+ "odd",
+ odd_names
+};
+
+BEGIN_TEST(odd)
+
+ must_pass(setup(&odd));
- strcpy(path_buffer, ".");
must_pass(gitfo_dirent(path_buffer,
sizeof(path_buffer),
one_entry,
- &state_loc));
+ &odd));
+
+ must_pass(check_counts(&odd));
- for (c = names; *c; c++)
- must_pass(strcmp("", *c));
+ must_pass(knockdown(&odd));
END_TEST
+