11#include "clar_libgit2.h"
22
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" ;
3+ static git_config * cfg ;
4+ static git_config * snapshot ;
85
9- cl_git_mkfile (filename , "[old]\nvalue = 5\n" );
6+ void test_config_snapshot__cleanup (void )
7+ {
8+ git_config_free (cfg );
9+ cfg = NULL ;
10+ git_config_free (snapshot );
11+ snapshot = NULL ;
12+ }
1013
11- cl_git_pass (git_config_open_ondisk (& cfg , filename ));
14+ void test_config_snapshot__create_snapshot (void )
15+ {
16+ int32_t i ;
1217
13- cl_git_pass (git_config_get_int32 (& tmp , cfg , "old.value" ));
14- cl_assert_equal_i (5 , tmp );
18+ cl_git_mkfile ("config" , "[old]\nvalue = 5\n" );
19+ cl_git_pass (git_config_open_ondisk (& cfg , "config" ));
20+ cl_git_pass (git_config_get_int32 (& i , cfg , "old.value" ));
21+ cl_assert_equal_i (5 , i );
1522
1623 cl_git_pass (git_config_snapshot (& snapshot , cfg ));
1724
1825 /* Change the value on the file itself (simulate external process) */
19- cl_git_mkfile (filename , "[old]\nvalue = 56\n" );
26+ cl_git_mkfile ("config" , "[old]\nvalue = 56\n" );
2027
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 );
28+ cl_git_pass (git_config_get_int32 (& i , cfg , "old.value" ));
29+ cl_assert_equal_i (56 , i );
30+ cl_git_pass (git_config_get_int32 (& i , snapshot , "old.value" ));
31+ cl_assert_equal_i (5 , i );
2632
2733 /* Change the value on the file itself (simulate external process) */
28- cl_git_mkfile (filename , "[old]\nvalue = 999\n" );
34+ cl_git_mkfile ("config" , "[old]\nvalue = 999\n" );
2935
30- cl_git_pass (git_config_snapshot (& new_snapshot , cfg ));
36+ /* Old snapshot should still have the old value */
37+ cl_git_pass (git_config_get_int32 (& i , snapshot , "old.value" ));
38+ cl_assert_equal_i (5 , i );
3139
3240 /* 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 );
4141 git_config_free (snapshot );
42- git_config_free (cfg );
42+ cl_git_pass (git_config_snapshot (& snapshot , cfg ));
43+ cl_git_pass (git_config_get_int32 (& i , snapshot , "old.value" ));
44+ cl_assert_equal_i (999 , i );
45+
46+ cl_git_pass (p_unlink ("config" ));
4347}
4448
4549static int count_me (const git_config_entry * entry , void * payload )
@@ -55,24 +59,44 @@ static int count_me(const git_config_entry *entry, void *payload)
5559
5660void test_config_snapshot__multivar (void )
5761{
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" );
62+ int count ;
6363
64- cl_git_pass (git_config_open_ondisk (& cfg , filename ));
64+ count = 0 ;
65+ cl_git_mkfile ("config" , "[old]\nvalue = 5\nvalue = 6\n" );
66+ cl_git_pass (git_config_open_ondisk (& cfg , "config" ));
6567 cl_git_pass (git_config_get_multivar_foreach (cfg , "old.value" , NULL , count_me , & count ));
68+ cl_assert_equal_i (2 , count );
6669
70+ count = 0 ;
71+ cl_git_pass (git_config_snapshot (& snapshot , cfg ));
72+ cl_git_pass (git_config_get_multivar_foreach (snapshot , "old.value" , NULL , count_me , & count ));
6773 cl_assert_equal_i (2 , count );
6874
75+ cl_git_pass (p_unlink ("config" ));
76+ }
77+
78+ void test_config_snapshot__includes (void )
79+ {
80+ int i ;
81+
82+ cl_git_mkfile ("including" , "[include]\npath = included" );
83+ cl_git_mkfile ("included" , "[section]\nkey = 1\n" );
84+
85+ cl_git_pass (git_config_open_ondisk (& cfg , "including" ));
6986 cl_git_pass (git_config_snapshot (& snapshot , cfg ));
70- git_config_free (cfg );
7187
72- count = 0 ;
73- cl_git_pass ( git_config_get_multivar_foreach ( snapshot , "old.value" , NULL , count_me , & count ) );
88+ cl_git_pass ( git_config_get_int32 ( & i , snapshot , "section.key" )) ;
89+ cl_assert_equal_i ( i , 1 );
7490
75- cl_assert_equal_i (2 , count );
91+ /* Rewrite "included" config */
92+ cl_git_mkfile ("included" , "[section]\nkey = 11\n" );
7693
77- git_config_free (snapshot );
94+ /* Assert that the live config changed, but snapshot remained the same */
95+ cl_git_pass (git_config_get_int32 (& i , cfg , "section.key" ));
96+ cl_assert_equal_i (i , 11 );
97+ cl_git_pass (git_config_get_int32 (& i , snapshot , "section.key" ));
98+ cl_assert_equal_i (i , 1 );
99+
100+ cl_git_pass (p_unlink ("including" ));
101+ cl_git_pass (p_unlink ("included" ));
78102}
0 commit comments