state.c 3.68 KB
Newer Older
Edward Thomson committed
1 2 3 4
#include "clar_libgit2.h"
#include "buffer.h"
#include "refs.h"
#include "posix.h"
5
#include "fileops.h"
Edward Thomson committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

static git_repository *_repo;
static git_buf _path;

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

void test_repo_state__cleanup(void)
{
	cl_git_sandbox_cleanup();
	git_buf_free(&_path);
}

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

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

33 34 35 36 37 38 39
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)
{
40
	cl_git_pass(git_repository_detach_head(_repo));
41 42 43 44 45 46 47
	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);
48 49
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
Edward Thomson committed
50 51 52 53
}

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

60 61 62 63 64 65 66 67 68
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
69 70
void test_repo_state__cherry_pick(void)
{
71 72
	setup_simple_state(GIT_CHERRYPICK_HEAD_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_CHERRYPICK);
73 74
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
75 76
}

77 78 79 80 81 82 83 84 85
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);
}

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

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

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

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

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

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);
130 131
	cl_git_pass(git_repository_state_cleanup(_repo));
	assert_repo_state(GIT_REPOSITORY_STATE_NONE);
Edward Thomson committed
132
}