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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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
*/