]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/push.c
push: add tests for the push negotiation callback
[libgit2.git] / tests / network / remote / push.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/commit.h"
3
4 static git_remote *_remote;
5 static git_repository *_repo, *_dummy;
6
7 void test_network_remote_push__initialize(void)
8 {
9 cl_fixture_sandbox("testrepo.git");
10 git_repository_open(&_repo, "testrepo.git");
11
12 /* We need a repository to have a remote */
13 cl_git_pass(git_repository_init(&_dummy, "dummy.git", true));
14 cl_git_pass(git_remote_create(&_remote, _dummy, "origin", cl_git_path_url("testrepo.git")));
15 }
16
17 void test_network_remote_push__cleanup(void)
18 {
19 git_remote_free(_remote);
20 _remote = NULL;
21
22 git_repository_free(_repo);
23 _repo = NULL;
24
25 git_repository_free(_dummy);
26 _dummy = NULL;
27
28 cl_fixture_cleanup("testrepo.git");
29 cl_fixture_cleanup("dummy.git");
30 }
31
32 int negotiation_cb(const git_push_update **updates, size_t len, void *payload)
33 {
34 const git_push_update *expected = payload;
35
36 cl_assert_equal_i(1, len);
37 cl_assert_equal_s(expected->src_refname, updates[0]->src_refname);
38 cl_assert_equal_s(expected->dst_refname, updates[0]->dst_refname);
39 cl_assert_equal_oid(&expected->src, &updates[0]->src);
40 cl_assert_equal_oid(&expected->dst, &updates[0]->dst);
41
42 return 0;
43 }
44
45 void test_network_remote_push__delete_notification(void)
46 {
47 git_push_options opts = GIT_PUSH_OPTIONS_INIT;
48 git_reference *ref;
49 git_push_update expected;
50 char *refspec = ":refs/heads/master";
51 const git_strarray refspecs = {
52 &refspec,
53 1,
54 };
55
56 cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/master"));
57
58 expected.src_refname = "";
59 expected.dst_refname = "refs/heads/master";
60 memset(&expected.dst, 0, sizeof(git_oid));
61 git_oid_cpy(&expected.src, git_reference_target(ref));
62
63 opts.callbacks.push_negotiation = negotiation_cb;
64 opts.callbacks.payload = &expected;
65 cl_git_pass(git_remote_push(_remote, &refspecs, &opts));
66
67 git_reference_free(ref);
68 cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, _repo, "refs/heads/master"));
69
70 }
71
72 void create_dummy_commit(git_reference **out, git_repository *repo)
73 {
74 git_index *index;
75 git_oid tree_id, commit_id;
76 git_signature *sig;
77
78 cl_git_pass(git_repository_index(&index, repo));
79 cl_git_pass(git_index_write_tree(&tree_id, index));
80 git_index_free(index);
81
82 cl_git_pass(git_signature_now(&sig, "Pusher Joe", "pjoe"));
83 cl_git_pass(git_commit_create_from_ids(&commit_id, repo, NULL, sig, sig,
84 NULL, "Empty tree\n", &tree_id, 0, NULL));
85 cl_git_pass(git_reference_create(out, repo, "refs/heads/empty-tree", &commit_id, true, "commit yo"));
86 git_signature_free(sig);
87 }
88
89 void test_network_remote_push__create_notification(void)
90 {
91 git_push_options opts = GIT_PUSH_OPTIONS_INIT;
92 git_reference *ref;
93 git_push_update expected;
94 char *refspec = "refs/heads/empty-tree";
95 const git_strarray refspecs = {
96 &refspec,
97 1,
98 };
99
100 create_dummy_commit(&ref, _dummy);
101
102 expected.src_refname = "refs/heads/empty-tree";
103 expected.dst_refname = "refs/heads/empty-tree";
104 git_oid_cpy(&expected.dst, git_reference_target(ref));
105 memset(&expected.src, 0, sizeof(git_oid));
106
107 opts.callbacks.push_negotiation = negotiation_cb;
108 opts.callbacks.payload = &expected;
109 cl_git_pass(git_remote_push(_remote, &refspecs, &opts));
110
111 git_reference_free(ref);
112 cl_git_pass(git_reference_lookup(&ref, _repo, "refs/heads/empty-tree"));
113 git_reference_free(ref);
114 }