state.c 2.24 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 40 41 42 43 44 45 46 47
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)
{
	cl_git_pass(git_repository_detach_head(_repo));
	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);
Edward Thomson committed
48 49 50 51
}

void test_repo_state__revert(void)
{
52 53
	setup_simple_state(GIT_REVERT_HEAD_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REVERT);
Edward Thomson committed
54 55 56 57
}

void test_repo_state__cherry_pick(void)
{
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	setup_simple_state(GIT_CHERRY_PICK_HEAD_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_CHERRY_PICK);
}

void test_repo_state__bisect(void)
{
	setup_simple_state(GIT_BISECT_LOG_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_BISECT);
}

void test_repo_state__rebase_interactive(void)
{
	setup_simple_state(GIT_REBASE_MERGE_INTERACTIVE_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REBASE_INTERACTIVE);
}

void test_repo_state__rebase_merge(void)
{
	setup_simple_state(GIT_REBASE_MERGE_DIR "whatever");
	assert_repo_state(GIT_REPOSITORY_STATE_REBASE_MERGE);
}

void test_repo_state__rebase(void)
{
	setup_simple_state(GIT_REBASE_APPLY_REBASING_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_REBASE);
}

void test_repo_state__apply_mailbox(void)
{
	setup_simple_state(GIT_REBASE_APPLY_APPLYING_FILE);
	assert_repo_state(GIT_REPOSITORY_STATE_APPLY_MAILBOX);
}

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);
Edward Thomson committed
96
}