test: Properly show error messages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
diff --git a/include/git2/errors.h b/include/git2/errors.h
index 09b1f26..253cb6a 100644
--- a/include/git2/errors.h
+++ b/include/git2/errors.h
@@ -146,6 +146,11 @@ GIT_EXTERN(const char *) git_lasterror(void);
*/
GIT_EXTERN(const char *) git_strerror(int num);
+/**
+ * Clear the latest library error
+ */
+GIT_EXTERN(void) git_clearerror(void);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/errors.c b/src/errors.c
index 7da02b4..e9022c3 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -108,6 +108,13 @@ int git__throw(int error, const char *msg, ...)
const char *git_lasterror(void)
{
+ if (!g_last_error[0])
+ return NULL;
+
return g_last_error;
}
+void git_clearerror(void)
+{
+ g_last_error[0] = '\0';
+}
diff --git a/src/fileops.c b/src/fileops.c
index 2a5eb74..3397aad 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -123,8 +123,7 @@ int gitfo_isdir(const char *path)
struct stat st;
int len, stat_error;
- if (!path)
- return git__throw(GIT_ENOTFOUND, "No path given to gitfo_isdir");
+ assert(path);
len = strlen(path);
@@ -140,10 +139,10 @@ int gitfo_isdir(const char *path)
}
if (stat_error < GIT_SUCCESS)
- return git__throw(GIT_ENOTFOUND, "%s does not exist", path);
+ return GIT_ERROR;
if (!S_ISDIR(st.st_mode))
- return git__throw(GIT_ENOTFOUND, "%s is not a directory", path);
+ return GIT_ERROR;
return GIT_SUCCESS;
}
diff --git a/tests/test_lib.c b/tests/test_lib.c
index 3136eb0..a4c39df 100755
--- a/tests/test_lib.c
+++ b/tests/test_lib.c
@@ -77,13 +77,16 @@ void git_test__init(git_test *t, const char *name, const char *description)
static void fail_test(git_test *tc, const char *file, int line, const char *message)
{
char buf[1024];
+ const char *last_error = git_lasterror();
snprintf(buf, 1024, "%s:%d", file, line);
tc->failed = 1;
tc->message = strdup(message);
tc->failed_pos = strdup(buf);
- tc->error_message = strdup(git_lasterror());
+
+ if (last_error)
+ tc->error_message = strdup(last_error);
if (tc->jump != 0)
longjmp(*(tc->jump), 0);
diff --git a/tests/test_lib.h b/tests/test_lib.h
index f3febe8..69ad2ed 100755
--- a/tests/test_lib.h
+++ b/tests/test_lib.h
@@ -26,6 +26,7 @@
#define BEGIN_TEST(TNAME, DESC) \
static void _gittest__##TNAME(git_test *_gittest) { \
git_test__init(_gittest, #TNAME, DESC); \
+ git_clearerror();\
{\
#define END_TEST }}