1 #include "clar_libgit2.h"
5 void test_copy__file(void)
8 const char *content
= "This is some stuff to copy\n";
10 cl_git_mkfile("copy_me", content
);
12 cl_git_pass(git_futils_cp("copy_me", "copy_me_two", 0664));
14 cl_git_pass(git_fs_path_lstat("copy_me_two", &st
));
15 cl_assert(S_ISREG(st
.st_mode
));
17 if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
18 cl_assert_equal_sz(strlen(content
), (size_t)st
.st_size
);
20 cl_git_pass(p_unlink("copy_me_two"));
21 cl_git_pass(p_unlink("copy_me"));
24 void test_copy__file_in_dir(void)
27 const char *content
= "This is some other stuff to copy\n";
29 cl_git_pass(git_futils_mkdir("an_dir/in_a_dir", 0775, GIT_MKDIR_PATH
));
30 cl_git_mkfile("an_dir/in_a_dir/copy_me", content
);
31 cl_assert(git_fs_path_isdir("an_dir"));
33 cl_git_pass(git_futils_mkpath2file
34 ("an_dir/second_dir/and_more/copy_me_two", 0775));
36 cl_git_pass(git_futils_cp
37 ("an_dir/in_a_dir/copy_me",
38 "an_dir/second_dir/and_more/copy_me_two",
41 cl_git_pass(git_fs_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st
));
42 cl_assert(S_ISREG(st
.st_mode
));
44 if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
45 cl_assert_equal_sz(strlen(content
), (size_t)st
.st_size
);
47 cl_git_pass(git_futils_rmdir_r("an_dir", NULL
, GIT_RMDIR_REMOVE_FILES
));
48 cl_assert(!git_fs_path_isdir("an_dir"));
52 static void assert_hard_link(const char *path
)
54 /* we assert this by checking that there's more than one link to the file */
57 cl_assert(git_fs_path_isfile(path
));
58 cl_git_pass(p_stat(path
, &st
));
59 cl_assert(st
.st_nlink
> 1);
63 void test_copy__tree(void)
66 const char *content
= "File content\n";
68 cl_git_pass(git_futils_mkdir("src/b", 0775, GIT_MKDIR_PATH
));
69 cl_git_pass(git_futils_mkdir("src/c/d", 0775, GIT_MKDIR_PATH
));
70 cl_git_pass(git_futils_mkdir("src/c/e", 0775, GIT_MKDIR_PATH
));
72 cl_git_mkfile("src/f1", content
);
73 cl_git_mkfile("src/b/f2", content
);
74 cl_git_mkfile("src/c/f3", content
);
75 cl_git_mkfile("src/c/d/f4", content
);
76 cl_git_mkfile("src/c/d/.f5", content
);
79 cl_assert(p_symlink("../../b/f2", "src/c/d/l1") == 0);
82 cl_assert(git_fs_path_isdir("src"));
83 cl_assert(git_fs_path_isdir("src/b"));
84 cl_assert(git_fs_path_isdir("src/c/d"));
85 cl_assert(git_fs_path_isfile("src/c/d/f4"));
87 /* copy with no empty dirs, yes links, no dotfiles, no overwrite */
90 git_futils_cp_r("src", "t1", GIT_CPDIR_COPY_SYMLINKS
, 0) );
92 cl_assert(git_fs_path_isdir("t1"));
93 cl_assert(git_fs_path_isdir("t1/b"));
94 cl_assert(git_fs_path_isdir("t1/c"));
95 cl_assert(git_fs_path_isdir("t1/c/d"));
96 cl_assert(!git_fs_path_isdir("t1/c/e"));
98 cl_assert(git_fs_path_isfile("t1/f1"));
99 cl_assert(git_fs_path_isfile("t1/b/f2"));
100 cl_assert(git_fs_path_isfile("t1/c/f3"));
101 cl_assert(git_fs_path_isfile("t1/c/d/f4"));
102 cl_assert(!git_fs_path_isfile("t1/c/d/.f5"));
104 memset(&st
, 0, sizeof(struct stat
));
105 cl_git_pass(git_fs_path_lstat("t1/c/f3", &st
));
106 cl_assert(S_ISREG(st
.st_mode
));
108 if (!cl_is_env_set("GITTEST_FLAKY_STAT"))
109 cl_assert_equal_sz(strlen(content
), (size_t)st
.st_size
);
112 memset(&st
, 0, sizeof(struct stat
));
113 cl_git_pass(git_fs_path_lstat("t1/c/d/l1", &st
));
114 cl_assert(S_ISLNK(st
.st_mode
));
117 cl_git_pass(git_futils_rmdir_r("t1", NULL
, GIT_RMDIR_REMOVE_FILES
));
118 cl_assert(!git_fs_path_isdir("t1"));
120 /* copy with empty dirs, no links, yes dotfiles, no overwrite */
123 git_futils_cp_r("src", "t2", GIT_CPDIR_CREATE_EMPTY_DIRS
| GIT_CPDIR_COPY_DOTFILES
, 0) );
125 cl_assert(git_fs_path_isdir("t2"));
126 cl_assert(git_fs_path_isdir("t2/b"));
127 cl_assert(git_fs_path_isdir("t2/c"));
128 cl_assert(git_fs_path_isdir("t2/c/d"));
129 cl_assert(git_fs_path_isdir("t2/c/e"));
131 cl_assert(git_fs_path_isfile("t2/f1"));
132 cl_assert(git_fs_path_isfile("t2/b/f2"));
133 cl_assert(git_fs_path_isfile("t2/c/f3"));
134 cl_assert(git_fs_path_isfile("t2/c/d/f4"));
135 cl_assert(git_fs_path_isfile("t2/c/d/.f5"));
138 memset(&st
, 0, sizeof(struct stat
));
139 cl_git_fail(git_fs_path_lstat("t2/c/d/l1", &st
));
142 cl_git_pass(git_futils_rmdir_r("t2", NULL
, GIT_RMDIR_REMOVE_FILES
));
143 cl_assert(!git_fs_path_isdir("t2"));
146 cl_git_pass(git_futils_cp_r("src", "t3", GIT_CPDIR_CREATE_EMPTY_DIRS
| GIT_CPDIR_LINK_FILES
, 0));
147 cl_assert(git_fs_path_isdir("t3"));
149 cl_assert(git_fs_path_isdir("t3"));
150 cl_assert(git_fs_path_isdir("t3/b"));
151 cl_assert(git_fs_path_isdir("t3/c"));
152 cl_assert(git_fs_path_isdir("t3/c/d"));
153 cl_assert(git_fs_path_isdir("t3/c/e"));
155 assert_hard_link("t3/f1");
156 assert_hard_link("t3/b/f2");
157 assert_hard_link("t3/c/f3");
158 assert_hard_link("t3/c/d/f4");
161 cl_git_pass(git_futils_rmdir_r("src", NULL
, GIT_RMDIR_REMOVE_FILES
));