aboutsummaryrefslogtreecommitdiffstats
path: root/storage/transactional/reference_test.go
blob: a6bd1ce2f65e38e2508af6a235eb6f69ffae6c70 (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
package transactional

import (
	. "gopkg.in/check.v1"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/go-git/go-git/v5/storage/memory"
)

var _ = Suite(&ReferenceSuite{})

type ReferenceSuite struct{}

func (s *ReferenceSuite) TestReference(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	rs := NewReferenceStorage(base, temporal)

	refA := plumbing.NewReferenceFromStrings("refs/a", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
	refB := plumbing.NewReferenceFromStrings("refs/b", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")

	err := base.SetReference(refA)
	c.Assert(err, IsNil)

	err = rs.SetReference(refB)
	c.Assert(err, IsNil)

	_, err = rs.Reference("refs/a")
	c.Assert(err, IsNil)

	_, err = rs.Reference("refs/b")
	c.Assert(err, IsNil)

	_, err = base.Reference("refs/b")
	c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
}

func (s *ReferenceSuite) TestRemoveReferenceTemporal(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	ref := plumbing.NewReferenceFromStrings("refs/a", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")

	rs := NewReferenceStorage(base, temporal)
	err := rs.SetReference(ref)
	c.Assert(err, IsNil)

	err = rs.RemoveReference("refs/a")
	c.Assert(err, IsNil)

	_, err = rs.Reference("refs/a")
	c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
}

func (s *ReferenceSuite) TestRemoveReferenceBase(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	ref := plumbing.NewReferenceFromStrings("refs/a", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")

	rs := NewReferenceStorage(base, temporal)
	err := base.SetReference(ref)
	c.Assert(err, IsNil)

	err = rs.RemoveReference("refs/a")
	c.Assert(err, IsNil)

	_, err = rs.Reference("refs/a")
	c.Assert(err, Equals, plumbing.ErrReferenceNotFound)
}

func (s *ReferenceSuite) TestCheckAndSetReferenceInBase(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()
	rs := NewReferenceStorage(base, temporal)

	err := base.SetReference(
		plumbing.NewReferenceFromStrings("foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"),
	)
	c.Assert(err, IsNil)

	err = rs.CheckAndSetReference(
		plumbing.NewReferenceFromStrings("foo", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52"),
		plumbing.NewReferenceFromStrings("foo", "482e0eada5de4039e6f216b45b3c9b683b83bfa"),
	)
	c.Assert(err, IsNil)

	e, err := rs.Reference(plumbing.ReferenceName("foo"))
	c.Assert(err, IsNil)
	c.Assert(e.Hash().String(), Equals, "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
}

func (s *ReferenceSuite) TestCommit(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	refA := plumbing.NewReferenceFromStrings("refs/a", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
	refB := plumbing.NewReferenceFromStrings("refs/b", "b66c08ba28aa1f81eb06a1127aa3936ff77e5e2c")
	refC := plumbing.NewReferenceFromStrings("refs/c", "c3f4688a08fd86f1bf8e055724c84b7a40a09733")

	rs := NewReferenceStorage(base, temporal)
	c.Assert(rs.SetReference(refA), IsNil)
	c.Assert(rs.SetReference(refB), IsNil)
	c.Assert(rs.SetReference(refC), IsNil)

	err := rs.Commit()
	c.Assert(err, IsNil)

	iter, err := base.IterReferences()
	c.Assert(err, IsNil)

	var count int
	iter.ForEach(func(ref *plumbing.Reference) error {
		count++
		return nil
	})

	c.Assert(count, Equals, 3)
}

func (s *ReferenceSuite) TestCommitDelete(c *C) {
	base := memory.NewStorage()
	temporal := memory.NewStorage()

	refA := plumbing.NewReferenceFromStrings("refs/a", "bc9968d75e48de59f0870ffb71f5e160bbbdcf52")
	refB := plumbing.NewReferenceFromStrings("refs/b", "b66c08ba28aa1f81eb06a1127aa3936ff77e5e2c")
	refC := plumbing.NewReferenceFromStrings("refs/c", "c3f4688a08fd86f1bf8e055724c84b7a40a09733")

	rs := NewReferenceStorage(base, temporal)
	c.Assert(base.SetReference(refA), IsNil)
	c.Assert(base.SetReference(refB), IsNil)
	c.Assert(base.SetReference(refC), IsNil)

	c.Assert(rs.RemoveReference(refA.Name()), IsNil)
	c.Assert(rs.RemoveReference(refB.Name()), IsNil)
	c.Assert(rs.RemoveReference(refC.Name()), IsNil)
	c.Assert(rs.SetReference(refC), IsNil)

	err := rs.Commit()
	c.Assert(err, IsNil)

	iter, err := base.IterReferences()
	c.Assert(err, IsNil)

	var count int
	iter.ForEach(func(ref *plumbing.Reference) error {
		count++
		return nil
	})

	c.Assert(count, Equals, 1)

	ref, err := rs.Reference(refC.Name())
	c.Assert(err, IsNil)
	c.Assert(ref.Hash().String(), Equals, "c3f4688a08fd86f1bf8e055724c84b7a40a09733")

}