futils.c 2.9 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3

4
/* Fixture setup and teardown */
5 6 7 8 9 10 11 12 13 14 15 16
void test_core_futils__initialize(void)
{
	cl_must_pass(p_mkdir("futils", 0777));
}

void test_core_futils__cleanup(void)
{
	cl_fixture_cleanup("futils");
}

void test_core_futils__writebuffer(void)
{
17 18
	git_str out = GIT_STR_INIT,
		append = GIT_STR_INIT;
19 20

	/* create a new file */
21 22
	git_str_puts(&out, "hello!\n");
	git_str_printf(&out, "this is a %s\n", "test");
23 24 25 26 27 28

	cl_git_pass(git_futils_writebuffer(&out, "futils/test-file", O_RDWR|O_CREAT, 0666));

	cl_assert_equal_file(out.ptr, out.size, "futils/test-file");

	/* append some more data */
29 30
	git_str_puts(&append, "And some more!\n");
	git_str_put(&out, append.ptr, append.size);
31 32 33 34 35

	cl_git_pass(git_futils_writebuffer(&append, "futils/test-file", O_RDWR|O_APPEND, 0666));

	cl_assert_equal_file(out.ptr, out.size, "futils/test-file");

36 37
	git_str_dispose(&out);
	git_str_dispose(&append);
38 39 40 41 42 43 44
}

void test_core_futils__write_hidden_file(void)
{
#ifndef GIT_WIN32
	cl_skip();
#else
45
	git_str out = GIT_STR_INIT, append = GIT_STR_INIT;
46 47
	bool hidden;

48
	git_str_puts(&out, "hidden file.\n");
49 50 51 52 53
	git_futils_writebuffer(&out, "futils/test-file", O_RDWR | O_CREAT, 0666);

	cl_git_pass(git_win32__set_hidden("futils/test-file", true));

	/* append some more data */
54 55
	git_str_puts(&append, "And some more!\n");
	git_str_put(&out, append.ptr, append.size);
56 57 58 59 60 61 62 63

	cl_git_pass(git_futils_writebuffer(&append, "futils/test-file", O_RDWR | O_APPEND, 0666));

	cl_assert_equal_file(out.ptr, out.size, "futils/test-file");

	cl_git_pass(git_win32__hidden(&hidden, "futils/test-file"));
	cl_assert(hidden);

64 65
	git_str_dispose(&out);
	git_str_dispose(&append);
66 67 68
#endif
}

69 70
void test_core_futils__recursive_rmdir_keeps_symlink_targets(void)
{
71
	if (!git_fs_path_supports_symlinks(clar_sandbox_path()))
72 73 74 75 76 77 78 79 80 81 82
		cl_skip();

	cl_git_pass(git_futils_mkdir_r("a/b", 0777));
	cl_git_pass(git_futils_mkdir_r("dir-target", 0777));
	cl_git_mkfile("dir-target/file", "Contents");
	cl_git_mkfile("file-target", "Contents");
	cl_must_pass(p_symlink("dir-target", "a/symlink"));
	cl_must_pass(p_symlink("file-target", "a/b/symlink"));

	cl_git_pass(git_futils_rmdir_r("a", NULL, GIT_RMDIR_REMOVE_FILES));

83 84
	cl_assert(git_fs_path_exists("dir-target"));
	cl_assert(git_fs_path_exists("file-target"));
85 86 87 88 89

	cl_must_pass(p_unlink("dir-target/file"));
	cl_must_pass(p_rmdir("dir-target"));
	cl_must_pass(p_unlink("file-target"));
}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

void test_core_futils__mktmp_umask(void)
{
#ifdef GIT_WIN32
	cl_skip();
#else
	git_str path = GIT_STR_INIT;
	struct stat st;
	int fd;

	umask(0);
	cl_assert((fd = git_futils_mktmp(&path, "foo", 0777)) >= 0);
	cl_must_pass(p_fstat(fd, &st));
	cl_assert_equal_i(st.st_mode & 0777, 0777);
	cl_must_pass(p_unlink(path.ptr));
	close(fd);

	umask(077);
	cl_assert((fd = git_futils_mktmp(&path, "foo", 0777)) >= 0);
	cl_must_pass(p_fstat(fd, &st));
	cl_assert_equal_i(st.st_mode & 0777, 0700);
	cl_must_pass(p_unlink(path.ptr));
	close(fd);
	git_str_dispose(&path);
#endif
}