]>
Commit | Line | Data |
---|---|---|
6c106eec BS |
1 | #include "clar_libgit2.h" |
2 | #include "index.h" | |
3 | ||
b482c420 BS |
4 | static const int index_entry_count = 109; |
5 | static const int index_entry_count_2 = 1437; | |
6c106eec BS |
6 | #define TEST_INDEX_PATH cl_fixture("testrepo.git/index") |
7 | #define TEST_INDEX2_PATH cl_fixture("gitgit.index") | |
8 | #define TEST_INDEXBIG_PATH cl_fixture("big.index") | |
9 | ||
10 | ||
11 | // Suite data | |
12 | struct test_entry { | |
13 | int index; | |
14 | char path[128]; | |
15 | git_off_t file_size; | |
16 | git_time_t mtime; | |
17 | }; | |
18 | ||
b482c420 | 19 | static struct test_entry test_entries[] = { |
6c106eec BS |
20 | {4, "Makefile", 5064, 0x4C3F7F33}, |
21 | {62, "tests/Makefile", 2631, 0x4C3F7F33}, | |
22 | {36, "src/index.c", 10014, 0x4C43368D}, | |
23 | {6, "git.git-authors", 2709, 0x4C3F7F33}, | |
24 | {48, "src/revobject.h", 1448, 0x4C3F7FE2} | |
25 | }; | |
26 | ||
27 | ||
28 | // Helpers | |
270303ca | 29 | static void copy_file(const char *src, const char *dst) |
6c106eec BS |
30 | { |
31 | git_buf source_buf = GIT_BUF_INIT; | |
32 | git_file dst_fd; | |
6c106eec | 33 | |
270303ca | 34 | cl_git_pass(git_futils_readbuffer(&source_buf, src)); |
6c106eec BS |
35 | |
36 | dst_fd = git_futils_creat_withpath(dst, 0777, 0666); | |
37 | if (dst_fd < 0) | |
38 | goto cleanup; | |
39 | ||
270303ca | 40 | cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size)); |
6c106eec BS |
41 | |
42 | cleanup: | |
43 | git_buf_free(&source_buf); | |
44 | p_close(dst_fd); | |
6c106eec BS |
45 | } |
46 | ||
270303ca | 47 | static void files_are_equal(const char *a, const char *b) |
6c106eec BS |
48 | { |
49 | git_buf buf_a = GIT_BUF_INIT; | |
50 | git_buf buf_b = GIT_BUF_INIT; | |
270303ca | 51 | int pass; |
6c106eec BS |
52 | |
53 | if (git_futils_readbuffer(&buf_a, a) < GIT_SUCCESS) | |
270303ca | 54 | cl_assert(0); |
6c106eec BS |
55 | |
56 | if (git_futils_readbuffer(&buf_b, b) < GIT_SUCCESS) { | |
57 | git_buf_free(&buf_a); | |
270303ca | 58 | cl_assert(0); |
6c106eec BS |
59 | } |
60 | ||
270303ca | 61 | pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size)); |
6c106eec BS |
62 | |
63 | git_buf_free(&buf_a); | |
64 | git_buf_free(&buf_b); | |
13ed2966 MS |
65 | |
66 | cl_assert(pass); | |
6c106eec BS |
67 | } |
68 | ||
69 | ||
70 | // Fixture setup and teardown | |
71 | void test_index_tests__initialize(void) | |
72 | { | |
73 | } | |
74 | ||
75 | void test_index_tests__cleanup(void) | |
76 | { | |
77 | } | |
78 | ||
79 | ||
80 | void test_index_tests__empty_index(void) | |
81 | { | |
82 | git_index *index; | |
83 | ||
84 | cl_git_pass(git_index_open(&index, "in-memory-index")); | |
85 | cl_assert(index->on_disk == 0); | |
86 | ||
87 | cl_assert(git_index_entrycount(index) == 0); | |
88 | cl_assert(index->entries.sorted); | |
89 | ||
90 | git_index_free(index); | |
91 | } | |
92 | ||
93 | void test_index_tests__default_test_index(void) | |
94 | { | |
95 | git_index *index; | |
96 | unsigned int i; | |
97 | git_index_entry **entries; | |
98 | ||
99 | cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); | |
100 | cl_assert(index->on_disk); | |
101 | ||
2df029ed | 102 | cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count); |
6c106eec BS |
103 | cl_assert(index->entries.sorted); |
104 | ||
105 | entries = (git_index_entry **)index->entries.contents; | |
106 | ||
b482c420 BS |
107 | for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { |
108 | git_index_entry *e = entries[test_entries[i].index]; | |
6c106eec | 109 | |
946a6dc4 | 110 | cl_assert_equal_s(e->path, test_entries[i].path); |
b482c420 BS |
111 | cl_assert(e->mtime.seconds == test_entries[i].mtime); |
112 | cl_assert(e->file_size == test_entries[i].file_size); | |
6c106eec BS |
113 | } |
114 | ||
115 | git_index_free(index); | |
116 | } | |
117 | ||
118 | void test_index_tests__gitgit_index(void) | |
119 | { | |
120 | git_index *index; | |
121 | ||
122 | cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH)); | |
123 | cl_assert(index->on_disk); | |
124 | ||
2df029ed | 125 | cl_assert(git_index_entrycount(index) == (unsigned int)index_entry_count_2); |
6c106eec BS |
126 | cl_assert(index->entries.sorted); |
127 | cl_assert(index->tree != NULL); | |
128 | ||
129 | git_index_free(index); | |
130 | } | |
131 | ||
132 | void test_index_tests__find_in_existing(void) | |
133 | { | |
134 | git_index *index; | |
135 | unsigned int i; | |
136 | ||
137 | cl_git_pass(git_index_open(&index, TEST_INDEX_PATH)); | |
138 | ||
b482c420 BS |
139 | for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { |
140 | int idx = git_index_find(index, test_entries[i].path); | |
141 | cl_assert(idx == test_entries[i].index); | |
6c106eec BS |
142 | } |
143 | ||
144 | git_index_free(index); | |
145 | } | |
146 | ||
147 | void test_index_tests__find_in_empty(void) | |
148 | { | |
149 | git_index *index; | |
150 | unsigned int i; | |
151 | ||
152 | cl_git_pass(git_index_open(&index, "fake-index")); | |
153 | ||
b482c420 BS |
154 | for (i = 0; i < ARRAY_SIZE(test_entries); ++i) { |
155 | int idx = git_index_find(index, test_entries[i].path); | |
6c106eec BS |
156 | cl_assert(idx == GIT_ENOTFOUND); |
157 | } | |
158 | ||
159 | git_index_free(index); | |
160 | } | |
161 | ||
162 | void test_index_tests__write(void) | |
163 | { | |
164 | git_index *index; | |
165 | ||
270303ca | 166 | copy_file(TEST_INDEXBIG_PATH, "index_rewrite"); |
6c106eec BS |
167 | |
168 | cl_git_pass(git_index_open(&index, "index_rewrite")); | |
169 | cl_assert(index->on_disk); | |
170 | ||
171 | cl_git_pass(git_index_write(index)); | |
270303ca | 172 | files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite"); |
6c106eec BS |
173 | |
174 | git_index_free(index); | |
175 | ||
176 | p_unlink("index_rewrite"); | |
177 | } | |
178 | ||
179 | void test_index_tests__sort0(void) | |
180 | { | |
181 | // sort the entires in an index | |
182 | /* | |
183 | * TODO: This no longer applies: | |
184 | * index sorting in Git uses some specific changes to the way | |
185 | * directories are sorted. | |
186 | * | |
187 | * We need to specificially check for this by creating a new | |
188 | * index, adding entries in random order and then | |
189 | * checking for consistency | |
190 | */ | |
191 | } | |
192 | ||
193 | void test_index_tests__sort1(void) | |
194 | { | |
195 | // sort the entires in an empty index | |
196 | git_index *index; | |
197 | ||
198 | cl_git_pass(git_index_open(&index, "fake-index")); | |
199 | ||
200 | /* FIXME: this test is slightly dumb */ | |
201 | cl_assert(index->entries.sorted); | |
202 | ||
203 | git_index_free(index); | |
204 | } | |
205 | ||
206 | void test_index_tests__add(void) | |
207 | { | |
208 | git_index *index; | |
209 | git_filebuf file = GIT_FILEBUF_INIT; | |
210 | git_repository *repo; | |
211 | git_index_entry *entry; | |
212 | git_oid id1; | |
213 | ||
214 | /* Intialize a new repository */ | |
2df029ed | 215 | cl_git_pass(git_repository_init(&repo, "./myrepo", 0)); |
6c106eec BS |
216 | |
217 | /* Ensure we're the only guy in the room */ | |
218 | cl_git_pass(git_repository_index(&index, repo)); | |
219 | cl_assert(git_index_entrycount(index) == 0); | |
220 | ||
221 | /* Create a new file in the working directory */ | |
222 | cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777)); | |
223 | cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0)); | |
224 | cl_git_pass(git_filebuf_write(&file, "hey there\n", 10)); | |
225 | cl_git_pass(git_filebuf_commit(&file, 0666)); | |
226 | ||
227 | /* Store the expected hash of the file/blob | |
228 | * This has been generated by executing the following | |
229 | * $ echo "hey there" | git hash-object --stdin | |
230 | */ | |
231 | cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6")); | |
232 | ||
233 | /* Add the new file to the index */ | |
234 | cl_git_pass(git_index_add(index, "test.txt", 0)); | |
235 | ||
236 | /* Wow... it worked! */ | |
237 | cl_assert(git_index_entrycount(index) == 1); | |
238 | entry = git_index_get(index, 0); | |
239 | ||
240 | /* And the built-in hashing mechanism worked as expected */ | |
241 | cl_assert(git_oid_cmp(&id1, &entry->oid) == 0); | |
242 | ||
243 | git_index_free(index); | |
244 | git_repository_free(repo); | |
245 | } | |
246 |