More status testing This "fixes" the broken t18 status tests to accurately reflect the new behavior for "created" untracked subdirectories. See discussion in the PR for more details. This also contains the submodules unit test that I forgot to git add, and ports most of the t18-status.c tests to clar (still missing a couple of the git_status_file() single file tests).
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 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 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
diff --git a/tests-clar/status/status_data.h b/tests-clar/status/status_data.h
index 1a68648..3957768 100644
--- a/tests-clar/status/status_data.h
+++ b/tests-clar/status/status_data.h
@@ -8,6 +8,8 @@ struct status_entry_counts {
int expected_entry_count;
};
+/* entries for a plain copy of tests/resources/status */
+
static const char *entry_paths0[] = {
"file_deleted",
"ignored_file",
@@ -48,3 +50,96 @@ static const unsigned int entry_statuses0[] = {
static const size_t entry_count0 = 15;
+/* entries for a copy of tests/resources/status with all content
+ * deleted from the working directory
+ */
+
+static const char *entry_paths2[] = {
+ "current_file",
+ "file_deleted",
+ "modified_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",
+ "subdir.txt",
+ "subdir/current_file",
+ "subdir/deleted_file",
+ "subdir/modified_file",
+};
+
+static const unsigned int entry_statuses2[] = {
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+};
+
+static const size_t entry_count2 = 15;
+
+/* entries for a copy of tests/resources/status with some mods */
+
+static const char *entry_paths3[] = {
+ ".HEADER",
+ "42-is-not-prime.sigh",
+ "README.md",
+ "current_file",
+ "current_file/",
+ "file_deleted",
+ "ignored_file",
+ "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",
+ "subdir",
+ "subdir/current_file",
+ "subdir/deleted_file",
+ "subdir/modified_file",
+};
+
+static const unsigned int entry_statuses3[] = {
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_IGNORED,
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_WT_NEW | GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_WT_DELETED | GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+ GIT_STATUS_WT_DELETED,
+};
+
+static const size_t entry_count3 = 21;
diff --git a/tests-clar/status/submodules.c b/tests-clar/status/submodules.c
new file mode 100644
index 0000000..ca6c2ef
--- /dev/null
+++ b/tests-clar/status/submodules.c
@@ -0,0 +1,104 @@
+#include "clar_libgit2.h"
+#include "buffer.h"
+#include "path.h"
+#include "posix.h"
+
+static git_repository *g_repo = NULL;
+
+void test_status_submodules__initialize(void)
+{
+ git_buf modpath = GIT_BUF_INIT;
+
+ g_repo = cl_git_sandbox_init("submodules");
+
+ cl_fixture_sandbox("testrepo.git");
+
+ cl_git_pass(git_buf_sets(&modpath, git_repository_workdir(g_repo)));
+ cl_assert(git_path_dirname_r(&modpath, modpath.ptr) >= 0);
+ cl_git_pass(git_buf_joinpath(&modpath, modpath.ptr, "testrepo.git\n"));
+
+ p_rename("submodules/gitmodules", "submodules/.gitmodules");
+ cl_git_append2file("submodules/.gitmodules", modpath.ptr);
+
+ p_rename("submodules/testrepo/.gitted", "submodules/testrepo/.git");
+}
+
+void test_status_submodules__cleanup(void)
+{
+ cl_git_sandbox_cleanup();
+}
+
+static int
+cb_status__count(const char *p, unsigned int s, void *payload)
+{
+ volatile int *count = (int *)payload;
+
+ GIT_UNUSED(p);
+ GIT_UNUSED(s);
+
+ (*count)++;
+
+ return 0;
+}
+
+void test_status_submodules__0(void)
+{
+ int counts = 0;
+
+ cl_assert(git_path_isdir("submodules/.git"));
+ cl_assert(git_path_isdir("submodules/testrepo/.git"));
+ cl_assert(git_path_isfile("submodules/.gitmodules"));
+
+ cl_git_pass(
+ git_status_foreach(g_repo, cb_status__count, &counts)
+ );
+
+ cl_assert(counts == 7);
+}
+
+static const char *expected_files[] = {
+ ".gitmodules",
+ "added",
+ "deleted",
+ "ignored",
+ "modified",
+ "testrepo",
+ "untracked"
+};
+
+static unsigned int expected_status[] = {
+ GIT_STATUS_INDEX_NEW | GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_INDEX_NEW,
+ GIT_STATUS_INDEX_DELETED,
+ GIT_STATUS_IGNORED,
+ GIT_STATUS_WT_MODIFIED,
+ GIT_STATUS_INDEX_NEW, /* submodule added in index, but not committed */
+ GIT_STATUS_WT_NEW
+};
+
+static int
+cb_status__match(const char *p, unsigned int s, void *payload)
+{
+ volatile int *index = (int *)payload;
+
+ cl_assert_strequal(expected_files[*index], p);
+ cl_assert(expected_status[*index] == s);
+ (*index)++;
+
+ return 0;
+}
+
+void test_status_submodules__1(void)
+{
+ int index = 0;
+
+ cl_assert(git_path_isdir("submodules/.git"));
+ cl_assert(git_path_isdir("submodules/testrepo/.git"));
+ cl_assert(git_path_isfile("submodules/.gitmodules"));
+
+ cl_git_pass(
+ git_status_foreach(g_repo, cb_status__match, &index)
+ );
+
+ cl_assert(index == 7);
+}
diff --git a/tests-clar/status/worktree.c b/tests-clar/status/worktree.c
index 98bb2b8..9ddb7d1 100644
--- a/tests-clar/status/worktree.c
+++ b/tests-clar/status/worktree.c
@@ -3,6 +3,8 @@
#include "ignore.h"
#include "status_data.h"
#include "posix.h"
+#include "util.h"
+#include "path.h"
/**
* Auxiliary methods
@@ -67,6 +69,7 @@ void test_status_worktree__cleanup(void)
/**
* Tests - Status determination on a working tree
*/
+/* this test is equivalent to t18-status.c:statuscb0 */
void test_status_worktree__whole_repository(void)
{
struct status_entry_counts counts;
@@ -86,6 +89,7 @@ void test_status_worktree__whole_repository(void)
cl_assert(counts.wrong_sorted_path == 0);
}
+/* this test is equivalent to t18-status.c:statuscb1 */
void test_status_worktree__empty_repository(void)
{
int count = 0;
@@ -96,6 +100,81 @@ void test_status_worktree__empty_repository(void)
cl_assert(count == 0);
}
+static int remove_file_cb(void *data, git_buf *file)
+{
+ const char *filename = git_buf_cstr(file);
+
+ GIT_UNUSED(data);
+
+ if (git__suffixcmp(filename, ".git") == 0)
+ return 0;
+
+ if (git_path_isdir(filename))
+ cl_git_pass(git_futils_rmdir_r(filename, 1));
+ else
+ cl_git_pass(p_unlink(git_buf_cstr(file)));
+
+ return 0;
+}
+
+/* this test is equivalent to t18-status.c:statuscb2 */
+void test_status_worktree__purged_worktree(void)
+{
+ struct status_entry_counts counts;
+ git_repository *repo = cl_git_sandbox_init("status");
+ git_buf workdir = GIT_BUF_INIT;
+
+ /* first purge the contents of the worktree */
+ cl_git_pass(git_buf_sets(&workdir, git_repository_workdir(repo)));
+ cl_git_pass(git_path_direach(&workdir, remove_file_cb, NULL));
+
+ /* now get status */
+ memset(&counts, 0x0, sizeof(struct status_entry_counts));
+ counts.expected_entry_count = entry_count2;
+ counts.expected_paths = entry_paths2;
+ counts.expected_statuses = entry_statuses2;
+
+ cl_git_pass(
+ git_status_foreach(repo, cb_status__normal, &counts)
+ );
+
+ cl_assert(counts.entry_count == counts.expected_entry_count);
+ cl_assert(counts.wrong_status_flags_count == 0);
+ cl_assert(counts.wrong_sorted_path == 0);
+}
+
+/* this test is equivalent to t18-status.c:statuscb3 */
+void test_status_worktree__swap_subdir_and_file(void)
+{
+ struct status_entry_counts counts;
+ git_repository *repo = cl_git_sandbox_init("status");
+
+ /* first alter the contents of the worktree */
+ cl_git_pass(p_rename("status/current_file", "status/swap"));
+ cl_git_pass(p_rename("status/subdir", "status/current_file"));
+ cl_git_pass(p_rename("status/swap", "status/subdir"));
+
+ cl_git_mkfile("status/.HEADER", "dummy");
+ cl_git_mkfile("status/42-is-not-prime.sigh", "dummy");
+ cl_git_mkfile("status/README.md", "dummy");
+
+ /* now get status */
+ memset(&counts, 0x0, sizeof(struct status_entry_counts));
+ counts.expected_entry_count = entry_count3;
+ counts.expected_paths = entry_paths3;
+ counts.expected_statuses = entry_statuses3;
+
+ cl_git_pass(
+ git_status_foreach(repo, cb_status__normal, &counts)
+ );
+
+ cl_assert(counts.entry_count == counts.expected_entry_count);
+ cl_assert(counts.wrong_status_flags_count == 0);
+ cl_assert(counts.wrong_sorted_path == 0);
+
+}
+
+/* this test is equivalent to t18-status.c:singlestatus0 */
void test_status_worktree__single_file(void)
{
int i;
@@ -110,6 +189,19 @@ void test_status_worktree__single_file(void)
}
}
+/* this test is equivalent to t18-status.c:singlestatus1 */
+void test_status_worktree__single_nonexistent_file(void)
+{
+ int error;
+ unsigned int status_flags;
+ git_repository *repo = cl_git_sandbox_init("status");
+
+ error = git_status_file(&status_flags, repo, "nonexistent");
+ cl_git_fail(error);
+ cl_assert(error == GIT_ENOTFOUND);
+}
+
+
void test_status_worktree__ignores(void)
{
int i, ignored;
diff --git a/tests/t18-status.c b/tests/t18-status.c
index bfd6906..8abff98 100644
--- a/tests/t18-status.c
+++ b/tests/t18-status.c
@@ -261,9 +261,7 @@ static const char *entry_paths3[] = {
"42-is-not-prime.sigh",
"README.md",
"current_file",
- "current_file/current_file",
- "current_file/modified_file",
- "current_file/new_file",
+ "current_file/",
"file_deleted",
"ignored_file",
"modified_file",
@@ -288,8 +286,6 @@ static const unsigned int entry_statuses3[] = {
GIT_STATUS_WT_NEW,
GIT_STATUS_WT_DELETED,
GIT_STATUS_WT_NEW,
- GIT_STATUS_WT_NEW,
- GIT_STATUS_WT_NEW,
GIT_STATUS_WT_DELETED,
GIT_STATUS_IGNORED,
GIT_STATUS_WT_MODIFIED,
@@ -308,7 +304,7 @@ static const unsigned int entry_statuses3[] = {
GIT_STATUS_WT_DELETED,
};
-#define ENTRY_COUNT3 23
+#define ENTRY_COUNT3 21
BEGIN_TEST(statuscb3, "test retrieving status for a worktree where a file and a subdir have been renamed and some files have been added")
git_repository *repo;