|
| 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 dep |
| 6 | + |
| 7 | +import ( |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "github.com/golang/dep/test" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/sdboyer/gps" |
| 13 | +) |
| 14 | + |
| 15 | +// TestProjectContext groups together test project files and helps test them |
| 16 | +type TestProjectContext struct { |
| 17 | + h *test.Helper |
| 18 | + tempDir string // Full path to the temp directory |
| 19 | + tempProjectDir string // Relative path of the project under the temp directory |
| 20 | + |
| 21 | + Context *Ctx |
| 22 | + Project *Project |
| 23 | + SourceManager *gps.SourceMgr |
| 24 | +} |
| 25 | + |
| 26 | +// NewTestProjectContext creates a new on-disk test project |
| 27 | +func NewTestProjectContext(h *test.Helper, projectName string) *TestProjectContext { |
| 28 | + pc := &TestProjectContext{h: h} |
| 29 | + |
| 30 | + // Create the test project directory |
| 31 | + pc.tempProjectDir = filepath.Join("src", projectName) |
| 32 | + h.TempDir(pc.tempProjectDir) |
| 33 | + pc.tempDir = h.Path(".") |
| 34 | + pc.Project = &Project{AbsRoot: filepath.Join(pc.tempDir, pc.tempProjectDir)} |
| 35 | + h.Cd(pc.Project.AbsRoot) |
| 36 | + h.Setenv("GOPATH", pc.tempDir) |
| 37 | + |
| 38 | + // Setup a Source Manager |
| 39 | + var err error |
| 40 | + pc.Context = &Ctx{GOPATH: pc.tempDir} |
| 41 | + pc.SourceManager, err = pc.Context.SourceManager() |
| 42 | + h.Must(errors.Wrap(err, "Unable to create a SourceManager")) |
| 43 | + |
| 44 | + return pc |
| 45 | +} |
| 46 | + |
| 47 | +// CopyFile copies a file from the testdata directory into the project |
| 48 | +// projectPath is the destination file path, relative to the project directory |
| 49 | +// testdataPath is the source path, relative to the testdata directory |
| 50 | +func (pc *TestProjectContext) CopyFile(projectPath string, testdataPath string) string { |
| 51 | + path := filepath.Join(pc.tempProjectDir, projectPath) |
| 52 | + pc.h.TempCopy(path, testdataPath) |
| 53 | + return path |
| 54 | +} |
| 55 | + |
| 56 | +func (pc *TestProjectContext) Load() { |
| 57 | + // TODO(carolynvs): Can't use Ctx.LoadProject until dep doesn't require a manifest.json at the project root or it also looks for lock.json |
| 58 | + var err error |
| 59 | + var m *Manifest |
| 60 | + mp := pc.getManifestPath() |
| 61 | + if pc.h.Exist(mp) { |
| 62 | + m, err = readManifest(pc.h.GetFile(mp)) |
| 63 | + pc.h.Must(err) |
| 64 | + } |
| 65 | + var l *Lock |
| 66 | + lp := pc.getLockPath() |
| 67 | + if pc.h.Exist(lp) { |
| 68 | + l, err = readLock(pc.h.GetFile(lp)) |
| 69 | + pc.h.Must(err) |
| 70 | + } |
| 71 | + pc.Project.Manifest = m |
| 72 | + pc.Project.Lock = l |
| 73 | +} |
| 74 | + |
| 75 | +// GetLockPath returns the full path to the lock |
| 76 | +func (pc *TestProjectContext) getLockPath() string { |
| 77 | + return filepath.Join(pc.Project.AbsRoot, LockName) |
| 78 | +} |
| 79 | + |
| 80 | +// GetManifestPath returns the full path to the manifest |
| 81 | +func (pc *TestProjectContext) getManifestPath() string { |
| 82 | + return filepath.Join(pc.Project.AbsRoot, ManifestName) |
| 83 | +} |
| 84 | + |
| 85 | +// GetVendorPath returns the full path to the vendor directory |
| 86 | +func (pc *TestProjectContext) getVendorPath() string { |
| 87 | + return filepath.Join(pc.Project.AbsRoot, "vendor") |
| 88 | +} |
| 89 | + |
| 90 | +// LockShouldMatchGolden returns an error when the lock does not match the golden lock. |
| 91 | +// goldenLockPath is the path to the golden lock file relative to the testdata directory |
| 92 | +// Updates the golden file when -UpdateGolden flag is present. |
| 93 | +func (pc *TestProjectContext) LockShouldMatchGolden(goldenLockPath string) error { |
| 94 | + got := pc.h.ReadLock() |
| 95 | + want := pc.h.GetTestFileString(goldenLockPath) |
| 96 | + return pc.shouldMatchGolden(goldenLockPath, want, got) |
| 97 | +} |
| 98 | + |
| 99 | +// LockShouldNotExist returns an error when the lock exists. |
| 100 | +func (pc *TestProjectContext) LockShouldNotExist() error { |
| 101 | + return pc.h.ShouldNotExist(pc.getLockPath()) |
| 102 | +} |
| 103 | + |
| 104 | +// ManifestShouldMatchGolden returns an error when the manifest does not match the golden manifest. |
| 105 | +// goldenManifestPath is the path to the golden manifest file, relative to the testdata directory |
| 106 | +// Updates the golden file when -UpdateGolden flag is present |
| 107 | +func (pc *TestProjectContext) ManifestShouldMatchGolden(goldenManifestPath string) error { |
| 108 | + got := pc.h.ReadManifest() |
| 109 | + want := pc.h.GetTestFileString(goldenManifestPath) |
| 110 | + return pc.shouldMatchGolden(goldenManifestPath, want, got) |
| 111 | +} |
| 112 | + |
| 113 | +// ManifestShouldNotExist returns an error when the lock exists. |
| 114 | +func (pc *TestProjectContext) ManifestShouldNotExist() error { |
| 115 | + return pc.h.ShouldNotExist(pc.getManifestPath()) |
| 116 | +} |
| 117 | + |
| 118 | +// ShouldMatchGolden returns an error when a file does not match the golden file. |
| 119 | +// goldenFile is the path to the golden file, relative to the testdata directory |
| 120 | +// Updates the golden file when -UpdateGolden flag is present |
| 121 | +func (pc *TestProjectContext) shouldMatchGolden(goldenFile string, want string, got string) error { |
| 122 | + if want != got { |
| 123 | + if *test.UpdateGolden { |
| 124 | + if err := pc.h.WriteTestFile(goldenFile, got); err != nil { |
| 125 | + return errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile) |
| 126 | + } |
| 127 | + } else { |
| 128 | + return errors.Errorf("expected %s, got %s", want, got) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// VendorShouldExist returns an error when the vendor directory does not exist. |
| 136 | +func (pc *TestProjectContext) VendorShouldExist() error { |
| 137 | + return pc.h.ShouldExist(pc.getVendorPath()) |
| 138 | +} |
| 139 | + |
| 140 | +// VendorFileShouldExist returns an error when the specified file does not exist in vendor. |
| 141 | +// filePath is the relative path to the file within vendor |
| 142 | +func (pc *TestProjectContext) VendorFileShouldExist(filePath string) error { |
| 143 | + fullPath := filepath.Join(pc.getVendorPath(), filePath) |
| 144 | + return pc.h.ShouldExist(fullPath) |
| 145 | +} |
| 146 | + |
| 147 | +// VendorShouldNotExist returns an error when the vendor directory exists. |
| 148 | +func (pc *TestProjectContext) VendorShouldNotExist() error { |
| 149 | + return pc.h.ShouldNotExist(pc.getVendorPath()) |
| 150 | +} |
| 151 | + |
| 152 | +// Release cleans up after test objects created by this instance |
| 153 | +func (pc *TestProjectContext) Release() { |
| 154 | + if pc.SourceManager != nil { |
| 155 | + pc.SourceManager.Release() |
| 156 | + } |
| 157 | +} |
0 commit comments