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

Commit aef755e

Browse files
authored
Merge pull request #470 from darkowlzz/356-fix-init-solver-and-versioninworkspace
Implement new spec for init
2 parents 164af8c + 8ab8cf5 commit aef755e

File tree

14 files changed

+145
-49
lines changed

14 files changed

+145
-49
lines changed

cmd/dep/init.go

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ import (
2121

2222
const initShortHelp = `Initialize a new project with manifest and lock files`
2323
const initLongHelp = `
24-
Initialize the project at filepath root by parsing its dependencies and writing
25-
manifest and lock files. If root isn't specified, use the current directory.
24+
Initialize the project at filepath root by parsing its dependencies, writing
25+
manifest and lock files, and vendoring the dependencies. If root isn't
26+
specified, use the current directory.
2627
2728
The version of each dependency will reflect the current state of the GOPATH. If
28-
a dependency doesn't exist in the GOPATH, it won't be written to the manifest,
29-
but it will be solved-for, and will appear in the lock.
30-
31-
Note: init may use the network to solve the dependency graph.
32-
33-
Note: init does NOT vendor dependencies at the moment. See dep ensure.
29+
a dependency doesn't exist in the GOPATH, the network would be used to
30+
solve-for, and the solution will appear in manifest and lock. Solved
31+
dependencies will be vendored in vendor/ dir relative to project root.
3432
`
3533

3634
func (cmd *initCommand) Name() string { return "init" }
@@ -139,33 +137,51 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
139137
)
140138
}
141139

142-
if len(pd.notondisk) > 0 {
143-
internal.Vlogf("Solving...")
144-
params := gps.SolveParameters{
145-
RootDir: root,
146-
RootPackageTree: pkgT,
147-
Manifest: m,
148-
Lock: l,
149-
ProjectAnalyzer: dep.Analyzer{},
150-
}
140+
// Run solver with project versions found on disk
141+
internal.Vlogf("Solving...")
142+
params := gps.SolveParameters{
143+
RootDir: root,
144+
RootPackageTree: pkgT,
145+
Manifest: m,
146+
Lock: l,
147+
ProjectAnalyzer: dep.Analyzer{},
148+
}
151149

152-
if *verbose {
153-
params.Trace = true
154-
params.TraceLogger = log.New(os.Stderr, "", 0)
155-
}
156-
s, err := gps.Prepare(params, sm)
157-
if err != nil {
158-
return errors.Wrap(err, "prepare solver")
159-
}
150+
if *verbose {
151+
params.Trace = true
152+
params.TraceLogger = log.New(os.Stderr, "", 0)
153+
}
154+
s, err := gps.Prepare(params, sm)
155+
if err != nil {
156+
return errors.Wrap(err, "prepare solver")
157+
}
160158

161-
soln, err := s.Solve()
162-
if err != nil {
163-
handleAllTheFailuresOfTheWorld(err)
164-
return err
159+
soln, err := s.Solve()
160+
if err != nil {
161+
handleAllTheFailuresOfTheWorld(err)
162+
return err
163+
}
164+
l = dep.LockFromInterface(soln)
165+
166+
// Pick notondisk project constraints from solution and add to manifest
167+
for k, _ := range pd.notondisk {
168+
for _, x := range l.Projects() {
169+
if k == x.Ident().ProjectRoot {
170+
m.Dependencies[k] = getProjectPropertiesFromVersion(x.Version())
171+
break
172+
}
165173
}
166-
l = dep.LockFromInterface(soln)
167174
}
168175

176+
// Run gps.Prepare with appropriate constraint solutions from solve run
177+
// to generate the final lock memo.
178+
s, err = gps.Prepare(params, sm)
179+
if err != nil {
180+
return errors.Wrap(err, "prepare solver")
181+
}
182+
183+
l.Memo = s.HashInputs()
184+
169185
internal.Vlogf("Writing manifest and lock files.")
170186

171187
var sw dep.SafeWriter
@@ -213,6 +229,34 @@ func hasImportPathPrefix(s, prefix string) bool {
213229
return strings.HasPrefix(s, prefix+"/")
214230
}
215231

232+
// getProjectPropertiesFromVersion takes a gps.Version and returns a proper
233+
// gps.ProjectProperties with Constraint value based on the provided version.
234+
func getProjectPropertiesFromVersion(v gps.Version) gps.ProjectProperties {
235+
pp := gps.ProjectProperties{}
236+
237+
// extract version and ignore if it's revision only
238+
switch tv := v.(type) {
239+
case gps.PairedVersion:
240+
v = tv.Unpair()
241+
case gps.Revision:
242+
return pp
243+
}
244+
245+
switch v.Type() {
246+
case gps.IsBranch, gps.IsVersion:
247+
pp.Constraint = v
248+
case gps.IsSemver:
249+
// TODO: remove "^" when https://github.com/golang/dep/issues/225 is ready.
250+
c, err := gps.NewSemverConstraint("^" + v.String())
251+
if err != nil {
252+
panic(err)
253+
}
254+
pp.Constraint = c
255+
}
256+
257+
return pp
258+
}
259+
216260
type projectData struct {
217261
constraints gps.ProjectConstraints // constraints that could be found
218262
dependencies map[gps.ProjectRoot][]string // all dependencies (imports) found by project root
@@ -267,16 +311,7 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm *gps.
267311
}
268312

269313
ondisk[pr] = v
270-
pp := gps.ProjectProperties{}
271-
switch v.Type() {
272-
case gps.IsBranch, gps.IsVersion, gps.IsRevision:
273-
pp.Constraint = v
274-
case gps.IsSemver:
275-
c, _ := gps.NewSemverConstraint("^" + v.String())
276-
pp.Constraint = c
277-
}
278-
279-
constraints[pr] = pp
314+
constraints[pr] = getProjectPropertiesFromVersion(v)
280315
}
281316

282317
internal.Vlogf("Analyzing transitive imports...")

cmd/dep/init_test.go

Lines changed: 48 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,46 @@ func TestIsStdLib(t *testing.T) {
3337
}
3438
}
3539
}
40+
41+
func TestGetProjectPropertiesFromVersion(t *testing.T) {
42+
cases := []struct {
43+
version gps.Version
44+
want gps.Version
45+
}{
46+
{
47+
version: gps.NewBranch("foo-branch"),
48+
want: gps.NewBranch("foo-branch"),
49+
},
50+
{
51+
version: gps.NewVersion("foo-version"),
52+
want: gps.NewVersion("foo-version"),
53+
},
54+
{
55+
version: gps.NewBranch("foo-branch").Is("some-revision"),
56+
want: gps.NewBranch("foo-branch"),
57+
},
58+
{
59+
version: gps.NewVersion("foo-version").Is("some-revision"),
60+
want: gps.NewVersion("foo-version"),
61+
},
62+
{
63+
version: gps.Revision("some-revision"),
64+
want: nil,
65+
},
66+
}
67+
68+
for _, c := range cases {
69+
actualProp := getProjectPropertiesFromVersion(c.version)
70+
if c.want != actualProp.Constraint {
71+
t.Fatalf("Expected project property to be %v, got %v", actualProp.Constraint, c.want)
72+
}
73+
}
74+
75+
// Test to have caret in semver version
76+
outsemver := getProjectPropertiesFromVersion(gps.NewVersion("v1.0.0"))
77+
wantSemver, _ := gps.NewSemverConstraint("^1.0.0")
78+
79+
if outsemver.Constraint.String() != wantSemver.String() {
80+
t.Fatalf("Expected semver to be %v, got %v", outsemver.Constraint, wantSemver)
81+
}
82+
}

