summaryrefslogtreecommitdiff
path: root/refs_test.go
blob: cd0287174edf595537721c5d090c3578ba7e7540 (plain)
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
package main

import (
	"bytes"
	"fmt"
	. "gopkg.in/check.v1"
	"sort"

	"github.com/coreos/go-semver/semver"
)

var _ = Suite(&RefsSuite{})

type RefsSuite struct{}

type refsTest struct {
	summary  string
	original string
	version  string
	changed  string
	versions []string
}

var refsTests = []refsTest{{
	"Version v0 works even without any references",
	reflines(
		"hash1 HEAD",
	),
	"v0",
	reflines(
		"hash1 HEAD",
	),
	nil,
}, {
	"Preserve original capabilities",
	reflines(
		"hash1 HEAD\x00caps",
	),
	"v0",
	reflines(
		"hash1 HEAD\x00caps",
	),
	nil,
}, {
	"Matching major version branch",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD",
		"00000000000000000000000000000000000hash2 refs/heads/v0",
		"00000000000000000000000000000000000hash3 refs/heads/v1",
		"00000000000000000000000000000000000hash4 refs/heads/v2",
	),
	"v1",
	reflines(
		"00000000000000000000000000000000000hash3 HEAD\x00symref=HEAD:refs/heads/v1",
		"00000000000000000000000000000000000hash3 refs/heads/master",
		"00000000000000000000000000000000000hash2 refs/heads/v0",
		"00000000000000000000000000000000000hash3 refs/heads/v1",
		"00000000000000000000000000000000000hash4 refs/heads/v2",
	),
	[]string{"v0", "v1", "v2"},
}, {
	"Matching minor version branch",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD",
		"00000000000000000000000000000000000hash2 refs/heads/v1.1",
		"00000000000000000000000000000000000hash3 refs/heads/v1.3",
		"00000000000000000000000000000000000hash4 refs/heads/v1.2",
	),
	"v1",
	reflines(
		"00000000000000000000000000000000000hash3 HEAD\x00symref=HEAD:refs/heads/v1.3",
		"00000000000000000000000000000000000hash3 refs/heads/master",
		"00000000000000000000000000000000000hash2 refs/heads/v1.1",
		"00000000000000000000000000000000000hash3 refs/heads/v1.3",
		"00000000000000000000000000000000000hash4 refs/heads/v1.2",
	),
	[]string{"v1.1", "v1.2", "v1.3"},
}, {
	"Disable original symref capability",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD\x00foo symref=bar baz",
		"00000000000000000000000000000000000hash2 refs/heads/v1",
	),
	"v1",
	reflines(
		"00000000000000000000000000000000000hash2 HEAD\x00symref=HEAD:refs/heads/v1 foo oldref=bar baz",
		"00000000000000000000000000000000000hash2 refs/heads/master",
		"00000000000000000000000000000000000hash2 refs/heads/v1",
	),
	[]string{"v1"},
}, {
	"Replace original master branch",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD",
		"00000000000000000000000000000000000hash1 refs/heads/master",
		"00000000000000000000000000000000000hash2 refs/heads/v1",
	),
	"v1",
	reflines(
		"00000000000000000000000000000000000hash2 HEAD\x00symref=HEAD:refs/heads/v1",
		"00000000000000000000000000000000000hash2 refs/heads/master",
		"00000000000000000000000000000000000hash2 refs/heads/v1",
	),
	[]string{"v1"},
}, {
	"Matching tag",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD",
		"00000000000000000000000000000000000hash2 refs/tags/v0",
		"00000000000000000000000000000000000hash3 refs/tags/v1",
		"00000000000000000000000000000000000hash4 refs/tags/v2",
	),
	"v1",
	reflines(
		"00000000000000000000000000000000000hash3 HEAD",
		"00000000000000000000000000000000000hash3 refs/heads/master",
		"00000000000000000000000000000000000hash2 refs/tags/v0",
		"00000000000000000000000000000000000hash3 refs/tags/v1",
		"00000000000000000000000000000000000hash4 refs/tags/v2",
	),
	[]string{"v0", "v1", "v2"},
}, {
	"Tag peeling",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD",
		"00000000000000000000000000000000000hash2 refs/heads/master",
		"00000000000000000000000000000000000hash3 refs/tags/v1",
		"00000000000000000000000000000000000hash4 refs/tags/v1^{}",
		"00000000000000000000000000000000000hash5 refs/tags/v2",
	),
	"v1",
	reflines(
		"00000000000000000000000000000000000hash4 HEAD",
		"00000000000000000000000000000000000hash4 refs/heads/master",
		"00000000000000000000000000000000000hash3 refs/tags/v1",
		"00000000000000000000000000000000000hash4 refs/tags/v1^{}",
		"00000000000000000000000000000000000hash5 refs/tags/v2",
	),
	[]string{"v1", "v1", "v2"},
}, {
	"Matching unstable versions",
	reflines(
		"00000000000000000000000000000000000hash1 HEAD",
		"00000000000000000000000000000000000hash2 refs/heads/master",
		"00000000000000000000000000000000000hash3 refs/heads/v1",
		"00000000000000000000000000000000000hash4 refs/heads/v1.1-unstable",
		"00000000000000000000000000000000000hash5 refs/heads/v1.3-unstable",
		"00000000000000000000000000000000000hash6 refs/heads/v1.2-unstable",
		"00000000000000000000000000000000000hash7 refs/heads/v2",
	),
	"v1-unstable",
	reflines(
		"00000000000000000000000000000000000hash5 HEAD\x00symref=HEAD:refs/heads/v1.3-unstable",
		"00000000000000000000000000000000000hash5 refs/heads/master",
		"00000000000000000000000000000000000hash3 refs/heads/v1",
		"00000000000000000000000000000000000hash4 refs/heads/v1.1-unstable",
		"00000000000000000000000000000000000hash5 refs/heads/v1.3-unstable",
		"00000000000000000000000000000000000hash6 refs/heads/v1.2-unstable",
		"00000000000000000000000000000000000hash7 refs/heads/v2",
	),
	[]string{"v1", "v1.1-unstable", "v1.2-unstable", "v1.3-unstable", "v2"},
}}

func reflines(lines ...string) string {
	var buf bytes.Buffer
	buf.WriteString("001e# service=git-upload-pack\n0000")
	for _, l := range lines {
		buf.WriteString(fmt.Sprintf("%04x%s\n", len(l)+5, l))
	}
	buf.WriteString("0000")
	return buf.String()
}

func (s *RefsSuite) TestChangeRefs(c *C) {
	for _, test := range refsTests {
		c.Logf(test.summary)

		v, err := semver.NewVersion(test.version)
		if err != nil {
			c.Fatalf("Test has an invalid version: %q: %v", test.version, err)
		}

		changed, versions, err := changeRefs([]byte(test.original), v)
		c.Assert(err, IsNil)

		c.Assert(string(changed), Equals, test.changed)

		sort.Sort(versions)

		var vs []string
		for _, v := range versions {
			vs = append(vs, v.String())
		}
		c.Assert(vs, DeepEquals, test.versions)
	}
}