snapshot.c 3.7 KB
Newer Older
1 2
#include "clar_libgit2.h"

3 4
#include "config_backend.h"

5 6
static git_config *cfg;
static git_config *snapshot;
7

8 9 10 11 12 13 14
void test_config_snapshot__cleanup(void)
{
	git_config_free(cfg);
	cfg = NULL;
	git_config_free(snapshot);
	snapshot = NULL;
}
15

16 17 18
void test_config_snapshot__create_snapshot(void)
{
	int32_t i;
19

20 21 22 23
	cl_git_mkfile("config", "[old]\nvalue = 5\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "config"));
	cl_git_pass(git_config_get_int32(&i, cfg, "old.value"));
	cl_assert_equal_i(5, i);
24 25 26 27

	cl_git_pass(git_config_snapshot(&snapshot, cfg));

	/* Change the value on the file itself (simulate external process) */
28
	cl_git_mkfile("config", "[old]\nvalue = 56\n");
29

30 31 32 33
	cl_git_pass(git_config_get_int32(&i, cfg, "old.value"));
	cl_assert_equal_i(56, i);
	cl_git_pass(git_config_get_int32(&i, snapshot, "old.value"));
	cl_assert_equal_i(5, i);
34 35

	/* Change the value on the file itself (simulate external process) */
36
	cl_git_mkfile("config", "[old]\nvalue = 999\n");
37

38 39 40
	/* Old snapshot should still have the old value */
	cl_git_pass(git_config_get_int32(&i, snapshot, "old.value"));
	cl_assert_equal_i(5, i);
41 42

	/* New snapshot should see new value */
43
	git_config_free(snapshot);
44 45 46 47 48
	cl_git_pass(git_config_snapshot(&snapshot, cfg));
	cl_git_pass(git_config_get_int32(&i, snapshot, "old.value"));
	cl_assert_equal_i(999, i);

	cl_git_pass(p_unlink("config"));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

static int count_me(const git_config_entry *entry, void *payload)
{
	int *n = (int *) payload;

	GIT_UNUSED(entry);

	(*n)++;

	return 0;
}

void test_config_snapshot__multivar(void)
{
64
	int count;
65

66 67 68
	count = 0;
	cl_git_mkfile("config", "[old]\nvalue = 5\nvalue = 6\n");
	cl_git_pass(git_config_open_ondisk(&cfg, "config"));
69 70 71 72
	cl_git_pass(git_config_get_multivar_foreach(cfg, "old.value", NULL, count_me, &count));
	cl_assert_equal_i(2, count);

	count = 0;
73
	cl_git_pass(git_config_snapshot(&snapshot, cfg));
74 75 76
	cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count));
	cl_assert_equal_i(2, count);

77
	cl_git_pass(p_unlink("config"));
78
}
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

void test_config_snapshot__includes(void)
{
	int i;

	cl_git_mkfile("including", "[include]\npath = included");
	cl_git_mkfile("included", "[section]\nkey = 1\n");

	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
	cl_git_pass(git_config_snapshot(&snapshot, cfg));

	cl_git_pass(git_config_get_int32(&i, snapshot, "section.key"));
	cl_assert_equal_i(i, 1);

	/* Rewrite "included" config */
	cl_git_mkfile("included", "[section]\nkey = 11\n");

	/* Assert that the live config changed, but snapshot remained the same */
	cl_git_pass(git_config_get_int32(&i, cfg, "section.key"));
	cl_assert_equal_i(i, 11);
	cl_git_pass(git_config_get_int32(&i, snapshot, "section.key"));
	cl_assert_equal_i(i, 1);

	cl_git_pass(p_unlink("including"));
	cl_git_pass(p_unlink("included"));
}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

void test_config_snapshot__snapshot(void)
{
	git_config *snapshot_snapshot;
	int i;

	cl_git_mkfile("configfile", "[section]\nkey = 1\n");

	cl_git_pass(git_config_open_ondisk(&cfg, "configfile"));
	cl_git_pass(git_config_snapshot(&snapshot, cfg));

	cl_git_pass(git_config_snapshot(&snapshot_snapshot, snapshot));

	cl_git_pass(git_config_get_int32(&i, snapshot_snapshot, "section.key"));
	cl_assert_equal_i(i, 1);

	git_config_free(snapshot_snapshot);

	cl_git_pass(p_unlink("configfile"));
}
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

void test_config_snapshot__snapshot_from_in_memony(void)
{
	const char *configuration = "[section]\nkey = 1\n";
	git_config_backend *backend;
	int i;

	cl_git_pass(git_config_new(&cfg));
	cl_git_pass(git_config_backend_from_string(&backend, configuration, strlen(configuration)));
	cl_git_pass(git_config_add_backend(cfg, backend, 0, NULL, 0));

	cl_git_pass(git_config_snapshot(&snapshot, cfg));
	cl_git_pass(git_config_get_int32(&i, snapshot, "section.key"));
	cl_assert_equal_i(i, 1);
}