Commit 0c2d0d4b90401088c5c67eea2c4c80588c854ff7

Patrick Steinhardt 2019-06-14T14:07:26

tests: object: refactor largefile test to not use `p_fallocate` The `p_fallocate` platform is currently in use in our tests, only, but it proved to be quite burdensome to get it implemented in a cross-platform way. The only "real" user is the test object::tree::read::largefile, where it's used to allocate a large file in the filesystem only to commit it to the repo and read its object back again. We can simplify this quite a bit by just using an in-memory buffer of 4GB. Sure, this cannot be used on platforms with low resources. But creating 4GB files is not any better, and we already skip the test if the environment variable "GITTEST_INVASIVE_FS_SIZE" is not set. So we're arguably not worse off than before.

diff --git a/tests/object/tree/read.c b/tests/object/tree/read.c
index 1ba058f..a20ec38 100644
--- a/tests/object/tree/read.c
+++ b/tests/object/tree/read.c
@@ -79,46 +79,36 @@ void test_object_tree_read__two(void)
 
 void test_object_tree_read__largefile(void)
 {
-	git_reference *ref;
+	const git_tree_entry *entry;
+	git_index_entry ie;
 	git_commit *commit;
+	git_object *object;
+	git_index *index;
 	git_tree *tree;
 	git_oid oid;
-	const git_tree_entry *entry;
-	git_object *object;
-	git_buf file = GIT_BUF_INIT;
-	int fd;
-	git_index *idx;
+	char *buf;
 
 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE"))
 		cl_skip();
 
-	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
-	cl_git_pass(git_repository_index(&idx, g_repo));
+	cl_assert(buf = git__calloc(1, BIGFILE_SIZE));
 
-	cl_git_pass(git_buf_puts(&file, git_repository_workdir(g_repo)));
-	cl_git_pass(git_buf_joinpath(&file, file.ptr, BIGFILE));
+	memset(&ie, 0, sizeof(ie));
+	ie.mode = GIT_FILEMODE_BLOB;
+	ie.path = BIGFILE;
 
-	fd = p_open(git_buf_cstr(&file), O_CREAT|O_RDWR, 0644);
-	cl_assert_(fd >= 0, "invalid file descriptor");
-
-	cl_must_pass(p_fallocate(fd, 0, BIGFILE_SIZE));
-	cl_must_pass(p_close(fd));
-
-	cl_git_pass(git_index_add_bypath(idx, BIGFILE));
-	cl_repo_commit_from_index(&oid, g_repo, NULL, 0, "bigfile");
+	cl_git_pass(git_repository_index(&index, g_repo));
+	cl_git_pass(git_index_add_frombuffer(index, &ie, buf, BIGFILE_SIZE));
+	cl_repo_commit_from_index(&oid, g_repo, NULL, 0, BIGFILE);
 
 	cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
 	cl_git_pass(git_commit_tree(&tree, commit));
-
-	entry = git_tree_entry_byname(tree, BIGFILE);
-	cl_assert_(entry, "entry was NULL");
-
+	cl_assert(entry = git_tree_entry_byname(tree, BIGFILE));
 	cl_git_pass(git_tree_entry_to_object(&object, g_repo, entry));
 
-	git_buf_dispose(&file);
 	git_object_free(object);
 	git_tree_free(tree);
-	git_index_free(idx);
+	git_index_free(index);
 	git_commit_free(commit);
-	git_reference_free(ref);
+	git__free(buf);
 }