Add unit test for writing a big index file Signed-off-by: Vicent Marti <tanoku@gmail.com>
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
diff --git a/tests/resources/big.index b/tests/resources/big.index
new file mode 100644
index 0000000..66932f1
Binary files /dev/null and b/tests/resources/big.index differ
diff --git a/tests/t06-index.c b/tests/t06-index.c
index 2d5de74..ded2370 100644
--- a/tests/t06-index.c
+++ b/tests/t06-index.c
@@ -134,77 +134,31 @@ END_TEST
BEGIN_TEST("write", index_write_test)
git_index *index;
- git_filelock out_file;
- must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
+ must_pass(copy_file(TEST_INDEXBIG_PATH, "index_rewrite"));
+
+ must_pass(git_index_open_bare(&index, "index_rewrite"));
must_pass(git_index_read(index));
must_be_true(index->on_disk);
- /*
- * TODO:
- * Don't write the index like this; make sure the filelocks
- * are manually set
- */
-/*
- must_pass(git_filelock_init(&out_file, "index_rewrite"));
- must_pass(git_filelock_lock(&out_file, 0));
- must_pass(git_index__write(index, &out_file));
- must_pass(git_filelock_commit(&out_file));
-*/
+ must_pass(git_index_write(index));
+ must_pass(cmp_files(TEST_INDEXBIG_PATH, "index_rewrite"));
git_index_free(index);
gitfo_unlink("index_rewrite");
END_TEST
-
-static void randomize_entries(git_index *index)
-{
- unsigned int i, j;
- git_index_entry *tmp;
- git_index_entry **entries;
-
- entries = (git_index_entry **)index->entries.contents;
-
- srand((unsigned int)time(NULL));
-
- for (i = 0; i < index->entries.length; ++i) {
- j = rand() % index->entries.length;
-
- tmp = entries[j];
- entries[j] = entries[i];
- entries[i] = tmp;
- }
-
- index->sorted = 0;
-}
-
BEGIN_TEST("sort", index_sort_test)
- git_index *index;
- unsigned int i;
- git_index_entry **entries;
-
- must_pass(git_index_open_bare(&index, TEST_INDEX_PATH));
- must_pass(git_index_read(index));
-
- randomize_entries(index);
-
/*
* TODO: This no longer applies:
* index sorting in Git uses some specific changes to the way
* directories are sorted.
+ *
+ * We need to specificially check for this by creating a new
+ * index, adding entries in random order and then
+ * checking for consistency
*/
-/*
- git_index__sort(index);
- must_be_true(index->sorted);
-
- entries = (git_index_entry **)index->entries.contents;
-
- for (i = 1; i < index->entries.length; ++i)
- must_be_true(strcmp(entries[i - 1]->path, entries[i]->path) < 0);
-*/
-
- git_index_free(index);
END_TEST
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index 5fd9a56..c93d31b 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -131,3 +131,47 @@ int cmp_objects(git_rawobj *o, object_data *d)
return -1;
return 0;
}
+
+int copy_file(const char *src, const char *dst)
+{
+ gitfo_buf source_buf;
+ git_file dst_fd;
+ int error = GIT_ERROR;
+
+ if (gitfo_read_file(&source_buf, src) < GIT_SUCCESS)
+ return GIT_ENOTFOUND;
+
+ dst_fd = gitfo_creat(dst, 0644);
+ if (dst_fd < 0)
+ goto cleanup;
+
+ error = gitfo_write(dst_fd, source_buf.data, source_buf.len);
+
+cleanup:
+ gitfo_free_buf(&source_buf);
+ gitfo_close(dst_fd);
+
+ return error;
+}
+
+int cmp_files(const char *a, const char *b)
+{
+ gitfo_buf buf_a, buf_b;
+ int error = GIT_ERROR;
+
+ if (gitfo_read_file(&buf_a, a) < GIT_SUCCESS)
+ return GIT_ERROR;
+
+ if (gitfo_read_file(&buf_b, b) < GIT_SUCCESS) {
+ gitfo_free_buf(&buf_a);
+ return GIT_ERROR;
+ }
+
+ if (buf_a.len == buf_b.len && !memcmp(buf_a.data, buf_b.data, buf_a.len))
+ error = GIT_SUCCESS;
+
+ gitfo_free_buf(&buf_a);
+ gitfo_free_buf(&buf_b);
+
+ return error;
+}
diff --git a/tests/test_helpers.h b/tests/test_helpers.h
index 7551fff..0a9a9e1 100644
--- a/tests/test_helpers.h
+++ b/tests/test_helpers.h
@@ -33,6 +33,7 @@
#define REPOSITORY_FOLDER (TEST_RESOURCES "/testrepo.git/")
#define TEST_INDEX_PATH (TEST_RESOURCES "/testrepo.git/index")
#define TEST_INDEX2_PATH (TEST_RESOURCES "/gitgit.index")
+#define TEST_INDEXBIG_PATH (TEST_RESOURCES "/big.index")
typedef struct object_data {
unsigned char *bytes; /* (compressed) bytes stored in object store */
@@ -55,5 +56,8 @@ extern int cmp_objects(git_rawobj *o, object_data *d);
extern int remove_loose_object(const char *odb_dir, git_object *object);
+extern int cmp_files(const char *a, const char *b);
+extern int copy_file(const char *source, const char *dest);
+
#endif
/* INCLUDE_test_helpers_h__ */