clar_libgit2.h 4.89 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_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)
Vicent Marti committed
23 24

/**
25
 * Wrapper for `clar_must_fail` -- this one is
Vicent Marti committed
26 27 28
 * just for consistency. Use with `git_` library
 * calls that are supposed to fail!
 */
29
#define cl_git_fail(expr) cl_must_fail(expr)
Vicent Marti committed
30

31 32
#define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)

33 34 35 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()); \
		cl_git_report_failure(_win32_res, __FILE__, __LINE__, "System call failed: " #expr); \
	} \
	} while(0)

44 45
void cl_git_report_failure(int, const char *, int, const char *);

46 47 48
#define cl_assert_at_line(expr,file,line) \
	clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)

49 50 51 52 53 54
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];
55
		p_snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
56 57 58 59
		clar__fail(file, line, err, buf, should_abort);
	}
}

60
#define cl_assert_equal_sz(sz1,sz2) do { \
nulltoken committed
61
	size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
62 63 64
	clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
} while (0)

65 66 67
#define cl_assert_in_range(L,V,H) \
	clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)

68
#define cl_assert_equal_file(DATA,SIZE,PATH) \
nulltoken committed
69
	clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
70 71

#define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
nulltoken committed
72
	clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
73 74 75 76 77 78 79

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

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
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))

100 101 102 103 104 105 106 107 108
/*
 * 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))

109 110
/* Write the contents of a buffer to disk */
void cl_git_mkfile(const char *filename, const char *content);
111
void cl_git_append2file(const char *filename, const char *new_content);
112
void cl_git_rewritefile(const char *filename, const char *new_content);
113 114
void cl_git_write2file(const char *path, const char *data,
	size_t datalen, int flags, unsigned int mode);
115

116 117 118
bool cl_toggle_filemode(const char *filename);
bool cl_is_chmod_supported(void);

119 120 121 122
/* Environment wrappers */
char *cl_getenv(const char *name);
int cl_setenv(const char *name, const char *value);

123 124 125
/* Reliable rename */
int cl_rename(const char *source, const char *dest);

126 127 128 129
/* Git sandbox setup helpers */

git_repository *cl_git_sandbox_init(const char *sandbox);
void cl_git_sandbox_cleanup(void);
130
git_repository *cl_git_sandbox_reopen(void);
131

132 133 134 135
/* Local-repo url helpers */
const char* cl_git_fixture_url(const char *fixturename);
const char* cl_git_path_url(const char *path);

136 137 138
/* Test repository cleaner */
int cl_git_remove_placeholders(const char *directory_path, const char *filename);

139 140 141 142 143 144 145 146
/* 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);

147 148
/* config setting helpers */
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
149
int cl_repo_get_bool(git_repository *repo, const char *cfg);
150

151 152
void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);

153 154 155 156 157 158 159
/* 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 *);
160

161 162
void cl_sandbox_set_search_path_defaults(void);

Vicent Marti committed
163
#endif