Blame tests/clar_libgit2.h

Packit Service 20376f
#ifndef __CLAR_LIBGIT2__
Packit Service 20376f
#define __CLAR_LIBGIT2__
Packit Service 20376f
Packit Service 20376f
#include "clar.h"
Packit Service 20376f
#include <git2.h>
Packit Service 20376f
#include <posix.h>
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Replace for `clar_must_pass` that passes the last library error as the
Packit Service 20376f
 * test failure message.
Packit Service 20376f
 *
Packit Service 20376f
 * Use this wrapper around all `git_` library calls that return error codes!
Packit Service 20376f
 */
Packit Service 20376f
#define cl_git_pass(expr) cl_git_expect((expr), 0, __FILE__, __LINE__)
Packit Service 20376f
Packit Service 20376f
#define cl_git_fail_with(error, expr) cl_git_expect((expr), error, __FILE__, __LINE__)
Packit Service 20376f
Packit Service 20376f
#define cl_git_expect(expr, expected, file, line) do { \
Packit Service 20376f
	int _lg2_error; \
Packit Service 20376f
	giterr_clear(); \
Packit Service 20376f
	if ((_lg2_error = (expr)) != expected) \
Packit Service 20376f
		cl_git_report_failure(_lg2_error, expected, file, line, "Function call failed: " #expr); \
Packit Service 20376f
	} while (0)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Wrapper for `clar_must_fail` -- this one is
Packit Service 20376f
 * just for consistency. Use with `git_` library
Packit Service 20376f
 * calls that are supposed to fail!
Packit Service 20376f
 */
Packit Service 20376f
#define cl_git_fail(expr) do { \
Packit Service 20376f
	giterr_clear(); \
Packit Service 20376f
	if ((expr) == 0) \
Packit Service 20376f
		cl_git_report_failure(0, 0, __FILE__, __LINE__, "Function call succeeded: " #expr); \
Packit Service 20376f
	} while (0)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Like cl_git_pass, only for Win32 error code conventions
Packit Service 20376f
 */
Packit Service 20376f
#define cl_win32_pass(expr) do { \
Packit Service 20376f
	int _win32_res; \
Packit Service 20376f
	if ((_win32_res = (expr)) == 0) { \
Packit Service 20376f
		giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
Packit Service 20376f
		cl_git_report_failure(_win32_res, 0, __FILE__, __LINE__, "System call failed: " #expr); \
Packit Service 20376f
	} \
Packit Service 20376f
	} while(0)
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Thread safe assertions; you cannot use `cl_git_report_failure` from a
Packit Service 20376f
 * child thread since it will try to `longjmp` to abort and "the effect of
Packit Service 20376f
 * a call to longjmp() where initialization of the jmp_buf structure was
Packit Service 20376f
 * not performed in the calling thread is undefined."
Packit Service 20376f
 *
Packit Service 20376f
 * Instead, callers can provide a clar thread error context to a thread,
Packit Service 20376f
 * which will populate and return it on failure.  Callers can check the
Packit Service 20376f
 * status with `cl_git_thread_check`.
Packit Service 20376f
 */
Packit Service 20376f
typedef struct {
Packit Service 20376f
	int error;
Packit Service 20376f
	const char *file;
Packit Service 20376f
	int line;
Packit Service 20376f
	const char *expr;
Packit Service 20376f
	char error_msg[4096];
Packit Service 20376f
} cl_git_thread_err;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_THREADS
Packit Service 20376f
# define cl_git_thread_pass(threaderr, expr) cl_git_thread_pass_(threaderr, (expr), __FILE__, __LINE__)
Packit Service 20376f
#else
Packit Service 20376f
# define cl_git_thread_pass(threaderr, expr) cl_git_pass(expr)
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#define cl_git_thread_pass_(__threaderr, __expr, __file, __line) do { \
Packit Service 20376f
	giterr_clear(); \
Packit Service 20376f
	if ((((cl_git_thread_err *)__threaderr)->error = (__expr)) != 0) { \
Packit Service 20376f
		const git_error *_last = giterr_last(); \
Packit Service 20376f
		((cl_git_thread_err *)__threaderr)->file = __file; \
Packit Service 20376f
		((cl_git_thread_err *)__threaderr)->line = __line; \
Packit Service 20376f
		((cl_git_thread_err *)__threaderr)->expr = "Function call failed: " #__expr; \
Packit Service 20376f
		p_snprintf(((cl_git_thread_err *)__threaderr)->error_msg, 4096, "thread 0x%" PRIxZ " - error %d - %s", \
Packit Service 20376f
			git_thread_currentid(), ((cl_git_thread_err *)__threaderr)->error, \
Packit Service 20376f
			_last ? _last->message : "<no message>"); \
Packit Service 20376f
		git_thread_exit(__threaderr); \
Packit Service 20376f
	} \
Packit Service 20376f
	} while (0)
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void) cl_git_thread_check(void *data)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_thread_err *threaderr = (cl_git_thread_err *)data;
Packit Service 20376f
	if (threaderr->error != 0)
Packit Service 20376f
		clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void cl_git_report_failure(int, int, const char *, int, const char *);
Packit Service 20376f
Packit Service 20376f
#define cl_assert_at_line(expr,file,line) \
Packit Service 20376f
	clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void) clar__assert_in_range(
Packit Service 20376f
	int lo, int val, int hi,
Packit Service 20376f
	const char *file, int line, const char *err, int should_abort)
