|
5 | 5 | "errors" |
6 | 6 | "os" |
7 | 7 | "path/filepath" |
| 8 | + "runtime/debug" |
8 | 9 | "strings" |
9 | 10 | "testing" |
10 | 11 | ) |
@@ -647,15 +648,105 @@ func TestRun(t *testing.T) { |
647 | 648 | } |
648 | 649 |
|
649 | 650 | func TestVersionFunc(t *testing.T) { |
650 | | - var buf bytes.Buffer |
651 | | - err := version(&buf) |
652 | | - if err != nil { |
653 | | - t.Errorf("version() returned error: %v", err) |
654 | | - } |
655 | | - output := buf.String() |
656 | | - if output != Version+"\n" { |
657 | | - t.Errorf("version() = %q, want %q", output, Version+"\n") |
658 | | - } |
| 651 | + t.Run("basic output", func(t *testing.T) { |
| 652 | + var buf bytes.Buffer |
| 653 | + err := version(&buf) |
| 654 | + if err != nil { |
| 655 | + t.Errorf("version() returned error: %v", err) |
| 656 | + } |
| 657 | + output := buf.String() |
| 658 | + // Output should contain something (either version or dev with commit info) |
| 659 | + if output == "" { |
| 660 | + t.Error("version() returned empty output") |
| 661 | + } |
| 662 | + }) |
| 663 | +} |
| 664 | + |
| 665 | +func TestVersionString(t *testing.T) { |
| 666 | + origVersion := Version |
| 667 | + origReadBuildInfo := readBuildInfo |
| 668 | + defer func() { |
| 669 | + Version = origVersion |
| 670 | + readBuildInfo = origReadBuildInfo |
| 671 | + }() |
| 672 | + |
| 673 | + t.Run("release version", func(t *testing.T) { |
| 674 | + Version = "v1.2.3" |
| 675 | + result := versionString() |
| 676 | + if result != "v1.2.3" { |
| 677 | + t.Errorf("versionString() = %q, want %q", result, "v1.2.3") |
| 678 | + } |
| 679 | + }) |
| 680 | + |
| 681 | + t.Run("dev with no build info", func(t *testing.T) { |
| 682 | + Version = "dev" |
| 683 | + readBuildInfo = func() (*debug.BuildInfo, bool) { |
| 684 | + return nil, false |
| 685 | + } |
| 686 | + result := versionString() |
| 687 | + if result != "dev" { |
| 688 | + t.Errorf("versionString() = %q, want %q", result, "dev") |
| 689 | + } |
| 690 | + }) |
| 691 | + |
| 692 | + t.Run("dev with build info but no vcs", func(t *testing.T) { |
| 693 | + Version = "dev" |
| 694 | + readBuildInfo = func() (*debug.BuildInfo, bool) { |
| 695 | + return &debug.BuildInfo{}, true |
| 696 | + } |
| 697 | + result := versionString() |
| 698 | + if result != "dev" { |
| 699 | + t.Errorf("versionString() = %q, want %q", result, "dev") |
| 700 | + } |
| 701 | + }) |
| 702 | + |
| 703 | + t.Run("dev with vcs revision", func(t *testing.T) { |
| 704 | + Version = "dev" |
| 705 | + readBuildInfo = func() (*debug.BuildInfo, bool) { |
| 706 | + return &debug.BuildInfo{ |
| 707 | + Settings: []debug.BuildSetting{ |
| 708 | + {Key: "vcs.revision", Value: "abc1234567890"}, |
| 709 | + {Key: "vcs.modified", Value: "false"}, |
| 710 | + }, |
| 711 | + }, true |
| 712 | + } |
| 713 | + result := versionString() |
| 714 | + if result != "dev (abc1234)" { |
| 715 | + t.Errorf("versionString() = %q, want %q", result, "dev (abc1234)") |
| 716 | + } |
| 717 | + }) |
| 718 | + |
| 719 | + t.Run("dev with vcs revision dirty", func(t *testing.T) { |
| 720 | + Version = "dev" |
| 721 | + readBuildInfo = func() (*debug.BuildInfo, bool) { |
| 722 | + return &debug.BuildInfo{ |
| 723 | + Settings: []debug.BuildSetting{ |
| 724 | + {Key: "vcs.revision", Value: "abc1234567890"}, |
| 725 | + {Key: "vcs.modified", Value: "true"}, |
| 726 | + }, |
| 727 | + }, true |
| 728 | + } |
| 729 | + result := versionString() |
| 730 | + if result != "dev (abc1234-dirty)" { |
| 731 | + t.Errorf("versionString() = %q, want %q", result, "dev (abc1234-dirty)") |
| 732 | + } |
| 733 | + }) |
| 734 | + |
| 735 | + t.Run("dev with short revision", func(t *testing.T) { |
| 736 | + Version = "dev" |
| 737 | + readBuildInfo = func() (*debug.BuildInfo, bool) { |
| 738 | + return &debug.BuildInfo{ |
| 739 | + Settings: []debug.BuildSetting{ |
| 740 | + {Key: "vcs.revision", Value: "abc"}, |
| 741 | + {Key: "vcs.modified", Value: "false"}, |
| 742 | + }, |
| 743 | + }, true |
| 744 | + } |
| 745 | + result := versionString() |
| 746 | + if result != "dev (abc)" { |
| 747 | + t.Errorf("versionString() = %q, want %q", result, "dev (abc)") |
| 748 | + } |
| 749 | + }) |
659 | 750 | } |
660 | 751 |
|
661 | 752 | // TestMainFunc tests the main() function by mocking exitFn and os.Args |
|
0 commit comments