]> git.proxmox.com Git - libgit2.git/blob - tests/config/snapshot.c
3ea07c11841d327a5c20edc1d743796aeb127849
[libgit2.git] / tests / config / snapshot.c
1 #include "clar_libgit2.h"
2
3 void test_config_snapshot__create_snapshot(void)
4 {
5 int32_t tmp;
6 git_config *cfg, *snapshot, *new_snapshot;
7 const char *filename = "config-ext-change";
8
9 cl_git_mkfile(filename, "[old]\nvalue = 5\n");
10
11 cl_git_pass(git_config_open_ondisk(&cfg, filename));
12
13 cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value"));
14 cl_assert_equal_i(5, tmp);
15
16 cl_git_pass(git_config_snapshot(&snapshot, cfg));
17
18 /* Change the value on the file itself (simulate external process) */
19 cl_git_mkfile(filename, "[old]\nvalue = 56\n");
20
21 cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value"));
22 cl_assert_equal_i(56, tmp);
23
24 cl_git_pass(git_config_get_int32(&tmp, snapshot, "old.value"));
25 cl_assert_equal_i(5, tmp);
26
27 /* Change the value on the file itself (simulate external process) */
28 cl_git_mkfile(filename, "[old]\nvalue = 999\n");
29
30 cl_git_pass(git_config_snapshot(&new_snapshot, cfg));
31
32 /* New snapshot should see new value */
33 cl_git_pass(git_config_get_int32(&tmp, new_snapshot, "old.value"));
34 cl_assert_equal_i(999, tmp);
35
36 /* Old snapshot should still have the old value */
37 cl_git_pass(git_config_get_int32(&tmp, snapshot, "old.value"));
38 cl_assert_equal_i(5, tmp);
39
40 git_config_free(new_snapshot);
41 git_config_free(snapshot);
42 git_config_free(cfg);
43 }
44
45 static int count_me(const git_config_entry *entry, void *payload)
46 {
47 int *n = (int *) payload;
48
49 GIT_UNUSED(entry);
50
51 (*n)++;
52
53 return 0;
54 }
55
56 void test_config_snapshot__multivar(void)
57 {
58 int count = 0;
59 git_config *cfg, *snapshot;
60 const char *filename = "config-file";
61
62 cl_git_mkfile(filename, "[old]\nvalue = 5\nvalue = 6\n");
63
64 cl_git_pass(git_config_open_ondisk(&cfg, filename));
65 cl_git_pass(git_config_get_multivar_foreach(cfg, "old.value", NULL, count_me, &count));
66
67 cl_assert_equal_i(2, count);
68
69 cl_git_pass(git_config_snapshot(&snapshot, cfg));
70 git_config_free(cfg);
71
72 count = 0;
73 cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count));
74
75 cl_assert_equal_i(2, count);
76
77 git_config_free(snapshot);
78 }