aboutsummaryrefslogtreecommitdiffstats
path: root/common_test.go
blob: f054f23bdcca614f54a18bde5f2ceb956590a1e1 (plain) (blame)
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
package git

import (
	"bytes"
	"errors"
	"io"
	"io/ioutil"
	"os"
	"testing"

	"gopkg.in/src-d/go-git.v4/clients"
	"gopkg.in/src-d/go-git.v4/clients/common"
	"gopkg.in/src-d/go-git.v4/core"
	"gopkg.in/src-d/go-git.v4/formats/packfile"

	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type BaseSuite struct {
	Repository *Repository
}

func (s *BaseSuite) SetUpSuite(c *C) {
	clients.InstallProtocol("mock", func(end common.Endpoint) common.GitUploadPackService {
		return &MockGitUploadPackService{endpoint: end}
	})

	s.Repository = NewMemoryRepository()
	err := s.Repository.Clone(&CloneOptions{URL: RepositoryFixture})
	c.Assert(err, IsNil)
}

const RepositoryFixture = "mock://formats/packfile/fixtures/git-fixture.ref-delta"

type MockGitUploadPackService struct {
	connected bool
	endpoint  common.Endpoint
	auth      common.AuthMethod
}

func (p *MockGitUploadPackService) Connect() error {
	p.connected = true
	return nil
}

func (p *MockGitUploadPackService) SetAuth(auth common.AuthMethod) error {
	p.auth = auth
	return nil
}

func (p *MockGitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
	if !p.connected {
		return nil, errors.New("not connected")
	}

	h := core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")

	c := common.NewCapabilities()
	c.Decode("6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEADmulti_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed no-done symref=HEAD:refs/heads/master agent=git/2:2.4.8~dbussink-fix-enterprise-tokens-compilation-1167-gc7006cf")

	ref := core.ReferenceName("refs/heads/master")
	branch := core.ReferenceName("refs/heads/branch")
	tag := core.ReferenceName("refs/tags/v1.0.0")
	return &common.GitUploadPackInfo{
		Capabilities: c,
		Refs: map[core.ReferenceName]*core.Reference{
			core.HEAD: core.NewSymbolicReference(core.HEAD, ref),
			ref:       core.NewHashReference(ref, h),
			tag:       core.NewHashReference(tag, h),
			branch:    core.NewHashReference(branch, core.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881")),
		},
	}, nil
}

func (p *MockGitUploadPackService) Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error) {
	if !p.connected {
		return nil, errors.New("not connected")
	}

	if len(r.Wants) == 1 {
		return os.Open("formats/packfile/fixtures/git-fixture.ref-delta")
	}

	return os.Open("fixtures/pack-63bbc2e1bde392e2205b30fa3584ddb14ef8bd41.pack")
}

func (p *MockGitUploadPackService) Disconnect() error {
	p.connected = false
	return nil
}

type packedFixture struct {
	url      string
	packfile string
}

var fixtureRepos = []packedFixture{
	{"https://github.com/tyba/git-fixture.git", "formats/packfile/fixtures/git-fixture.ofs-delta"},
	{"https://github.com/jamesob/desk.git", "formats/packfile/fixtures/jamesob-desk.pack"},
	{"https://github.com/spinnaker/spinnaker.git", "formats/packfile/fixtures/spinnaker-spinnaker.pack"},
}

func unpackFixtures(c *C, fixtures ...[]packedFixture) map[string]*Repository {
	repos := make(map[string]*Repository, 0)
	for _, group := range fixtures {
		for _, fixture := range group {
			if _, existing := repos[fixture.url]; existing {
				continue
			}

			comment := Commentf("fixture packfile: %q", fixture.packfile)

			repos[fixture.url] = NewMemoryRepository()

			f, err := os.Open(fixture.packfile)
			c.Assert(err, IsNil, comment)

			// increase memory consumption to speed up tests
			data, err := ioutil.ReadAll(f)
			c.Assert(err, IsNil)
			memStream := bytes.NewReader(data)
			r := packfile.NewStream(memStream)

			d := packfile.NewDecoder(r, repos[fixture.url].s.ObjectStorage())
			err = d.Decode()
			c.Assert(err, IsNil, comment)

			c.Assert(f.Close(), IsNil, comment)
		}
	}

	return repos
}

type SuiteCommon struct{}

var _ = Suite(&SuiteCommon{})

var countLinesTests = [...]struct {
	i string // the string we want to count lines from
	e int    // the expected number of lines in i
}{
	{"", 0},
	{"a", 1},
	{"a\n", 1},
	{"a\nb", 2},
	{"a\nb\n", 2},
	{"a\nb\nc", 3},
	{"a\nb\nc\n", 3},
	{"a\n\n\nb\n", 4},
	{"first line\n\tsecond line\nthird line\n", 3},
}

func (s *SuiteCommon) TestCountLines(c *C) {
	for i, t := range countLinesTests {
		o := countLines(t.i)
		c.Assert(o, Equals, t.e, Commentf("subtest %d, input=%q", i, t.i))
	}
}