Commit a1dcc830 by Edward Thomson

tests: provide better pass/failure error messages

Provide more detailed messages when conditions pass or fail
unexpectedly.  In particular, this provides the error messages when a
test fails with a different error code than was expected.
parent cc172642
...@@ -21,8 +21,8 @@ static void assert_is_ignored_( ...@@ -21,8 +21,8 @@ static void assert_is_ignored_(
{ {
int is_ignored = 0; int is_ignored = 0;
cl_git_pass_( cl_git_exec(
git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), file, line); git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), 0, file, line);
clar__assert_equal( clar__assert_equal(
file, line, "expected != is_ignored", 1, "%d", file, line, "expected != is_ignored", 1, "%d",
......
...@@ -4,12 +4,20 @@ ...@@ -4,12 +4,20 @@
#include "git2/sys/repository.h" #include "git2/sys/repository.h"
void cl_git_report_failure( void cl_git_report_failure(
int error, const char *file, int line, const char *fncall) int error, int expected, const char *file, int line, const char *fncall)
{ {
char msg[4096]; char msg[4096];
const git_error *last = giterr_last(); const git_error *last = giterr_last();
p_snprintf(msg, 4096, "error %d - %s",
error, last ? last->message : "<no message>"); if (expected)
p_snprintf(msg, 4096, "error %d (expected %d) - %s",
error, expected, last ? last->message : "<no message>");
else if (error || last)
p_snprintf(msg, 4096, "error %d - %s",
error, last ? last->message : "<no message>");
else
p_snprintf(msg, 4096, "no error, expected non-zero return");
clar__assert(0, file, line, fncall, msg, 1); clar__assert(0, file, line, fncall, msg, 1);
} }
......
...@@ -12,13 +12,15 @@ ...@@ -12,13 +12,15 @@
* *
* Use this wrapper around all `git_` library calls that return error codes! * Use this wrapper around all `git_` library calls that return error codes!
*/ */
#define cl_git_pass(expr) cl_git_pass_((expr), __FILE__, __LINE__) #define cl_git_pass(expr) cl_git_exec((expr), 0, __FILE__, __LINE__)
#define cl_git_pass_(expr, file, line) do { \ #define cl_git_fail_with(error, expr) cl_git_exec((expr), error, __FILE__, __LINE__)
#define cl_git_exec(expr, expected, file, line) do { \
int _lg2_error; \ int _lg2_error; \
giterr_clear(); \ giterr_clear(); \
if ((_lg2_error = (expr)) != 0) \ if ((_lg2_error = (expr)) != expected) \
cl_git_report_failure(_lg2_error, file, line, "Function call failed: " #expr); \ cl_git_report_failure(_lg2_error, expected, file, line, "Function call failed: " #expr); \
} while (0) } while (0)
/** /**
...@@ -26,9 +28,11 @@ ...@@ -26,9 +28,11 @@
* just for consistency. Use with `git_` library * just for consistency. Use with `git_` library
* calls that are supposed to fail! * calls that are supposed to fail!
*/ */
#define cl_git_fail(expr) cl_must_fail(expr) #define cl_git_fail(expr) do { \
giterr_clear(); \
#define cl_git_fail_with(expr, error) cl_assert_equal_i(error,expr) if ((expr) == 0) \
cl_git_report_failure(0, 0, __FILE__, __LINE__, "Function call succeeded: " #expr); \
} while (0)
/** /**
* Like cl_git_pass, only for Win32 error code conventions * Like cl_git_pass, only for Win32 error code conventions
...@@ -37,7 +41,7 @@ ...@@ -37,7 +41,7 @@
int _win32_res; \ int _win32_res; \
if ((_win32_res = (expr)) == 0) { \ if ((_win32_res = (expr)) == 0) { \
giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \ giterr_set(GITERR_OS, "Returned: %d, system error code: %d", _win32_res, GetLastError()); \
cl_git_report_failure(_win32_res, __FILE__, __LINE__, "System call failed: " #expr); \ cl_git_report_failure(_win32_res, 0, __FILE__, __LINE__, "System call failed: " #expr); \
} \ } \
} while(0) } while(0)
...@@ -86,7 +90,7 @@ GIT_INLINE(void) cl_git_thread_check(void *data) ...@@ -86,7 +90,7 @@ GIT_INLINE(void) cl_git_thread_check(void *data)
clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1); clar__assert(0, threaderr->file, threaderr->line, threaderr->expr, threaderr->error_msg, 1);
} }
void cl_git_report_failure(int, const char *, int, const char *); void cl_git_report_failure(int, int, const char *, int, const char *);
#define cl_assert_at_line(expr,file,line) \ #define cl_assert_at_line(expr,file,line) \
clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1) clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
......
...@@ -56,8 +56,8 @@ static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char ...@@ -56,8 +56,8 @@ static int GIT_FORMAT_PRINTF(2, 3) cl_setenv_printf(const char *name, const char
static void env_pass_(const char *path, const char *file, int line) static void env_pass_(const char *path, const char *file, int line)
{ {
git_repository *repo; git_repository *repo;
cl_git_pass_(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line); cl_git_exec(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line);
cl_git_pass_(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line); cl_git_exec(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line);
cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line); cl_assert_at_line(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0, file, line);
cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line); cl_assert_at_line(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0, file, line);
cl_assert_at_line(!git_repository_is_bare(repo), file, line); 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 ...@@ -98,24 +98,24 @@ static void env_check_objects_(bool a, bool t, bool p, const char *file, int lin
cl_git_pass(git_oid_fromstr(&oid_a, "45141a79a77842c59a63229403220a4e4be74e3d")); cl_git_pass(git_oid_fromstr(&oid_a, "45141a79a77842c59a63229403220a4e4be74e3d"));
cl_git_pass(git_oid_fromstr(&oid_t, "1385f264afb75a56a5bec74243be9b367ba4ca08")); cl_git_pass(git_oid_fromstr(&oid_t, "1385f264afb75a56a5bec74243be9b367ba4ca08"));
cl_git_pass(git_oid_fromstr(&oid_p, "0df1a5865c8abfc09f1f2182e6a31be550e99f07")); cl_git_pass(git_oid_fromstr(&oid_p, "0df1a5865c8abfc09f1f2182e6a31be550e99f07"));
cl_git_pass_(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line); cl_git_exec(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL), 0, file, line);
if (a) { if (a) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line); cl_git_exec(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), 0, file, line);
git_object_free(object); git_object_free(object);
} else { } else {
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line); cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_a, GIT_OBJ_BLOB), file, line);
} }
if (t) { if (t) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line); cl_git_exec(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), 0, file, line);
git_object_free(object); git_object_free(object);
} else { } else {
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line); cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_t, GIT_OBJ_BLOB), file, line);
} }
if (p) { if (p) {
cl_git_pass_(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line); cl_git_exec(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), 0, file, line);
git_object_free(object); git_object_free(object);
} else { } else {
cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line); cl_git_fail_at_line(git_object_lookup(&object, repo, &oid_p, GIT_OBJ_COMMIT), file, line);
......
...@@ -20,8 +20,8 @@ static void assert_ignored_( ...@@ -20,8 +20,8 @@ static void assert_ignored_(
bool expected, const char *filepath, const char *file, int line) bool expected, const char *filepath, const char *file, int line)
{ {
int is_ignored = 0; int is_ignored = 0;
cl_git_pass_( cl_git_exec(
git_status_should_ignore(&is_ignored, g_repo, filepath), file, line); git_status_should_ignore(&is_ignored, g_repo, filepath), 0, file, line);
clar__assert( clar__assert(
(expected != 0) == (is_ignored != 0), (expected != 0) == (is_ignored != 0),
file, line, "expected != is_ignored", filepath, 1); file, line, "expected != is_ignored", filepath, 1);
......
...@@ -204,7 +204,7 @@ void assert__submodule_exists( ...@@ -204,7 +204,7 @@ void assert__submodule_exists(
git_submodule *sm; git_submodule *sm;
int error = git_submodule_lookup(&sm, repo, name); int error = git_submodule_lookup(&sm, repo, name);
if (error) if (error)
cl_git_report_failure(error, file, line, msg); cl_git_report_failure(error, 0, file, line, msg);
cl_assert_at_line(sm != NULL, file, line); cl_assert_at_line(sm != NULL, file, line);
git_submodule_free(sm); git_submodule_free(sm);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment