github.com/golang/dep@v0.5.4/test_project_context_test.go (about) 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/gps" 11 "github.com/golang/dep/internal/test" 12 "github.com/pkg/errors" 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.SourceManager 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 // Set up a Source Manager 39 var err error 40 pc.Context = &Ctx{ 41 GOPATH: pc.tempDir, 42 Out: discardLogger(), 43 Err: discardLogger(), 44 } 45 pc.SourceManager, err = pc.Context.SourceManager() 46 h.Must(errors.Wrap(err, "Unable to create a SourceManager")) 47 48 return pc 49 } 50 51 // CopyFile copies a file from the testdata directory into the project 52 // projectPath is the destination file path, relative to the project directory 53 // testdataPath is the source path, relative to the testdata directory 54 func (pc *TestProjectContext) CopyFile(projectPath string, testdataPath string) string { 55 path := filepath.Join(pc.tempProjectDir, projectPath) 56 pc.h.TempCopy(path, testdataPath) 57 return path 58 } 59 60 func (pc *TestProjectContext) Load() { 61 // TODO(carolynvs): Can't use Ctx.LoadProject until dep doesn't require a manifest at the project root or it also looks for lock 62 var err error 63 var m *Manifest 64 mp := pc.getManifestPath() 65 if pc.h.Exist(mp) { 66 mf := pc.h.GetFile(mp) 67 defer mf.Close() 68 var warns []error 69 m, warns, err = readManifest(mf) 70 for _, warn := range warns { 71 pc.Context.Err.Printf("dep: WARNING: %v\n", warn) 72 } 73 pc.h.Must(errors.Wrapf(err, "Unable to read manifest at %s", mp)) 74 } 75 var l *Lock 76 lp := pc.getLockPath() 77 if pc.h.Exist(lp) { 78 lf := pc.h.GetFile(lp) 79 defer lf.Close() 80 l, err = readLock(lf) 81 pc.h.Must(errors.Wrapf(err, "Unable to read lock at %s", lp)) 82 } 83 pc.Project.Manifest = m 84 pc.Project.Lock = l 85 } 86 87 // GetLockPath returns the full path to the lock 88 func (pc *TestProjectContext) getLockPath() string { 89 return filepath.Join(pc.Project.AbsRoot, LockName) 90 } 91 92 // GetManifestPath returns the full path to the manifest 93 func (pc *TestProjectContext) getManifestPath() string { 94 return filepath.Join(pc.Project.AbsRoot, ManifestName) 95 } 96 97 // GetVendorPath returns the full path to the vendor directory 98 func (pc *TestProjectContext) getVendorPath() string { 99 return filepath.Join(pc.Project.AbsRoot, "vendor") 100 } 101 102 // LockShouldMatchGolden returns an error when the lock does not match the golden lock. 103 // goldenLockPath is the path to the golden lock file relative to the testdata directory 104 // Updates the golden file when -UpdateGolden flag is present. 105 func (pc *TestProjectContext) LockShouldMatchGolden(goldenLockPath string) error { 106 got := pc.h.ReadLock() 107 return pc.ShouldMatchGolden(goldenLockPath, got) 108 } 109 110 // LockShouldNotExist returns an error when the lock exists. 111 func (pc *TestProjectContext) LockShouldNotExist() error { 112 return pc.h.ShouldNotExist(pc.getLockPath()) 113 } 114 115 // ManifestShouldMatchGolden returns an error when the manifest does not match the golden manifest. 116 // goldenManifestPath is the path to the golden manifest file, relative to the testdata directory 117 // Updates the golden file when -UpdateGolden flag is present 118 func (pc *TestProjectContext) ManifestShouldMatchGolden(goldenManifestPath string) error { 119 got := pc.h.ReadManifest() 120 return pc.ShouldMatchGolden(goldenManifestPath, got) 121 } 122 123 // ManifestShouldNotExist returns an error when the lock exists. 124 func (pc *TestProjectContext) ManifestShouldNotExist() error { 125 return pc.h.ShouldNotExist(pc.getManifestPath()) 126 } 127 128 // ShouldMatchGolden returns an error when a file does not match the golden file. 129 // goldenFile is the path to the golden file, relative to the testdata directory 130 // Updates the golden file when -UpdateGolden flag is present 131 func (pc *TestProjectContext) ShouldMatchGolden(goldenFile string, got string) error { 132 want := pc.h.GetTestFileString(goldenFile) 133 if want != got { 134 if *test.UpdateGolden { 135 if err := pc.h.WriteTestFile(goldenFile, got); err != nil { 136 return errors.Wrapf(err, "Unable to write updated golden file %s", goldenFile) 137 } 138 } else { 139 return errors.Errorf("expected %s, got %s", want, got) 140 } 141 } 142 143 return nil 144 } 145 146 // VendorShouldExist returns an error when the vendor directory does not exist. 147 func (pc *TestProjectContext) VendorShouldExist() error { 148 return pc.h.ShouldExist(pc.getVendorPath()) 149 } 150 151 // VendorFileShouldExist returns an error when the specified file does not exist in vendor. 152 // filePath is the relative path to the file within vendor 153 func (pc *TestProjectContext) VendorFileShouldExist(filePath string) error { 154 fullPath := filepath.Join(pc.getVendorPath(), filePath) 155 return pc.h.ShouldExist(fullPath) 156 } 157 158 // VendorShouldNotExist returns an error when the vendor directory exists. 159 func (pc *TestProjectContext) VendorShouldNotExist() error { 160 return pc.h.ShouldNotExist(pc.getVendorPath()) 161 } 162 163 // Release cleans up after test objects created by this instance 164 func (pc *TestProjectContext) Release() { 165 if pc.SourceManager != nil { 166 pc.SourceManager.Release() 167 } 168 }