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

Commit bf6fcea

Browse files
committed
allow cachedir override using env var
- main - Read and use env var `DEPCACHEDIR` for instantiating dep context. - context - Add field `Cachedir` to struct `Ctx`. This holds the value of env var `DEPCACHEDIR`. - Use `Ctx.Cachedir` while instantiating `gps.SourceMgr` if present, fallback to `$GOPATH/pkg/dep` otherwise. - source_manager - Add a getter func `Cachedir` to facilitate testing in `context_test.go`. - context_test - Add test to check `gps.SourceMgr` is instantiated with appropriate `cachedir`. - integration_test - Add test to check environment variable `DEPCACHEDIR` is loaded and used if present.
1 parent a445d4d commit bf6fcea

File tree

8 files changed

+128
-1
lines changed

8 files changed

+128
-1
lines changed

cmd/dep/integration_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,46 @@ func TestIntegration(t *testing.T) {
5252
})
5353
}
5454

55+
func TestDepCachedir(t *testing.T) {
56+
t.Parallel()
57+
58+
test.NeedsExternalNetwork(t)
59+
test.NeedsGit(t)
60+
61+
wd, err := os.Getwd()
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
66+
initPath := filepath.Join("testdata", "cachedir")
67+
68+
testProj := integration.NewTestProject(t, initPath, wd, true, runMain)
69+
defer testProj.Cleanup()
70+
71+
testProj.TempDir("cachedir")
72+
cachedir := testProj.Path("cachedir")
73+
testProj.Setenv("DEPCACHEDIR", cachedir)
74+
75+
// Running `dep ensure` will pull in the dependency into cachedir.
76+
err = testProj.DoRun([]string{"ensure"})
77+
if err != nil {
78+
// Log the error output from running `dep ensure`, could be useful.
79+
t.Log(testProj.GetStderr())
80+
t.Fatalf("got an unexpected error: %s", err.Error())
81+
}
82+
83+
// Check that the cache was created in the cachedir. Our fixture has the dependency
84+
// `github.com/sdboyer/deptest`
85+
_, err = os.Stat(testProj.Path("cachedir", "sources", "https---github.lhy31512.workers.dev-sdboyer-deptest"))
86+
if err != nil {
87+
if os.IsNotExist(err) {
88+
t.Fatal("Expected cachedir to have been populated but none was found")
89+
} else {
90+
t.Fatalf("Got unexpected error: %s", err)
91+
}
92+
}
93+
}
94+
5595
// execCmd is a test.RunFunc which runs the program in another process.
5696
func execCmd(prog string, args []string, stdout, stderr io.Writer, dir string, env []string) error {
5797
cmd := exec.Command(prog, args...)

cmd/dep/main.go

+5
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,17 @@ func (c *Config) Run() (exitCode int) {
145145
return
146146
}
147147

148+
// Cachedir is loaded from env if present. `$GOPATH/pkg/dep` is used as the
149+
// fallback cache location.
150+
cachedir := getEnv(c.Env, "DEPCACHEDIR")
151+
148152
// Set up dep context.
149153
ctx := &dep.Ctx{
150154
Out: outLogger,
151155
Err: errLogger,
152156
Verbose: *verbose,
153157
DisableLocking: getEnv(c.Env, "DEPNOLOCK") != "",
158+
Cachedir: cachedir,
154159
}
155160

156161
GOPATHS := filepath.SplitList(getEnv(c.Env, "GOPATH"))

cmd/dep/testdata/cachedir/Gopkg.lock

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/dep/testdata/cachedir/Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[[constraint]]
3+
name = "github.com/sdboyer/deptest"
4+
version = "1.0.0"

cmd/dep/testdata/cachedir/main.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2016 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+
_ "github.com/sdboyer/deptest"
9+
)
10+
11+
func main() {
12+
}

context.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Ctx struct {
4040
Out, Err *log.Logger // Required loggers.
4141
Verbose bool // Enables more verbose logging.
4242
DisableLocking bool // When set, no lock file will be created to protect against simultaneous dep processes.
43+
Cachedir string // Cache directory loaded from environment.
4344
}
4445

4546
// SetPaths sets the WorkingDir and GOPATHs fields. If GOPATHs is empty, then
@@ -87,8 +88,14 @@ func defaultGOPATH() string {
8788
// SourceManager produces an instance of gps's built-in SourceManager
8889
// initialized to log to the receiver's logger.
8990
func (c *Ctx) SourceManager() (*gps.SourceMgr, error) {
91+
cachedir := c.Cachedir
92+
if cachedir == "" {
93+
// When `DEPCACHEDIR` isn't set in the env, fallback to `$GOPATH/pkg/dep`.
94+
cachedir = filepath.Join(c.GOPATH, "pkg", "dep")
95+
}
96+
9097
return gps.NewSourceManager(gps.SourceManagerConfig{
91-
Cachedir: filepath.Join(c.GOPATH, "pkg", "dep"),
98+
Cachedir: cachedir,
9299
Logger: c.Out,
93100
DisableLocking: c.DisableLocking,
94101
})

context_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,42 @@ func TestDetectGOPATH(t *testing.T) {
485485
}
486486
}
487487
}
488+
489+
func TestDepCachedir(t *testing.T) {
490+
h := test.NewHelper(t)
491+
defer h.Cleanup()
492+
493+
h.TempDir("cache")
494+
// Create the directory for fallback cachedir location.
495+
h.TempDir(filepath.Join("go", "pkg", "dep"))
496+
497+
testCachedir := h.Path("cache")
498+
gopath := h.Path("go")
499+
discardLgr := discardLogger()
500+
501+
cases := []struct {
502+
cachedir string
503+
wantCachedir string
504+
}{
505+
// If `Cachedir` is not set in the context, it should use `$GOPATH/pkg/dep`.
506+
{cachedir: "", wantCachedir: h.Path(filepath.Join("go", "pkg", "dep"))},
507+
// If `Cachedir` is set in the context, it should use that.
508+
{cachedir: testCachedir, wantCachedir: testCachedir},
509+
}
510+
511+
for _, c := range cases {
512+
ctx := &Ctx{
513+
GOPATH: gopath,
514+
Cachedir: c.cachedir,
515+
Out: discardLgr,
516+
Err: discardLgr,
517+
}
518+
sm, err := ctx.SourceManager()
519+
h.Must(err)
520+
defer sm.Release()
521+
522+
if sm.Cachedir() != c.wantCachedir {
523+
t.Errorf("expected cachedir to be %s, got %s", c.wantCachedir, sm.Cachedir())
524+
}
525+
}
526+
}

internal/gps/source_manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ func NewSourceManager(c SourceManagerConfig) (*SourceMgr, error) {
292292
return sm, nil
293293
}
294294

295+
// Cachedir is a getter for the cachedir location.
296+
func (sm *SourceMgr) Cachedir() string {
297+
return sm.cachedir
298+
}
299+
295300
// UseDefaultSignalHandling sets up typical os.Interrupt signal handling for a
296301
// SourceMgr.
297302
func (sm *SourceMgr) UseDefaultSignalHandling() {

0 commit comments

Comments
 (0)