tests.c 12.2 KB
Newer Older
Ben Straub committed
1 2 3
#include "clar_libgit2.h"
#include "index.h"

4 5
static const size_t index_entry_count = 109;
static const size_t index_entry_count_2 = 1437;
Ben Straub committed
6 7 8 9 10 11 12
#define TEST_INDEX_PATH cl_fixture("testrepo.git/index")
#define TEST_INDEX2_PATH cl_fixture("gitgit.index")
#define TEST_INDEXBIG_PATH cl_fixture("big.index")


// Suite data
struct test_entry {
13
   size_t index;
Ben Straub committed
14 15 16 17 18
   char path[128];
   git_off_t file_size;
   git_time_t mtime;
};

Ben Straub committed
19
static struct test_entry test_entries[] = {
Ben Straub committed
20 21 22 23 24 25 26 27
   {4, "Makefile", 5064, 0x4C3F7F33},
   {62, "tests/Makefile", 2631, 0x4C3F7F33},
   {36, "src/index.c", 10014, 0x4C43368D},
   {6, "git.git-authors", 2709, 0x4C3F7F33},
   {48, "src/revobject.h", 1448, 0x4C3F7FE2}
};

// Helpers
28
static void copy_file(const char *src, const char *dst)
Ben Straub committed
29 30 31 32
{
	git_buf source_buf = GIT_BUF_INIT;
	git_file dst_fd;

33
	cl_git_pass(git_futils_readbuffer(&source_buf, src));
Ben Straub committed
34

35
	dst_fd = git_futils_creat_withpath(dst, 0777, 0666); //-V536
Ben Straub committed
36 37 38
	if (dst_fd < 0)
		goto cleanup;

39
	cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size));
Ben Straub committed
40 41 42 43 44 45

cleanup:
	git_buf_free(&source_buf);
	p_close(dst_fd);
}

46
static void files_are_equal(const char *a, const char *b)
Ben Straub committed
47 48 49
{
	git_buf buf_a = GIT_BUF_INIT;
	git_buf buf_b = GIT_BUF_INIT;
50
	int pass;
Ben Straub committed
51

52
	if (git_futils_readbuffer(&buf_a, a) < 0)
53
		cl_assert(0);
Ben Straub committed
54

55
	if (git_futils_readbuffer(&buf_b, b) < 0) {
Ben Straub committed
56
		git_buf_free(&buf_a);
57
		cl_assert(0);
Ben Straub committed
58 59
	}

60
	pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size));
Ben Straub committed
61 62 63

	git_buf_free(&buf_a);
	git_buf_free(&buf_b);
64 65

	cl_assert(pass);
Ben Straub committed
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
}


// Fixture setup and teardown
void test_index_tests__initialize(void)
{
}

void test_index_tests__empty_index(void)
{
   git_index *index;

   cl_git_pass(git_index_open(&index, "in-memory-index"));
   cl_assert(index->on_disk == 0);

   cl_assert(git_index_entrycount(index) == 0);
   cl_assert(index->entries.sorted);

   git_index_free(index);
}

void test_index_tests__default_test_index(void)
{
   git_index *index;
   unsigned int i;
   git_index_entry **entries;

   cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
   cl_assert(index->on_disk);

96
   cl_assert(git_index_entrycount(index) == index_entry_count);
Ben Straub committed
97 98 99 100
   cl_assert(index->entries.sorted);

   entries = (git_index_entry **)index->entries.contents;

Ben Straub committed
101 102
   for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
      git_index_entry *e = entries[test_entries[i].index];
Ben Straub committed
103

Vicent Martí committed
104
      cl_assert_equal_s(e->path, test_entries[i].path);
Ben Straub committed
105 106
      cl_assert(e->mtime.seconds == test_entries[i].mtime);
      cl_assert(e->file_size == test_entries[i].file_size);
Ben Straub committed
107 108 109 110 111 112 113 114 115 116 117 118
   }

   git_index_free(index);
}

void test_index_tests__gitgit_index(void)
{
   git_index *index;

   cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH));
   cl_assert(index->on_disk);

119
   cl_assert(git_index_entrycount(index) == index_entry_count_2);
Ben Straub committed
120 121 122 123 124 125 126 127 128 129 130 131 132
   cl_assert(index->entries.sorted);
   cl_assert(index->tree != NULL);

   git_index_free(index);
}

void test_index_tests__find_in_existing(void)
{
   git_index *index;
   unsigned int i;

   cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));

Ben Straub committed
133
   for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
134 135 136 137
	  size_t idx;

	  cl_assert(!git_index_find(&idx, index, test_entries[i].path));
	  cl_assert(idx == test_entries[i].index);
Ben Straub committed
138 139 140 141 142 143 144 145 146 147 148 149
   }

   git_index_free(index);
}

void test_index_tests__find_in_empty(void)
{
   git_index *index;
   unsigned int i;

   cl_git_pass(git_index_open(&index, "fake-index"));

Ben Straub committed
150
   for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
151
      cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path));
