t18-status.c 5.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2,
 * as published by the Free Software Foundation.
 *
 * In addition to the permissions in the GNU General Public License,
 * the authors give you unlimited permission to link the compiled
 * version of this file into combinations with other programs,
 * and to distribute those combinations without any restriction
 * coming from the use of this file.  (The General Public License
 * restrictions do apply in other respects; for example, they cover
 * modification of the file, and distribution when not linked into
 * a combined executable.)
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
25

26 27 28 29 30
#include "test_lib.h"
#include "test_helpers.h"
#include "fileops.h"
#include "git2/status.h"

31
static const char *test_blob_oid = "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a";
32

33 34
#define STATUS_WORKDIR_FOLDER TEST_RESOURCES "/status/"
#define STATUS_REPOSITORY_TEMP_FOLDER TEMP_REPO_FOLDER ".gitted/"
35 36

BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
37
	git_oid expected_id, actual_id;
38 39
	char filename[] = "new_file";
	int fd;
40

41 42 43 44
	fd = p_creat(filename, 0644);
	must_pass(fd);
	must_pass(p_write(fd, "new_file\n", 9));
	must_pass(p_close(fd));
45

46
	must_pass(git_odb_hashfile(&actual_id, filename, GIT_OBJ_BLOB));
47

48
	must_pass(git_oid_fromstr(&expected_id, test_blob_oid));
49 50
	must_be_true(git_oid_cmp(&expected_id, &actual_id) == 0);

51
	must_pass(p_unlink(filename));
52 53
END_TEST

54 55 56 57 58 59 60 61 62 63 64 65 66
static const char *entry_paths[] = {
	"current_file",
	"file_deleted",
	"modified_file",
	"new_file",
	"staged_changes",
	"staged_changes_file_deleted",
	"staged_changes_modified_file",
	"staged_delete_file_deleted",
	"staged_delete_modified_file",
	"staged_new_file",
	"staged_new_file_deleted_file",
	"staged_new_file_modified_file",
67 68 69 70 71

	"subdir/current_file",
	"subdir/deleted_file",
	"subdir/modified_file",
	"subdir/new_file",
72
};
73
static const unsigned int entry_statuses[] = {
74 75 76 77 78 79 80 81 82 83 84 85
	GIT_STATUS_CURRENT,
	GIT_STATUS_WT_DELETED,
	GIT_STATUS_WT_MODIFIED,
	GIT_STATUS_WT_NEW,
	GIT_STATUS_INDEX_MODIFIED,
	GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_DELETED,
	GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED,
	GIT_STATUS_INDEX_DELETED,
	GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW,
	GIT_STATUS_INDEX_NEW,
	GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_DELETED,
	GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED,
86 87 88 89 90

	GIT_STATUS_CURRENT,
	GIT_STATUS_WT_DELETED,
	GIT_STATUS_WT_MODIFIED,
	GIT_STATUS_WT_NEW,
91
};
92
#define ENTRY_COUNT 16
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

static unsigned int get_expected_entry_status(const char *path)
{
	int i;

	for (i = 0; i < ENTRY_COUNT; ++i)
		if (!strcmp(path, entry_paths[i]))
			return entry_statuses[i];

	return (unsigned int)-1;
}

struct status_entry_counts {
	int wrong_status_flags_count;
	int entry_count;
};

static int status_cb(const char *path, unsigned int status_flags, void *payload)
{
	unsigned int expected_status_flags = get_expected_entry_status(path);
	struct status_entry_counts *counts = (struct status_entry_counts *)payload;

	counts->entry_count++;
	if (status_flags != expected_status_flags)
		counts->wrong_status_flags_count++;

	return GIT_SUCCESS;
}

BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
	git_repository *repo;
	struct status_entry_counts counts;

126 127 128
	must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
	must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
129 130 131 132 133 134 135 136

	memset(&counts, 0x0, sizeof(struct status_entry_counts));
	git_status_foreach(repo, status_cb, &counts);
	must_be_true(counts.entry_count == ENTRY_COUNT);
	must_be_true(counts.wrong_status_flags_count == 0);

	git_repository_free(repo);

137
	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
138 139
END_TEST

140 141 142 143 144
BEGIN_TEST(singlestatus0, "test retrieving status for single file")
	git_repository *repo;
	unsigned int status_flags;
	int i;

145 146 147
	must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
	must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
148 149 150 151 152 153 154 155

	for (i = 0; i < ENTRY_COUNT; ++i) {
		must_pass(git_status_file(&status_flags, repo, entry_paths[i]));
		must_be_true(status_flags == entry_statuses[i]);
	}

	git_repository_free(repo);

156
	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
157 158
END_TEST

159 160 161 162 163
BEGIN_TEST(singlestatus1, "test retrieving status for nonexistent file")
	git_repository *repo;
	unsigned int status_flags;
	int error;

164 165 166
	must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
	must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
	must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
167 168 169 170 171 172 173

	// "nonexistent" does not exist in HEAD, Index or the worktree
	error = git_status_file(&status_flags, repo, "nonexistent");
	must_be_true(error == GIT_ENOTFOUND);

	git_repository_free(repo);

174
	git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
175 176
END_TEST

177 178
BEGIN_SUITE(status)
	ADD_TEST(file0);
179

180
	ADD_TEST(statuscb0);
181

182
	ADD_TEST(singlestatus0);
183
	ADD_TEST(singlestatus1);
184
END_SUITE