]> git.proxmox.com Git - libgit2.git/blob - tests/core/stat.c
59a13464938a2b74587831b22e0a000a20379b42
[libgit2.git] / tests / core / stat.c
1 #include "clar_libgit2.h"
2 #include "fileops.h"
3 #include "path.h"
4 #include "posix.h"
5
6 void test_core_stat__initialize(void)
7 {
8 cl_git_pass(git_futils_mkdir("root/d1/d2", 0755, GIT_MKDIR_PATH));
9 cl_git_mkfile("root/file", "whatever\n");
10 cl_git_mkfile("root/d1/file", "whatever\n");
11 }
12
13 void test_core_stat__cleanup(void)
14 {
15 git_futils_rmdir_r("root", NULL, GIT_RMDIR_REMOVE_FILES);
16 }
17
18 #define cl_assert_error(val) \
19 do { err = errno; cl_assert_equal_i((val), err); } while (0)
20
21 void test_core_stat__0(void)
22 {
23 struct stat st;
24 int err;
25
26 cl_assert_equal_i(0, p_lstat("root", &st));
27 cl_assert(S_ISDIR(st.st_mode));
28 cl_assert_error(0);
29
30 cl_assert_equal_i(0, p_lstat("root/", &st));
31 cl_assert(S_ISDIR(st.st_mode));
32 cl_assert_error(0);
33
34 cl_assert_equal_i(0, p_lstat("root/file", &st));
35 cl_assert(S_ISREG(st.st_mode));
36 cl_assert_error(0);
37
38 cl_assert_equal_i(0, p_lstat("root/d1", &st));
39 cl_assert(S_ISDIR(st.st_mode));
40 cl_assert_error(0);
41
42 cl_assert_equal_i(0, p_lstat("root/d1/", &st));
43 cl_assert(S_ISDIR(st.st_mode));
44 cl_assert_error(0);
45
46 cl_assert_equal_i(0, p_lstat("root/d1/file", &st));
47 cl_assert(S_ISREG(st.st_mode));
48 cl_assert_error(0);
49
50 cl_assert(p_lstat("root/missing", &st) < 0);
51 cl_assert_error(ENOENT);
52
53 cl_assert(p_lstat("root/missing/but/could/be/created", &st) < 0);
54 cl_assert_error(ENOENT);
55
56 cl_assert(p_lstat_posixly("root/missing/but/could/be/created", &st) < 0);
57 cl_assert_error(ENOENT);
58
59 cl_assert(p_lstat("root/d1/missing", &st) < 0);
60 cl_assert_error(ENOENT);
61
62 cl_assert(p_lstat("root/d1/missing/deeper/path", &st) < 0);
63 cl_assert_error(ENOENT);
64
65 cl_assert(p_lstat_posixly("root/d1/missing/deeper/path", &st) < 0);
66 cl_assert_error(ENOENT);
67
68 cl_assert(p_lstat_posixly("root/d1/file/deeper/path", &st) < 0);
69 cl_assert_error(ENOTDIR);
70
71 cl_assert(p_lstat("root/file/invalid", &st) < 0);
72 #ifdef GIT_WIN32
73 cl_assert_error(ENOENT);
74 #else
75 cl_assert_error(ENOTDIR);
76 #endif
77
78 cl_assert(p_lstat_posixly("root/file/invalid", &st) < 0);
79 cl_assert_error(ENOTDIR);
80
81 cl_assert(p_lstat("root/file/invalid/deeper_path", &st) < 0);
82 #ifdef GIT_WIN32
83 cl_assert_error(ENOENT);
84 #else
85 cl_assert_error(ENOTDIR);
86 #endif
87
88 cl_assert(p_lstat_posixly("root/file/invalid/deeper_path", &st) < 0);
89 cl_assert_error(ENOTDIR);
90
91 cl_assert(p_lstat_posixly("root/d1/file/extra", &st) < 0);
92 cl_assert_error(ENOTDIR);
93
94 cl_assert(p_lstat_posixly("root/d1/file/further/invalid/items", &st) < 0);
95 cl_assert_error(ENOTDIR);
96 }
97
98 void test_core_stat__root(void)
99 {
100 const char *sandbox = clar_sandbox_path();
101 git_buf root = GIT_BUF_INIT;
102 int root_len;
103 struct stat st;
104
105 root_len = git_path_root(sandbox);
106 cl_assert(root_len >= 0);
107
108 git_buf_set(&root, sandbox, root_len+1);
109
110 cl_must_pass(p_stat(root.ptr, &st));
111 cl_assert(S_ISDIR(st.st_mode));
112
113 git_buf_dispose(&root);
114 }