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

Commit

Permalink
allow cachedir override using env var
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
sudo-suhas committed Oct 7, 2017
1 parent a4c6879 commit e3930c6
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 1 deletion.
40 changes: 40 additions & 0 deletions cmd/dep/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ func TestIntegration(t *testing.T) {
})
}

func TestDepCachedir(t *testing.T) {
t.Parallel()

test.NeedsExternalNetwork(t)
test.NeedsGit(t)

wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

initPath := filepath.Join("testdata", "cachedir")

testProj := integration.NewTestProject(t, initPath, wd, runMain)
defer testProj.Cleanup()

testProj.TempDir("cachedir")
cachedir := testProj.Path("cachedir")
testProj.Setenv("DEPCACHEDIR", cachedir)

// Running `dep ensure` will pull in the dependency into cachedir.
err = testProj.DoRun([]string{"ensure"})
if err != nil {
// Log the error output from running `dep ensure`, could be useful.
t.Log(testProj.GetStderr())
t.Fatalf("got an unexpected error: %s", err.Error())
}

// Check that the cache was created in the cachedir. Our fixture has the dependency
// `github.com/sdboyer/deptest`
_, err = os.Stat(testProj.Path("cachedir", "sources", "https---github.com-sdboyer-deptest"))
if err != nil {
if os.IsNotExist(err) {
t.Fatal("Expected cachedir to have been populated but none was found")
} else {
t.Fatalf("Got unexpected error: %s", err)
}
}
}

// execCmd is a test.RunFunc which runs the program in another process.
func execCmd(prog string, args []string, stdout, stderr io.Writer, dir string, env []string) error {
cmd := exec.Command(prog, args...)
Expand Down
5 changes: 5 additions & 0 deletions cmd/dep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,17 @@ func (c *Config) Run() (exitCode int) {
return
}

// Cachedir is loaded from env if present. `$GOPATH/pkg/dep` is used as the
// fallback cache location.
cachedir := getEnv(c.Env, "DEPCACHEDIR")

// Set up dep context.
ctx := &dep.Ctx{
Out: outLogger,
Err: errLogger,
Verbose: *verbose,
DisableLocking: getEnv(c.Env, "DEPNOLOCK") != "",
Cachedir: cachedir,
}

GOPATHS := filepath.SplitList(getEnv(c.Env, "GOPATH"))
Expand Down
15 changes: 15 additions & 0 deletions cmd/dep/testdata/cachedir/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/dep/testdata/cachedir/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"
12 changes: 12 additions & 0 deletions cmd/dep/testdata/cachedir/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
_ "github.com/sdboyer/deptest"
)

func main() {
}
9 changes: 8 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Ctx struct {
Out, Err *log.Logger // Required loggers.
Verbose bool // Enables more verbose logging.
DisableLocking bool // When set, no lock file will be created to protect against simultaneous dep processes.
Cachedir string // Cache directory loaded from environment.
}

// SetPaths sets the WorkingDir and GOPATHs fields. If GOPATHs is empty, then
Expand Down Expand Up @@ -87,8 +88,14 @@ func defaultGOPATH() string {
// SourceManager produces an instance of gps's built-in SourceManager
// initialized to log to the receiver's logger.
func (c *Ctx) SourceManager() (*gps.SourceMgr, error) {
cachedir := c.Cachedir
if cachedir == "" {
// When `DEPCACHEDIR` isn't set in the env, fallback to `$GOPATH/pkg/dep`.
cachedir = filepath.Join(c.GOPATH, "pkg", "dep")
}

return gps.NewSourceManager(gps.SourceManagerConfig{
Cachedir: filepath.Join(c.GOPATH, "pkg", "dep"),
Cachedir: cachedir,
Logger: c.Out,
DisableLocking: c.DisableLocking,
})
Expand Down
39 changes: 39 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,42 @@ func TestDetectGOPATH(t *testing.T) {
}
}
}

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

h.TempDir("cache")
// Create the directory for fallback cachedir location.
h.TempDir(filepath.Join("go", "pkg", "dep"))

testCachedir := h.Path("cache")
gopath := h.Path("go")
discardLgr := discardLogger()

cases := []struct {
cachedir string
wantCachedir string
}{
// If `Cachedir` is not set in the context, it should use `$GOPATH/pkg/dep`.
{cachedir: "", wantCachedir: h.Path(filepath.Join("go", "pkg", "dep"))},
// If `Cachedir` is set in the context, it should use that.
{cachedir: testCachedir, wantCachedir: testCachedir},
}

for _, c := range cases {
ctx := &Ctx{
GOPATH: gopath,
Cachedir: c.cachedir,
Out: discardLgr,
Err: discardLgr,
}
sm, err := ctx.SourceManager()
h.Must(err)
defer sm.Release()

if sm.Cachedir() != c.wantCachedir {
t.Errorf("expected cachedir to be %s, got %s", c.wantCachedir, sm.Cachedir())
}
}
}
5 changes: 5 additions & 0 deletions internal/gps/source_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ func NewSourceManager(c SourceManagerConfig) (*SourceMgr, error) {
return sm, nil
}

// Cachedir returns the location of the cache directory.
func (sm *SourceMgr) Cachedir() string {
return sm.cachedir
}

// UseDefaultSignalHandling sets up typical os.Interrupt signal handling for a
// SourceMgr.
func (sm *SourceMgr) UseDefaultSignalHandling() {
Expand Down

0 comments on commit e3930c6

Please sign in to comment.