]> git.proxmox.com Git - libgit2.git/blame - tests/iterator/iterator_helpers.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / iterator / iterator_helpers.c
CommitLineData
de034cd2
ET
1#include "clar_libgit2.h"
2#include "iterator.h"
3#include "repository.h"
22a2d3d5 4#include "futils.h"
de034cd2
ET
5#include "iterator_helpers.h"
6#include <stdarg.h>
7
8static void assert_at_end(git_iterator *i, bool verbose)
9{
10 const git_index_entry *end;
11 int error = git_iterator_advance(&end, i);
12
13 if (verbose && error != GIT_ITEROVER)
14 fprintf(stderr, "Expected end of iterator, got '%s'\n", end->path);
15
16 cl_git_fail_with(GIT_ITEROVER, error);
17}
18
19void expect_iterator_items(
20 git_iterator *i,
498d0801 21 size_t expected_flat,
de034cd2 22 const char **expected_flat_paths,
498d0801 23 size_t expected_total,
de034cd2
ET
24 const char **expected_total_paths)
25{
26 const git_index_entry *entry;
498d0801 27 size_t count;
de034cd2
ET
28 int no_trees = !(git_iterator_flags(i) & GIT_ITERATOR_INCLUDE_TREES);
29 bool v = false;
498d0801 30 int error;
de034cd2
ET
31
32 if (v) fprintf(stderr, "== %s ==\n", no_trees ? "notrees" : "trees");
33
34 count = 0;
35
36 while (!git_iterator_advance(&entry, i)) {
37 if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
38
39 if (no_trees)
40 cl_assert(entry->mode != GIT_FILEMODE_TREE);
41
42 if (expected_flat_paths) {
43 const char *expect_path = expected_flat_paths[count];
44 size_t expect_len = strlen(expect_path);
45
46 cl_assert_equal_s(expect_path, entry->path);
47
48 if (expect_path[expect_len - 1] == '/')
49 cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
50 else
51 cl_assert(entry->mode != GIT_FILEMODE_TREE);
52 }
53
eae0bfdc 54 cl_assert(++count <= expected_flat);
de034cd2
ET
55 }
56
57 assert_at_end(i, v);
58 cl_assert_equal_i(expected_flat, count);
59
60 cl_git_pass(git_iterator_reset(i));
61
62 count = 0;
63 cl_git_pass(git_iterator_current(&entry, i));
64
65 if (v) fprintf(stderr, "-- %s --\n", no_trees ? "notrees" : "trees");
66
67 while (entry != NULL) {
68 if (v) fprintf(stderr, " %s %07o\n", entry->path, (int)entry->mode);
69
70 if (no_trees)
71 cl_assert(entry->mode != GIT_FILEMODE_TREE);
72
73 if (expected_total_paths) {
74 const char *expect_path = expected_total_paths[count];
75 size_t expect_len = strlen(expect_path);
76
77 cl_assert_equal_s(expect_path, entry->path);
78
79 if (expect_path[expect_len - 1] == '/')
80 cl_assert_equal_i(GIT_FILEMODE_TREE, entry->mode);
81 else
82 cl_assert(entry->mode != GIT_FILEMODE_TREE);
83 }
84
85 if (entry->mode == GIT_FILEMODE_TREE) {
86 error = git_iterator_advance_into(&entry, i);
87
88 /* could return NOTFOUND if directory is empty */
89 cl_assert(!error || error == GIT_ENOTFOUND);
90
91 if (error == GIT_ENOTFOUND) {
92 error = git_iterator_advance(&entry, i);
93 cl_assert(!error || error == GIT_ITEROVER);
94 }
95 } else {
96 error = git_iterator_advance(&entry, i);
97 cl_assert(!error || error == GIT_ITEROVER);
98 }
99
100 if (++count >= expected_total)
101 break;
102 }
103
104 assert_at_end(i, v);
105 cl_assert_equal_i(expected_total, count);
106}
107
0ef0b71c
ET
108
109void expect_advance_over(
110 git_iterator *i,
111 const char *expected_path,
112 git_iterator_status_t expected_status)
113{
114 const git_index_entry *entry;
115 git_iterator_status_t status;
116 int error;
117
118 cl_git_pass(git_iterator_current(&entry, i));
119 cl_assert_equal_s(expected_path, entry->path);
120
121 error = git_iterator_advance_over(&entry, &status, i);
122 cl_assert(!error || error == GIT_ITEROVER);
123 cl_assert_equal_i(expected_status, status);
124}
125
126void expect_advance_into(
127 git_iterator *i,
128 const char *expected_path)
129{
130 const git_index_entry *entry;
131 int error;
132
133 cl_git_pass(git_iterator_current(&entry, i));
134 cl_assert_equal_s(expected_path, entry->path);
135
136 if (S_ISDIR(entry->mode))
137 error = git_iterator_advance_into(&entry, i);
138 else
139 error = git_iterator_advance(&entry, i);
140
141 cl_assert(!error || error == GIT_ITEROVER);
142}
143