Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6eb9e39

Browse files
committed
push: move main test function to git_remote_push()
We have the step-by-step method in the initialization function as we want to remove references based on the list of references which are already there, and we can use the convenience function for testing the main push.
1 parent 3149547 commit 6eb9e39

File tree

3 files changed

+80
-59
lines changed

3 files changed

+80
-59
lines changed

tests/online/push.c

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -89,46 +89,38 @@ static int cred_acquire_cb(
8989
return -1;
9090
}
9191

92-
/* the results of a push status. when used for expected values, msg may be NULL
93-
* to indicate that it should not be matched. */
94-
typedef struct {
95-
const char *ref;
96-
int success;
97-
const char *msg;
98-
} push_status;
99-
10092
/**
10193
* git_push_status_foreach callback that records status entries.
10294
* @param data (git_vector *) of push_status instances
10395
*/
104-
static int record_push_status_cb(const char *ref, const char *msg, void *data)
96+
static int record_push_status_cb(const char *ref, const char *msg, void *payload)
10597
{
106-
git_vector *statuses = (git_vector *)data;
98+
record_callbacks_data *data = (record_callbacks_data *) payload;
10799
push_status *s;
108100

109-
cl_assert(s = git__malloc(sizeof(*s)));
110-
s->ref = ref;
101+
cl_assert(s = git__calloc(1, sizeof(*s)));
102+
if (ref)
103+
cl_assert(s->ref = git__strdup(ref));
111104
s->success = (msg == NULL);
112-
s->msg = msg;
105+
if (msg)
106+
cl_assert(s->msg = git__strdup(msg));
113107

114-
git_vector_insert(statuses, s);
108+
git_vector_insert(&data->statuses, s);
115109

116110
return 0;
117111
}
118112

