|
5 | 5 | package dep
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "bytes" |
8 | 9 | "errors"
|
| 10 | + "io/ioutil" |
| 11 | + "log" |
9 | 12 | "reflect"
|
10 | 13 | "strings"
|
11 | 14 | "testing"
|
@@ -372,3 +375,110 @@ func TestValidateManifest(t *testing.T) {
|
372 | 375 | }
|
373 | 376 | }
|
374 | 377 | }
|
| 378 | + |
| 379 | +func TestValidateProjectRoots(t *testing.T) { |
| 380 | + cases := []struct { |
| 381 | + name string |
| 382 | + manifest Manifest |
| 383 | + wantError error |
| 384 | + wantWarn []string |
| 385 | + }{ |
| 386 | + { |
| 387 | + name: "empty Manifest", |
| 388 | + manifest: Manifest{}, |
| 389 | + wantError: nil, |
| 390 | + wantWarn: []string{}, |
| 391 | + }, |
| 392 | + { |
| 393 | + name: "valid project root", |
| 394 | + manifest: Manifest{ |
| 395 | + Constraints: map[gps.ProjectRoot]gps.ProjectProperties{ |
| 396 | + gps.ProjectRoot("github.com/golang/dep"): { |
| 397 | + Constraint: gps.Any(), |
| 398 | + }, |
| 399 | + }, |
| 400 | + }, |
| 401 | + wantError: nil, |
| 402 | + wantWarn: []string{}, |
| 403 | + }, |
| 404 | + { |
| 405 | + name: "invalid project roots in Constraints and Overrides", |
| 406 | + manifest: Manifest{ |
| 407 | + Constraints: map[gps.ProjectRoot]gps.ProjectProperties{ |
| 408 | + gps.ProjectRoot("github.com/golang/dep/foo"): { |
| 409 | + Constraint: gps.Any(), |
| 410 | + }, |
| 411 | + gps.ProjectRoot("github.com/golang/go/xyz"): { |
| 412 | + Constraint: gps.Any(), |
| 413 | + }, |
| 414 | + gps.ProjectRoot("github.com/golang/fmt"): { |
| 415 | + Constraint: gps.Any(), |
| 416 | + }, |
| 417 | + }, |
| 418 | + Ovr: map[gps.ProjectRoot]gps.ProjectProperties{ |
| 419 | + gps.ProjectRoot("github.com/golang/mock/bar"): { |
| 420 | + Constraint: gps.Any(), |
| 421 | + }, |
| 422 | + gps.ProjectRoot("github.com/golang/mock"): { |
| 423 | + Constraint: gps.Any(), |
| 424 | + }, |
| 425 | + }, |
| 426 | + }, |
| 427 | + wantError: errInvalidProjectRoot, |
| 428 | + wantWarn: []string{ |
| 429 | + "the name for \"github.com/golang/dep/foo\" should be changed to \"github.com/golang/dep\"", |
| 430 | + "the name for \"github.com/golang/mock/bar\" should be changed to \"github.com/golang/mock\"", |
| 431 | + "the name for \"github.com/golang/go/xyz\" should be changed to \"github.com/golang/go\"", |
| 432 | + }, |
| 433 | + }, |
| 434 | + { |
| 435 | + name: "invalid source path", |
| 436 | + manifest: Manifest{ |
| 437 | + Constraints: map[gps.ProjectRoot]gps.ProjectProperties{ |
| 438 | + gps.ProjectRoot("github.com/golang"): { |
| 439 | + Constraint: gps.Any(), |
| 440 | + }, |
| 441 | + }, |
| 442 | + }, |
| 443 | + wantError: errInvalidProjectRoot, |
| 444 | + wantWarn: []string{}, |
| 445 | + }, |
| 446 | + } |
| 447 | + |
| 448 | + h := test.NewHelper(t) |
| 449 | + defer h.Cleanup() |
| 450 | + |
| 451 | + h.TempDir("src") |
| 452 | + pwd := h.Path(".") |
| 453 | + |
| 454 | + // Capture the stderr to verify the warnings |
| 455 | + stderrOutput := &bytes.Buffer{} |
| 456 | + errLogger := log.New(stderrOutput, "", 0) |
| 457 | + ctx := &Ctx{ |
| 458 | + GOPATH: pwd, |
| 459 | + Out: log.New(ioutil.Discard, "", 0), |
| 460 | + Err: errLogger, |
| 461 | + } |
| 462 | + |
| 463 | + sm, err := ctx.SourceManager() |
| 464 | + h.Must(err) |
| 465 | + defer sm.Release() |
| 466 | + |
| 467 | + for _, c := range cases { |
| 468 | + t.Run(c.name, func(t *testing.T) { |
| 469 | + // Empty the buffer for every case |
| 470 | + stderrOutput.Reset() |
| 471 | + err := ValidateProjectRoots(ctx, &c.manifest, sm) |
| 472 | + if err != c.wantError { |
| 473 | + t.Fatalf("Unexpected error while validating project roots:\n\t(GOT): %v\n\t(WNT): %v", err, c.wantError) |
| 474 | + } |
| 475 | + |
| 476 | + warnings := stderrOutput.String() |
| 477 | + for _, warn := range c.wantWarn { |
| 478 | + if !strings.Contains(warnings, warn) { |
| 479 | + t.Fatalf("Expected ValidateProjectRoot errors to contain: %q", warn) |
| 480 | + } |
| 481 | + } |
| 482 | + }) |
| 483 | + } |
| 484 | +} |
0 commit comments