]> git.proxmox.com Git - libgit2.git/blame - tests/t18-status.c
Merge pull request #378 from kiryl/Wuninitialized
[libgit2.git] / tests / t18-status.c
CommitLineData
205166d2
JP
1/*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
d4760a42 25
205166d2
JP
26#include "test_lib.h"
27#include "test_helpers.h"
28#include "fileops.h"
29#include "git2/status.h"
30
3af6b34a 31static const char *test_blob_oid = "d4fa8600b4f37d7516bef4816ae2c64dbf029e3a";
205166d2 32
d4760a42 33#define STATUS_WORKDIR_FOLDER TEST_RESOURCES "/status/"
34#define STATUS_REPOSITORY_TEMP_FOLDER TEMP_REPO_FOLDER ".gitted/"
2b90cc26
JP
35
36BEGIN_TEST(file0, "test retrieving OID from a file apart from the ODB")
205166d2 37 git_oid expected_id, actual_id;
d4760a42 38 char filename[] = "new_file";
39 int fd;
205166d2 40
d4760a42 41 fd = p_creat(filename, 0644);
42 must_pass(fd);
43 must_pass(p_write(fd, "new_file\n", 9));
44 must_pass(p_close(fd));
205166d2 45
d4760a42 46 must_pass(git_odb_hashfile(&actual_id, filename, GIT_OBJ_BLOB));
205166d2 47
d4760a42 48 must_pass(git_oid_fromstr(&expected_id, test_blob_oid));
205166d2
JP
49 must_be_true(git_oid_cmp(&expected_id, &actual_id) == 0);
50
d4760a42 51 must_pass(p_unlink(filename));
205166d2
JP
52END_TEST
53
3af6b34a
JP
54static const char *entry_paths[] = {
55 "current_file",
56 "file_deleted",
57 "modified_file",
58 "new_file",
59 "staged_changes",
60 "staged_changes_file_deleted",
61 "staged_changes_modified_file",
62 "staged_delete_file_deleted",
63 "staged_delete_modified_file",
64 "staged_new_file",
65 "staged_new_file_deleted_file",
66 "staged_new_file_modified_file",
34dfea27
JP
67
68 "subdir/current_file",
69 "subdir/deleted_file",
70 "subdir/modified_file",
71 "subdir/new_file",
3af6b34a 72};
20361b2f 73static const unsigned int entry_statuses[] = {
3af6b34a
JP
74 GIT_STATUS_CURRENT,
75 GIT_STATUS_WT_DELETED,
76 GIT_STATUS_WT_MODIFIED,
77 GIT_STATUS_WT_NEW,
78 GIT_STATUS_INDEX_MODIFIED,
79 GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_DELETED,
80 GIT_STATUS_INDEX_MODIFIED | GIT_STATUS_WT_MODIFIED,
81 GIT_STATUS_INDEX_DELETED,
82 GIT_STATUS_INDEX_DELETED | GIT_STATUS_WT_NEW,
83 GIT_STATUS_INDEX_NEW,
84 GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_DELETED,
85 GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED,
34dfea27
JP
86
87 GIT_STATUS_CURRENT,
88 GIT_STATUS_WT_DELETED,
89 GIT_STATUS_WT_MODIFIED,
90 GIT_STATUS_WT_NEW,
3af6b34a 91};
34dfea27 92#define ENTRY_COUNT 16
3af6b34a
JP
93
94static unsigned int get_expected_entry_status(const char *path)
95{
96 int i;
97
98 for (i = 0; i < ENTRY_COUNT; ++i)
99 if (!strcmp(path, entry_paths[i]))
100 return entry_statuses[i];
101
102 return (unsigned int)-1;
103}
104
105struct status_entry_counts {
106 int wrong_status_flags_count;
107 int entry_count;
108};
109
110static int status_cb(const char *path, unsigned int status_flags, void *payload)
111{
112 unsigned int expected_status_flags = get_expected_entry_status(path);
113 struct status_entry_counts *counts = (struct status_entry_counts *)payload;
114
115 counts->entry_count++;
116 if (status_flags != expected_status_flags)
117 counts->wrong_status_flags_count++;
118
119 return GIT_SUCCESS;
120}
121
122BEGIN_TEST(statuscb0, "test retrieving status for worktree of repository")
3af6b34a
JP
123 git_repository *repo;
124 struct status_entry_counts counts;
125
d4760a42 126 must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
127 must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
128 must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
3af6b34a
JP
129
130 memset(&counts, 0x0, sizeof(struct status_entry_counts));
131 git_status_foreach(repo, status_cb, &counts);
132 must_be_true(counts.entry_count == ENTRY_COUNT);
133 must_be_true(counts.wrong_status_flags_count == 0);
134
135 git_repository_free(repo);
136
d4760a42 137 git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
3af6b34a
JP
138END_TEST
139
20361b2f 140BEGIN_TEST(singlestatus0, "test retrieving status for single file")
20361b2f
JP
141 git_repository *repo;
142 unsigned int status_flags;
143 int i;
144
d4760a42 145 must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
146 must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
147 must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
20361b2f
JP
148
149 for (i = 0; i < ENTRY_COUNT; ++i) {
150 must_pass(git_status_file(&status_flags, repo, entry_paths[i]));
151 must_be_true(status_flags == entry_statuses[i]);
152 }
153
154 git_repository_free(repo);
155
d4760a42 156 git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
20361b2f
JP
157END_TEST
158
3b2a423c 159BEGIN_TEST(singlestatus1, "test retrieving status for nonexistent file")
3b2a423c
JP
160 git_repository *repo;
161 unsigned int status_flags;
162 int error;
163
d4760a42 164 must_pass(copydir_recurs(STATUS_WORKDIR_FOLDER, TEMP_REPO_FOLDER));
165 must_pass(git_futils_mv_atomic(STATUS_REPOSITORY_TEMP_FOLDER, TEST_STD_REPO_FOLDER));
166 must_pass(git_repository_open(&repo, TEST_STD_REPO_FOLDER));
3b2a423c
JP
167
168 // "nonexistent" does not exist in HEAD, Index or the worktree
169 error = git_status_file(&status_flags, repo, "nonexistent");
170 must_be_true(error == GIT_ENOTFOUND);
171
172 git_repository_free(repo);
173
d4760a42 174 git_futils_rmdir_r(TEMP_REPO_FOLDER, 1);
3b2a423c
JP
175END_TEST
176
205166d2
JP
177BEGIN_SUITE(status)
178 ADD_TEST(file0);
d4760a42 179
3af6b34a 180 ADD_TEST(statuscb0);
d4760a42 181
20361b2f 182 ADD_TEST(singlestatus0);
3b2a423c 183 ADD_TEST(singlestatus1);
7757be33 184END_SUITE