|
5 | 5 | package main
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "bytes" |
8 | 9 | "errors"
|
9 | 10 | "go/build"
|
| 11 | + "io/ioutil" |
| 12 | + "log" |
| 13 | + "strings" |
10 | 14 | "testing"
|
11 | 15 |
|
| 16 | + "github.com/golang/dep" |
12 | 17 | "github.com/golang/dep/internal/gps"
|
13 | 18 | "github.com/golang/dep/internal/gps/pkgtree"
|
| 19 | + "github.com/golang/dep/internal/test" |
14 | 20 | )
|
15 | 21 |
|
16 | 22 | func TestInvalidEnsureFlagCombinations(t *testing.T) {
|
@@ -139,3 +145,103 @@ func TestCheckErrors(t *testing.T) {
|
139 | 145 | })
|
140 | 146 | }
|
141 | 147 | }
|
| 148 | + |
| 149 | +func TestValidateUpdateArgs(t *testing.T) { |
| 150 | + cases := []struct { |
| 151 | + name string |
| 152 | + args []string |
| 153 | + wantError error |
| 154 | + wantWarn []string |
| 155 | + lockedProjects []string |
| 156 | + }{ |
| 157 | + { |
| 158 | + name: "empty args", |
| 159 | + args: []string{}, |
| 160 | + wantError: nil, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "not project root", |
| 164 | + args: []string{"github.com/golang/dep/cmd"}, |
| 165 | + wantError: errUpdateArgsValidation, |
| 166 | + wantWarn: []string{ |
| 167 | + "github.com/golang/dep/cmd is not a project root, try github.com/golang/dep instead", |
| 168 | + }, |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "not present in lock", |
| 172 | + args: []string{"github.com/golang/dep"}, |
| 173 | + wantError: errUpdateArgsValidation, |
| 174 | + wantWarn: []string{ |
| 175 | + "github.com/golang/dep is not present in Gopkg.lock, cannot -update it", |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "cannot specify alternate sources", |
| 180 | + args: []string{"github.com/golang/dep:github.com/example/dep"}, |
| 181 | + wantError: errUpdateArgsValidation, |
| 182 | + wantWarn: []string{ |
| 183 | + "cannot specify alternate sources on -update (github.com/example/dep)", |
| 184 | + }, |
| 185 | + lockedProjects: []string{"github.com/golang/dep"}, |
| 186 | + }, |
| 187 | + { |
| 188 | + name: "version constraint passed", |
| 189 | + args: []string{"github.com/golang/dep@master"}, |
| 190 | + wantError: errUpdateArgsValidation, |
| 191 | + wantWarn: []string{ |
| 192 | + "version constraint master passed for github.com/golang/dep, but -update follows constraints declared in Gopkg.toml, not CLI arguments", |
| 193 | + }, |
| 194 | + lockedProjects: []string{"github.com/golang/dep"}, |
| 195 | + }, |
| 196 | + } |
| 197 | + |
| 198 | + h := test.NewHelper(t) |
| 199 | + defer h.Cleanup() |
| 200 | + |
| 201 | + h.TempDir("src") |
| 202 | + pwd := h.Path(".") |
| 203 | + |
| 204 | + stderrOutput := &bytes.Buffer{} |
| 205 | + errLogger := log.New(stderrOutput, "", 0) |
| 206 | + ctx := &dep.Ctx{ |
| 207 | + GOPATH: pwd, |
| 208 | + Out: log.New(ioutil.Discard, "", 0), |
| 209 | + Err: errLogger, |
| 210 | + } |
| 211 | + |
| 212 | + sm, err := ctx.SourceManager() |
| 213 | + h.Must(err) |
| 214 | + defer sm.Release() |
| 215 | + |
| 216 | + p := new(dep.Project) |
| 217 | + params := p.MakeParams() |
| 218 | + |
| 219 | + for _, c := range cases { |
| 220 | + t.Run(c.name, func(t *testing.T) { |
| 221 | + // Empty the buffer for every case |
| 222 | + stderrOutput.Reset() |
| 223 | + |
| 224 | + // Fill up the locked projects |
| 225 | + lockedProjects := []gps.LockedProject{} |
| 226 | + for _, lp := range c.lockedProjects { |
| 227 | + pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot(lp)} |
| 228 | + lockedProjects = append(lockedProjects, gps.NewLockedProject(pi, gps.NewVersion("v1.0.0"), []string{})) |
| 229 | + } |
| 230 | + |
| 231 | + // Add lock to project |
| 232 | + p.Lock = &dep.Lock{P: lockedProjects} |
| 233 | + |
| 234 | + err := validateUpdateArgs(ctx, c.args, p, sm, ¶ms) |
| 235 | + if err != c.wantError { |
| 236 | + t.Fatalf("Unexpected error while validating update args:\n\t(GOT): %v\n\t(WNT): %v", err, c.wantError) |
| 237 | + } |
| 238 | + |
| 239 | + warnings := stderrOutput.String() |
| 240 | + for _, warn := range c.wantWarn { |
| 241 | + if !strings.Contains(warnings, warn) { |
| 242 | + t.Fatalf("Expected validateUpdateArgs errors to contain: %q", warn) |
| 243 | + } |
| 244 | + } |
| 245 | + }) |
| 246 | + } |
| 247 | +} |
0 commit comments