119-
static void do_verify_push_status(git_push *push, const push_status expected[], const size_t expected_len)
113+
static void do_verify_push_status(record_callbacks_data *data, const push_status expected[], const size_t expected_len)
120114
{
121-
git_vector actual = GIT_VECTOR_INIT;
115+
git_vector *actual = &data->statuses;
122116
push_status *iter;
123117
bool failed = false;
124118
size_t i;
125119

126-
git_push_status_foreach(push, record_push_status_cb, &actual);
127-
128-
if (expected_len != actual.length)
120+
if (expected_len != actual->length)
129121
failed = true;
130122
else
131-
git_vector_foreach(&actual, i, iter)
123+
git_vector_foreach(actual, i, iter)
132124
if (strcmp(expected[i].ref, iter->ref) ||
133125
(expected[i].success != iter->success) ||
134126
(expected[i].msg && (!iter->msg || strcmp(expected[i].msg, iter->msg)))) {
@@ -149,7 +141,7 @@ static void do_verify_push_status(git_push *push, const push_status expected[],
149141

150142
git_buf_puts(&msg, "\nACTUAL:\n");
151143

152-
git_vector_foreach(&actual, i, iter) {
144+
git_vector_foreach(actual, i, iter) {
153145
if (iter->success)
154146
git_buf_printf(&msg, "%s: success\n", iter->ref);
155147
else
@@ -161,10 +153,10 @@ static void do_verify_push_status(git_push *push, const push_status expected[],
161153
git_buf_free(&msg);
162154
}
163155

164-
git_vector_foreach(&actual, i, iter)
156+
git_vector_foreach(actual, i, iter)
165157
git__free(iter);
166158

167-
git_vector_free(&actual);
159+
git_vector_free(actual);
168160
}
169161

170162
/**
@@ -431,22 +423,24 @@ void test_online_push__cleanup(void)
431423
static int push_pack_progress_cb(
432424
int stage, unsigned int current, unsigned int total, void* payload)
433425
{
434-
int *calls = (int *)payload;
426+
record_callbacks_data *data = (record_callbacks_data *) payload;
435427
GIT_UNUSED(stage); GIT_UNUSED(current); GIT_UNUSED(total);
436-
if (*calls < 0)
437-
return *calls;
438-
(*calls)++;
428+
if (data->pack_progress_calls < 0)
429+
return data->pack_progress_calls;
430+
431+
data->pack_progress_calls++;
439432
return 0;
440433
}
441434

442435
static int push_transfer_progress_cb(
443436
unsigned int current, unsigned int total, size_t bytes, void* payload)
444437
{
445-
int *calls = (int *)payload;
438+
record_callbacks_data *data = (record_callbacks_data *) payload;
446439
GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
447-
if (*calls < 0)
448-
return *calls;
449-
(*calls)++;
440+
if (data->transfer_progress_calls < 0)
441+
return data->transfer_progress_calls;
442+
443+
data->transfer_progress_calls++;
450444
return 0;
451445
}
452446

@@ -466,62 +460,61 @@ static void do_push(
466460
expected_ref expected_refs[], size_t expected_refs_len,
467461
int expected_ret, int check_progress_cb, int check_update_tips_cb)
468462
{
469-
git_push *push;
470463
git_push_options opts = GIT_PUSH_OPTIONS_INIT;
471464
size_t i;
472-
int pack_progress_calls = 0, transfer_progress_calls = 0;
465+
int error;
466+
git_strarray specs;
473467
git_signature *pusher;
468+
git_remote_callbacks callbacks;
469+
record_callbacks_data *data;
474470

475471
if (_remote) {
476472
/* Auto-detect the number of threads to use */
477473
opts.pb_parallelism = 0;
478474

479475
cl_git_pass(git_signature_now(&pusher, "Foo Bar", "[email protected]"));
480-
cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH));
481476

482-
cl_git_pass(git_push_new(&push, _remote));
483-
cl_git_pass(git_push_set_options(push, &opts));
477+
memcpy(&callbacks, git_remote_get_callbacks(_remote), sizeof(callbacks));
478+
data = callbacks.payload;
484479

485-
if (check_progress_cb) {
486-
/* if EUSER, then abort in transfer */
487-
if (expected_ret == GIT_EUSER)
488-
transfer_progress_calls = GIT_EUSER;
480+
callbacks.pack_progress = push_pack_progress_cb;
481+
callbacks.push_transfer_progress = push_transfer_progress_cb;
482+
callbacks.push_update_reference = record_push_status_cb;
483+
cl_git_pass(git_remote_set_callbacks(_remote, &callbacks));
489484

490-
cl_git_pass(
491-
git_push_set_callbacks(
492-
push, push_pack_progress_cb, &pack_progress_calls,
493-
push_transfer_progress_cb, &transfer_progress_calls));
494-
}
485+
specs.count = refspecs_len;
486+
specs.strings = git__calloc(refspecs_len, sizeof(char *));
487+
cl_assert(specs.strings);
495488

496489
for (i = 0; i < refspecs_len; i++)
497-
cl_git_pass(git_push_add_refspec(push, refspecs[i]));
490+
specs.strings[i] = (char *) refspecs[i];
491+
492+
/* if EUSER, then abort in transfer */
493+
if (check_progress_cb && expected_ret == GIT_EUSER)
494+
data->transfer_progress_calls = GIT_EUSER;
495+
496+
error = git_remote_push(_remote, &specs, &opts, pusher, "test push");
497+
git__free(specs.strings);
498498

499499
if (expected_ret < 0) {
500-
cl_git_fail_with(git_push_finish(push), expected_ret);
501-
cl_assert_equal_i(0, git_push_unpack_ok(push));
500+
cl_git_fail_with(expected_ret, error);
502501
} else {
503-
cl_git_pass(git_push_finish(push));
504-
cl_assert_equal_i(1, git_push_unpack_ok(push));
502+
cl_git_pass(error);
505503
}
506504

507-
if (check_progress_cb && !expected_ret) {
508-
cl_assert(pack_progress_calls > 0);
509-
cl_assert(transfer_progress_calls > 0);
505+
if (check_progress_cb && expected_ret == 0) {
506+
cl_assert(data->pack_progress_calls > 0);
507+
cl_assert(data->transfer_progress_calls > 0);
510508
}
511509

512-
do_verify_push_status(push, expected_statuses, expected_statuses_len);
510+
do_verify_push_status(data, expected_statuses, expected_statuses_len);
513511

514512
verify_refs(_remote, expected_refs, expected_refs_len);
515-
516-
cl_git_pass(git_push_update_tips(push, pusher, "test push"));
517513
verify_tracking_branches(_remote, expected_refs, expected_refs_len);
518514

519515
if (check_update_tips_cb)
520516
verify_update_tips_callback(_remote, expected_refs, expected_refs_len);
521517

522-
git_push_free(push);
523-
524-
git_remote_disconnect(_remote);
525518
git_signature_free(pusher);
526519
}
527520

tests/online/push_util.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,31 @@ void updated_tip_free(updated_tip *t)
1414
git__free(t);
1515
}
1616

17+
void push_status_free(push_status *s)
18+
{
19+
git__free(s->ref);
20+
git__free(s->msg);
21+
git__free(s);
22+
}
23+
1724
void record_callbacks_data_clear(record_callbacks_data *data)
1825
{
1926
size_t i;
2027
updated_tip *tip;
28+
push_status *status;
2129

2230
git_vector_foreach(&data->updated_tips, i, tip)
2331
updated_tip_free(tip);
2432

2533
git_vector_free(&data->updated_tips);
34+
35+
git_vector_foreach(&data->statuses, i, status)
36+
push_status_free(status);
37+
38+
git_vector_free(&data->statuses);
39+
40+
data->pack_progress_calls = 0;
41+
data->transfer_progress_calls = 0;
2642
}
2743

2844
int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)

tests/online/push_util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ typedef struct {
2222

2323
typedef struct {
2424
git_vector updated_tips;
25+
git_vector statuses;
26+
int pack_progress_calls;
27+
int transfer_progress_calls;
2528
} record_callbacks_data;
2629

2730
typedef struct {
2831
const char *name;
2932
const git_oid *oid;
3033
} expected_ref;
3134

35+
/* the results of a push status. when used for expected values, msg may be NULL
36+
* to indicate that it should not be matched. */
37+
typedef struct {
38+
char *ref;
39+
int success;
40+
char *msg;
41+
} push_status;
42+
43+
3244
void updated_tip_free(updated_tip *t);
3345

3446
void record_callbacks_data_clear(record_callbacks_data *data);

0 commit comments

Comments
 (0)