Commit a060cccc0a51ee7eff92b674039bc7fc91dc5d46

Ryan Roden-Corrent 2015-04-16T10:53:22

Unittest to validate config entry deletion bug. Add a unittest to validate bug #3043, where a duplicate empty config header could cause deletion of a config entry to fail silently. The bug is currently unresolved and this test will fail.

diff --git a/tests/config/write.c b/tests/config/write.c
index 32e6f27..6318c49 100644
--- a/tests/config/write.c
+++ b/tests/config/write.c
@@ -6,6 +6,7 @@ void test_config_write__initialize(void)
 	cl_fixture_sandbox("config/config9");
 	cl_fixture_sandbox("config/config15");
 	cl_fixture_sandbox("config/config17");
+	cl_fixture_sandbox("config/config21");
 }
 
 void test_config_write__cleanup(void)
@@ -13,6 +14,7 @@ void test_config_write__cleanup(void)
 	cl_fixture_cleanup("config9");
 	cl_fixture_cleanup("config15");
 	cl_fixture_cleanup("config17");
+	cl_fixture_cleanup("config21");
 }
 
 void test_config_write__replace_value(void)
@@ -106,6 +108,35 @@ void test_config_write__delete_value_at_specific_level(void)
 	git_config_free(cfg);
 }
 
+/*
+ * This test exposes a bug where duplicate empty section headers could prevent
+ * deletion of config entries.
+ */
+void test_config_write__delete_value_with_duplicate_header(void)
+{
+	const char *file_name  = "config21";
+	const char *entry_name = "remote.origin.url";
+	git_config *cfg;
+	git_config_entry *entry;
+
+	/* Make sure the expected entry exists */
+	cl_git_pass(git_config_open_ondisk(&cfg, file_name));
+	cl_git_pass(git_config_get_entry(&entry, cfg, entry_name));
+
+	/* Delete that entry */
+	cl_git_pass(git_config_delete_entry(cfg, entry_name));
+
+	/* Reopen the file and make sure the entry no longer exists */
+	git_config_entry_free(entry);
+	git_config_free(cfg);
+	cl_git_pass(git_config_open_ondisk(&cfg, file_name));
+	cl_git_fail(git_config_get_entry(&entry, cfg, entry_name));
+
+	/* Cleanup */
+	git_config_entry_free(entry);
+	git_config_free(cfg);
+}
+
 void test_config_write__write_subsection(void)
 {
 	git_config *cfg;
diff --git a/tests/resources/config/config21 b/tests/resources/config/config21
new file mode 100644
index 0000000..a63b52f
--- /dev/null
+++ b/tests/resources/config/config21
@@ -0,0 +1,6 @@
+# This configuration can occur after removing and re-adding the origin remote
+[remote "origin"]
+[branch "master"]
+  remote = "origin"
+[remote "origin"]
+  url = "foo"