config_helpers.c 1.35 KB
Newer Older
1 2 3
#include "clar_libgit2.h"
#include "config_helpers.h"
#include "repository.h"
4
#include "buffer.h"
5 6 7 8 9 10 11

void assert_config_entry_existence(
	git_repository *repo,
	const char *name,
	bool is_supposed_to_exist)
{
	git_config *config;
12
	git_config_entry *entry = NULL;
13 14 15 16
	int result;

	cl_git_pass(git_repository_config__weakptr(&config, repo));
	
17 18
	result = git_config_get_entry(&entry, config, name);
	git_config_entry_free(entry);
19 20 21 22 23 24

	if (is_supposed_to_exist)
		cl_git_pass(result);
	else
		cl_assert_equal_i(GIT_ENOTFOUND, result);
}
25 26 27 28 29 30 31

void assert_config_entry_value(
	git_repository *repo,
	const char *name,
	const char *expected_value)
{
	git_config *config;
32
	git_buf buf = GIT_BUF_INIT;
33 34 35

	cl_git_pass(git_repository_config__weakptr(&config, repo));

36
	cl_git_pass(git_config_get_string_buf(&buf, config, name));
37

38
	cl_assert_equal_s(expected_value, git_buf_cstr(&buf));
39
	git_buf_dispose(&buf);
40
}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

static int count_config_entries_cb(
	const git_config_entry *entry,
	void *payload)
{
	int *how_many = (int *)payload;

	GIT_UNUSED(entry);

	(*how_many)++;

	return 0;
}

int count_config_entries_match(git_repository *repo, const char *pattern)
{
	git_config *config;
	int how_many = 0;

	cl_git_pass(git_repository_config(&config, repo));

	cl_assert_equal_i(0, git_config_foreach_match(
		config,	pattern, count_config_entries_cb, &how_many));

	git_config_free(config);

	return how_many;
}