config: ensure we can write to an empty file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
diff --git a/tests/config/write.c b/tests/config/write.c
index bcc8757..1832466 100644
--- a/tests/config/write.c
+++ b/tests/config/write.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "buffer.h"
+#include "fileops.h"
void test_config_write__initialize(void)
{
@@ -393,3 +394,37 @@ void test_config_write__outside_change(void)
git_config_free(cfg);
}
+
+void test_config_write__to_empty_file(void)
+{
+ git_config *cfg;
+ const char *filename = "config-file";
+ git_buf result = GIT_BUF_INIT;
+
+ cl_git_mkfile(filename, "");
+ cl_git_pass(git_config_open_ondisk(&cfg, filename));
+ cl_git_pass(git_config_set_string(cfg, "section.name", "value"));
+ git_config_free(cfg);
+
+ cl_git_pass(git_futils_readbuffer(&result, "config-file"));
+ cl_assert_equal_s("[section]\n\tname = value\n", result.ptr);
+
+ git_buf_free(&result);
+}
+
+void test_config_write__to_file_with_only_comment(void)
+{
+ git_config *cfg;
+ const char *filename = "config-file";
+ git_buf result = GIT_BUF_INIT;
+
+ cl_git_mkfile(filename, "\n\n");
+ cl_git_pass(git_config_open_ondisk(&cfg, filename));
+ cl_git_pass(git_config_set_string(cfg, "section.name", "value"));
+ git_config_free(cfg);
+
+ cl_git_pass(git_futils_readbuffer(&result, "config-file"));
+ cl_assert_equal_s("\n\n[section]\n\tname = value\n", result.ptr);
+
+ git_buf_free(&result);
+}