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