stats.c 8.65 KB
Newer Older
1 2 3 4 5 6 7
#include "clar.h"
#include "clar_libgit2.h"

#include "buffer.h"
#include "commit.h"
#include "diff.h"

8 9
static git_repository *_repo;
static git_diff_stats *_stats;
10 11 12

void test_diff_stats__initialize(void)
{
13
	_repo = cl_git_sandbox_init("diff_format_email");
14 15 16 17
}

void test_diff_stats__cleanup(void)
{
18
	git_diff_stats_free(_stats); _stats = NULL;
19 20 21
	cl_git_sandbox_cleanup();
}

22 23
static void diff_stats_from_commit_oid(
	git_diff_stats **stats, const char *oidstr, bool rename)
24 25
{
	git_oid oid;
26 27 28 29 30 31 32 33 34 35 36 37 38
	git_commit *commit;
	git_diff *diff;

	git_oid_fromstr(&oid, oidstr);
	cl_git_pass(git_commit_lookup(&commit, _repo, &oid));
	cl_git_pass(git_diff__commit(&diff, _repo, commit, NULL));
	if (rename)
		cl_git_pass(git_diff_find_similar(diff, NULL));
	cl_git_pass(git_diff_get_stats(stats, diff));

	git_diff_free(diff);
	git_commit_free(commit);
}
39

40 41 42
void test_diff_stats__stat(void)
{
	git_buf buf = GIT_BUF_INIT;
43 44 45 46
	const char *stat =
	" file1.txt | 8 +++++---\n" \
	" 1 file changed, 5 insertions(+), 3 deletions(-)\n";

47 48
	diff_stats_from_commit_oid(
		&_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
49

50 51 52
	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(5, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(3, git_diff_stats_deletions(_stats));
53

54
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
55 56
	cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
	git_buf_free(&buf);
57 58 59 60

	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 80));
	cl_assert(strcmp(git_buf_cstr(&buf), stat) == 0);
	git_buf_free(&buf);
61 62 63 64 65 66 67 68 69 70
}

