]> git.proxmox.com Git - libgit2.git/blob - tests/clar_libgit2.h
Merge branch 'master' into pr/3938
[libgit2.git] / tests / clar_libgit2.h
1 #ifndef __CLAR_LIBGIT2__
2 #define __CLAR_LIBGIT2__
3
4 #include "clar.h"
5 #include <git2.h>
6 #include <posix.h>
7 #include "common.h"
8
9 /**
10 * Replace for `clar_must_pass` that passes the last library error as the
11 * test failure message.
12 *
13 * Use this wrapper around all `git_` library calls that return error codes!
14 */
15 #define cl_git_pass(expr) cl_git_pass_((expr), __FILE__, __LINE__)
16
17 #define cl_git_pass_(expr, file, line) do { \
18 int _lg2_error; \
19 giterr_clear(); \
20 if ((_lg2_error = (expr)) != 0) \
21 cl_git_report_failure(_lg2_error, file, line, "Function call failed: " #expr); \
22 } while (0)
23
24 /**
25 * Wrapper for `clar_must_fail` -- this one is
26 * just for consistency. Use with `git_` library
27 * calls that are supposed to fail!
28 */
29 #define cl_git_fail(expr) cl_must_fail(expr)
30
31 #define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)
32
33 /**
34 * Like cl_git_pass, only for Win32 error code conventions
35 */
36 #define cl_win32_pass(expr) do { \
37 int _win32_res; \
38 if ((_win32_res = (expr)) == 0) { \
39 giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
40 cl_git_report_failure(_win32_res, __FILE__, __LINE__, "System call failed: " #expr); \
41 } \
42 } while(0)
43
44 /**
45 * Thread safe assertions; you cannot use `cl_git_report_failure` from a
46 * child thread since it will try to `longjmp` to abort and "the effect of
47 * a call to longjmp() where initialization of the jmp_buf structure was
48 * not performed in the calling thread is undefined."
49 *
50 * Instead, callers can provide a clar thread error context to a thread,
51 * which will populate and return it on failure. Callers can check the
52 * status with `cl_git_thread_check`.
53 */
54 typedef struct {
55 int error;
56 const char *file;
57 int line;
58 const char *expr;
59 char error_msg[4096];
60 } cl_git_thread_err;
61
62 #ifdef GIT_THREADS
63 # define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__)
64 #else
65 # define cl_git_thread_pass(threaderr, expr) cl_git_pass(expr)
66 #endif
67
68 #define cl_git_thread_pass_(__threaderr, __expr, __file, __line) do { \
69 giterr_clear(); \
70 if ((((cl_git_thread_err *)__threaderr)->error = (__expr)) != 0) { \
71 const git_error *_last = giterr_last(); \
72 ((cl_git_thread_err *)__threaderr)->file = __file; \
73 ((cl_git_thread_err *)__threaderr)->line = __line; \
74 ((cl_git_thread_err *)__threaderr)->expr = "Function call failed: " #__expr; \
75 p_snprintf(((cl_git_thread_err *)__threaderr)->error_msg, 4096, "thread 0x%" PRIxZ " - error %d - %s", \
76 git_thread_currentid(), ((cl_git_thread_err *)__threaderr)->error, \
77 _last ? _last->message : "<no message>"); \
78 git_thread_exit(__threaderr); \
79 } \
80 } while (0)
81
82 GIT_INLINE(void) cl_git_thread_check(void *data)
83 {
84 cl_git_thread_err *threaderr = (cl_git_thread_err *)data;
85 if (threaderr->error != 0)
86 clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1);
87 }
88
89 void cl_git_report_failure(int, const char *, int, const char *);
90
91 #define cl_assert_at_line(expr,file,line) \
92 clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
93
94 GIT_INLINE(void) clar__assert_in_range(
95 int lo, int val, int hi,
96 const char *file, int line, const char *err, int should_abort)
97 {
98 if (lo > val || hi < val) {
99 char buf[128];
100 p_snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
101 clar__fail(file, line, err, buf, should_abort);
102 }
103 }
104
105 #define cl_assert_equal_sz(sz1,sz2) do { \
106 size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
107 clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
108 } while (0)
109
110 #define cl_assert_in_range(L,V,H) \
111 clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
112
113 #define cl_assert_equal_file(DATA,SIZE,PATH) \
114 clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
115
116 #define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
117 clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
118
119 void clar__assert_equal_file(
120 const char *expected_data,
121 size_t expected_size,
122 int ignore_cr,
123 const char *path,
124 const char *file,
125 int line);
126
127 GIT_INLINE(void) clar__assert_equal_oid(
128 const char *file, int line, const char *desc,
129 const git_oid *one, const git_oid *two)
130 {
131 if (git_oid_cmp(one, two)) {
132 char err[] = "\"........................................\" != \"........................................\"";
133
134 git_oid_fmt(&err[1], one);
135 git_oid_fmt(&err[47], two);
136
137 clar__fail(file, line, desc, err, 1);
138 }
139 }
140
141 #define cl_assert_equal_oid(one, two) \
142 clar__assert_equal_oid(__FILE__, __LINE__, \
143 "OID mismatch: " #one " != " #two, (one), (two))
144
145 /*
146 * Some utility macros for building long strings
147 */
148 #define REP4(STR) STR STR STR STR
149 #define REP15(STR) REP4(STR) REP4(STR) REP4(STR) STR STR STR
150 #define REP16(STR) REP4(REP4(STR))
151 #define REP256(STR) REP16(REP16(STR))
152 #define REP1024(STR) REP4(REP256(STR))
153
154 /* Write the contents of a buffer to disk */
155 void cl_git_mkfile(const char *filename, const char *content);
156 void cl_git_append2file(const char *filename, const char *new_content);
157 void cl_git_rewritefile(const char *filename, const char *new_content);
158 void cl_git_write2file(const char *path, const char *data,
159 size_t datalen, int flags, unsigned int mode);
160 void cl_git_rmfile(const char *filename);
161
162 bool cl_toggle_filemode(const char *filename);
163 bool cl_is_chmod_supported(void);
164
165 /* Environment wrappers */
166 char *cl_getenv(const char *name);
167 bool cl_is_env_set(const char *name);
168 int cl_setenv(const char *name, const char *value);
169
170 /* Reliable rename */
171 int cl_rename(const char *source, const char *dest);
172
173 /* Git sandbox setup helpers */
174
175 git_repository *cl_git_sandbox_init(const char *sandbox);
176 git_repository *cl_git_sandbox_init_new(const char *name);
177 void cl_git_sandbox_cleanup(void);
178 git_repository *cl_git_sandbox_reopen(void);
179
180 /* Local-repo url helpers */
181 const char* cl_git_fixture_url(const char *fixturename);
182 const char* cl_git_path_url(const char *path);
183
184 /* Test repository cleaner */
185 int cl_git_remove_placeholders(const char *directory_path, const char *filename);
186
187 /* commit creation helpers */
188 void cl_repo_commit_from_index(
189 git_oid *out,
190 git_repository *repo,
191 git_signature *sig,
192 git_time_t time,
193 const char *msg);
194
195 /* config setting helpers */
196 void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
197 int cl_repo_get_bool(git_repository *repo, const char *cfg);
198
199 void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
200
201 /* set up a fake "home" directory and set libgit2 GLOBAL search path.
202 *
203 * automatically configures cleanup function to restore the regular search
204 * path, although you can call it explicitly if you wish (with NULL).
205 */
206 void cl_fake_home(void);
207 void cl_fake_home_cleanup(void *);
208
209 void cl_sandbox_set_search_path_defaults(void);
210
211 #ifdef GIT_WIN32
212 bool cl_sandbox_supports_8dot3(void);
213 #endif
214
215 #endif