cmd/dep/testdata/harness_tests/ensure/empty/case1/final/Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/testdata/harness_tests/ensure/empty/case1/final/Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@
5050
# source = "https://github.com/myfork/package.git"
5151

5252

53+
54+
[[dependencies]]
55+
name = "github.com/sdboyer/deptest"
56+
version = "^1.0.0"

cmd/dep/testdata/harness_tests/ensure/override/case1/final/Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@
5050
# source = "https://github.com/myfork/package.git"
5151

5252

53+
54+
[[dependencies]]
55+
name = "github.com/sdboyer/deptest"
56+
version = "^1.0.0"

cmd/dep/testdata/harness_tests/ensure/pkg-errors/case1/final/Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/testdata/harness_tests/init/case1/final/Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/testdata/harness_tests/init/case1/final/Gopkg.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55

66
[[dependencies]]
77
name = "github.com/sdboyer/deptestdos"
8-
revision = "a0196baa11ea047dd65037287451d36b861b00ea"

cmd/dep/testdata/harness_tests/init/case2/final/Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/testdata/harness_tests/init/case2/final/Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
[[dependencies]]
33
name = "github.com/sdboyer/deptest"
44
version = ">=0.8.0, <1.0.0"
5+
6+
[[dependencies]]
7+
name = "github.com/sdboyer/deptestdos"
8+
version = "^2.0.0"

cmd/dep/testdata/harness_tests/init/case3/final/Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/testdata/harness_tests/init/case3/final/Gopkg.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55

66
[[dependencies]]
77
name = "github.com/sdboyer/deptestdos"
8-
revision = "a0196baa11ea047dd65037287451d36b861b00ea"

cmd/dep/testdata/harness_tests/init/skip-hidden/final/Gopkg.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[[dependencies]]
3+
name = "github.com/sdboyer/deptest"
4+
version = "^1.0.0"

0 commit comments

Comments
 (0)