void test_diff_stats__multiple_hunks(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt | 5 +++--\n" \
	" file3.txt | 6 ++++--\n" \
	" 2 files changed, 7 insertions(+), 4 deletions(-)\n";

71 72
	diff_stats_from_commit_oid(
		&_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
73

74 75 76
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(7, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(4, git_diff_stats_deletions(_stats));
77

78 79
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
80 81 82 83 84 85 86 87 88 89
	git_buf_free(&buf);
}

void test_diff_stats__numstat(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	"3       2       file2.txt\n"
	"4       2       file3.txt\n";

90 91
	diff_stats_from_commit_oid(
		&_stats, "cd471f0d8770371e1bc78bcbb38db4c7e4106bd2", false);
92

93 94
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
95 96 97 98 99 100 101 102 103
	git_buf_free(&buf);
}

void test_diff_stats__shortstat(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" 1 file changed, 5 insertions(+), 3 deletions(-)\n";

104 105
	diff_stats_from_commit_oid(
		&_stats, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", false);
106

107 108 109
	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(5, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(3, git_diff_stats_deletions(_stats));
110

111 112
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_SHORT, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
113 114 115 116 117 118 119 120 121
	git_buf_free(&buf);
}

void test_diff_stats__rename(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt => file2.txt.renamed | 1 +\n"
	" file3.txt => file3.txt.renamed | 4 +++-\n"
122
	" 2 files changed, 4 insertions(+), 1 deletion(-)\n";
123

124 125
	diff_stats_from_commit_oid(
		&_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", true);
126

127 128 129
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(4, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(1, git_diff_stats_deletions(_stats));
130

131 132
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
133 134 135 136 137 138 139 140 141 142 143
	git_buf_free(&buf);
}

void test_diff_stats__rename_nochanges(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed => file2.txt.renamed2 | 0\n"
	" file3.txt.renamed => file3.txt.renamed2 | 0\n"
	" 2 files changed, 0 insertions(+), 0 deletions(-)\n";

144 145
	diff_stats_from_commit_oid(
		&_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", true);
146

147 148 149
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
150

151 152
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
153 154 155 156 157 158 159 160 161
	git_buf_free(&buf);
}

void test_diff_stats__rename_and_modifiy(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed2                      | 2 +-\n"
	" file3.txt.renamed2 => file3.txt.renamed | 0\n"
162
	" 2 files changed, 1 insertion(+), 1 deletion(-)\n";
163

164 165
	diff_stats_from_commit_oid(
		&_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", true);
166

167 168 169
	cl_assert_equal_sz(2, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(1, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(1, git_diff_stats_deletions(_stats));
170

171 172
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
173 174 175 176 177 178 179 180 181 182 183 184 185
	git_buf_free(&buf);
}

void test_diff_stats__rename_no_find(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt         | 5 -----\n"
	" file2.txt.renamed | 6 ++++++\n"
	" file3.txt         | 5 -----\n"
	" file3.txt.renamed | 7 +++++++\n"
	" 4 files changed, 13 insertions(+), 10 deletions(-)\n";

186 187
	diff_stats_from_commit_oid(
		&_stats, "8947a46e2097638ca6040ad4877246f4186ec3bd", false);
188

189 190 191
	cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(13, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(10, git_diff_stats_deletions(_stats));
192

193 194
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
195 196 197 198 199 200 201 202 203 204 205 206 207
	git_buf_free(&buf);
}

void test_diff_stats__rename_nochanges_no_find(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed  | 6 ------\n"
	" file2.txt.renamed2 | 6 ++++++\n"
	" file3.txt.renamed  | 7 -------\n"
	" file3.txt.renamed2 | 7 +++++++\n"
	" 4 files changed, 13 insertions(+), 13 deletions(-)\n";

208 209
	diff_stats_from_commit_oid(
		&_stats, "3991dce9e71a0641ca49a6a4eea6c9e7ff402ed4", false);
210

211 212 213
	cl_assert_equal_sz(4, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(13, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(13, git_diff_stats_deletions(_stats));
214

215 216
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
217 218 219 220 221 222 223 224 225 226 227 228
	git_buf_free(&buf);
}

void test_diff_stats__rename_and_modifiy_no_find(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file2.txt.renamed2 | 2 +-\n"
	" file3.txt.renamed  | 7 +++++++\n"
	" file3.txt.renamed2 | 7 -------\n"
	" 3 files changed, 8 insertions(+), 8 deletions(-)\n";

229 230
	diff_stats_from_commit_oid(
		&_stats, "4ca10087e696d2ba78d07b146a118e9a7096ed4f", false);
231

232 233 234
	cl_assert_equal_sz(3, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(8, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(8, git_diff_stats_deletions(_stats));
235

236 237
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
238 239 240 241 242 243 244 245 246
	git_buf_free(&buf);
}

void test_diff_stats__binary(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" binary.bin | Bin 3 -> 0 bytes\n"
	" 1 file changed, 0 insertions(+), 0 deletions(-)\n";
247
	/* TODO: Actually 0 bytes here should be 5!. Seems like we don't load the new content for binary files? */
248

249 250
	diff_stats_from_commit_oid(
		&_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
251

252 253 254
	cl_assert_equal_sz(1, git_diff_stats_files_changed(_stats));
	cl_assert_equal_sz(0, git_diff_stats_insertions(_stats));
	cl_assert_equal_sz(0, git_diff_stats_deletions(_stats));
255

256 257
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
258 259 260 261 262 263 264 265 266
	git_buf_free(&buf);
}

void test_diff_stats__binary_numstat(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	"-       -       binary.bin\n";

267 268
	diff_stats_from_commit_oid(
		&_stats, "8d7523f6fcb2404257889abe0d96f093d9f524f9", false);
269

270 271
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_NUMBER, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
272 273 274 275 276 277 278 279 280
	git_buf_free(&buf);
}

void test_diff_stats__mode_change(void)
{
	git_buf buf = GIT_BUF_INIT;
	const char *stat =
	" file1.txt.renamed | 0\n" \
	" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
281
		" mode change 100644 => 100755 file1.txt.renamed\n";
282

283 284
	diff_stats_from_commit_oid(
		&_stats, "7ade76dd34bba4733cf9878079f9fd4a456a9189", false);
285

286 287
	cl_git_pass(git_diff_stats_to_buf(&buf, _stats, GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY, 0));
	cl_assert_equal_s(stat, git_buf_cstr(&buf));
288 289
	git_buf_free(&buf);
}