errors.c 1.62 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include "clar_libgit2.h"
#include "common.h"
#include "util.h"
#include "posix.h"

void test_core_errors__new_school(void)
{
	char *str_in_error;

10 11
	giterr_clear();
	cl_assert(giterr_last() == NULL);
12 13 14

	giterr_set_oom(); /* internal fn */

15 16 17
	cl_assert(giterr_last() != NULL);
	cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
	str_in_error = strstr(giterr_last()->message, "memory");
18 19
	cl_assert(str_in_error != NULL);

20
	giterr_clear();
21 22 23

	giterr_set(GITERR_REPOSITORY, "This is a test"); /* internal fn */

24 25
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "This is a test");
26 27
	cl_assert(str_in_error != NULL);

28 29
	giterr_clear();
	cl_assert(giterr_last() == NULL);
30

31
	do {
32
		struct stat st;
33
		memset(&st, 0, sizeof(st));
nulltoken committed
34
		cl_assert(p_lstat("this_file_does_not_exist", &st) < 0);
35
		GIT_UNUSED(st);
36
	} while (false);
37 38
	giterr_set(GITERR_OS, "stat failed"); /* internal fn */

39 40
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "stat failed");
41 42 43 44 45
	cl_assert(str_in_error != NULL);
	cl_assert(git__prefixcmp(str_in_error, "stat failed: ") == 0);
	cl_assert(strlen(str_in_error) > strlen("stat failed: "));

#ifdef GIT_WIN32
46
	giterr_clear();
47 48 49 50 51

	/* The MSDN docs use this to generate a sample error */
	cl_assert(GetProcessId(NULL) == 0);
	giterr_set(GITERR_OS, "GetProcessId failed"); /* internal fn */

52 53
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "GetProcessId failed");
54 55 56 57 58
	cl_assert(str_in_error != NULL);
	cl_assert(git__prefixcmp(str_in_error, "GetProcessId failed: ") == 0);
	cl_assert(strlen(str_in_error) > strlen("GetProcessId failed: "));
#endif

59
	giterr_clear();
60
}