rename.c 2.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "clar_libgit2.h"
#include "config.h"

static git_repository *g_repo = NULL;
static git_config *g_config = NULL;

void test_config_rename__initialize(void)
{
    g_repo = cl_git_sandbox_init("testrepo.git");
	cl_git_pass(git_repository_config(&g_config, g_repo));
}

void test_config_rename__cleanup(void)
{
	git_config_free(g_config);
	g_config = NULL;

	cl_git_sandbox_cleanup();
	g_repo = NULL;
}

void test_config_rename__can_rename(void)
{
24
	git_config_entry *ce;
25 26 27 28

	cl_git_pass(git_config_get_entry(
		&ce, g_config, "branch.track-local.remote"));
	cl_assert_equal_s(".", ce->value);
29
	git_config_entry_free(ce);
30 31 32 33 34 35 36 37 38 39

	cl_git_fail(git_config_get_entry(
		&ce, g_config, "branch.local-track.remote"));

	cl_git_pass(git_config_rename_section(
		g_repo, "branch.track-local", "branch.local-track"));

	cl_git_pass(git_config_get_entry(
		&ce, g_config, "branch.local-track.remote"));
	cl_assert_equal_s(".", ce->value);
40
	git_config_entry_free(ce);
41 42 43 44 45 46 47

	cl_git_fail(git_config_get_entry(
		&ce, g_config, "branch.track-local.remote"));
}

void test_config_rename__prevent_overwrite(void)
{
48
	git_config_entry *ce;
49 50 51 52 53 54 55

	cl_git_pass(git_config_set_string(
		g_config, "branch.local-track.remote", "yellow"));

	cl_git_pass(git_config_get_entry(
		&ce, g_config, "branch.local-track.remote"));
	cl_assert_equal_s("yellow", ce->value);
56
	git_config_entry_free(ce);
57 58 59 60 61 62 63

	cl_git_pass(git_config_rename_section(
		g_repo, "branch.track-local", "branch.local-track"));

	cl_git_pass(git_config_get_entry(
		&ce, g_config, "branch.local-track.remote"));
	cl_assert_equal_s(".", ce->value);
64
	git_config_entry_free(ce);
65

66 67 68 69 70 71
	/* so, we don't currently prevent overwrite... */
	/* {
		const git_error *err;
		cl_assert((err = giterr_last()) != NULL);
		cl_assert(err->message != NULL);
	} */
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

static void assert_invalid_config_section_name(
	git_repository *repo, const char *name)
{
	cl_git_fail_with(
		git_config_rename_section(repo, "branch.remoteless", name),
		GIT_EINVALIDSPEC);
}

void test_config_rename__require_a_valid_new_name(void)
{
	assert_invalid_config_section_name(g_repo, "");
	assert_invalid_config_section_name(g_repo, "bra\nch");
	assert_invalid_config_section_name(g_repo, "branc#");
	assert_invalid_config_section_name(g_repo, "bra\nch.duh");
	assert_invalid_config_section_name(g_repo, "branc#.duh");
}