Skip to content

Commit b13f0da

Browse files
author
Edward Thomson
authored
Merge pull request libgit2#4130 from libgit2/ethomson/clar_messages
Improve clar messages
2 parents d0c72a9 + c52480f commit b13f0da

File tree

7 files changed

+36
-24
lines changed

7 files changed

+36
-24
lines changed

src/unix/posix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extern char *p_realpath(const char *, char *);
5050
#define p_strcasecmp(s1, s2) strcasecmp(s1, s2)
5151
#define p_strncasecmp(s1, s2, c) strncasecmp(s1, s2, c)
5252
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a)
53-
#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
53+
#define p_snprintf(b, c, ...) snprintf(b, c, __VA_ARGS__)
5454
#define p_mkstemp(p) mkstemp(p)
5555
#define p_chdir(p) chdir(p)
5656
#define p_chmod(p,m) chmod(p, m)

tests/attr/ignore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ static void assert_is_ignored_(
2121
{
2222
int is_ignored = 0;
2323

24-
cl_git_pass_(
25-
git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), file, line);
24+
cl_git_expect(
25+
git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), 0, file, line);
2626

2727
clar__assert_equal(
2828
file, line, "expected != is_ignored", 1, "%d",

tests/clar_libgit2.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44
#include "git2/sys/repository.h"
55

66
void cl_git_report_failure(
7-
int error, const char *file, int line, const char *fncall)
7+
int error, int expected, const char *file, int line, const char *fncall)
88
{
99
char msg[4096];
1010
const git_error *last = giterr_last();
11-
p_snprintf(msg, 4096, "error %d - %s",
12-
error, last ? last->message : "<no message>");
11+
12+
if (expected)
13+
p_snprintf(msg, 4096, "error %d (expected %d) - %s",
14+
error, expected, last ? last->message : "<no message>");
15+
else if (error || last)
16+
p_snprintf(msg, 4096, "error %d - %s",
17+
error, last ? last->message : "<no message>");
18+
else
19+
p_snprintf(msg, 4096, "no error, expected non-zero return");
20+
1321
clar__assert(0, file, line, fncall, msg, 1);
1422
}
1523

tests/clar_libgit2.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@
1212
*
1313
* Use this wrapper around all `git_` library calls that return error codes!
1414
*/
15-
#define cl_git_pass(expr) cl_git_pass_((expr), __FILE__, __LINE__)
15+
#define cl_git_pass(expr) cl_git_expect((expr), 0, __FILE__, __LINE__)
1616

17-
#define cl_git_pass_(expr, file, line) do { \
17+
#define cl_git_fail_with(error, expr) cl_git_expect((expr), error, __FILE__, __LINE__)
18+
19+
#define cl_git_expect(expr, expected, file, line) do { \
1820
int _lg2_error; \
1921
giterr_clear(); \
20-
if ((_lg2_error = (expr)) != 0) \
21-
cl_git_report_failure(_lg2_error, file, line, "Function call failed: " #expr); \
22+
if ((_lg2_error = (expr)) != expected) \
23+
cl_git_report_failure(_lg2_error, expected, file, line, "Function call failed: " #expr); \
2224
} while (0)
2325

2426
/**
2527
* Wrapper for `clar_must_fail` -- this one is
2628
* just for consistency. Use with `git_` library
2729
* calls that are supposed to fail!
2830
*/
29-
#define cl_git_fail(expr) cl_must_fail(expr)
30-
31-
#define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr)
31+
#define cl_git_fail(expr) do { \
32+
giterr_clear(); \
33+
if ((expr) == 0) \
34+
cl_git_report_failure(0, 0, __FILE__, __LINE__, "Function call succeeded: " #expr); \
35+
} while (0)
3236

3337
/**
3438
* Like cl_git_pass, only for Win32 error code conventions
@@ -37,7 +41,7 @@
3741
int _win32_res; \
3842
if ((_win32_res = (expr)) == 0) { \
3943
giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
40-
cl_git_report_failure(_win32_res, __FILE__, __LINE__, "System call failed: " #expr); \
44+
cl_git_report_failure(_win32_res, 0, __FILE__, __LINE__, "System call failed: " #expr); \
4145
} \
4246
} while(0)
4347

@@ -86,7 +90,7 @@ GIT_INLINE(void) cl_git_thread_check(void *data)
8690
clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1);
8791
}
8892

89-
void cl_git_report_failure(int, const char *, int, const char *);
93+
void cl_git_report_failure(int, int, const char *, int, const char *);
9094

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

tests/repo/env.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char
5656
static void env_pass_(const char *path, const char *file, int line)
5757
{
5858
git_repository *repo;
59-
cl_git_pass_(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
60-
cl_git_pass_(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
59+
cl_git_expect(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line);
60+
cl_git_expect(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line);
6161
cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line);
6262
cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line);
6363
cl_assert_at_line(!git_repository_is_bare(repo), file, line);
@@ -98,24 +98,24 @@ static void env_check_objects_(bool a, bool t, bool p, const char *file, int lin
9898
cl_git_pass(git_oid_fromstr(&oid_a, "45141a79a77842c59a63229403220a4e4be74e3d"));
9999
cl_git_pass(git_oid_fromstr(&oid_t, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
100100
cl_git_pass(git_oid_fromstr(&oid_p, "0df1a5865c8abfc09f1f2182e6a31be550e99f07"));
101-
cl_git_pass_(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line);
101+
cl_git_expect(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line);
102102

103103
if (a) {
104-
cl_git_pass_(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
104+
cl_git_expect(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), 0, file, line);
105105
git_object_free(object);
106106
} else {
107107
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
108108
}
109109

110110
if (t) {
111-
cl_git_pass_(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
111+
cl_git_expect(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), 0, file, line);
112112
git_object_free(object);
113113
} else {
114114
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
115115
}
116116

117117
if (p) {
118-
cl_git_pass_(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
118+
cl_git_expect(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), 0, file, line);
119119
git_object_free(object);
120120
} else {
121121
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);

tests/status/ignore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ static void assert_ignored_(
2020
bool expected, const char *filepath, const char *file, int line)
2121
{
2222
int is_ignored = 0;
23-
cl_git_pass_(
24-
git_status_should_ignore(&is_ignored, g_repo, filepath), file, line);
23+
cl_git_expect(
24+
git_status_should_ignore(&is_ignored, g_repo, filepath), 0, file, line);
2525
clar__assert(
2626
(expected != 0) == (is_ignored != 0),
2727
file, line, "expected != is_ignored", filepath, 1);

tests/submodule/submodule_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void assert__submodule_exists(
204204
git_submodule *sm;
205205
int error = git_submodule_lookup(&sm, repo, name);
206206
if (error)
207-
cl_git_report_failure(error, file, line, msg);
207+
cl_git_report_failure(error, 0, file, line, msg);
208208
cl_assert_at_line(sm != NULL, file, line);
209209
git_submodule_free(sm);
210210
}

0 commit comments

Comments
 (0)