github.com/golang/dep@v0.5.4/project_test.go (about) 1 // Copyright 2017 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 "fmt" 9 "os" 10 "path/filepath" 11 "runtime" 12 "strings" 13 "testing" 14 15 "github.com/golang/dep/gps" 16 "github.com/golang/dep/internal/test" 17 ) 18 19 func TestFindRoot(t *testing.T) { 20 wd, err := os.Getwd() 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 want := filepath.Join(wd, "testdata", "rootfind") 26 got1, err := findProjectRoot(want) 27 if err != nil { 28 t.Errorf("Unexpected error while finding root: %s", err) 29 } else if want != got1 { 30 t.Errorf("findProjectRoot directly on root dir should have found %s, got %s", want, got1) 31 } 32 33 got2, err := findProjectRoot(filepath.Join(want, "subdir")) 34 if err != nil { 35 t.Errorf("Unexpected error while finding root: %s", err) 36 } else if want != got2 { 37 t.Errorf("findProjectRoot on subdir should have found %s, got %s", want, got2) 38 } 39 40 got3, err := findProjectRoot(filepath.Join(want, "nonexistent")) 41 if err != nil { 42 t.Errorf("Unexpected error while finding root: %s", err) 43 } else if want != got3 { 44 t.Errorf("findProjectRoot on nonexistent subdir should still work and give %s, got %s", want, got3) 45 } 46 47 root := "/" 48 p, err := findProjectRoot(root) 49 if p != "" { 50 t.Errorf("findProjectRoot with path %s returned non empty string: %s", root, p) 51 } 52 if err != errProjectNotFound { 53 t.Errorf("findProjectRoot want: %#v got: %#v", errProjectNotFound, err) 54 } 55 56 // The following test does not work on windows because syscall.Stat does not 57 // return a "not a directory" error. 58 if runtime.GOOS != "windows" { 59 got4, err := findProjectRoot(filepath.Join(want, ManifestName)) 60 if err == nil { 61 t.Errorf("Should have err'd when trying subdir of file, but returned %s", got4) 62 } 63 } 64 } 65 66 func TestCheckGopkgFilenames(t *testing.T) { 67 // We are trying to skip this test on file systems which are case-sensiive. We could 68 // have used `fs.IsCaseSensitiveFilesystem` for this check. However, the code we are 69 // testing also relies on `fs.IsCaseSensitiveFilesystem`. So a bug in 70 // `fs.IsCaseSensitiveFilesystem` could prevent this test from being run. This is the 71 // only scenario where we prefer the OS heuristic over doing the actual work of 72 // validating filesystem case sensitivity via `fs.IsCaseSensitiveFilesystem`. 73 if runtime.GOOS != "windows" && runtime.GOOS != "darwin" { 74 t.Skip("skip this test on non-Windows, non-macOS") 75 } 76 77 errMsgFor := func(filetype, filename string) func(string) string { 78 return func(name string) string { 79 return fmt.Sprintf("%s filename %q does not match %q", filetype, name, filename) 80 } 81 } 82 83 manifestErrMsg := errMsgFor("manifest", ManifestName) 84 lockErrMsg := errMsgFor("lock", LockName) 85 86 invalidMfName := strings.ToLower(ManifestName) 87 invalidLfName := strings.ToLower(LockName) 88 89 cases := []struct { 90 wantErr bool 91 createFiles []string 92 wantErrMsg string 93 }{ 94 // No error should be returned when the project contains a valid manifest file 95 // but no lock file. 96 {false, []string{ManifestName}, ""}, 97 // No error should be returned when the project contains a valid manifest file as 98 // well as a valid lock file. 99 {false, []string{ManifestName, LockName}, ""}, 100 // Error indicating the project was not found should be returned if a manifest 101 // file is not found. 102 {true, nil, errProjectNotFound.Error()}, 103 // Error should be returned if the project has a manifest file with invalid name 104 // but no lock file. 105 {true, []string{invalidMfName}, manifestErrMsg(invalidMfName)}, 106 // Error should be returned if the project has a valid manifest file and an 107 // invalid lock file. 108 {true, []string{ManifestName, invalidLfName}, lockErrMsg(invalidLfName)}, 109 } 110 111 for _, c := range cases { 112 h := test.NewHelper(t) 113 defer h.Cleanup() 114 115 // Create a temporary directory which we will use as the project folder. 116 h.TempDir("") 117 tmpPath := h.Path(".") 118 119 // Create any files that are needed for the test before invoking 120 // `checkGopkgFilenames`. 121 for _, file := range c.createFiles { 122 h.TempFile(file, "") 123 } 124 err := checkGopkgFilenames(tmpPath) 125 126 if c.wantErr { 127 if err == nil { 128 // We were expecting an error but did not get one. 129 t.Fatalf("unexpected error message: \n\t(GOT) nil\n\t(WNT) %s", c.wantErrMsg) 130 } else if err.Error() != c.wantErrMsg { 131 // We got an error but it is not the one we were expecting. 132 t.Fatalf("unexpected error message: \n\t(GOT) %s\n\t(WNT) %s", err.Error(), c.wantErrMsg) 133 } 134 } else if err != nil { 135 // Error was not expected but still we got one 136 t.Fatalf("unexpected error message: \n\t(GOT) %+v", err) 137 } 138 } 139 } 140 141 func TestProjectMakeParams(t *testing.T) { 142 m := NewManifest() 143 m.Ignored = []string{"ignoring this"} 144 145 p := Project{ 146 AbsRoot: "someroot", 147 ImportRoot: gps.ProjectRoot("Some project root"), 148 Manifest: m, 149 Lock: &Lock{}, 150 } 151 p.ChangedLock = p.Lock 152 153 solveParam := p.MakeParams() 154 155 if solveParam.Manifest != p.Manifest { 156 t.Error("makeParams() returned gps.SolveParameters with incorrect Manifest") 157 } 158 159 if solveParam.Lock != p.Lock { 160 t.Error("makeParams() returned gps.SolveParameters with incorrect Lock") 161 } 162 } 163 164 func TestBackupVendor(t *testing.T) { 165 h := test.NewHelper(t) 166 defer h.Cleanup() 167 168 pc := NewTestProjectContext(h, "vendorbackupproject") 169 defer pc.Release() 170 171 dummyFile := filepath.Join("vendor", "badinput_fileroot") 172 pc.CopyFile(dummyFile, "txn_writer/badinput_fileroot") 173 pc.Load() 174 175 if err := pc.VendorShouldExist(); err != nil { 176 t.Fatal(err) 177 } 178 179 // Create a backup 180 wantName := "_vendor-sfx" 181 vendorbak, err := BackupVendor("vendor", "sfx") 182 if err != nil { 183 t.Fatal(err) 184 } 185 186 if vendorbak != wantName { 187 t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, wantName) 188 } 189 190 if err = pc.h.ShouldExist(vendorbak); err != nil { 191 t.Fatal(err) 192 } 193 194 if err = pc.h.ShouldExist(vendorbak + string(filepath.Separator) + "badinput_fileroot"); err != nil { 195 t.Fatal(err) 196 } 197 198 // Should return error on creating backup with existing filename 199 vendorbak, err = BackupVendor("vendor", "sfx") 200 201 if err != errVendorBackupFailed { 202 t.Fatalf("Vendor backup error is not as expected: \n\t(GOT) %v\n\t(WNT) %v", err, errVendorBackupFailed) 203 } 204 205 if vendorbak != "" { 206 t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, "") 207 } 208 209 // Delete vendor 210 if err = os.RemoveAll("vendor"); err != nil { 211 t.Fatal(err) 212 } 213 214 // Should return empty backup file name when no vendor exists 215 vendorbak, err = BackupVendor("vendor", "sfx") 216 if err != nil { 217 t.Fatal(err) 218 } 219 220 if vendorbak != "" { 221 t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, "") 222 } 223 }