clar_libgit2.h 6.93 KB
Newer Older
1 2
#ifndef __CLAR_LIBGIT2__
#define __CLAR_LIBGIT2__
Vicent Marti committed
3

4
#include "clar.h"
Vicent Marti committed
5
#include <git2.h>
6
#include <posix.h>
Vicent Marti committed
7 8 9
#include "common.h"

/**
10 11
 * Replace for `clar_must_pass` that passes the last library error as the
 * test failure message.
Vicent Marti committed
12
 *
13
 * Use this wrapper around all `git_` library calls that return error codes!
Vicent Marti committed
14
 */
15
#define cl_git_pass(expr) cl_git_expect((expr), 0, __FILE__, __LINE__)
16

17
#define cl_git_fail_with(error, expr) cl_git_expect((expr), error, __FILE__, __LINE__)
18

19
#define cl_git_expect(expr, expected, file, line) do { \
20
	int _lg2_error; \
21
	giterr_clear(); \
22 23
	if ((_lg2_error = (expr)) != expected) \
		cl_git_report_failure(_lg2_error, expected, file, line, "Function call failed: " #expr); \
24
	} while (0)
Vicent Marti committed
25 26

/**
27
 * Wrapper for `clar_must_fail` -- this one is
Vicent Marti committed
28 29 30
 * just for consistency. Use with `git_` library
 * calls that are supposed to fail!
 */
31 32 33 34 35
#define cl_git_fail(expr) do { \
	giterr_clear(); \
	if ((expr) == 0) \
		cl_git_report_failure(0, 0, __FILE__, __LINE__, "Function call succeeded: " #expr); \
	} while (0)
36

37 38 39 40 41 42 43
/**
 * Like cl_git_pass, only for Win32 error code conventions
 */
#define cl_win32_pass(expr) do { \
	int _win32_res; \
	if ((_win32_res = (expr)) == 0) { \
		giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
44
		cl_git_report_failure(_win32_res, 0, __FILE__, __LINE__, "System call failed: " #expr); \
45 46 47
	} \
	} while(0)

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/**
 * Thread safe assertions; you cannot use `cl_git_report_failure` from a
 * child thread since it will try to `longjmp` to abort and "the effect of
 * a call to longjmp() where initialization of the jmp_buf structure was
 * not performed in the calling thread is undefined."
 *
 * Instead, callers can provide a clar thread error context to a thread,
 * which will populate and return it on failure.  Callers can check the
 * status with `cl_git_thread_check`.
 */
typedef struct {
	int error;
	const char *file;
	int line;
	const char *expr;
	char error_msg[4096];
} cl_git_thread_err;

66 67 68 69 70
#ifdef GIT_THREADS
# define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__)
#else
# define cl_git_thread_pass(threaderr, expr) cl_git_pass(expr)
#endif
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

#define cl_git_thread_pass_(__threaderr, __expr, __file, __line) do { \
	giterr_clear(); \
	if ((((cl_git_thread_err *)__threaderr)->error = (__expr)) != 0) { \
		const git_error *_last = giterr_last(); \
		((cl_git_thread_err *)__threaderr)->file = __file; \
		((cl_git_thread_err *)__threaderr)->line = __line; \
		((cl_git_thread_err *)__threaderr)->expr = "Function call failed: " #__expr; \
		p_snprintf(((cl_git_thread_err *)__threaderr)->error_msg, 4096, "thread 0x%" PRIxZ " - error %d - %s", \
			git_thread_currentid(), ((cl_git_thread_err *)__threaderr)->error, \
			_last ? _last->message : "<no message>"); \
		git_thread_exit(__threaderr); \
	} \
	} while (0)

86
GIT_INLINE(void) cl_git_thread_check(void *data)
87 88 89 90 91 92
{
	cl_git_thread_err *threaderr = (cl_git_thread_err *)data;
	if (threaderr->error != 0)
		clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1);
}

93
void cl_git_report_failure(int, int, const char *, int, const char *);
94

95 96 97
#define cl_assert_at_line(expr,file,line) \
	clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)

