]> git.proxmox.com Git - libgit2.git/blame - tests/core/futils.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / futils.c
CommitLineData
8e736a73 1#include "clar_libgit2.h"
22a2d3d5 2#include "futils.h"
8e736a73 3
ac3d33df 4/* Fixture setup and teardown */
8e736a73
ET
5void test_core_futils__initialize(void)
6{
7 cl_must_pass(p_mkdir("futils", 0777));
8}
9
10void test_core_futils__cleanup(void)
11{
12 cl_fixture_cleanup("futils");
13}
14
15void test_core_futils__writebuffer(void)
16{
e579e0f7
MB
17 git_str out = GIT_STR_INIT,
18 append = GIT_STR_INIT;
8e736a73
ET
19
20 /* create a new file */
e579e0f7
MB
21 git_str_puts(&out, "hello!\n");
22 git_str_printf(&out, "this is a %s\n", "test");
8e736a73
ET
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 */
e579e0f7
MB
29 git_str_puts(&append, "And some more!\n");
30 git_str_put(&out, append.ptr, append.size);
8e736a73
ET
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
e579e0f7
MB
36 git_str_dispose(&out);
37 git_str_dispose(&append);
8e736a73
ET
38}
39
40void test_core_futils__write_hidden_file(void)
41{
42#ifndef GIT_WIN32
43 cl_skip();
44#else
e579e0f7 45 git_str out = GIT_STR_INIT, append = GIT_STR_INIT;
8e736a73
ET
46 bool hidden;
47
e579e0f7 48 git_str_puts(&out, "hidden file.\n");
8e736a73
ET
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 */
e579e0f7
MB
54 git_str_puts(&append, "And some more!\n");
55 git_str_put(&out, append.ptr, append.size);
8e736a73
ET
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
e579e0f7
MB
64 git_str_dispose(&out);
65 git_str_dispose(&append);
8e736a73
ET
66#endif
67}
68
22a2d3d5
UG
69void test_core_futils__recursive_rmdir_keeps_symlink_targets(void)
70{
e579e0f7 71 if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
22a2d3d5
UG
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
e579e0f7
MB
83 cl_assert(git_fs_path_exists("dir-target"));
84 cl_assert(git_fs_path_exists("file-target"));
22a2d3d5
UG
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}
e579e0f7
MB
90
91void test_core_futils__mktmp_umask(void)
92{
93#ifdef GIT_WIN32
94 cl_skip();
95#else
96 git_str path = GIT_STR_INIT;
97 struct stat st;
98 int fd;
99
100 umask(0);
101 cl_assert((fd = git_futils_mktmp(&path, "foo", 0777)) >= 0);
102 cl_must_pass(p_fstat(fd, &st));
103 cl_assert_equal_i(st.st_mode & 0777, 0777);
104 cl_must_pass(p_unlink(path.ptr));
105 close(fd);
106
107 umask(077);
108 cl_assert((fd = git_futils_mktmp(&path, "foo", 0777)) >= 0);
109 cl_must_pass(p_fstat(fd, &st));
110 cl_assert_equal_i(st.st_mode & 0777, 0700);
111 cl_must_pass(p_unlink(path.ptr));
112 close(fd);
113 git_str_dispose(&path);
114#endif
115}