Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-09-14 03:30:21 +0400
committerRussell Belfer <rb@github.com>2013-09-17 20:31:46 +0400
commit13f36ffb9e1c4fb70b44a477d716873fecfc0407 (patch)
treee5071f42071b085e90e77ffdaeb1304e19f8d0e1 /tests-clar
parentfa9cc14880cb50ea626c4bb0fcf1b68acdd73186 (diff)
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.
Diffstat (limited to 'tests-clar')
-rw-r--r--tests-clar/checkout/checkout_helpers.c58
-rw-r--r--tests-clar/checkout/checkout_helpers.h13
-rw-r--r--tests-clar/clar_libgit2.c55
-rw-r--r--tests-clar/clar_libgit2.h14
4 files changed, 71 insertions, 69 deletions
diff --git a/tests-clar/checkout/checkout_helpers.c b/tests-clar/checkout/checkout_helpers.c
index f55f7b611..06b4e0682 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 0e8da31d1..705ee903d 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 340943ca8..522f73634 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 8dcfdee48..76299e4e3 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
*/