Commit 13f36ffb9e1c4fb70b44a477d716873fecfc0407

Russell Belfer 2013-09-13T16:30:21

Add clar helpers for testing file equality These are a couple of new clar helpers for testing that a file has expected contents that I extracted from the checkout code. Actually wrote this as part of an abandoned earlier attempt at a new filters API, but it will be useful now for some of the tests I'm going to write.

diff --git a/src/fileops.c b/src/fileops.c
index 3b271e6..bd845e9 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -56,18 +56,8 @@ int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode
 
 int git_futils_creat_locked(const char *path, const mode_t mode)
 {
-	int fd;
-
-#ifdef GIT_WIN32
-	git_win32_path buf;
-
-	git_win32_path_from_c(buf, path);
-	fd = _wopen(buf, O_WRONLY | O_CREAT | O_TRUNC |
+	int fd = p_open(path, O_WRONLY | O_CREAT | O_TRUNC |
 		O_EXCL | O_BINARY | O_CLOEXEC, mode);
-#else
-	fd = open(path, O_WRONLY | O_CREAT | O_TRUNC |
-		O_EXCL | O_BINARY | O_CLOEXEC, mode);
-#endif
 
 	if (fd < 0) {
 		giterr_set(GITERR_OS, "Failed to create locked file '%s'", path);
diff --git a/tests-clar/checkout/checkout_helpers.c b/tests-clar/checkout/checkout_helpers.c
index f55f7b6..06b4e06 100644
--- a/tests-clar/checkout/checkout_helpers.c
+++ b/tests-clar/checkout/checkout_helpers.c
@@ -3,22 +3,6 @@
 #include "refs.h"
 #include "fileops.h"
 
-/* this is essentially the code from git__unescape modified slightly */
-void strip_cr_from_buf(git_buf *buf)
-{
-	char *scan, *pos = buf->ptr, *end = pos + buf->size;
-
-	for (scan = pos; scan < end; pos++, scan++) {
-		if (*scan == '\r')
-			scan++; /* skip '\r' */
-		if (pos != scan)
-			*pos = *scan;
-	}
-
-	*pos = '\0';
-	buf->size = (pos - buf->ptr);
-}
-
 void assert_on_branch(git_repository *repo, const char *branch)
 {
 	git_reference *head;
@@ -50,48 +34,6 @@ void reset_index_to_treeish(git_object *treeish)
 	git_index_free(index);
 }
 
-static void check_file_contents_internal(
-	const char *path,
-	const char *expected_content,
-	bool strip_cr,
-	const char *file,
-	int line,
-	const char *msg)
-{
-	int fd;
-	char data[1024] = {0};
-	git_buf buf = GIT_BUF_INIT;
-	size_t expected_len = expected_content ? strlen(expected_content) : 0;
-
-	fd = p_open(path, O_RDONLY);
-	cl_assert(fd >= 0);
-
-	buf.ptr = data;
-	buf.size = p_read(fd, buf.ptr, sizeof(data));
-
-	cl_git_pass(p_close(fd));
-
-	if (strip_cr)
-		strip_cr_from_buf(&buf);
-
-	clar__assert_equal(file, line, "strlen(expected_content) != strlen(actual_content)", 1, PRIuZ, expected_len, (size_t)buf.size);
-	clar__assert_equal(file, line, msg, 1, "%s", expected_content, buf.ptr);
-}
-
-void check_file_contents_at_line(
-	const char *path, const char *expected,
-	const char *file, int line, const char *msg)
-{
-	check_file_contents_internal(path, expected, false, file, line, msg);
-}
-
-void check_file_contents_nocr_at_line(
-	const char *path, const char *expected,
-	const char *file, int line, const char *msg)
-{
-	check_file_contents_internal(path, expected, true, file, line, msg);
-}
-
 int checkout_count_callback(
 	git_checkout_notify_t why,
 	const char *path,
diff --git a/tests-clar/checkout/checkout_helpers.h b/tests-clar/checkout/checkout_helpers.h
index 0e8da31..705ee90 100644
--- a/tests-clar/checkout/checkout_helpers.h
+++ b/tests-clar/checkout/checkout_helpers.h
@@ -2,23 +2,14 @@
 #include "git2/object.h"
 #include "git2/repository.h"
 
-extern void strip_cr_from_buf(git_buf *buf);
 extern void assert_on_branch(git_repository *repo, const char *branch);
 extern void reset_index_to_treeish(git_object *treeish);
 
-extern void check_file_contents_at_line(
-	const char *path, const char *expected,
-	const char *file, int line, const char *msg);
-
-extern void check_file_contents_nocr_at_line(
-	const char *path, const char *expected,
-	const char *file, int line, const char *msg);
-
 #define check_file_contents(PATH,EXP) \
-	check_file_contents_at_line(PATH,EXP,__FILE__,__LINE__,"String mismatch: " #EXP " != " #PATH)
+	cl_assert_equal_file(EXP,0,PATH)
 
 #define check_file_contents_nocr(PATH,EXP) \
-	check_file_contents_nocr_at_line(PATH,EXP,__FILE__,__LINE__,"String mismatch: " #EXP " != " #PATH)
+	cl_assert_equal_file_ignore_cr(EXP,0,PATH)
 
 typedef struct {
 	int n_conflicts;
diff --git a/tests-clar/clar_libgit2.c b/tests-clar/clar_libgit2.c
index 340943c..522f736 100644
--- a/tests-clar/clar_libgit2.c
+++ b/tests-clar/clar_libgit2.c
@@ -354,3 +354,58 @@ int cl_repo_get_bool(git_repository *repo, const char *cfg)
 	git_config_free(config);
 	return val;
 }
+
+/* this is essentially the code from git__unescape modified slightly */
+static size_t strip_cr_from_buf(char *start, size_t len)
+{
+	char *scan, *trail, *end = start + len;
+
+	for (scan = trail = start; scan < end; trail++, scan++) {
+		while (*scan == '\r')
+			scan++; /* skip '\r' */
+
+		if (trail != scan)
+			*trail = *scan;
+	}
+
+	*trail = '\0';
+
+	return (trail - start);
+}
+
+void clar__assert_equal_file(
+	const char *expected_data,
+	size_t expected_bytes,
+	int ignore_cr,
+	const char *path,
+	const char *file,
+	size_t line)
+{
+	char buf[4000];
+	ssize_t bytes, total_bytes = 0;
+	int fd = p_open(path, O_RDONLY | O_BINARY);
+	cl_assert(fd >= 0);
+
+	if (expected_data && !expected_bytes)
+		expected_bytes = strlen(expected_data);
+
+	while ((bytes = p_read(fd, buf, sizeof(buf))) != 0) {
+		clar__assert(
+			bytes > 0, file, line, "error reading from file", path, 1);
+
+		if (ignore_cr)
+			bytes = strip_cr_from_buf(buf, bytes);
+
+		clar__assert(memcmp(expected_data, buf, bytes) == 0,
+			file, line, "file content mismatch", path, 1);
+
+		expected_data += bytes;
+		total_bytes   += bytes;
+	}
+
+	p_close(fd);
+
+	clar__assert(!bytes, file, line, "error reading from file", path, 1);
+	clar__assert_equal(file, line, "mismatched file length", 1, "%"PRIuZ,
+		(size_t)expected_bytes, (size_t)total_bytes);
+}
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index 8dcfdee..76299e4 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -46,6 +46,20 @@ GIT_INLINE(void) clar__assert_in_range(
 #define cl_assert_in_range(L,V,H) \
 	clar__assert_in_range((L),(V),(H),__FILE__,__LINE__,"Range check: " #V " in [" #L "," #H "]", 1)
 
+#define cl_assert_equal_file(DATA,SIZE,PATH) \
+	clar__assert_equal_file(DATA,SIZE,0,PATH,__FILE__,__LINE__)
+
+#define cl_assert_equal_file_ignore_cr(DATA,SIZE,PATH) \
+	clar__assert_equal_file(DATA,SIZE,1,PATH,__FILE__,__LINE__)
+
+void clar__assert_equal_file(
+	const char *expected_data,
+	size_t expected_size,
+	int ignore_cr,
+	const char *path,
+	const char *file,
+	size_t line);
+
 /*
  * Some utility macros for building long strings
  */