98 99 100 101 102 103
GIT_INLINE(void) clar__assert_in_range(
	int lo, int val, int hi,
	const char *file, int line, const char *err, int should_abort)
{
	if (lo > val || hi < val) {
		char buf[128];
104
		p_snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
105 106 107 108
		clar__fail(file, line, err, buf, should_abort);
	}
}

109
#define cl_assert_equal_sz(sz1,sz2) do { \
nulltoken committed
110
	size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
111 112 113
	clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
} while (0)

114 115 116
#define cl_assert_in_range(L,V,H) \
	clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)

117
#define cl_assert_equal_file(DATA,SIZE,PATH) \
nulltoken committed
118
	clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
119 120

#define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
nulltoken committed
121
	clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
122 123 124 125 126 127 128

void clar__assert_equal_file(
	const char *expected_data,
	size_t expected_size,
	int ignore_cr,
	const char *path,
	const char *file,
nulltoken committed
129
	int line);
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
GIT_INLINE(void) clar__assert_equal_oid(
	const char *file, int line, const char *desc,
	const git_oid *one, const git_oid *two)
{
	if (git_oid_cmp(one, two)) {
		char err[] = "\"........................................\" != \"........................................\"";

		git_oid_fmt(&err[1], one);
		git_oid_fmt(&err[47], two);

		clar__fail(file, line, desc, err, 1);
	}
}

#define cl_assert_equal_oid(one, two) \
	clar__assert_equal_oid(__FILE__, __LINE__, \
		"OID mismatch: " #one " != " #two, (one), (two))

149 150 151 152 153 154 155 156 157
/*
 * Some utility macros for building long strings
 */
#define REP4(STR)	 STR STR STR STR
#define REP15(STR)	 REP4(STR) REP4(STR) REP4(STR) STR STR STR
#define REP16(STR)	 REP4(REP4(STR))
#define REP256(STR)  REP16(REP16(STR))
#define REP1024(STR) REP4(REP256(STR))

158 159
/* Write the contents of a buffer to disk */
void cl_git_mkfile(const char *filename, const char *content);
160
void cl_git_append2file(const char *filename, const char *new_content);
161
void cl_git_rewritefile(const char *filename, const char *new_content);
162 163
void cl_git_write2file(const char *path, const char *data,
	size_t datalen, int flags, unsigned int mode);
164
void cl_git_rmfile(const char *filename);
165

166 167 168
bool cl_toggle_filemode(const char *filename);
bool cl_is_chmod_supported(void);

169 170
/* Environment wrappers */
char *cl_getenv(const char *name);
171
bool cl_is_env_set(const char *name);
172 173
int cl_setenv(const char *name, const char *value);

174 175 176
/* Reliable rename */
int cl_rename(const char *source, const char *dest);

177 178 179
/* Git sandbox setup helpers */

git_repository *cl_git_sandbox_init(const char *sandbox);
180
git_repository *cl_git_sandbox_init_new(const char *name);
181
void cl_git_sandbox_cleanup(void);
182
git_repository *cl_git_sandbox_reopen(void);
183

184 185 186 187
/* Local-repo url helpers */
const char* cl_git_fixture_url(const char *fixturename);
const char* cl_git_path_url(const char *path);

188 189 190
/* Test repository cleaner */
int cl_git_remove_placeholders(const char *directory_path, const char *filename);

191 192 193 194 195 196 197 198
/* commit creation helpers */
void cl_repo_commit_from_index(
	git_oid *out,
	git_repository *repo,
	git_signature *sig,
	git_time_t time,
	const char *msg);

199 200
/* config setting helpers */
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
201
int cl_repo_get_bool(git_repository *repo, const char *cfg);
202

203 204
void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);

205 206 207 208 209 210 211
/* set up a fake "home" directory and set libgit2 GLOBAL search path.
 *
 * automatically configures cleanup function to restore the regular search
 * path, although you can call it explicitly if you wish (with NULL).
 */
void cl_fake_home(void);
void cl_fake_home_cleanup(void *);
212

213 214
void cl_sandbox_set_search_path_defaults(void);

215 216 217 218
#ifdef GIT_WIN32
bool cl_sandbox_supports_8dot3(void);
#endif

Vicent Marti committed
219
#endif