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

Commit

Permalink
Add importer and test
Browse files Browse the repository at this point in the history
  • Loading branch information
darkowlzz committed May 16, 2017
1 parent 60110fd commit e6726da
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/dep/godep_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
)

type godepFile struct {
json godepJSON
json godepJson
loggers *dep.Loggers
}

func newGodepFile(loggers *dep.Loggers) *godepFile {
return &godepFile{loggers: loggers}
}

type godepJSON struct {
type godepJson struct {
Name string `json:"ImportPath"`
Imports []godepPackage `json:"Deps"`
}
Expand All @@ -32,7 +32,7 @@ type godepPackage struct {

// load parses Godeps.json in projectDir and unmarshals the json to godepFile.json
func (g *godepFile) load(projectDir string) error {
j := filepath.Join(projectDir, godepJSONpath)
j := filepath.Join(projectDir, "Godeps", godepJsonName)
if g.loggers.Verbose {
g.loggers.Err.Printf("godep: Loading %s", j)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/dep/godep_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

const testGodepProjectRoot = "github.com/golang/notexist"

func TestGodepJSONLoad(t *testing.T) {
func TestGodepJsonLoad(t *testing.T) {
// This is same as cmd/dep/testdata/Godeps.json
wantJSON := godepJSON{
wantJSON := godepJson{
Name: "github.com/golang/notexist",
Imports: []godepPackage{
{
Expand All @@ -32,7 +32,7 @@ func TestGodepJSONLoad(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

h.TempCopy(filepath.Join(testGodepProjectRoot, godepJSONpath), "Godeps.json")
h.TempCopy(filepath.Join(testGodepProjectRoot, "Godeps", godepJsonName), "Godeps.json")

projectRoot := h.Path(testGodepProjectRoot)

Expand Down Expand Up @@ -66,7 +66,7 @@ func TestGodepConvertProject(t *testing.T) {

f := godepFile{
loggers: loggers,
json: godepJSON{
json: godepJson{
Name: "github.com/foo/bar",
Imports: []godepPackage{
{
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestGodepConvertBadInput_EmptyPackageName(t *testing.T) {

f := godepFile{
loggers: loggers,
json: godepJSON{
json: godepJson{
Imports: []godepPackage{{ImportPath: ""}},
},
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/dep/godep_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"github.com/golang/dep/internal/gps"
)

// Godeps/Godeps.json
const godepJSONpath = "Godeps/Godeps.json"
const godepJsonName = "Godeps.json"

type godepImporter struct {
loggers *dep.Loggers
Expand All @@ -21,7 +20,7 @@ func newGodepImporter(loggers *dep.Loggers, sm gps.SourceManager) *godepImporter
}

func (i godepImporter) HasConfig(dir string) bool {
y := filepath.Join(dir, godepJSONpath)
y := filepath.Join(dir, "Godeps", godepJsonName)
if _, err := os.Stat(y); err != nil {
return false
}
Expand All @@ -30,11 +29,11 @@ func (i godepImporter) HasConfig(dir string) bool {
}

func (i godepImporter) Import(dir string, pr gps.ProjectRoot) (*dep.Manifest, *dep.Lock, error) {
files := newGlideFiles(i.loggers)
err := files.load(dir)
file := newGodepFile(i.loggers)
err := file.load(dir)
if err != nil {
return nil, nil, err
}

return files.convert(string(pr), i.sm)
return file.convert(string(pr), i.sm)
}
48 changes: 48 additions & 0 deletions cmd/dep/godep_importer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"log"
"os"
"path/filepath"
"testing"

"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/test"
)

func TestGodepImport(t *testing.T) {
h := test.NewHelper(t)
defer h.Cleanup()

cacheDir := "gps-repocache"
h.TempDir(cacheDir)
h.TempDir("src")
h.TempDir(filepath.Join("src", testGlideProjectRoot))
h.TempCopy(filepath.Join(testGodepProjectRoot, "Godeps", godepJsonName), "Godeps.json")

loggers := &dep.Loggers{
Out: log.New(os.Stdout, "", 0),
Err: log.New(os.Stderr, "", 0),
Verbose: true,
}
projectRoot := h.Path(testGodepProjectRoot)
sm, err := gps.NewSourceManager(h.Path(cacheDir))
h.Must(err)

i := newGodepImporter(loggers, sm)
if !i.HasConfig(projectRoot) {
t.Fatal("Expected the importer to detect the godep configuration file")
}

m, l, err := i.Import(projectRoot, gps.ProjectRoot(testGodepProjectRoot))
h.Must(err)

if m == nil {
t.Fatal("Expected the manifest to be generated")
}

if l == nil {
t.Fatal("Expected the lock to be generated")
}
}

0 comments on commit e6726da

Please sign in to comment.