]> git.proxmox.com Git - libgit2.git/blob - tests/core/futils.c
2c8294c876fc9b9741ed5cd3d1076e7fc9f31960
[libgit2.git] / tests / core / futils.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3
4 /* Fixture setup and teardown */
5 void test_core_futils__initialize(void)
6 {
7 cl_must_pass(p_mkdir("futils", 0777));
8 }
9
10 void test_core_futils__cleanup(void)
11 {
12 cl_fixture_cleanup("futils");
13 }
14
15 void test_core_futils__writebuffer(void)
16 {
17 git_buf out = GIT_BUF_INIT,
18 append = GIT_BUF_INIT;
19
20 /* create a new file */
21 git_buf_puts(&out, "hello!\n");
22 git_buf_printf(&out, "this is a %s\n", "test");
23
24 cl_git_pass(git_futils_writebuffer(&out, "futils/test-file", O_RDWR|O_CREAT, 0666));
25
26 cl_assert_equal_file(out.ptr, out.size, "futils/test-file");
27
28 /* append some more data */
29 git_buf_puts(&append, "And some more!\n");
30 git_buf_put(&out, append.ptr, append.size);
31
32 cl_git_pass(git_futils_writebuffer(&append, "futils/test-file", O_RDWR|O_APPEND, 0666));
33
34 cl_assert_equal_file(out.ptr, out.size, "futils/test-file");
35
36 git_buf_dispose(&out);
37 git_buf_dispose(&append);
38 }
39
40 void test_core_futils__write_hidden_file(void)
41 {
42 #ifndef GIT_WIN32
43 cl_skip();
44 #else
45 git_buf out = GIT_BUF_INIT, append = GIT_BUF_INIT;
46 bool hidden;
47
48 git_buf_puts(&out, "hidden file.\n");
49 git_futils_writebuffer(&out, "futils/test-file", O_RDWR | O_CREAT, 0666);
50
51 cl_git_pass(git_win32__set_hidden("futils/test-file", true));
52
53 /* append some more data */
54 git_buf_puts(&append, "And some more!\n");
55 git_buf_put(&out, append.ptr, append.size);
56
57 cl_git_pass(git_futils_writebuffer(&append, "futils/test-file", O_RDWR | O_APPEND, 0666));
58
59 cl_assert_equal_file(out.ptr, out.size, "futils/test-file");
60
61 cl_git_pass(git_win32__hidden(&hidden, "futils/test-file"));
62 cl_assert(hidden);
63
64 git_buf_dispose(&out);
65 git_buf_dispose(&append);
66 #endif
67 }
68
69 void test_core_futils__recursive_rmdir_keeps_symlink_targets(void)
70 {
71 if (!git_path_supports_symlinks(clar_sandbox_path()))
72 cl_skip();
73
74 cl_git_pass(git_futils_mkdir_r("a/b", 0777));
75 cl_git_pass(git_futils_mkdir_r("dir-target", 0777));
76 cl_git_mkfile("dir-target/file", "Contents");
77 cl_git_mkfile("file-target", "Contents");
78 cl_must_pass(p_symlink("dir-target", "a/symlink"));
79 cl_must_pass(p_symlink("file-target", "a/b/symlink"));
80
81 cl_git_pass(git_futils_rmdir_r("a", NULL, GIT_RMDIR_REMOVE_FILES));
82
83 cl_assert(git_path_exists("dir-target"));
84 cl_assert(git_path_exists("file-target"));
85
86 cl_must_pass(p_unlink("dir-target/file"));
87 cl_must_pass(p_rmdir("dir-target"));
88 cl_must_pass(p_unlink("file-target"));
89 }