]> git.proxmox.com Git - libgit2.git/blob - tests/clar_libgit2.h
Merge pull request #1997 from mgbowen/merge-options-init-fix
[libgit2.git] / tests / clar_libgit2.h
1 #ifndef __CLAR_LIBGIT2__
2 #define __CLAR_LIBGIT2__
3
4 #include "clar.h"
5 #include <git2.h>
6 #include "common.h"
7
8 /**
9 * Replace for `clar_must_pass` that passes the last library error as the
10 * test failure message.
11 *
12 * Use this wrapper around all `git_` library calls that return error codes!
13 */
14 #define cl_git_pass(expr) do { \
15 int _lg2_error; \
16 giterr_clear(); \
17 if ((_lg2_error = (expr)) != 0) \
18 cl_git_report_failure(_lg2_error, __FILE__, __LINE__, "Function call failed: " #expr); \
19 } while (0)
20
21 /**
22 * Wrapper for `clar_must_fail` -- this one is
23 * just for consistency. Use with `git_` library
24 * calls that are supposed to fail!
25 */
26 #define cl_git_fail(expr) cl_must_fail(expr)
27
28 #define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)
29
30 void cl_git_report_failure(int, const char *, int, const char *);
31
32 #define cl_assert_at_line(expr,file,line) \
33 clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
34
35 GIT_INLINE(void) clar__assert_in_range(
36 int lo, int val, int hi,
37 const char *file, int line, const char *err, int should_abort)
38 {
39 if (lo > val || hi < val) {
40 char buf[128];
41 snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
42 clar__fail(file, line, err, buf, should_abort);
43 }
44 }
45
46 #define cl_assert_equal_sz(sz1,sz2) do { \
47 size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
48 clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
49 } while (0)
50
51 #define cl_assert_in_range(L,V,H) \
52 clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
53
54 #define cl_assert_equal_file(DATA,SIZE,PATH) \
55 clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
56
57 #define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
58 clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
59
60 void clar__assert_equal_file(
61 const char *expected_data,
62 size_t expected_size,
63 int ignore_cr,
64 const char *path,
65 const char *file,
66 int line);
67
68 /*
69 * Some utility macros for building long strings
70 */
71 #define REP4(STR) STR STR STR STR
72 #define REP15(STR) REP4(STR) REP4(STR) REP4(STR) STR STR STR
73 #define REP16(STR) REP4(REP4(STR))
74 #define REP256(STR) REP16(REP16(STR))
75 #define REP1024(STR) REP4(REP256(STR))
76
77 /* Write the contents of a buffer to disk */
78 void cl_git_mkfile(const char *filename, const char *content);
79 void cl_git_append2file(const char *filename, const char *new_content);
80 void cl_git_rewritefile(const char *filename, const char *new_content);
81 void cl_git_write2file(const char *path, const char *data,
82 size_t datalen, int flags, unsigned int mode);
83
84 bool cl_toggle_filemode(const char *filename);
85 bool cl_is_chmod_supported(void);
86
87 /* Environment wrappers */
88 char *cl_getenv(const char *name);
89 int cl_setenv(const char *name, const char *value);
90
91 /* Reliable rename */
92 int cl_rename(const char *source, const char *dest);
93
94 /* Git sandbox setup helpers */
95
96 git_repository *cl_git_sandbox_init(const char *sandbox);
97 void cl_git_sandbox_cleanup(void);
98 git_repository *cl_git_sandbox_reopen(void);
99
100 /* Local-repo url helpers */
101 const char* cl_git_fixture_url(const char *fixturename);
102 const char* cl_git_path_url(const char *path);
103
104 /* Test repository cleaner */
105 int cl_git_remove_placeholders(const char *directory_path, const char *filename);
106
107 /* commit creation helpers */
108 void cl_repo_commit_from_index(
109 git_oid *out,
110 git_repository *repo,
111 git_signature *sig,
112 git_time_t time,
113 const char *msg);
114
115 /* config setting helpers */
116 void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
117 int cl_repo_get_bool(git_repository *repo, const char *cfg);
118
119 void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
120
121 #endif