]> git.proxmox.com Git - libgit2.git/blob - tests/clar_libgit2.h
Merge pull request #2459 from libgit2/cmn/http-url-path
[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) cl_git_pass_((expr), __FILE__, __LINE__)
15
16 #define cl_git_pass_(expr, file, line) do { \
17 int _lg2_error; \
18 giterr_clear(); \
19 if ((_lg2_error = (expr)) != 0) \
20 cl_git_report_failure(_lg2_error, file, line, "Function call failed: " #expr); \
21 } while (0)
22
23 /**
24 * Wrapper for `clar_must_fail` -- this one is
25 * just for consistency. Use with `git_` library
26 * calls that are supposed to fail!
27 */
28 #define cl_git_fail(expr) cl_must_fail(expr)
29
30 #define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)
31
32 /**
33 * Like cl_git_pass, only for Win32 error code conventions
34 */
35 #define cl_win32_pass(expr) do { \
36 int _win32_res; \
37 if ((_win32_res = (expr)) == 0) { \
38 giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
39 cl_git_report_failure(_win32_res, __FILE__, __LINE__, "System call failed: " #expr); \
40 } \
41 } while(0)
42
43 void cl_git_report_failure(int, const char *, int, const char *);
44
45 #define cl_assert_at_line(expr,file,line) \
46 clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
47
48 GIT_INLINE(void) clar__assert_in_range(
49 int lo, int val, int hi,
50 const char *file, int line, const char *err, int should_abort)
51 {
52 if (lo > val || hi < val) {
53 char buf[128];
54 snprintf(buf, sizeof(buf), "%d not in [%d,%d]", val, lo, hi);
55 clar__fail(file, line, err, buf, should_abort);
56 }
57 }
58
59 #define cl_assert_equal_sz(sz1,sz2) do { \
60 size_t __sz1 = (size_t)(sz1), __sz2 = (size_t)(sz2); \
61 clar__assert_equal(__FILE__,__LINE__,#sz1 " != " #sz2, 1, "%"PRIuZ, __sz1, __sz2); \
62 } while (0)
63
64 #define cl_assert_in_range(L,V,H) \
65 clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
66
67 #define cl_assert_equal_file(DATA,SIZE,PATH) \
68 clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,(int)__LINE__)
69
70 #define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
71 clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,(int)__LINE__)
72
73 void clar__assert_equal_file(
74 const char *expected_data,
75 size_t expected_size,
76 int ignore_cr,
77 const char *path,
78 const char *file,
79 int line);
80
81 GIT_INLINE(void) clar__assert_equal_oid(
82 const char *file, int line, const char *desc,
83 const git_oid *one, const git_oid *two)
84 {
85 if (git_oid_cmp(one, two)) {
86 char err[] = "\"........................................\" != \"........................................\"";
87
88 git_oid_fmt(&err[1], one);
89 git_oid_fmt(&err[47], two);
90
91 clar__fail(file, line, desc, err, 1);
92 }
93 }
94
95 #define cl_assert_equal_oid(one, two) \
96 clar__assert_equal_oid(__FILE__, __LINE__, \
97 "OID mismatch: " #one " != " #two, (one), (two))
98
99 /*
100 * Some utility macros for building long strings
101 */
102 #define REP4(STR) STR STR STR STR
103 #define REP15(STR) REP4(STR) REP4(STR) REP4(STR) STR STR STR
104 #define REP16(STR) REP4(REP4(STR))
105 #define REP256(STR) REP16(REP16(STR))
106 #define REP1024(STR) REP4(REP256(STR))
107
108 /* Write the contents of a buffer to disk */
109 void cl_git_mkfile(const char *filename, const char *content);
110 void cl_git_append2file(const char *filename, const char *new_content);
111 void cl_git_rewritefile(const char *filename, const char *new_content);
112 void cl_git_write2file(const char *path, const char *data,
113 size_t datalen, int flags, unsigned int mode);
114
115 bool cl_toggle_filemode(const char *filename);
116 bool cl_is_chmod_supported(void);
117
118 /* Environment wrappers */
119 char *cl_getenv(const char *name);
120 int cl_setenv(const char *name, const char *value);
121
122 /* Reliable rename */
123 int cl_rename(const char *source, const char *dest);
124
125 /* Git sandbox setup helpers */
126
127 git_repository *cl_git_sandbox_init(const char *sandbox);
128 void cl_git_sandbox_cleanup(void);
129 git_repository *cl_git_sandbox_reopen(void);
130
131 /* Local-repo url helpers */
132 const char* cl_git_fixture_url(const char *fixturename);
133 const char* cl_git_path_url(const char *path);
134
135 /* Test repository cleaner */
136 int cl_git_remove_placeholders(const char *directory_path, const char *filename);
137
138 /* commit creation helpers */
139 void cl_repo_commit_from_index(
140 git_oid *out,
141 git_repository *repo,
142 git_signature *sig,
143 git_time_t time,
144 const char *msg);
145
146 /* config setting helpers */
147 void cl_repo_set_bool(git_repository *repo, const char *cfg, int value);
148 int cl_repo_get_bool(git_repository *repo, const char *cfg);
149
150 void cl_repo_set_string(git_repository *repo, const char *cfg, const char *value);
151
152 /* set up a fake "home" directory and set libgit2 GLOBAL search path.
153 *
154 * automatically configures cleanup function to restore the regular search
155 * path, although you can call it explicitly if you wish (with NULL).
156 */
157 void cl_fake_home(void);
158 void cl_fake_home_cleanup(void *);
159
160 void cl_sandbox_set_search_path_defaults(void);
161
162 #endif