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

Commit 155a6de

Browse files
authored
Merge pull request #571 from niranjan92/master
dep init - support relative path
2 parents e04faa2 + d8abdbf commit 155a6de

File tree

9 files changed

+139
-4
lines changed

9 files changed

+139
-4
lines changed

cmd/dep/init.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
7171
root = ctx.WorkingDir
7272
} else {
7373
root = args[0]
74+
if !filepath.IsAbs(args[0]) {
75+
root = filepath.Join(ctx.WorkingDir, args[0])
76+
}
77+
if err := os.MkdirAll(root, os.FileMode(0777)); err != nil {
78+
return errors.Errorf("unable to create directory %s , err %v", root, err)
79+
}
7480
}
7581

7682
mf := filepath.Join(root, dep.ManifestName)

cmd/dep/integration_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ func TestIntegration(t *testing.T) {
4343
}
4444
return nil
4545
})
46+
47+
filepath.Walk(filepath.Join("testdata", "init_path_tests"),
48+
func(path string, info os.FileInfo, err error) error {
49+
if err != nil {
50+
t.Fatal("error walking filepath")
51+
}
52+
53+
wd, err := os.Getwd()
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
if filepath.Base(path) == "testcase.json" {
59+
parse := strings.Split(path, string(filepath.Separator))
60+
testName := strings.Join(parse[2:len(parse)-1], "/")
61+
t.Run(testName, func(t *testing.T) {
62+
t.Parallel()
63+
64+
t.Run("external", testRelativePath(testName, wd, true, execCmd))
65+
t.Run("internal", testRelativePath(testName, wd, false, runMain))
66+
})
67+
}
68+
return nil
69+
})
4670
}
4771

4872
// execCmd is a test.RunFunc which runs the program in another process.
@@ -85,7 +109,7 @@ func testIntegration(name, wd string, externalProc bool, run test.RunFunc) func(
85109
t.Parallel()
86110

87111
// Set up environment
88-
testCase := test.NewTestCase(t, name, wd)
112+
testCase := test.NewTestCase(t, name, "harness_tests", wd)
89113
defer testCase.Cleanup()
90114
testProj := test.NewTestProject(t, testCase.InitialPath(), wd, externalProc, run)
91115
defer testProj.Cleanup()
@@ -128,3 +152,50 @@ func testIntegration(name, wd string, externalProc bool, run test.RunFunc) func(
128152
testCase.CompareVendorPaths(testProj.GetVendorPaths())
129153
}
130154
}
155+
156+
func testRelativePath(name, wd string, externalProc bool, run test.RunFunc) func(t *testing.T) {
157+
return func(t *testing.T) {
158+
t.Parallel()
159+
160+
// Set up environment
161+
testCase := test.NewTestCase(t, name, "init_path_tests", wd)
162+
defer testCase.Cleanup()
163+
testProj := test.NewTestProject(t, testCase.InitialPath(), wd, externalProc, run)
164+
defer testProj.Cleanup()
165+
166+
// Create and checkout the vendor revisions
167+
for ip, rev := range testCase.VendorInitial {
168+
testProj.GetVendorGit(ip)
169+
testProj.RunGit(testProj.VendorPath(ip), "checkout", rev)
170+
}
171+
172+
// Create and checkout the import revisions
173+
for ip, rev := range testCase.GopathInitial {
174+
testProj.RunGo("get", ip)
175+
testProj.RunGit(testProj.Path("src", ip), "checkout", rev)
176+
}
177+
178+
// Run commands
179+
testProj.RecordImportPaths()
180+
181+
var err error
182+
for i, args := range testCase.Commands {
183+
err = testProj.DoRun(args)
184+
if err != nil && i < len(testCase.Commands)-1 {
185+
t.Fatalf("cmd %s raised an unexpected error: %s", args[0], err.Error())
186+
}
187+
}
188+
189+
// Check error raised in final command
190+
testCase.CompareError(err, testProj.GetStderr())
191+
192+
// Check output
193+
testCase.CompareOutput(testProj.GetStdout())
194+
195+
// Check final manifest and lock
196+
testCase.CompareFile(dep.ManifestName, testProj.ProjPath(dep.ManifestName))
197+
testCase.CompareFile(dep.LockName, testProj.ProjPath(dep.LockName))
198+
199+
testProj.CompareImportPaths()
200+
}
201+
}

cmd/dep/remove_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestRemoveErrors(t *testing.T) {
2727

2828
func removeErrors(name, wd string, externalProc bool, run test.RunFunc) func(*testing.T) {
2929
return func(t *testing.T) {
30-
testCase := test.NewTestCase(t, name, wd)
30+
testCase := test.NewTestCase(t, name, "harness_tests", wd)
3131
testProj := test.NewTestProject(t, testCase.InitialPath(), wd, externalProc, run)
3232
defer testProj.Cleanup()
3333

cmd/dep/testdata/init_path_tests/relative_path/final/project_dir/Gopkg.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
[[dependencies]]
3+
branch = "master"
4+
name = "github.com/sdboyer/deptest"
5+
6+
[[dependencies]]
7+
name = "github.com/sdboyer/deptestdos"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package foo
6+
7+
import "github.com/sdboyer/deptest"
8+
9+
func Foo() deptest.Foo {
10+
var y deptest.Foo
11+
12+
return y
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/sdboyer/deptestdos"
11+
)
12+
13+
func main() {
14+
var x deptestdos.Bar
15+
y := foo.FooFunc()
16+
17+
fmt.Println(x, y)
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"commands": [
3+
["init", "project_dir"]
4+
],
5+
"init-path": "project_dir"
6+
7+
}

internal/test/integration_testcase.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ type IntegrationTestCase struct {
3333
GopathInitial map[string]string `json:"gopath-initial"`
3434
VendorInitial map[string]string `json:"vendor-initial"`
3535
VendorFinal []string `json:"vendor-final"`
36+
InitPath string `json:"init-path"`
3637
}
3738

38-
func NewTestCase(t *testing.T, name, wd string) *IntegrationTestCase {
39-
rootPath := filepath.FromSlash(filepath.Join(wd, "testdata", "harness_tests", name))
39+
func NewTestCase(t *testing.T, name, test_dir, wd string) *IntegrationTestCase {
40+
rootPath := filepath.FromSlash(filepath.Join(wd, "testdata", test_dir, name))
4041
n := &IntegrationTestCase{
4142
t: t,
4243
name: name,

0 commit comments

Comments
 (0)