Skip to content

Commit 304e58c

Browse files
committed
tests: config::snapshot: modernize tests
Modernize the tests in config::snapshot to make them easier to understand. Most important, include a cleanup function that frees config and snapshot and unlink config files at the end of each test.
1 parent 8f7fd98 commit 304e58c

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

tests/config/snapshot.c

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,49 @@
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");
20-
21-
cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value"));
22-
cl_assert_equal_i(56, tmp);
26+
cl_git_mkfile("config", "[old]\nvalue = 56\n");
2327

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

4549
static int count_me(const git_config_entry *entry, void *payload)
@@ -55,24 +59,18 @@ static int count_me(const git_config_entry *entry, void *payload)
5559

5660
void test_config_snapshot__multivar(void)
5761
{
58-
int count = 0;
59-
git_config *cfg, *snapshot;
60-
const char *filename = "config-file";
62+
int count;
6163

62-
cl_git_mkfile(filename, "[old]\nvalue = 5\nvalue = 6\n");
63-
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));
66-
6768
cl_assert_equal_i(2, count);
6869

69-
cl_git_pass(git_config_snapshot(&snapshot, cfg));
70-
git_config_free(cfg);
71-
7270
count = 0;
71+
cl_git_pass(git_config_snapshot(&snapshot, cfg));
7372
cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count));
74-
7573
cl_assert_equal_i(2, count);
7674

77-
git_config_free(snapshot);
75+
cl_git_pass(p_unlink("config"));
7876
}

0 commit comments

Comments
 (0)