Merge pull request #1192 from arrbee/fix-win32-checkout-test Fix win32 checkout test
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
diff --git a/tests-clar/checkout/checkout_helpers.c b/tests-clar/checkout/checkout_helpers.c
new file mode 100644
index 0000000..79e80c1
--- /dev/null
+++ b/tests-clar/checkout/checkout_helpers.c
@@ -0,0 +1,84 @@
+#include "clar_libgit2.h"
+#include "checkout_helpers.h"
+#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;
+ git_buf bname = GIT_BUF_INIT;
+
+ cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
+ cl_assert_(git_reference_type(head) == GIT_REF_SYMBOLIC, branch);
+
+ cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch));
+ cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
+
+ git_reference_free(head);
+ git_buf_free(&bname);
+}
+
+void reset_index_to_treeish(git_object *treeish)
+{
+ git_object *tree;
+ git_index *index;
+ git_repository *repo = git_object_owner(treeish);
+
+ cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJ_TREE));
+
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_index_read_tree(index, (git_tree *)tree));
+ cl_git_pass(git_index_write(index));
+
+ git_object_free(tree);
+ git_index_free(index);
+}
+
+static void test_file_contents_internal(
+ const char *path, const char *expectedcontents, bool strip_cr)
+{
+ int fd;
+ char data[1024] = {0};
+ git_buf buf = GIT_BUF_INIT;
+ size_t expectedlen = strlen(expectedcontents);
+
+ fd = p_open(path, O_RDONLY);
+ cl_assert(fd >= 0);
+
+ buf.ptr = data;
+ buf.size = p_read(fd, buf.ptr, 1024);
+
+ cl_git_pass(p_close(fd));
+
+ if (strip_cr)
+ strip_cr_from_buf(&buf);
+
+ cl_assert_equal_i((int)expectedlen, (int)buf.size);
+ cl_assert_equal_s(expectedcontents, buf.ptr);
+}
+
+void test_file_contents(const char *path, const char *expected)
+{
+ test_file_contents_internal(path, expected, false);
+}
+
+void test_file_contents_nocr(const char *path, const char *expected)
+{
+ test_file_contents_internal(path, expected, true);
+}
diff --git a/tests-clar/checkout/checkout_helpers.h b/tests-clar/checkout/checkout_helpers.h
new file mode 100644
index 0000000..2c3a4b5
--- /dev/null
+++ b/tests-clar/checkout/checkout_helpers.h
@@ -0,0 +1,9 @@
+#include "buffer.h"
+#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 test_file_contents(const char *path, const char *expected);
+extern void test_file_contents_nocr(const char *path, const char *expected);
diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index fe1f687..2dc0871 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -1,25 +1,11 @@
#include "clar_libgit2.h"
+#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "repository.h"
static git_repository *g_repo;
-static void reset_index_to_treeish(git_object *treeish)
-{
- git_object *tree;
- git_index *index;
-
- cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJ_TREE));
-
- cl_git_pass(git_repository_index(&index, g_repo));
- cl_git_pass(git_index_read_tree(index, (git_tree *)tree));
- cl_git_pass(git_index_write(index));
-
- git_object_free(tree);
- git_index_free(index);
-}
-
void test_checkout_index__initialize(void)
{
git_tree *tree;
@@ -41,23 +27,6 @@ void test_checkout_index__cleanup(void)
cl_git_sandbox_cleanup();
}
-static void test_file_contents(const char *path, const char *expectedcontents)
-{
- int fd;
- char buffer[1024] = {0};
- size_t expectedlen, actuallen;
-
- fd = p_open(path, O_RDONLY);
- cl_assert(fd >= 0);
-
- expectedlen = strlen(expectedcontents);
- actuallen = p_read(fd, buffer, 1024);
- cl_git_pass(p_close(fd));
-
- cl_assert_equal_sz(actuallen, expectedlen);
- cl_assert_equal_s(buffer, expectedcontents);
-}
-
void test_checkout_index__cannot_checkout_a_bare_repository(void)
{
test_checkout_index__cleanup();
@@ -501,6 +470,10 @@ void test_checkout_index__can_update_prefixed_files(void)
{
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
+ cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
+
cl_git_mkfile("./testrepo/READ", "content\n");
cl_git_mkfile("./testrepo/README.after", "content\n");
cl_git_pass(p_mkdir("./testrepo/branch_file", 0777));
@@ -508,13 +481,17 @@ void test_checkout_index__can_update_prefixed_files(void)
cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n");
cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777));
- opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
+ opts.checkout_strategy =
+ GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
- test_file_contents("./testrepo/README", "hey there\n");
- test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
- test_file_contents("./testrepo/new.txt", "my new file\n");
+ /* remove untracked will remove the .gitattributes file before the blobs
+ * were created, so they will have had crlf filtering applied on Windows
+ */
+ test_file_contents_nocr("./testrepo/README", "hey there\n");
+ test_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n");
+ test_file_contents_nocr("./testrepo/new.txt", "my new file\n");
cl_assert(!git_path_exists("testrepo/READ"));
cl_assert(!git_path_exists("testrepo/README.after"));
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
index ff5c43a..691f03d 100644
--- a/tests-clar/checkout/tree.c
+++ b/tests-clar/checkout/tree.c
@@ -1,4 +1,5 @@
#include "clar_libgit2.h"
+#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "repository.h"
@@ -137,21 +138,6 @@ void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
cl_assert_equal_i(false, git_path_isfile("testrepo/readme.txt"));
}
-static void assert_on_branch(git_repository *repo, const char *branch)
-{
- git_reference *head;
- git_buf bname = GIT_BUF_INIT;
-
- cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
- cl_assert_(git_reference_type(head) == GIT_REF_SYMBOLIC, branch);
-
- cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch));
- cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
-
- git_reference_free(head);
- git_buf_free(&bname);
-}
-
void test_checkout_tree__can_switch_branches(void)
{
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
@@ -241,28 +227,11 @@ void test_checkout_tree__can_remove_ignored(void)
cl_assert(!git_path_isfile("testrepo/ignored_file"));
}
-/* this is essentially the code from git__unescape modified slightly */
-static void strip_cr_from_buf(git_buf *buf)
-{
- char *scan, *pos = buf->ptr;
-
- for (scan = pos; *scan; pos++, scan++) {
- if (*scan == '\r')
- scan++; /* skip '\r' */
- if (pos != scan)
- *pos = *scan;
- }
-
- *pos = '\0';
- buf->size = (pos - buf->ptr);
-}
-
void test_checkout_tree__can_update_only(void)
{
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
git_oid oid;
git_object *obj = NULL;
- git_buf buf = GIT_BUF_INIT;
/* first let's get things into a known state - by checkout out the HEAD */
@@ -273,10 +242,7 @@ void test_checkout_tree__can_update_only(void)
cl_assert(!git_path_isdir("testrepo/a"));
- cl_git_pass(git_futils_readbuffer(&buf, "testrepo/branch_file.txt"));
- strip_cr_from_buf(&buf);
- cl_assert_equal_s("hi\nbye!\n", buf.ptr);
- git_buf_free(&buf);
+ test_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
/* now checkout branch but with update only */
@@ -297,11 +263,7 @@ void test_checkout_tree__can_update_only(void)
cl_assert(!git_path_isdir("testrepo/a"));
/* but this file still should have been updated */
- cl_git_pass(git_futils_readbuffer(&buf, "testrepo/branch_file.txt"));
- strip_cr_from_buf(&buf);
- cl_assert_equal_s("hi\n", buf.ptr);
-
- git_buf_free(&buf);
+ test_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
git_object_free(obj);
}