github.com/golang/dep@v0.5.4/gps/solver_inputs_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 gps 6 7 import ( 8 "io/ioutil" 9 "log" 10 "math/rand" 11 "strconv" 12 "strings" 13 "testing" 14 15 "github.com/golang/dep/gps/pkgtree" 16 "github.com/golang/dep/internal/test" 17 ) 18 19 // TestBadSolveOpts exercises the different possible inputs to a solver that can 20 // be determined as invalid in Prepare(), without any further work 21 func TestBadSolveOpts(t *testing.T) { 22 pn := strconv.FormatInt(rand.Int63(), 36) 23 fix := basicFixtures["no dependencies"] 24 fix.ds[0].n = ProjectRoot(pn) 25 26 sm := newdepspecSM(fix.ds, nil) 27 params := SolveParameters{ 28 mkBridgeFn: overrideMkBridge, 29 } 30 31 _, err := Prepare(params, nil) 32 if err == nil { 33 t.Errorf("Prepare should have errored on nil SourceManager") 34 } else if !strings.Contains(err.Error(), "non-nil SourceManager") { 35 t.Error("Prepare should have given error on nil SourceManager, but gave:", err) 36 } 37 38 _, err = Prepare(params, sm) 39 if err == nil { 40 t.Errorf("Prepare should have errored without ProjectAnalyzer") 41 } else if !strings.Contains(err.Error(), "must provide a ProjectAnalyzer") { 42 t.Error("Prepare should have given error without ProjectAnalyzer, but gave:", err) 43 } 44 45 params.ProjectAnalyzer = naiveAnalyzer{} 46 _, err = Prepare(params, sm) 47 if err == nil { 48 t.Errorf("Prepare should have errored on empty root") 49 } else if !strings.Contains(err.Error(), "non-empty root directory") { 50 t.Error("Prepare should have given error on empty root, but gave:", err) 51 } 52 53 params.RootDir = pn 54 _, err = Prepare(params, sm) 55 if err == nil { 56 t.Errorf("Prepare should have errored on empty name") 57 } else if !strings.Contains(err.Error(), "non-empty import root") { 58 t.Error("Prepare should have given error on empty import root, but gave:", err) 59 } 60 61 params.RootPackageTree = pkgtree.PackageTree{ 62 ImportRoot: pn, 63 } 64 _, err = Prepare(params, sm) 65 if err == nil { 66 t.Errorf("Prepare should have errored on empty name") 67 } else if !strings.Contains(err.Error(), "at least one package") { 68 t.Error("Prepare should have given error on empty import root, but gave:", err) 69 } 70 71 params.RootPackageTree = pkgtree.PackageTree{ 72 ImportRoot: pn, 73 Packages: map[string]pkgtree.PackageOrErr{ 74 pn: { 75 P: pkgtree.Package{ 76 ImportPath: pn, 77 Name: pn, 78 }, 79 }, 80 }, 81 } 82 params.TraceLogger = log.New(ioutil.Discard, "", 0) 83 84 params.Manifest = simpleRootManifest{ 85 ovr: ProjectConstraints{ 86 ProjectRoot("foo"): ProjectProperties{}, 87 }, 88 } 89 _, err = Prepare(params, sm) 90 if err == nil { 91 t.Errorf("Should have errored on override with empty ProjectProperties") 92 } else if !strings.Contains(err.Error(), "foo, but without any non-zero properties") { 93 t.Error("Prepare should have given error override with empty ProjectProperties, but gave:", err) 94 } 95 96 params.Manifest = simpleRootManifest{ 97 ig: pkgtree.NewIgnoredRuleset([]string{"foo"}), 98 req: map[string]bool{"foo": true}, 99 } 100 _, err = Prepare(params, sm) 101 if err == nil { 102 t.Errorf("Should have errored on pkg both ignored and required") 103 } else if !strings.Contains(err.Error(), "was given as both a required and ignored package") { 104 t.Error("Prepare should have given error with single ignore/require conflict error, but gave:", err) 105 } 106 107 params.Manifest = simpleRootManifest{ 108 ig: pkgtree.NewIgnoredRuleset([]string{"foo", "bar"}), 109 req: map[string]bool{"foo": true, "bar": true}, 110 } 111 _, err = Prepare(params, sm) 112 if err == nil { 113 t.Errorf("Should have errored on pkg both ignored and required") 114 } else if !strings.Contains(err.Error(), "multiple packages given as both required and ignored:") { 115 t.Error("Prepare should have given error with multiple ignore/require conflict error, but gave:", err) 116 } 117 118 params.Manifest = simpleRootManifest{ 119 ig: pkgtree.NewIgnoredRuleset([]string{"foo*"}), 120 req: map[string]bool{"foo/bar": true}, 121 } 122 _, err = Prepare(params, sm) 123 if err == nil { 124 t.Errorf("Should have errored on pkg both ignored (with wildcard) and required") 125 } else if !strings.Contains(err.Error(), "was given as both a required and ignored package") { 126 t.Error("Prepare should have given error with single ignore/require conflict error, but gave:", err) 127 } 128 params.Manifest = nil 129 130 params.ToChange = []ProjectRoot{"foo"} 131 _, err = Prepare(params, sm) 132 if err == nil { 133 t.Errorf("Should have errored on non-empty ToChange without a lock provided") 134 } else if !strings.Contains(err.Error(), "update specifically requested for") { 135 t.Error("Prepare should have given error on ToChange without Lock, but gave:", err) 136 } 137 138 params.Lock = safeLock{ 139 p: []LockedProject{ 140 NewLockedProject(mkPI("bar"), Revision("makebelieve"), nil), 141 }, 142 } 143 _, err = Prepare(params, sm) 144 if err == nil { 145 t.Errorf("Should have errored on ToChange containing project not in lock") 146 } else if !strings.Contains(err.Error(), "cannot update foo as it is not in the lock") { 147 t.Error("Prepare should have given error on ToChange with item not present in Lock, but gave:", err) 148 } 149 150 params.Lock, params.ToChange = nil, nil 151 _, err = Prepare(params, sm) 152 if err != nil { 153 t.Error("Basic conditions satisfied, prepare should have completed successfully, err as:", err) 154 } 155 156 // swap out the test mkBridge override temporarily, just to make sure we get 157 // the right error 158 params.mkBridgeFn = nil 159 160 _, err = Prepare(params, sm) 161 if err == nil { 162 t.Errorf("Should have errored on nonexistent root") 163 } else if !strings.Contains(err.Error(), "could not read project root") { 164 t.Error("Prepare should have given error nonexistent project root dir, but gave:", err) 165 } 166 167 // Pointing it at a file should also be an err 168 params.RootDir = "solve_test.go" 169 _, err = Prepare(params, sm) 170 if err == nil { 171 t.Errorf("Should have errored on file for RootDir") 172 } else if !strings.Contains(err.Error(), "is a file, not a directory") { 173 t.Error("Prepare should have given error on file as RootDir, but gave:", err) 174 } 175 } 176 177 func TestValidateParams(t *testing.T) { 178 h := test.NewHelper(t) 179 defer h.Cleanup() 180 181 cacheDir := "gps-cache" 182 h.TempDir(cacheDir) 183 sm, err := NewSourceManager(SourceManagerConfig{ 184 Cachedir: h.Path(cacheDir), 185 Logger: log.New(test.Writer{TB: t}, "", 0), 186 }) 187 h.Must(err) 188 defer sm.Release() 189 190 h.TempDir("src") 191 192 testcases := []struct { 193 imports []string 194 err bool 195 }{ 196 {[]string{"google.com/non-existing/package"}, true}, 197 {[]string{"google.com/non-existing/package/subpkg"}, true}, 198 {[]string{"github.com/sdboyer/testrepo"}, false}, 199 {[]string{"github.com/sdboyer/testrepo/subpkg"}, false}, 200 } 201 202 params := SolveParameters{ 203 ProjectAnalyzer: naiveAnalyzer{}, 204 RootDir: h.Path("src"), 205 RootPackageTree: pkgtree.PackageTree{ 206 ImportRoot: "github.com/sdboyer/dep", 207 }, 208 } 209 210 for _, tc := range testcases { 211 params.RootPackageTree.Packages = map[string]pkgtree.PackageOrErr{ 212 "github.com/sdboyer/dep": { 213 P: pkgtree.Package{ 214 Name: "github.com/sdboyer/dep", 215 ImportPath: "github.com/sdboyer/dep", 216 Imports: tc.imports, 217 }, 218 }, 219 } 220 221 err = ValidateParams(params, sm) 222 if tc.err && err == nil { 223 t.Fatalf("expected an error when deducing package fails, got none") 224 } else if !tc.err && err != nil { 225 t.Fatalf("deducing packges should have succeeded, got err: %#v", err) 226 } 227 } 228 }