Ben Straub committed
152 153 154 155 156 157 158 159 160
   }

   git_index_free(index);
}

void test_index_tests__write(void)
{
   git_index *index;

161
   copy_file(TEST_INDEXBIG_PATH, "index_rewrite");
Ben Straub committed
162 163 164 165 166

   cl_git_pass(git_index_open(&index, "index_rewrite"));
   cl_assert(index->on_disk);

   cl_git_pass(git_index_write(index));
167
   files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite");
Ben Straub committed
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

   git_index_free(index);

   p_unlink("index_rewrite");
}

void test_index_tests__sort0(void)
{
   // sort the entires in an 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
   */
}

void test_index_tests__sort1(void)
{
   // sort the entires in an empty index
   git_index *index;

   cl_git_pass(git_index_open(&index, "fake-index"));

   /* FIXME: this test is slightly dumb */
   cl_assert(index->entries.sorted);

   git_index_free(index);
}

Vicent Marti committed
201 202 203 204 205 206
static void cleanup_myrepo(void *opaque)
{
	GIT_UNUSED(opaque);
	cl_fixture_cleanup("myrepo");
}

Ben Straub committed
207 208
void test_index_tests__add(void)
{
Vicent Marti committed
209 210 211 212 213
	git_index *index;
	git_filebuf file = GIT_FILEBUF_INIT;
	git_repository *repo;
	const git_index_entry *entry;
	git_oid id1;
Ben Straub committed
214

Vicent Marti committed
215
	cl_set_cleanup(&cleanup_myrepo, NULL);
Ben Straub committed
216

Vicent Marti committed
217 218
	/* Intialize a new repository */
	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
Ben Straub committed
219

Vicent Marti committed
220 221 222
	/* Ensure we're the only guy in the room */
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);
Ben Straub committed
223

Vicent Marti committed
224 225 226 227 228
	/* Create a new file in the working directory */
	cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
	cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0));
	cl_git_pass(git_filebuf_write(&file, "hey there\n", 10));
	cl_git_pass(git_filebuf_commit(&file, 0666));
Ben Straub committed
229

Vicent Marti committed
230 231 232 233 234
	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ echo "hey there" | git hash-object --stdin
	 */
	cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
Ben Straub committed
235

Vicent Marti committed
236
	/* Add the new file to the index */
237
	cl_git_pass(git_index_add_bypath(index, "test.txt"));
Ben Straub committed
238

Vicent Marti committed
239 240 241
	/* Wow... it worked! */
	cl_assert(git_index_entrycount(index) == 1);
	entry = git_index_get_byindex(index, 0);
Ben Straub committed
242

Vicent Marti committed
243 244
	/* And the built-in hashing mechanism worked as expected */
	cl_assert(git_oid_cmp(&id1, &entry->oid) == 0);
Edward Thomson committed
245

Vicent Marti committed
246 247 248 249 250 251
	/* Test access by path instead of index */
	cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
	cl_assert(git_oid_cmp(&id1, &entry->oid) == 0);

	git_index_free(index);
	git_repository_free(repo);
Ben Straub committed
252 253
}

Russell Belfer committed
254 255 256 257 258 259
static void cleanup_1397(void *opaque)
{
	GIT_UNUSED(opaque);
	cl_git_sandbox_cleanup();
}

260 261 262 263 264 265 266
void test_index_tests__add_issue_1397(void)
{
	git_index *index;
	git_repository *repo;
	const git_index_entry *entry;
	git_oid id1;

Russell Belfer committed
267
	cl_set_cleanup(&cleanup_1397, NULL);
268 269 270

	repo = cl_git_sandbox_init("issue_1397");

Russell Belfer committed
271
	cl_repo_set_bool(repo, "core.autocrlf", true);
272 273 274 275 276 277 278 279 280 281 282 283

	/* Ensure we're the only guy in the room */
	cl_git_pass(git_repository_index(&index, repo));

	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ git hash-object crlf_file.txt
	 */
	cl_git_pass(git_oid_fromstr(&id1, "8312e0889a9cbab77c732b6bc39b51a683e3a318"));

	/* Make sure the initial SHA-1 is correct */
	cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL);
Russell Belfer committed
284
	cl_assert_(git_oid_cmp(&id1, &entry->oid) == 0, "first oid check");
285 286 287 288 289 290

	/* Update the index */
	cl_git_pass(git_index_add_bypath(index, "crlf_file.txt"));

	/* Check the new SHA-1 */
	cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL);
Russell Belfer committed
291
	cl_assert_(git_oid_cmp(&id1, &entry->oid) == 0, "second oid check");
292 293 294 295

	git_index_free(index);
}

296
void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void)
297 298 299 300 301 302 303
{
	git_repository *bare_repo;
	git_index *index;

	cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git")));
	cl_git_pass(git_repository_index(&index, bare_repo));

304
	cl_assert_equal_i(GIT_EBAREREPO, git_index_add_bypath(index, "test.txt"));
305 306 307 308

	git_index_free(index);
	git_repository_free(bare_repo);
}
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

