errors.c 2.19 KB
Newer Older
1
#include "clar_libgit2.h"
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

void test_core_errors__public_api(void)
{
	char *str_in_error;

	giterr_clear();
	cl_assert(giterr_last() == NULL);

	giterr_set_oom();

	cl_assert(giterr_last() != NULL);
	cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
	str_in_error = strstr(giterr_last()->message, "memory");
	cl_assert(str_in_error != NULL);

	giterr_clear();

	giterr_set_str(GITERR_REPOSITORY, "This is a test");

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

	giterr_clear();
	cl_assert(giterr_last() == NULL);
}

29 30 31 32 33 34 35 36
#include "common.h"
#include "util.h"
#include "posix.h"

void test_core_errors__new_school(void)
{
	char *str_in_error;

37 38
	giterr_clear();
	cl_assert(giterr_last() == NULL);
39 40 41

	giterr_set_oom(); /* internal fn */

42 43 44
	cl_assert(giterr_last() != NULL);
	cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
	str_in_error = strstr(giterr_last()->message, "memory");
45 46
	cl_assert(str_in_error != NULL);

47
	giterr_clear();
48 49 50

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

51 52
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "This is a test");
53 54
	cl_assert(str_in_error != NULL);

55 56
	giterr_clear();
	cl_assert(giterr_last() == NULL);
57

58
	do {
59
		struct stat st;
60
		memset(&st, 0, sizeof(st));
nulltoken committed
61
		cl_assert(p_lstat("this_file_does_not_exist", &st) < 0);
62
		GIT_UNUSED(st);
63
	} while (false);
64 65
	giterr_set(GITERR_OS, "stat failed"); /* internal fn */

66 67
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "stat failed");
68 69 70 71 72
	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
73
	giterr_clear();
74 75 76 77 78

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

79 80
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "GetProcessId failed");
81 82 83 84 85
	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

86
	giterr_clear();
87
}