Packit Service 20376f
{
Packit Service 20376f
	if (lo > val || hi < val) {
Packit Service 20376f
		char buf[128];
Packit Service 20376f
		p_snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
Packit Service 20376f
		clar__fail(file, line, err, buf, should_abort);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define cl_assert_equal_sz(sz1,sz2) do { \
Packit Service 20376f
	size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
Packit Service 20376f
	clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
Packit Service 20376f
} while (0)
Packit Service 20376f
Packit Service 20376f
#define cl_assert_in_range(L,V,H) \
Packit Service 20376f
	clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
Packit Service 20376f
Packit Service 20376f
#define cl_assert_equal_file(DATA,SIZE,PATH) \
Packit Service 20376f
	clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
Packit Service 20376f
Packit Service 20376f
#define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
Packit Service 20376f
	clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
Packit Service 20376f
Packit Service 20376f
void clar__assert_equal_file(
Packit Service 20376f
	const char *expected_data,
Packit Service 20376f
	size_t expected_size,
Packit Service 20376f
	int ignore_cr,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const char *file,
Packit Service 20376f
	int line);
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(void) clar__assert_equal_oid(
Packit Service 20376f
	const char *file, int line, const char *desc,
Packit Service 20376f
	const git_oid *one, const git_oid *two)
Packit Service 20376f
{
Packit Service 20376f
	if (git_oid_cmp(one, two)) {
Packit Service 20376f
		char err[] = "\"........................................\" != \"........................................\"";
Packit Service 20376f
Packit Service 20376f
		git_oid_fmt(&err[1], one);
Packit Service 20376f
		git_oid_fmt(&err[47], two);
Packit Service 20376f
Packit Service 20376f
		clar__fail(file, line, desc, err, 1);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define cl_assert_equal_oid(one, two) \
Packit Service 20376f
	clar__assert_equal_oid(__FILE__, __LINE__, \
Packit Service 20376f
		"OID mismatch: " #one " != " #two, (one), (two))
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * Some utility macros for building long strings
Packit Service 20376f
 */
Packit Service 20376f
#define REP4(STR)	 STR STR STR STR
Packit Service 20376f
#define REP15(STR)	 REP4(STR) REP4(STR) REP4(STR) STR STR STR
Packit Service 20376f
#define REP16(STR)	 REP4(REP4(STR))
Packit Service 20376f
#define REP256(STR)  REP16(REP16(STR))
Packit Service 20376f
#define REP1024(STR) REP4(REP256(STR))
Packit Service 20376f
Packit Service 20376f
/* Write the contents of a buffer to disk */
Packit Service 20376f
void cl_git_mkfile(const char *filename, const char *content);
Packit Service 20376f
void cl_git_append2file(const char *filename, const char *new_content);
Packit Service 20376f
void cl_git_rewritefile(const char *filename, const char *new_content);
Packit Service 20376f
void cl_git_write2file(const char *path, const char *data,
Packit Service 20376f
	size_t datalen, int flags, unsigned int mode);
Packit Service 20376f
void cl_git_rmfile(const char *filename);
Packit Service 20376f
Packit Service 20376f
bool cl_toggle_filemode(const char *filename);
Packit Service 20376f
bool cl_is_chmod_supported(void);
Packit Service 20376f
Packit Service 20376f
/* Environment wrappers */
Packit Service 20376f
char *cl_getenv(const char *name);
Packit Service 20376f
bool cl_is_env_set(const char *name);
Packit Service 20376f
int cl_setenv(const char *name, const char *value);
Packit Service 20376f
Packit Service 20376f
/* Reliable rename */
Packit Service 20376f
int cl_rename(const char *source, const char *dest);
Packit Service 20376f
Packit Service 20376f
/* Git sandbox setup helpers */
Packit Service 20376f
Packit Service 20376f
git_repository *cl_git_sandbox_init(const char *sandbox);
Packit Service 20376f
git_repository *cl_git_sandbox_init_new(const char *name);
Packit Service 20376f
void cl_git_sandbox_cleanup(void);
Packit Service 20376f
git_repository *cl_git_sandbox_reopen(void);
Packit Service 20376f
Packit Service 20376f
/* Local-repo url helpers */
Packit Service 20376f
const char* cl_git_fixture_url(const char *fixturename);
Packit Service 20376f
const char* cl_git_path_url(const char *path);
Packit Service 20376f
Packit Service 20376f
/* Test repository cleaner */
Packit Service 20376f
int cl_git_remove_placeholders(const char *directory_path, const char *filename);
Packit Service 20376f
Packit Service 20376f
/* commit creation helpers */
Packit Service 20376f
void cl_repo_commit_from_index(
Packit Service 20376f
	git_oid *out,
Packit Service 20376f
	git_repository *repo,
Packit Service 20376f
	git_signature *sig,
Packit Service 20376f
	git_time_t time,
Packit Service 20376f
	const char *msg);
Packit Service 20376f
Packit Service 20376f
/* config setting helpers */
Packit Service 20376f
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
Packit Service 20376f
int cl_repo_get_bool(git_repository *repo, const char *cfg);
Packit Service 20376f
Packit Service 20376f
void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
Packit Service 20376f
Packit Service 20376f
/* set up a fake "home" directory and set libgit2 GLOBAL search path.
Packit Service 20376f
 *
Packit Service 20376f
 * automatically configures cleanup function to restore the regular search
Packit Service 20376f
 * path, although you can call it explicitly if you wish (with NULL).
Packit Service 20376f
 */
Packit Service 20376f
void cl_fake_home(void);
Packit Service 20376f
void cl_fake_home_cleanup(void *);
Packit Service 20376f
Packit Service 20376f
void cl_sandbox_set_search_path_defaults(void);
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
bool cl_sandbox_supports_8dot3(void);
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
#endif