/* Test that writing an invalid filename fails */
void test_index_tests__write_invalid_filename(void)
{
	git_repository *repo;
	git_index *index;
	git_oid expected;

	p_mkdir("read_tree", 0700);

	cl_git_pass(git_repository_init(&repo, "./read_tree", 0));
	cl_git_pass(git_repository_index(&index, repo));

	cl_assert(git_index_entrycount(index) == 0);

	cl_git_mkfile("./read_tree/.git/hello", NULL);

326
	cl_git_pass(git_index_add_bypath(index, ".git/hello"));
327 328 329 330 331 332 333 334 335

	/* write-tree */
	cl_git_fail(git_index_write_tree(&expected, index));

	git_index_free(index);
	git_repository_free(repo);

	cl_fixture_cleanup("read_tree");
}
336 337 338 339 340 341 342 343 344 345 346 347 348

void test_index_tests__remove_entry(void)
{
	git_repository *repo;
	git_index *index;

	p_mkdir("index_test", 0770);

	cl_git_pass(git_repository_init(&repo, "index_test", 0));
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

	cl_git_mkfile("index_test/hello", NULL);
349
	cl_git_pass(git_index_add_bypath(index, "hello"));
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_read(index)); /* reload */
	cl_assert(git_index_entrycount(index) == 1);
	cl_assert(git_index_get_bypath(index, "hello", 0) != NULL);

	cl_git_pass(git_index_remove(index, "hello", 0));
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_read(index)); /* reload */
	cl_assert(git_index_entrycount(index) == 0);
	cl_assert(git_index_get_bypath(index, "hello", 0) == NULL);

	git_index_free(index);
	git_repository_free(repo);
	cl_fixture_cleanup("index_test");
}

void test_index_tests__remove_directory(void)
{
	git_repository *repo;
	git_index *index;

	p_mkdir("index_test", 0770);

	cl_git_pass(git_repository_init(&repo, "index_test", 0));
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert_equal_i(0, (int)git_index_entrycount(index));

	p_mkdir("index_test/a", 0770);
	cl_git_mkfile("index_test/a/1.txt", NULL);
	cl_git_mkfile("index_test/a/2.txt", NULL);
	cl_git_mkfile("index_test/a/3.txt", NULL);
	cl_git_mkfile("index_test/b.txt", NULL);

385 386 387 388
	cl_git_pass(git_index_add_bypath(index, "a/1.txt"));
	cl_git_pass(git_index_add_bypath(index, "a/2.txt"));
	cl_git_pass(git_index_add_bypath(index, "a/3.txt"));
	cl_git_pass(git_index_add_bypath(index, "b.txt"));
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_read(index)); /* reload */
	cl_assert_equal_i(4, (int)git_index_entrycount(index));
	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL);
	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
	cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);

	cl_git_pass(git_index_remove(index, "a/1.txt", 0));
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_read(index)); /* reload */
	cl_assert_equal_i(3, (int)git_index_entrycount(index));
	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
	cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);

	cl_git_pass(git_index_remove_directory(index, "a", 0));
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_read(index)); /* reload */
	cl_assert_equal_i(1, (int)git_index_entrycount(index));
	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);

	git_index_free(index);
	git_repository_free(repo);
	cl_fixture_cleanup("index_test");
}
419 420 421 422 423 424

void test_index_tests__preserves_case(void)
{
	git_repository *repo;
	git_index *index;
	const git_index_entry *entry;
425
	int index_caps;
426 427 428 429 430 431

	cl_set_cleanup(&cleanup_myrepo, NULL);

	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
	cl_git_pass(git_repository_index(&index, repo));

432 433
	index_caps = git_index_caps(index);

434 435 436 437 438 439 440
	cl_git_rewritefile("myrepo/test.txt", "hey there\n");
	cl_git_pass(git_index_add_bypath(index, "test.txt"));

	cl_git_pass(p_rename("myrepo/test.txt", "myrepo/TEST.txt"));
	cl_git_rewritefile("myrepo/TEST.txt", "hello again\n");
	cl_git_pass(git_index_add_bypath(index, "TEST.txt"));

441 442 443 444
	if (index_caps & GIT_INDEXCAP_IGNORE_CASE)
		cl_assert_equal_i(1, (int)git_index_entrycount(index));
	else
		cl_assert_equal_i(2, (int)git_index_entrycount(index));
445 446 447 448 449 450

	/* Test access by path instead of index */
	cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
	/* The path should *not* have changed without an explicit remove */
	cl_assert(git__strcmp(entry->path, "test.txt") == 0);

451 452 453 454 455 456 457
	cl_assert((entry = git_index_get_bypath(index, "TEST.txt", 0)) != NULL);
	if (index_caps & GIT_INDEXCAP_IGNORE_CASE)
		/* The path should *not* have changed without an explicit remove */
		cl_assert(git__strcmp(entry->path, "test.txt") == 0);
	else
		cl_assert(git__strcmp(entry->path, "TEST.txt") == 0);

458 459 460 461
	git_index_free(index);
	git_repository_free(repo);
}