remote.c 2.38 KB
Newer Older
1 2 3 4 5
#include "clar_libgit2.h"
#include "branch.h"
#include "remote.h"

static git_repository *g_repo;
6 7 8
static const char *remote_tracking_branch_name = "refs/remotes/test/master";
static const char *expected_remote_name = "test";
static int expected_remote_name_length;
9 10 11 12 13

void test_refs_branches_remote__initialize(void)
{
	g_repo = cl_git_sandbox_init("testrepo");

14
	expected_remote_name_length = (int)strlen(expected_remote_name) + 1;
15 16 17 18 19 20 21 22 23 24 25
}

void test_refs_branches_remote__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_refs_branches_remote__can_get_remote_for_branch(void)
{
	char remotename[1024] = {0};

26 27
	cl_assert_equal_i(expected_remote_name_length,
		git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name));
28

29 30 31
	cl_assert_equal_i(expected_remote_name_length,
		git_branch_remote_name(remotename, expected_remote_name_length, g_repo,
			remote_tracking_branch_name));
32

33
	cl_assert_equal_s("test", remotename);
34 35 36 37 38 39
}

void test_refs_branches_remote__insufficient_buffer_returns_error(void)
{
	char remotename[1024] = {0};

40 41
	cl_assert_equal_i(expected_remote_name_length,
		git_branch_remote_name(NULL, 0, g_repo, remote_tracking_branch_name));
42

43 44
	cl_git_fail_with(git_branch_remote_name(remotename,
		expected_remote_name_length - 1, g_repo, remote_tracking_branch_name),
45
			expected_remote_name_length);
46 47 48 49
}

void test_refs_branches_remote__no_matching_remote_returns_error(void)
{
50
	const char *unknown = "refs/remotes/nonexistent/master";
51

52
	giterr_clear();
53 54
	cl_git_fail_with(git_branch_remote_name(
		NULL, 0, g_repo, unknown), GIT_ENOTFOUND);
55
	cl_assert(giterr_last() != NULL);
56 57 58 59
}

void test_refs_branches_remote__local_remote_returns_error(void)
{
60
	const char *local = "refs/heads/master";
61

62
	giterr_clear();
63 64
	cl_git_fail_with(git_branch_remote_name(
		NULL, 0, g_repo, local), GIT_ERROR);
65
	cl_assert(giterr_last() != NULL);
66 67 68 69 70 71 72 73 74 75
}

void test_refs_branches_remote__ambiguous_remote_returns_error(void)
{
	git_remote *remote;

	/* Create the remote */
	cl_git_pass(git_remote_create(&remote, g_repo, "addtest", "http://github.com/libgit2/libgit2"));

	/* Update the remote fetch spec */
76
	git_remote_clear_refspecs(remote);
77
	cl_git_pass(git_remote_add_fetch(remote, "refs/heads/*:refs/remotes/test/*"));
78 79 80 81
	cl_git_pass(git_remote_save(remote));

	git_remote_free(remote);

82
	giterr_clear();
83 84
	cl_git_fail_with(git_branch_remote_name(NULL, 0, g_repo,
		remote_tracking_branch_name), GIT_EAMBIGUOUS);
85
	cl_assert(giterr_last() != NULL);
86
}