Commit 835211dc4b6afef650222ca2e24545938e28e0cb

Patrick Steinhardt 2019-08-01T13:23:16

tests: config: assert behaviour around includes Add a few tests that verify some behaviour centered around includes. The first set of tests verifies that we correctly override values depending on the order of includes and other keys, the second set asserts that we can correctly snapshot configuration files with includes.

diff --git a/tests/config/include.c b/tests/config/include.c
index 48261dd..e2b0fc9 100644
--- a/tests/config/include.c
+++ b/tests/config/include.c
@@ -202,3 +202,33 @@ void test_config_include__included_variables_cannot_be_modified(void)
 	cl_git_pass(p_unlink("top-level"));
 	cl_git_pass(p_unlink("included"));
 }
+
+void test_config_include__variables_in_included_override_including(void)
+{
+	int i;
+
+	cl_git_mkfile("top-level", "[foo]\nbar = 1\n[include]\npath = included");
+	cl_git_mkfile("included", "[foo]\nbar = 2");
+
+	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
+	cl_git_pass(git_config_get_int32(&i, cfg, "foo.bar"));
+	cl_assert_equal_i(i, 2);
+
+	cl_git_pass(p_unlink("top-level"));
+	cl_git_pass(p_unlink("included"));
+}
+
+void test_config_include__variables_in_including_override_included(void)
+{
+	int i;
+
+	cl_git_mkfile("top-level", "[include]\npath = included\n[foo]\nbar = 1");
+	cl_git_mkfile("included", "[foo]\nbar = 2");
+
+	cl_git_pass(git_config_open_ondisk(&cfg, "top-level"));
+	cl_git_pass(git_config_get_int32(&i, cfg, "foo.bar"));
+	cl_assert_equal_i(i, 1);
+
+	cl_git_pass(p_unlink("top-level"));
+	cl_git_pass(p_unlink("included"));
+}
diff --git a/tests/config/snapshot.c b/tests/config/snapshot.c
index 2e0679e..61562d2 100644
--- a/tests/config/snapshot.c
+++ b/tests/config/snapshot.c
@@ -74,3 +74,29 @@ void test_config_snapshot__multivar(void)
 
 	cl_git_pass(p_unlink("config"));
 }
+
+void test_config_snapshot__includes(void)
+{
+	int i;
+
+	cl_git_mkfile("including", "[include]\npath = included");
+	cl_git_mkfile("included", "[section]\nkey = 1\n");
+
+	cl_git_pass(git_config_open_ondisk(&cfg, "including"));
+	cl_git_pass(git_config_snapshot(&snapshot, cfg));
+
+	cl_git_pass(git_config_get_int32(&i, snapshot, "section.key"));
+	cl_assert_equal_i(i, 1);
+
+	/* Rewrite "included" config */
+	cl_git_mkfile("included", "[section]\nkey = 11\n");
+
+	/* Assert that the live config changed, but snapshot remained the same */
+	cl_git_pass(git_config_get_int32(&i, cfg, "section.key"));
+	cl_assert_equal_i(i, 11);
+	cl_git_pass(git_config_get_int32(&i, snapshot, "section.key"));
+	cl_assert_equal_i(i, 1);
+
+	cl_git_pass(p_unlink("including"));
+	cl_git_pass(p_unlink("included"));
+}