trace.c 2.02 KB
Newer Older
Edward Thomson committed
1
#include "clar_libgit2.h"
2
#include "clar_libgit2_trace.h"
Edward Thomson committed
3 4 5 6 7 8
#include "trace.h"

static int written = 0;

static void trace_callback(git_trace_level_t level, const char *message)
{
9 10
	GIT_UNUSED(level);

Edward Thomson committed
11 12 13 14 15 16 17
	cl_assert(strcmp(message, "Hello world!") == 0);

	written = 1;
}

void test_trace_trace__initialize(void)
{
18 19 20
	/* If global tracing is enabled, disable for the duration of this test. */
	cl_global_trace_disable();

Edward Thomson committed
21 22 23 24 25 26 27
	git_trace_set(GIT_TRACE_INFO, trace_callback);
	written = 0;
}

void test_trace_trace__cleanup(void)
{
	git_trace_set(GIT_TRACE_NONE, NULL);
28 29 30

	/* If global tracing was enabled, restart it. */
	cl_global_trace_register();
Edward Thomson committed
31 32 33 34 35 36
}

void test_trace_trace__sets(void)
{
#ifdef GIT_TRACE
	cl_assert(git_trace_level() == GIT_TRACE_INFO);
37 38
#else
	cl_skip();
Edward Thomson committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#endif
}

void test_trace_trace__can_reset(void)
{
#ifdef GIT_TRACE
	cl_assert(git_trace_level() == GIT_TRACE_INFO);
	cl_git_pass(git_trace_set(GIT_TRACE_ERROR, trace_callback));

	cl_assert(written == 0);
	git_trace(GIT_TRACE_INFO, "Hello %s!", "world");
	cl_assert(written == 0);

	git_trace(GIT_TRACE_ERROR, "Hello %s!", "world");
	cl_assert(written == 1);
54 55
#else
	cl_skip();
Edward Thomson committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
#endif
}

void test_trace_trace__can_unset(void)
{
#ifdef GIT_TRACE
	cl_assert(git_trace_level() == GIT_TRACE_INFO);
	cl_git_pass(git_trace_set(GIT_TRACE_NONE, NULL));

	cl_assert(git_trace_level() == GIT_TRACE_NONE);

	cl_assert(written == 0);
	git_trace(GIT_TRACE_FATAL, "Hello %s!", "world");
	cl_assert(written == 0);
70 71
#else
	cl_skip();
Edward Thomson committed
72 73 74 75 76 77 78 79 80
#endif
}

void test_trace_trace__skips_higher_level(void)
{
#ifdef GIT_TRACE
	cl_assert(written == 0);
	git_trace(GIT_TRACE_DEBUG, "Hello %s!", "world");
	cl_assert(written == 0);
81 82
#else
	cl_skip();
Edward Thomson committed
83 84 85 86 87 88 89 90 91
#endif
}

void test_trace_trace__writes(void)
{
#ifdef GIT_TRACE
	cl_assert(written == 0);
	git_trace(GIT_TRACE_INFO, "Hello %s!", "world");
	cl_assert(written == 1);
92 93
#else
	cl_skip();
Edward Thomson committed
94 95 96 97 98 99 100 101 102
#endif
}

void test_trace_trace__writes_lower_level(void)
{
#ifdef GIT_TRACE
	cl_assert(written == 0);
	git_trace(GIT_TRACE_ERROR, "Hello %s!", "world");
	cl_assert(written == 1);
103 104
#else
	cl_skip();
Edward Thomson committed
105 106
#endif
}