summaryrefslogtreecommitdiff
path: root/ccan/tal/path/test/run-rel.c
blob: 7083c4bd1174ef7cc8092f8545f5ca2f2b08f330 (plain)
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
#include <ccan/tal/path/path.h>
#include <ccan/tal/path/path.c>
#include <ccan/tap/tap.h>

int main(void)
{
	char cwd[1024], *path, *ctx = tal_strdup(NULL, "ctx");

	plan_tests(19);

	if (!getcwd(cwd, sizeof(cwd)))
		abort();

	unlink("run-rel-link");
	rmdir("run-rel-foo");
	if (mkdir("run-rel-foo", 0700) != 0)
		abort();
	if (symlink("run-rel-foo", "run-rel-link") != 0)
		abort();

	path = path_rel(ctx, ".", "run-rel-foo");
	ok1(streq(path, "run-rel-foo"));
	ok1(tal_parent(path) == ctx);
	tal_free(path);

	path = path_rel(ctx, "run-rel-foo", ".");
	ok1(streq(path, ".."));
	ok1(tal_parent(path) == ctx);
	tal_free(path);

	path = path_rel(ctx, ".", "run-rel-link");
	/* This doesn't specify whether it preserves links. */
	ok1(streq(path, "run-rel-link") || streq(path, "run-rel-foo"));
	ok1(tal_parent(path) == ctx);
	tal_free(path);

	path = path_rel(ctx, "/", ".");
	ok1(streq(path, cwd + 1));
	ok1(tal_parent(path) == ctx);
	tal_free(path);

	path = path_rel(ctx, "run-rel-foo", "run-rel-foo");
	ok1(streq(path, "."));
	ok1(tal_parent(path) == ctx);
	tal_free(path);

	path = path_rel(ctx, take(tal_strdup(ctx, ".")), "run-rel-foo");
	ok1(streq(path, "run-rel-foo"));
	ok1(tal_parent(path) == ctx);
	tal_free(path);
	ok1(tal_first(ctx) == NULL);

	path = path_rel(ctx, ".", take(tal_strdup(ctx, "run-rel-foo")));
	ok1(streq(path, "run-rel-foo"));
	ok1(tal_parent(path) == ctx);
	tal_free(path);
	ok1(tal_first(ctx) == NULL);

	path = path_rel(ctx, take(tal_strdup(ctx, ".")),
			take(tal_strdup(ctx, "run-rel-foo")));
	ok1(streq(path, "run-rel-foo"));
	ok1(tal_parent(path) == ctx);
	tal_free(path);
	ok1(tal_first(ctx) == NULL);

	tal_free(ctx);

	return exit_status();
}