tal/path: new module
[ccan] / ccan / tal / path / test / run-pushd.c
1 #include <ccan/tal/path/path.h>
2 #include <ccan/tal/path/path.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         struct path_pushd *pd;
8         char path1[1024], path2[1024], *ctx = tal_strdup(NULL, "ctx");
9
10         /* This is how many tests you plan to run */
11         plan_tests(19);
12
13         /* Test pushd/popd */
14         if (!getcwd(path1, sizeof(path1)))
15                 abort();
16
17         pd = path_pushd(NULL, "non-existent-dir");
18         ok1(errno == ENOENT);
19         ok1(!pd);
20
21         errno = -100;
22         pd = path_pushd(ctx, take(tal_strdup(ctx, "non-existent-dir")));
23         ok1(errno == ENOENT);
24         ok1(!pd);
25         ok1(!tal_first(ctx));
26
27         errno = -100;
28         pd = path_pushd(ctx, take(NULL));
29         ok1(!pd);
30         ok1(!tal_first(ctx));
31         ok1(errno == -100);
32
33         pd = path_pushd(ctx, "/tmp");
34         ok1(pd);
35         ok1(tal_parent(pd) == ctx);
36
37         if (!getcwd(path2, sizeof(path2)))
38                 abort();
39
40         ok1(streq(path2, "/tmp"));
41         path_popd(pd);
42
43         if (!getcwd(path2, sizeof(path2)))
44                 abort();
45         ok1(streq(path2, path1));
46
47         pd = path_pushd(ctx, take(tal_strdup(ctx, "/tmp")));
48         ok1(pd);
49         ok1(tal_parent(pd) == ctx);
50         path_popd(pd);
51         if (!getcwd(path2, sizeof(path2)))
52                 abort();
53         ok1(streq(path2, path1));
54         ok1(!tal_first(ctx));
55
56         /* Without fchdir, we can't push a path which no longer exists. */
57         if (mkdir("run-pushd-dir", 0700) != 0)
58                 abort();
59         if (chdir("run-pushd-dir") != 0)
60                 abort();
61         if (rmdir("../run-pushd-dir") != 0)
62                 abort();
63
64         pd = path_pushd(ctx, path1);
65 #if HAVE_FCHDIR
66         ok1(pd);
67         ok1(path_popd(pd));
68 #else
69         ok1(errno == ENOENT);
70         ok1(!pd);
71 #endif
72         ok1(!tal_first(ctx));
73         tal_free(ctx);
74         return exit_status();
75 }