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

Commit

Permalink
Attempt to fetch version for no comment imports
Browse files Browse the repository at this point in the history
- Fetches versions list of project and looks for version corresponding
to the given revision.
- Adds test TestGodepConvertBadInput_EmptyPackageName for the same.
  • Loading branch information
darkowlzz committed May 18, 2017
1 parent c637683 commit f2b9cba
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 10 deletions.
34 changes: 29 additions & 5 deletions cmd/dep/godep_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ func (g *godepFile) convert(projectName string, sm gps.SourceManager) (*dep.Mani
return nil, nil, err
}

// Rev must not be empty
if pkg.Rev == "" {
err := errors.New("godep: Invalid godep configuration, Rev is required")
return nil, nil, err
}

if pkg.Comment == "" {
// When there's no comment, try to get corresponding version for the Rev
// and fill Comment.
// Get all the versions
pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(pkg.ImportPath)}
versions, err := sm.ListVersions(pi)
if err != nil {
return nil, nil, err
}
// Sort the versions in descending order, newer versions first
gps.SortPairedForUpgrade(versions)
// Match Rev with versions' underlying revision
for _, v := range versions {
if string(v.Underlying()) == pkg.Rev {
pkg.Comment = v.String()
break
}
}
}

if pkg.Comment != "" {
// If there's a comment, use it to create project constraint
pc, err := g.buildProjectConstraint(pkg, sm)
Expand All @@ -77,11 +103,9 @@ func (g *godepFile) convert(projectName string, sm gps.SourceManager) (*dep.Mani
manifest.Dependencies[pc.Ident.ProjectRoot] = gps.ProjectProperties{Source: pc.Ident.Source, Constraint: pc.Constraint}
}

if pkg.Rev != "" {
// Use the revision to create lock project
lp := g.buildLockedProject(pkg, manifest)
lock.P = append(lock.P, lp)
}
// Use the revision and comment to create lock project
lp := g.buildLockedProject(pkg, manifest)
lock.P = append(lock.P, lp)
}

return manifest, lock, nil
Expand Down
79 changes: 74 additions & 5 deletions cmd/dep/godep_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ func TestGodepConvertProject(t *testing.T) {
Imports: []godepPackage{
{
ImportPath: "github.com/sdboyer/deptest",
Rev: "6a741be0cc55ecbe4f45690ebfd606a956d5f14a",
Comment: "v1.0.0",
// This revision has 2 versions attached to it, v1.0.0 & v0.8.0.
Rev: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
Comment: "v0.8.0",
},
},
},
Expand All @@ -93,9 +94,77 @@ func TestGodepConvertProject(t *testing.T) {
t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none")
}

v := d.Constraint.String()
if v != "v0.8.0" {
t.Fatalf("Expected manifest constraint to be v0.8.0, got %s", v)
}

p := lock.P[0]
if p.Ident().ProjectRoot != "github.com/sdboyer/deptest" {
t.Fatalf("Expected the lock to have a project for 'github.com/sdboyer/deptest' but got '%s'", p.Ident().ProjectRoot)
}

lv := p.Version()
lpv, ok := lv.(gps.PairedVersion)
if !ok {
t.Fatalf("Expected locked version to be PairedVersion but got %T", lv)
}

rev := lpv.Underlying()
if rev != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" {
t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev)
}

ver := lpv.String()
if ver != "v0.8.0" {
t.Fatalf("Expected locked version to be 'v0.8.0', got %s", ver)
}
}

func TestGodepConvertProject_EmptyComment(t *testing.T) {
loggers := &dep.Loggers{
Out: log.New(os.Stdout, "", 0),
Err: log.New(os.Stdout, "", 0),
Verbose: true,
}

h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("src")

f := godepFile{
loggers: loggers,
json: godepJson{
Name: "github.com/foo/bar",
Imports: []godepPackage{
{
ImportPath: "github.com/sdboyer/deptest",
// This revision has 2 versions attached to it, v1.0.0 & v0.8.0.
Rev: "ff2948a2ac8f538c4ecd55962e919d1e13e74baf",
},
},
},
}

sm, err := gps.NewSourceManager(h.Path("."))
if err != nil {
t.Fatal(err)
}
defer sm.Release()

manifest, lock, err := f.convert("", sm)
if err != nil {
t.Fatal(err)
}

d, ok := manifest.Dependencies["github.com/sdboyer/deptest"]
if !ok {
t.Fatal("Expected the manifest to have a dependency for 'github.com/sdboyer/deptest' but got none")
}

v := d.Constraint.String()
if v != "v1.0.0" {
t.Fatalf("Expected manifest constraint to be master, got %s", v)
t.Fatalf("Expected manifest constraint to be v1.0.0, got %s", v)
}

p := lock.P[0]
Expand All @@ -110,8 +179,8 @@ func TestGodepConvertProject(t *testing.T) {
}

rev := lpv.Underlying()
if rev != "6a741be0cc55ecbe4f45690ebfd606a956d5f14a" {
t.Fatalf("Expected locked revision to be '6a741be0cc55ecbe4f45690ebfd606a956d5f14a', got %s", rev)
if rev != "ff2948a2ac8f538c4ecd55962e919d1e13e74baf" {
t.Fatalf("Expected locked revision to be 'ff2948a2ac8f538c4ecd55962e919d1e13e74baf', got %s", rev)
}

ver := lpv.String()
Expand Down

0 comments on commit f2b9cba

Please sign in to comment.