allow cachedir override using env var

source
- main.go: Read and use env var `DEPCACHEDIR` for instantiating dep context.
- context.go:
  - 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.go: Add a getter func `Cachedir` to facilitate testing in
  `context_test.go`.

test
- context_test.go Add test to check `gps.SourceMgr` is instantiated with
  appropriate `cachedir`.
- integration_test.go: Add test to check environment variable `DEPCACHEDIR` is
  loaded and used if present.

misc
- update changelog
This commit is contained in:
Suhas Karanth 2017-10-03 10:02:46 +05:30
Родитель c6d1fe0db0
Коммит 6c1220894d
9 изменённых файлов: 130 добавлений и 1 удалений

Просмотреть файл

@ -4,6 +4,8 @@ NEW FEATURES:
* Add support for importing from [glock](https://github.com/robfig/glock) based projects (#1422).
* Add support for importing from [govendor](https://github.com/kardianos/govendor)
based projects (#815).
* Allow override of cache directory location using environment variable
`DEPCACHEDIR`. ([#1234](https://github.com/golang/dep/pull/1234))
BUG FIXES:

Просмотреть файл

@ -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...)

Просмотреть файл

@ -180,12 +180,17 @@ func (c *Config) Run() int {
return errorExitCode
}
// 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"))

15
cmd/dep/testdata/cachedir/Gopkg.lock сгенерированный поставляемый Normal file
Просмотреть файл

@ -0,0 +1,15 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/sdboyer/deptest"
packages = ["."]
revision = "ff2948a2ac8f538c4ecd55962e919d1e13e74baf"
version = "v1.0.0"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "14b07b05e0f01051b03887ab2bf80b516bc5510ea92f75f76c894b1745d8850c"
solver-name = "gps-cdcl"
solver-version = 1

4
cmd/dep/testdata/cachedir/Gopkg.toml поставляемый Normal file
Просмотреть файл

@ -0,0 +1,4 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

12
cmd/dep/testdata/cachedir/main.go поставляемый Normal file
Просмотреть файл

@ -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() {
}

Просмотреть файл

@ -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
@ -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,
})

Просмотреть файл

@ -488,3 +488,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())
}
}
}

Просмотреть файл

@ -291,6 +291,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() {