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

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

/**
9 10
 * Replace for `clar_must_pass` that passes the last library error as the
 * test failure message.
Vicent Marti committed
11
 *
12
 * Use this wrapper around all `git_` library calls that return error codes!
Vicent Marti committed
13 14
 */
#define cl_git_pass(expr) do { \
15
	int _lg2_error; \
16
	giterr_clear(); \
17 18 19
	if ((_lg2_error = (expr)) != 0) \
		cl_git_report_failure(_lg2_error, __FILE__, __LINE__, "Function call failed: " #expr); \
	} while (0)
Vicent Marti committed
20 21

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

28 29 30 31
#define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)

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

32 33 34
#define cl_assert_at_line(expr,file,line) \
	clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)

35 36 37 38 39 40 41 42 43 44 45 46 47 48
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];
		snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
		clar__fail(file, line, err, buf, should_abort);
	}
}

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

49 50 51 52 53 54 55 56 57
/*
 * 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))

58 59
/* Write the contents of a buffer to disk */
void cl_git_mkfile(const char *filename, const char *content);
60
void cl_git_append2file(const char *filename, const char *new_content);
61
void cl_git_rewritefile(const char *filename, const char *new_content);
62
void cl_git_write2file(const char *filename, const char *new_content, int flags, unsigned int mode);
63

64 65 66
bool cl_toggle_filemode(const char *filename);
bool cl_is_chmod_supported(void);

67 68 69 70
/* Environment wrappers */
char *cl_getenv(const char *name);
int cl_setenv(const char *name, const char *value);

71 72 73
/* Reliable rename */
int cl_rename(const char *source, const char *dest);

74 75 76 77
/* Git sandbox setup helpers */

git_repository *cl_git_sandbox_init(const char *sandbox);
void cl_git_sandbox_cleanup(void);
78
git_repository *cl_git_sandbox_reopen(void);
79

80 81 82 83
/* Local-repo url helpers */
const char* cl_git_fixture_url(const char *fixturename);
const char* cl_git_path_url(const char *path);

84 85 86
/* Test repository cleaner */
int cl_git_remove_placeholders(const char *directory_path, const char *filename);

87 88
/* config setting helpers */
void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
89
int cl_repo_get_bool(git_repository *repo, const char *cfg);
90

Vicent Marti committed
91
#endif