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