]>
Commit | Line | Data |
---|---|---|
55ebd7d3 CMN |
1 | #include "clar_libgit2.h" |
2 | ||
22a2d3d5 | 3 | #include "config_backend.h" |
55ebd7d3 | 4 | |
22a2d3d5 UG |
5 | static git_config *cfg; |
6 | static git_config *snapshot; | |
55ebd7d3 | 7 | |
22a2d3d5 UG |
8 | void test_config_snapshot__cleanup(void) |
9 | { | |
10 | git_config_free(cfg); | |
11 | cfg = NULL; | |
12 | git_config_free(snapshot); | |
13 | snapshot = NULL; | |
14 | } | |
55ebd7d3 | 15 | |
22a2d3d5 UG |
16 | void test_config_snapshot__create_snapshot(void) |
17 | { | |
18 | int32_t i; | |
19 | ||
20 | cl_git_mkfile("config", "[old]\nvalue = 5\n"); | |
21 | cl_git_pass(git_config_open_ondisk(&cfg, "config")); | |
22 | cl_git_pass(git_config_get_int32(&i, cfg, "old.value")); | |
23 | cl_assert_equal_i(5, i); | |
55ebd7d3 CMN |
24 | |
25 | cl_git_pass(git_config_snapshot(&snapshot, cfg)); | |
26 | ||
27 | /* Change the value on the file itself (simulate external process) */ | |
22a2d3d5 | 28 | cl_git_mkfile("config", "[old]\nvalue = 56\n"); |
55ebd7d3 | 29 | |
22a2d3d5 UG |
30 | cl_git_pass(git_config_get_int32(&i, cfg, "old.value")); |
31 | cl_assert_equal_i(56, i); | |
32 | cl_git_pass(git_config_get_int32(&i, snapshot, "old.value")); | |
33 | cl_assert_equal_i(5, i); | |
5490c9d4 AR |
34 | |
35 | /* Change the value on the file itself (simulate external process) */ | |
22a2d3d5 | 36 | cl_git_mkfile("config", "[old]\nvalue = 999\n"); |
5490c9d4 | 37 | |
22a2d3d5 UG |
38 | /* Old snapshot should still have the old value */ |
39 | cl_git_pass(git_config_get_int32(&i, snapshot, "old.value")); | |
40 | cl_assert_equal_i(5, i); | |
5490c9d4 AR |
41 | |
42 | /* New snapshot should see new value */ | |
55ebd7d3 | 43 | git_config_free(snapshot); |
22a2d3d5 UG |
44 | cl_git_pass(git_config_snapshot(&snapshot, cfg)); |
45 | cl_git_pass(git_config_get_int32(&i, snapshot, "old.value")); | |
46 | cl_assert_equal_i(999, i); | |
47 | ||
48 | cl_git_pass(p_unlink("config")); | |
55ebd7d3 CMN |
49 | } |
50 | ||
51 | static int count_me(const git_config_entry *entry, void *payload) | |
52 | { | |
53 | int *n = (int *) payload; | |
54 | ||
55 | GIT_UNUSED(entry); | |
56 | ||
57 | (*n)++; | |
58 | ||
59 | return 0; | |
60 | } | |
61 | ||
62 | void test_config_snapshot__multivar(void) | |
63 | { | |
22a2d3d5 | 64 | int count; |
55ebd7d3 | 65 | |
22a2d3d5 UG |
66 | count = 0; |
67 | cl_git_mkfile("config", "[old]\nvalue = 5\nvalue = 6\n"); | |
68 | cl_git_pass(git_config_open_ondisk(&cfg, "config")); | |
55ebd7d3 | 69 | cl_git_pass(git_config_get_multivar_foreach(cfg, "old.value", NULL, count_me, &count)); |
22a2d3d5 | 70 | cl_assert_equal_i(2, count); |
55ebd7d3 | 71 | |
22a2d3d5 UG |
72 | count = 0; |
73 | cl_git_pass(git_config_snapshot(&snapshot, cfg)); | |
74 | cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count)); | |
55ebd7d3 CMN |
75 | cl_assert_equal_i(2, count); |
76 | ||
22a2d3d5 UG |
77 | cl_git_pass(p_unlink("config")); |
78 | } | |
79 | ||
80 | void test_config_snapshot__includes(void) | |
81 | { | |
82 | int i; | |
83 | ||
84 | cl_git_mkfile("including", "[include]\npath = included"); | |
85 | cl_git_mkfile("included", "[section]\nkey = 1\n"); | |
86 | ||
87 | cl_git_pass(git_config_open_ondisk(&cfg, "including")); | |
55ebd7d3 | 88 | cl_git_pass(git_config_snapshot(&snapshot, cfg)); |
55ebd7d3 | 89 | |
22a2d3d5 UG |
90 | cl_git_pass(git_config_get_int32(&i, snapshot, "section.key")); |
91 | cl_assert_equal_i(i, 1); | |
55ebd7d3 | 92 | |
22a2d3d5 UG |
93 | /* Rewrite "included" config */ |
94 | cl_git_mkfile("included", "[section]\nkey = 11\n"); | |
55ebd7d3 | 95 | |
22a2d3d5 UG |
96 | /* Assert that the live config changed, but snapshot remained the same */ |
97 | cl_git_pass(git_config_get_int32(&i, cfg, "section.key")); | |
98 | cl_assert_equal_i(i, 11); | |
99 | cl_git_pass(git_config_get_int32(&i, snapshot, "section.key")); | |
100 | cl_assert_equal_i(i, 1); | |
101 | ||
102 | cl_git_pass(p_unlink("including")); | |
103 | cl_git_pass(p_unlink("included")); | |
104 | } | |
105 | ||
106 | void test_config_snapshot__snapshot(void) | |
107 | { | |
108 | git_config *snapshot_snapshot; | |
109 | int i; | |
110 | ||
111 | cl_git_mkfile("configfile", "[section]\nkey = 1\n"); | |
112 | ||
113 | cl_git_pass(git_config_open_ondisk(&cfg, "configfile")); | |
114 | cl_git_pass(git_config_snapshot(&snapshot, cfg)); | |
115 | ||
116 | cl_git_pass(git_config_snapshot(&snapshot_snapshot, snapshot)); | |
117 | ||
118 | cl_git_pass(git_config_get_int32(&i, snapshot_snapshot, "section.key")); | |
119 | cl_assert_equal_i(i, 1); | |
120 | ||
121 | git_config_free(snapshot_snapshot); | |
122 | ||
123 | cl_git_pass(p_unlink("configfile")); | |
124 | } | |
125 | ||
126 | void test_config_snapshot__snapshot_from_in_memony(void) | |
127 | { | |
128 | const char *configuration = "[section]\nkey = 1\n"; | |
129 | git_config_backend *backend; | |
130 | int i; | |
131 | ||
132 | cl_git_pass(git_config_new(&cfg)); | |
133 | cl_git_pass(git_config_backend_from_string(&backend, configuration, strlen(configuration))); | |
134 | cl_git_pass(git_config_add_backend(cfg, backend, 0, NULL, 0)); | |
135 | ||
136 | cl_git_pass(git_config_snapshot(&snapshot, cfg)); | |
137 | cl_git_pass(git_config_get_int32(&i, snapshot, "section.key")); | |
138 | cl_assert_equal_i(i, 1); | |
55ebd7d3 | 139 | } |