| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/go-git/go-git/v5/plumbing" |
| 7 |
. "gopkg.in/check.v1" |
| 8 |
) |
| 9 |
|
| 10 |
type RefSpecSuite struct{} |
| 11 |
|
| 12 |
var _ = Suite(&RefSpecSuite{}) |
| 13 |
|
| 14 |
func Test(t *testing.T) { TestingT(t) } |
| 15 |
|
| 16 |
func (s *RefSpecSuite) TestRefSpecIsValid(c *C) { |
| 17 |
spec := RefSpec("+refs/heads/*:refs/remotes/origin/*") |
| 18 |
c.Assert(spec.Validate(), Equals, nil) |
| 19 |
|
| 20 |
spec = RefSpec("refs/heads/*:refs/remotes/origin/") |
| 21 |
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedWildcard) |
| 22 |
|
| 23 |
spec = RefSpec("refs/heads/master:refs/remotes/origin/master") |
| 24 |
c.Assert(spec.Validate(), Equals, nil) |
| 25 |
|
| 26 |
spec = RefSpec(":refs/heads/master") |
| 27 |
c.Assert(spec.Validate(), Equals, nil) |
| 28 |
|
| 29 |
spec = RefSpec(":refs/heads/*") |
| 30 |
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedWildcard) |
| 31 |
|
| 32 |
spec = RefSpec(":*") |
| 33 |
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedWildcard) |
| 34 |
|
| 35 |
spec = RefSpec("refs/heads/*") |
| 36 |
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedSeparator) |
| 37 |
|
| 38 |
spec = RefSpec("refs/heads:") |
| 39 |
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedSeparator) |
| 40 |
|
| 41 |
spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/foo") |
| 42 |
c.Assert(spec.Validate(), Equals, nil) |
| 43 |
|
| 44 |
spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/*") |
| 45 |
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedWildcard) |
| 46 |
} |
| 47 |
|
| 48 |
func (s *RefSpecSuite) TestRefSpecIsForceUpdate(c *C) { |
| 49 |
spec := RefSpec("+refs/heads/*:refs/remotes/origin/*") |
| 50 |
c.Assert(spec.IsForceUpdate(), Equals, true) |
| 51 |
|
| 52 |
spec = RefSpec("refs/heads/*:refs/remotes/origin/*") |
| 53 |
c.Assert(spec.IsForceUpdate(), Equals, false) |
| 54 |
} |
| 55 |
|
| 56 |
func (s *RefSpecSuite) TestRefSpecIsDelete(c *C) { |
| 57 |
spec := RefSpec(":refs/heads/master") |
| 58 |
c.Assert(spec.IsDelete(), Equals, true) |
| 59 |
|
| 60 |
spec = RefSpec("+refs/heads/*:refs/remotes/origin/*") |
| 61 |
c.Assert(spec.IsDelete(), Equals, false) |
| 62 |
|
| 63 |
spec = RefSpec("refs/heads/*:refs/remotes/origin/*") |
| 64 |
c.Assert(spec.IsDelete(), Equals, false) |
| 65 |
} |
| 66 |
|
| 67 |
func (s *RefSpecSuite) TestRefSpecIsExactSHA1(c *C) { |
| 68 |
spec := RefSpec("foo:refs/heads/master") |
| 69 |
c.Assert(spec.IsExactSHA1(), Equals, false) |
| 70 |
|
| 71 |
spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/foo") |
| 72 |
c.Assert(spec.IsExactSHA1(), Equals, true) |
| 73 |
} |
| 74 |
|
| 75 |
func (s *RefSpecSuite) TestRefSpecSrc(c *C) { |
| 76 |
spec := RefSpec("refs/heads/*:refs/remotes/origin/*") |
| 77 |
c.Assert(spec.Src(), Equals, "refs/heads/*") |
| 78 |
|
| 79 |
spec = RefSpec("+refs/heads/*:refs/remotes/origin/*") |
| 80 |
c.Assert(spec.Src(), Equals, "refs/heads/*") |
| 81 |
|
| 82 |
spec = RefSpec(":refs/heads/master") |
| 83 |
c.Assert(spec.Src(), Equals, "") |
| 84 |
|
| 85 |
spec = RefSpec("refs/heads/love+hate:refs/heads/love+hate") |
| 86 |
c.Assert(spec.Src(), Equals, "refs/heads/love+hate") |
| 87 |
|
| 88 |
spec = RefSpec("+refs/heads/love+hate:refs/heads/love+hate") |
| 89 |
c.Assert(spec.Src(), Equals, "refs/heads/love+hate") |
| 90 |
} |
| 91 |
|
| 92 |
func (s *RefSpecSuite) TestRefSpecMatch(c *C) { |
| 93 |
spec := RefSpec("refs/heads/master:refs/remotes/origin/master") |
| 94 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/foo")), Equals, false) |
| 95 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, true) |
| 96 |
|
| 97 |
spec = RefSpec("+refs/heads/master:refs/remotes/origin/master") |
| 98 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/foo")), Equals, false) |
| 99 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, true) |
| 100 |
|
| 101 |
spec = RefSpec(":refs/heads/master") |
| 102 |
c.Assert(spec.Match(plumbing.ReferenceName("")), Equals, true) |
| 103 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/master")), Equals, false) |
| 104 |
|
| 105 |
spec = RefSpec("refs/heads/love+hate:heads/love+hate") |
| 106 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/love+hate")), Equals, true) |
| 107 |
|
| 108 |
spec = RefSpec("+refs/heads/love+hate:heads/love+hate") |
| 109 |
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/love+hate")), Equals, true) |
| 110 |
} |
| 111 |
|
| 112 |
func (s *RefSpecSuite) TestRefSpecMatchGlob(c *C) { |
| 113 |
tests := map[string]map[string]bool{ |
| 114 |
"refs/heads/*:refs/remotes/origin/*": { |
| 115 |
"refs/tag/foo": false, |
| 116 |
"refs/heads/foo": true, |
| 117 |
}, |
| 118 |
"refs/heads/*bc:refs/remotes/origin/*bc": { |
| 119 |
"refs/heads/abc": true, |
| 120 |
"refs/heads/bc": true, |
| 121 |
"refs/heads/abx": false, |
| 122 |
}, |
| 123 |
"refs/heads/a*c:refs/remotes/origin/a*c": { |
| 124 |
"refs/heads/abc": true, |
| 125 |
"refs/heads/ac": true, |
| 126 |
"refs/heads/abx": false, |
| 127 |
}, |
| 128 |
"refs/heads/ab*:refs/remotes/origin/ab*": { |
| 129 |
"refs/heads/abc": true, |
| 130 |
"refs/heads/ab": true, |
| 131 |
"refs/heads/xbc": false, |
| 132 |
}, |
| 133 |
} |
| 134 |
|
| 135 |
for specStr, data := range tests { |
| 136 |
spec := RefSpec(specStr) |
| 137 |
for ref, matches := range data { |
| 138 |
c.Assert(spec.Match(plumbing.ReferenceName(ref)), |
| 139 |
Equals, |
| 140 |
matches, |
| 141 |
Commentf("while matching spec %q against ref %q", specStr, ref), |
| 142 |
) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func (s *RefSpecSuite) TestRefSpecDst(c *C) { |
| 148 |
spec := RefSpec("refs/heads/master:refs/remotes/origin/master") |
| 149 |
c.Assert( |
| 150 |
spec.Dst(plumbing.ReferenceName("refs/heads/master")).String(), Equals, |
| 151 |
"refs/remotes/origin/master", |
| 152 |
) |
| 153 |
} |
| 154 |
|
| 155 |
func (s *RefSpecSuite) TestRefSpecDstBlob(c *C) { |
| 156 |
ref := "refs/heads/abc" |
| 157 |
tests := map[string]string{ |
| 158 |
"refs/heads/*:refs/remotes/origin/*": "refs/remotes/origin/abc", |
| 159 |
"refs/heads/*bc:refs/remotes/origin/*": "refs/remotes/origin/a", |
| 160 |
"refs/heads/*bc:refs/remotes/origin/*bc": "refs/remotes/origin/abc", |
| 161 |
"refs/heads/a*c:refs/remotes/origin/*": "refs/remotes/origin/b", |
| 162 |
"refs/heads/a*c:refs/remotes/origin/a*c": "refs/remotes/origin/abc", |
| 163 |
"refs/heads/ab*:refs/remotes/origin/*": "refs/remotes/origin/c", |
| 164 |
"refs/heads/ab*:refs/remotes/origin/ab*": "refs/remotes/origin/abc", |
| 165 |
"refs/heads/*abc:refs/remotes/origin/*abc": "refs/remotes/origin/abc", |
| 166 |
"refs/heads/abc*:refs/remotes/origin/abc*": "refs/remotes/origin/abc", |
| 167 |
// for these two cases, git specifically logs: |
| 168 |
// error: * Ignoring funny ref 'refs/remotes/origin/' locally |
| 169 |
// and ignores the ref; go-git does not currently do this validation, |
| 170 |
// but probably should. |
| 171 |
// "refs/heads/*abc:refs/remotes/origin/*": "", |
| 172 |
// "refs/heads/abc*:refs/remotes/origin/*": "", |
| 173 |
} |
| 174 |
|
| 175 |
for specStr, dst := range tests { |
| 176 |
spec := RefSpec(specStr) |
| 177 |
c.Assert(spec.Dst(plumbing.ReferenceName(ref)).String(), |
| 178 |
Equals, |
| 179 |
dst, |
| 180 |
Commentf("while getting dst from spec %q with ref %q", specStr, ref), |
| 181 |
) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func (s *RefSpecSuite) TestRefSpecReverse(c *C) { |
| 186 |
spec := RefSpec("refs/heads/*:refs/remotes/origin/*") |
| 187 |
c.Assert( |
| 188 |
spec.Reverse(), Equals, |
| 189 |
RefSpec("refs/remotes/origin/*:refs/heads/*"), |
| 190 |
) |
| 191 |
} |
| 192 |
|
| 193 |
func (s *RefSpecSuite) TestMatchAny(c *C) { |
| 194 |
specs := []RefSpec{ |
| 195 |
"refs/heads/bar:refs/remotes/origin/foo", |
| 196 |
"refs/heads/foo:refs/remotes/origin/bar", |
| 197 |
} |
| 198 |
|
| 199 |
c.Assert(MatchAny(specs, plumbing.ReferenceName("refs/heads/foo")), Equals, true) |
| 200 |
c.Assert(MatchAny(specs, plumbing.ReferenceName("refs/heads/bar")), Equals, true) |
| 201 |
c.Assert(MatchAny(specs, plumbing.ReferenceName("refs/heads/master")), Equals, false) |
| 202 |
} |