Commit 304e58c00487466aa5bd129b1eeb5169d75005e7

Patrick Steinhardt 2019-08-01T13:20:17

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.

diff --git a/tests/config/snapshot.c b/tests/config/snapshot.c
index 3ea07c1..2e0679e 100644
--- a/tests/config/snapshot.c
+++ b/tests/config/snapshot.c
@@ -1,45 +1,49 @@
 #include "clar_libgit2.h"
 
-void test_config_snapshot__create_snapshot(void)
-{
-	int32_t tmp;
-	git_config *cfg, *snapshot, *new_snapshot;
-	const char *filename = "config-ext-change";
+static git_config *cfg;
+static git_config *snapshot;
 
-	cl_git_mkfile(filename, "[old]\nvalue = 5\n");
+void test_config_snapshot__cleanup(void)
+{
+	git_config_free(cfg);
+	cfg = NULL;
+	git_config_free(snapshot);
+	snapshot = NULL;
+}
 
-	cl_git_pass(git_config_open_ondisk(&cfg, filename));
+void test_config_snapshot__create_snapshot(void)
+{
+	int32_t i;
 
-	cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value"));
-	cl_assert_equal_i(5, tmp);
+	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);
 
 	cl_git_pass(git_config_snapshot(&snapshot, cfg));
 
 	/* Change the value on the file itself (simulate external process) */
-	cl_git_mkfile(filename, "[old]\nvalue = 56\n");
-
-	cl_git_pass(git_config_get_int32(&tmp, cfg, "old.value"));
-	cl_assert_equal_i(56, tmp);
+	cl_git_mkfile("config", "[old]\nvalue = 56\n");
 
-	cl_git_pass(git_config_get_int32(&tmp, snapshot, "old.value"));
-	cl_assert_equal_i(5, tmp);
+	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);
 
 	/* Change the value on the file itself (simulate external process) */
-	cl_git_mkfile(filename, "[old]\nvalue = 999\n");
+	cl_git_mkfile("config", "[old]\nvalue = 999\n");
 
-	cl_git_pass(git_config_snapshot(&new_snapshot, cfg));
+	/* 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);
 
 	/* New snapshot should see new value */
-	cl_git_pass(git_config_get_int32(&tmp, new_snapshot, "old.value"));
-	cl_assert_equal_i(999, tmp);
-
-	/* Old snapshot should still have the old value */
-	cl_git_pass(git_config_get_int32(&tmp, snapshot, "old.value"));
-	cl_assert_equal_i(5, tmp);
-	
-	git_config_free(new_snapshot);
 	git_config_free(snapshot);
-	git_config_free(cfg);
+	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"));
 }
 
 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)
 
 void test_config_snapshot__multivar(void)
 {
-	int count = 0;
-	git_config *cfg, *snapshot;
-	const char *filename = "config-file";
+	int count;
 
-	cl_git_mkfile(filename, "[old]\nvalue = 5\nvalue = 6\n");
-
-	cl_git_pass(git_config_open_ondisk(&cfg, filename));
+	count = 0;
+	cl_git_mkfile("config", "[old]\nvalue = 5\nvalue = 6\n");
+	cl_git_pass(git_config_open_ondisk(&cfg, "config"));
 	cl_git_pass(git_config_get_multivar_foreach(cfg, "old.value", NULL, count_me, &count));
-
 	cl_assert_equal_i(2, count);
 
-	cl_git_pass(git_config_snapshot(&snapshot, cfg));
-	git_config_free(cfg);
-
 	count = 0;
+	cl_git_pass(git_config_snapshot(&snapshot, cfg));
 	cl_git_pass(git_config_get_multivar_foreach(snapshot, "old.value", NULL, count_me, &count));
-
 	cl_assert_equal_i(2, count);
 
-	git_config_free(snapshot);
+	cl_git_pass(p_unlink("config"));
 }