]> git.proxmox.com Git - libgit2.git/blame - tests/clar_libgit2.h
Update upstream source from tag 'upstream/1.0.0+dfsg.1'
[libgit2.git] / tests / clar_libgit2.h
CommitLineData
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"
0c9c969a 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 */
c52480fd 15#define cl_git_pass(expr) cl_git_expect((expr), 0, __FILE__, __LINE__)
c8c91433 16
c52480fd 17#define cl_git_fail_with(error, expr) cl_git_expect((expr), error, __FILE__, __LINE__)
a1dcc830 18
c52480fd 19#define cl_git_expect(expr, expected, file, line) do { \
e9e20c84 20 int _lg2_error; \
ac3d33df 21 git_error_clear(); \
a1dcc830
ET
22 if ((_lg2_error = (expr)) != expected) \
23 cl_git_report_failure(_lg2_error, expected, file, 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(), \
a1dcc830
ET
34 cl_git_report_failure(0, 0, __FILE__, __LINE__, "Function call succeeded: " #expr); \
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()); \
a1dcc830 44 cl_git_report_failure(_win32_res, 0, __FILE__, __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 */
58typedef struct {
59 int error;
60 const char *file;
61 int line;
62 const char *expr;
63 char error_msg[4096];
64} cl_git_thread_err;
65
6367c58c
ET
66#ifdef GIT_THREADS
67# define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__)
68#else
69# define cl_git_thread_pass(threaderr, expr) cl_git_pass(expr)
70#endif
bbf22f82
ET
71
72#define cl_git_thread_pass_(__threaderr, __expr, __file, __line) do { \
ac3d33df 73 git_error_clear(); \
bbf22f82 74 if ((((cl_git_thread_err *)__threaderr)->error = (__expr)) != 0) { \
ac3d33df 75 const git_error *_last = git_error_last(); \
bbf22f82
ET
76 ((cl_git_thread_err *)__threaderr)->file = __file; \
77 ((cl_git_thread_err *)__threaderr)->line = __line; \
78 ((cl_git_thread_err *)__threaderr)->expr = "Function call failed: " #__expr; \
79 p_snprintf(((cl_git_thread_err *)__threaderr)->error_msg, 4096, "thread 0x%" PRIxZ " - error %d - %s", \
80 git_thread_currentid(), ((cl_git_thread_err *)__threaderr)->error, \
81 _last ? _last->message : "<no message>"); \
82 git_thread_exit(__threaderr); \
83 } \
84 } while (0)
85
ab0cc5a0 86GIT_INLINE(void) cl_git_thread_check(void *data)
bbf22f82
ET
87{
88 cl_git_thread_err *threaderr = (cl_git_thread_err *)data;
89 if (threaderr->error != 0)
90 clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1);
91}
92
a1dcc830 93void cl_git_report_failure(int, int, const char *, int, const char *);
e9e20c84 94
f9775a37
RB
95#define cl_assert_at_line(expr,file,line) \
96 clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
97
d730d3f4
RB
98GIT_INLINE(void) clar__assert_in_range(
99 int lo, int val, int hi,
100 const char *file, int line, const char *err, int should_abort)
101{
102 if (lo > val || hi < val) {
103 char buf[128];
c7dd0a56 104 p_snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
d730d3f4
RB
105 clar__fail(file, line, err, buf, should_abort);
106 }
107}
108
f60ed4e6 109#define cl_assert_equal_sz(sz1,sz2) do { \
8a1e925d 110 size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
f60ed4e6
RB
111 clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
112} while (0)
113
d730d3f4
RB
114#define cl_assert_in_range(L,V,H) \
115 clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
116
13f36ffb 117#define cl_assert_equal_file(DATA,SIZE,PATH) \
8a1e925d 118 clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
13f36ffb
RB
119
120#define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
8a1e925d 121 clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
13f36ffb
RB
122
123void clar__assert_equal_file(
124 const char *expected_data,
125 size_t expected_size,
126 int ignore_cr,
127 const char *path,
128 const char *file,
8a1e925d 129 int line);
13f36ffb 130
0cee70eb
ET
131GIT_INLINE(void) clar__assert_equal_oid(
132 const char *file, int line, const char *desc,
133 const git_oid *one, const git_oid *two)
134{
135 if (git_oid_cmp(one, two)) {
136 char err[] = "\"........................................\" != \"........................................\"";
137
138 git_oid_fmt(&err[1], one);
139 git_oid_fmt(&err[47], two);
140
141 clar__fail(file, line, desc, err, 1);
142 }
143}
144
145#define cl_assert_equal_oid(one, two) \
146 clar__assert_equal_oid(__FILE__, __LINE__, \
147 "OID mismatch: " #one " != " #two, (one), (two))
148
97769280
RB
149/*
150 * Some utility macros for building long strings
151 */
152#define REP4(STR) STR STR STR STR
153#define REP15(STR) REP4(STR) REP4(STR) REP4(STR) STR STR STR
154#define REP16(STR) REP4(REP4(STR))
155#define REP256(STR) REP16(REP16(STR))
156#define REP1024(STR) REP4(REP256(STR))
157
1d415455
VM
158/* Write the contents of a buffer to disk */
159void cl_git_mkfile(const char *filename, const char *content);
ce49c7a8 160void cl_git_append2file(const char *filename, const char *new_content);
7784bcbb 161void cl_git_rewritefile(const char *filename, const char *new_content);
634f10f6
RB
162void cl_git_write2file(const char *path, const char *data,
163 size_t datalen, int flags, unsigned int mode);
36fc5497 164void cl_git_rmfile(const char *filename);
1d415455 165
0abd7244
RB
166bool cl_toggle_filemode(const char *filename);
167bool cl_is_chmod_supported(void);
168
e272efcb
BS
169/* Environment wrappers */
170char *cl_getenv(const char *name);
e069c621 171bool cl_is_env_set(const char *name);
e272efcb
BS
172int cl_setenv(const char *name, const char *value);
173
c08b8a3a
PK
174/* Reliable rename */
175int cl_rename(const char *source, const char *dest);
176
854eccbb
RB
177/* Git sandbox setup helpers */
178
179git_repository *cl_git_sandbox_init(const char *sandbox);
1c4b5cee 180git_repository *cl_git_sandbox_init_new(const char *name);
854eccbb 181void cl_git_sandbox_cleanup(void);
1f9e41ee 182git_repository *cl_git_sandbox_reopen(void);
854eccbb 183
ac3d33df
JK
184/*
185 * build a sandbox-relative from path segments
186 * is_dir will add a trailing slash
187 * vararg must be a NULL-terminated char * list
188 */
189const char *cl_git_sandbox_path(int is_dir, ...);
190
2ff1a0d0
BS
191/* Local-repo url helpers */
192const char* cl_git_fixture_url(const char *fixturename);
193const char* cl_git_path_url(const char *path);
194
f5a0e734 195/* Test repository cleaner */
196int cl_git_remove_placeholders(const char *directory_path, const char *filename);
197
155fa234
RB
198/* commit creation helpers */
199void cl_repo_commit_from_index(
200 git_oid *out,
201 git_repository *repo,
202 git_signature *sig,
203 git_time_t time,
204 const char *msg);
205
1323c6d1
RB
206/* config setting helpers */
207void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
9ce4f7da 208int cl_repo_get_bool(git_repository *repo, const char *cfg);
1323c6d1 209
f77127da
ET
210void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
211
0f603132
RB
212/* set up a fake "home" directory and set libgit2 GLOBAL search path.
213 *
214 * automatically configures cleanup function to restore the regular search
215 * path, although you can call it explicitly if you wish (with NULL).
216 */
217void cl_fake_home(void);
218void cl_fake_home_cleanup(void *);
a9528b8f 219
8487e237
RB
220void cl_sandbox_set_search_path_defaults(void);
221
0c9c969a
UG
222#ifdef GIT_WIN32
223# define cl_msleep(x) Sleep(x)
224#else
225# define cl_msleep(x) usleep(1000 * (x))
226#endif
227
07c989e9
ET
228#ifdef GIT_WIN32
229bool cl_sandbox_supports_8dot3(void);
230#endif
231
f1558d9b 232#endif