Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit fd224cc

Browse files
committed
Nit fixes to init algo and add test
- Removes getVersionConstituents(), inline its simplified implementation. - Adds test for getProjectPropertiesFromVersion() function.
1 parent e82dcc8 commit fd224cc

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

cmd/dep/init.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -231,40 +231,29 @@ func hasImportPathPrefix(s, prefix string) bool {
231231
return strings.HasPrefix(s, prefix+"/")
232232
}
233233

234-
// getVersionConstituents extracts version constituents
235-
func getVersionConstituents(v gps.Version) (version, revision string) {
236-
switch tv := v.(type) {
237-
case gps.UnpairedVersion:
238-
version = tv.String()
239-
case gps.Revision:
240-
revision = tv.String()
241-
case gps.PairedVersion:
242-
version = tv.Unpair().String()
243-
revision = tv.Underlying().String()
244-
}
245-
246-
return version, revision
247-
}
248-
249234
// getProjectPropertiesFromVersion takes a gps.Version and returns a proper
250235
// gps.ProjectProperties with Constraint value based on the provided version.
251236
func getProjectPropertiesFromVersion(v gps.Version) gps.ProjectProperties {
252237
pp := gps.ProjectProperties{}
253238

254-
// constituent version and revision. Ignoring revison for manifest.
255-
cv, _ := getVersionConstituents(v)
239+
// extract version and ignore if it's revision only
240+
switch tv := v.(type) {
241+
case gps.PairedVersion:
242+
v = tv.Unpair()
243+
case gps.Revision:
244+
return pp
245+
}
256246

257247
switch v.Type() {
258-
case gps.IsBranch:
259-
pp.Constraint = gps.NewBranch(cv)
260-
case gps.IsVersion:
261-
pp.Constraint = gps.NewVersion(cv)
248+
case gps.IsBranch, gps.IsVersion:
249+
pp.Constraint = v
262250
case gps.IsSemver:
263251
// TODO: remove "^" when https://github.com/golang/dep/issues/225 is ready.
264-
c, _ := gps.NewSemverConstraint("^" + cv)
252+
c, err := gps.NewSemverConstraint("^" + v.String())
253+
if err != nil {
254+
panic(err)
255+
}
265256
pp.Constraint = c
266-
case gps.IsRevision:
267-
pp.Constraint = nil
268257
}
269258

270259
return pp

cmd/dep/init_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package main
66

7-
import "testing"
7+
import (
8+
"testing"
9+
10+
"github.com/sdboyer/gps"
11+
)
812

913
func TestContains(t *testing.T) {
1014
a := []string{"a", "b", "abcd"}
@@ -33,3 +37,44 @@ func TestIsStdLib(t *testing.T) {
3337
}
3438
}
3539
}
40+
41+
func TestGetProjectPropertiesFromVersion(t *testing.T) {
42+
cases := []struct {
43+
version gps.Version
44+
expected gps.Version
45+
}{
46+
{
47+
version: gps.NewBranch("foo-branch").Is("some-revision"),
48+
expected: gps.NewBranch("foo-branch"),
49+
},
50+
{
51+
version: gps.NewVersion("foo-version").Is("some-revision"),
52+
expected: gps.NewVersion("foo-version"),
53+
},
54+
{
55+
version: gps.Revision("alsjd934"),
56+
expected: nil,
57+
},
58+
// This fails. Hence, testing separately below.
59+
// {
60+
// version: gps.NewVersion("v1.0.0"),
61+
// expected: gps.NewVersion("^1.0.0"),
62+
// },
63+
}
64+
65+
for _, c := range cases {
66+
actualProp := getProjectPropertiesFromVersion(c.version)
67+
if c.expected != actualProp.Constraint {
68+
t.Fatalf("Expected %q to be equal to %q", actualProp.Constraint, c.expected)
69+
}
70+
}
71+
72+
outsemver := getProjectPropertiesFromVersion(gps.NewVersion("v1.0.0"))
73+
expectedSemver, _ := gps.NewSemverConstraint("^1.0.0")
74+
// Comparing outsemver.Constraint and expectedSemver fails with error
75+
// "comparing uncomparable type semver.rangeConstraint", although they have
76+
// same value and same type "gps.semverConstraint" as per "reflect".
77+
if outsemver.Constraint.String() != expectedSemver.String() {
78+
t.Fatalf("Expected %q to be equal to %q", outsemver, expectedSemver)
79+
}
80+
}

0 commit comments

Comments
 (0)