|
| 1 | +package version |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + flag "github.com/spf13/pflag" |
| 10 | +) |
| 11 | + |
| 12 | +func TestVersionFlag(t *testing.T) { |
| 13 | + appName := "onex-test" |
| 14 | + initialFlagValue := *versionFlag |
| 15 | + initialVersion := Get() |
| 16 | + testcases := []struct { |
| 17 | + name string |
| 18 | + flags []string |
| 19 | + expectError string |
| 20 | + expectExit bool |
| 21 | + expectPrintVersion string |
| 22 | + expectGitVersion string |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "no flag", |
| 26 | + flags: []string{}, |
| 27 | + expectGitVersion: initialVersion.GitVersion, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "false", |
| 31 | + flags: []string{"--version=false"}, |
| 32 | + expectGitVersion: initialVersion.GitVersion, |
| 33 | + }, |
| 34 | + |
| 35 | + { |
| 36 | + name: "valueless", |
| 37 | + flags: []string{"--version"}, |
| 38 | + expectGitVersion: initialVersion.GitVersion, |
| 39 | + expectExit: true, |
| 40 | + expectPrintVersion: appName + " " + initialVersion.GitVersion, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "true", |
| 44 | + flags: []string{"--version=true"}, |
| 45 | + expectGitVersion: initialVersion.GitVersion, |
| 46 | + expectExit: true, |
| 47 | + expectPrintVersion: appName + " " + initialVersion.GitVersion, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "raw", |
| 51 | + flags: []string{"--version=raw"}, |
| 52 | + expectGitVersion: initialVersion.GitVersion, |
| 53 | + expectExit: true, |
| 54 | + expectPrintVersion: fmt.Sprintf("%s", strings.TrimSpace(initialVersion.Text())), |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "truthy", |
| 58 | + flags: []string{"--version=T"}, |
| 59 | + expectGitVersion: initialVersion.GitVersion, |
| 60 | + expectExit: true, |
| 61 | + expectPrintVersion: appName + " " + initialVersion.GitVersion, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "override", |
| 65 | + flags: []string{"--version=v0.0.0-custom"}, |
| 66 | + expectGitVersion: "v0.0.0-custom", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "override and exit", |
| 70 | + flags: []string{"--version=v0.0.0-custom", "--version"}, |
| 71 | + expectGitVersion: "v0.0.0-custom", |
| 72 | + expectExit: true, |
| 73 | + expectPrintVersion: appName + " v0.0.0-custom", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "invalid override semver", |
| 77 | + flags: []string{"--version=vX"}, |
| 78 | + expectError: `could not parse "vX"`, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "invalid override major", |
| 82 | + flags: []string{"--version=v1.0.0"}, |
| 83 | + expectError: `must match major/minor/patch`, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "invalid override minor", |
| 87 | + flags: []string{"--version=v0.1.0"}, |
| 88 | + expectError: `must match major/minor/patch`, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "invalid override patch", |
| 92 | + flags: []string{"--version=v0.0.1"}, |
| 93 | + expectError: `must match major/minor/patch`, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tc := range testcases { |
| 98 | + t.Run(tc.name, func(t *testing.T) { |
| 99 | + |
| 100 | + originalOutput := output |
| 101 | + originalExit := exit |
| 102 | + |
| 103 | + outputBuffer := &bytes.Buffer{} |
| 104 | + output = outputBuffer |
| 105 | + exitCalled := false |
| 106 | + exit = func(code int) { exitCalled = true } |
| 107 | + |
| 108 | + t.Cleanup(func() { |
| 109 | + output = originalOutput |
| 110 | + exit = originalExit |
| 111 | + *versionFlag = versionValue(initialFlagValue) |
| 112 | + err := SetDynamicVersion(initialVersion.GitVersion) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 119 | + AddFlags(fs) |
| 120 | + err := fs.Parse(tc.flags) |
| 121 | + if tc.expectError != "" { |
| 122 | + if err == nil { |
| 123 | + t.Fatal("expected error, got none") |
| 124 | + } |
| 125 | + if !strings.Contains(err.Error(), tc.expectError) { |
| 126 | + t.Fatalf("expected error containing %q, got %q", tc.expectError, err.Error()) |
| 127 | + } |
| 128 | + return |
| 129 | + } else if err != nil { |
| 130 | + t.Fatalf("unexpected parse error: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + if e, a := tc.expectGitVersion, Get().GitVersion; e != a { |
| 134 | + t.Fatalf("gitversion: expected %v, got %v", e, a) |
| 135 | + } |
| 136 | + |
| 137 | + PrintAndExitIfRequested(appName) |
| 138 | + if e, a := tc.expectExit, exitCalled; e != a { |
| 139 | + t.Fatalf("exit(): expected %v, got %v", e, a) |
| 140 | + } |
| 141 | + if e, a := tc.expectPrintVersion, strings.TrimSpace(outputBuffer.String()); e != a { |
| 142 | + t.Fatalf("print version: expected %v, got %v", e, a) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments