state.c 3.66 KB
Newer Older
Edward Thomson committed
1 2 3
#include "clar_libgit2.h"
#include "refs.h"
#include "posix.h"
4
#include "futils.h"
Edward Thomson committed
5 6

static git_repository *_repo;
7
static git_str _path;
Edward Thomson committed
8 9 10 11 12 13 14 15 16

void test_repo_state__initialize(void)
{
	_repo = cl_git_sandbox_init("testrepo.git");
}

void test_repo_state__cleanup(void)
{
	cl_git_sandbox_cleanup();
17
	git_str_dispose(&_path);
Edward Thomson committed
18 19
}

20
static void setup_simple_state(const char *filename)
Edward Thomson committed
21
{
22 23 24
	cl_git_pass(git_str_joinpath(&_path, git_repository_path(_repo), filename));
	git_futils_mkpath2file(git_str_cstr(&_path), 0777);
	cl_git_mkfile(git_str_cstr(&_path), "dummy");
Edward Thomson committed
25 26
}

27
static void assert_repo_state(git_repository_state_t state)
Edward Thomson committed
28
{
29 30
	cl_assert_equal_i(state, git_repository_state(_repo));
}
Edward Thomson committed
31

32 33 34 35 36 37 38
void test_repo_state__none_with_HEAD_attached(void)
{
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
}

void test_repo_state__none_with_HEAD_detached(void)
{
39
	cl_git_pass(git_repository_detach_head(_repo));
40 41 42 43 44 45 46
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
}

void test_repo_state__merge(void)
{
	setup_simple_state(GIT_MERGE_HEAD_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_MERGE);
47 48
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
Edward Thomson committed
49 50 51 52
}

void test_repo_state__revert(void)
{
53 54
	setup_simple_state(GIT_REVERT_HEAD_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REVERT);
55 56
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
Edward Thomson committed
57 58
}

59 60 61 62 63 64 65 66 67
void test_repo_state__revert_sequence(void)
{
	setup_simple_state(GIT_REVERT_HEAD_FILE);
	setup_simple_state(GIT_SEQUENCER_TODO_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REVERT_SEQUENCE);
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
}

Edward Thomson committed
68 69
void test_repo_state__cherry_pick(void)
{
70 71
	setup_simple_state(GIT_CHERRYPICK_HEAD_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_CHERRYPICK);
72 73
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
74 75
}

76 77 78 79 80 81 82 83 84
void test_repo_state__cherrypick_sequence(void)
{
	setup_simple_state(GIT_CHERRYPICK_HEAD_FILE);
	setup_simple_state(GIT_SEQUENCER_TODO_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_CHERRYPICK_SEQUENCE);
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
}

85 86 87 88
void test_repo_state__bisect(void)
{
	setup_simple_state(GIT_BISECT_LOG_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_BISECT);
89 90
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
91 92 93 94 95 96
}

void test_repo_state__rebase_interactive(void)
{
	setup_simple_state(GIT_REBASE_MERGE_INTERACTIVE_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REBASE_INTERACTIVE);
97 98
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
99 100 101 102 103 104
}

void test_repo_state__rebase_merge(void)
{
	setup_simple_state(GIT_REBASE_MERGE_DIR "whatever");
	assert_repo_state(GIT_REPOSITORY_STATE_REBASE_MERGE);
105 106
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
107 108 109 110 111 112
}

void test_repo_state__rebase(void)
{
	setup_simple_state(GIT_REBASE_APPLY_REBASING_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REBASE);
113 114
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
115 116 117 118 119 120
}

void test_repo_state__apply_mailbox(void)
{
	setup_simple_state(GIT_REBASE_APPLY_APPLYING_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX);
121 122
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
123 124 125 126 127 128
}

void test_repo_state__apply_mailbox_or_rebase(void)
{
	setup_simple_state(GIT_REBASE_APPLY_DIR "whatever");
	assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX_OR_REBASE);
129 130
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
Edward Thomson committed
131
}