get.porter.sh/porter@v1.3.0/pkg/porter/version_test.go (about) 1 package porter 2 3 import ( 4 "context" 5 "runtime" 6 "strings" 7 "testing" 8 9 "get.porter.sh/porter/pkg" 10 "get.porter.sh/porter/pkg/printer" 11 "get.porter.sh/porter/pkg/test" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestPrintVersion(t *testing.T) { 16 pkg.Commit = "abc123" 17 pkg.Version = "v1.2.3" 18 defer func() { 19 pkg.Commit = "" 20 pkg.Version = "" 21 }() 22 23 ctx := context.Background() 24 p := NewTestPorter(t) 25 defer p.Close() 26 27 opts := VersionOpts{} 28 err := opts.Validate() 29 require.NoError(t, err) 30 require.NoError(t, p.PrintVersion(ctx, opts)) 31 32 gotOutput := p.TestConfig.TestContext.GetOutput() 33 wantOutput := "porter v1.2.3 (abc123)" 34 if !strings.Contains(gotOutput, wantOutput) { 35 t.Fatalf("invalid output:\nWANT:\t%q\nGOT:\t%q\n", wantOutput, gotOutput) 36 } 37 } 38 39 func TestPrintJsonVersion(t *testing.T) { 40 pkg.Commit = "abc123" 41 pkg.Version = "v1.2.3" 42 defer func() { 43 pkg.Commit = "" 44 pkg.Version = "" 45 }() 46 47 ctx := context.Background() 48 p := NewTestPorter(t) 49 defer p.Close() 50 51 opts := VersionOpts{} 52 opts.RawFormat = string(printer.FormatJson) 53 err := opts.Validate() 54 require.NoError(t, err) 55 require.NoError(t, p.PrintVersion(ctx, opts)) 56 57 gotOutput := p.TestConfig.TestContext.GetOutput() 58 wantOutput := `{ 59 "name": "porter", 60 "version": "v1.2.3", 61 "commit": "abc123" 62 } 63 ` 64 if !strings.Contains(gotOutput, wantOutput) { 65 t.Fatalf("invalid output:\nWANT:\t%q\nGOT:\t%q\n", wantOutput, gotOutput) 66 } 67 } 68 69 func TestPrintDebugInfoJsonVersion(t *testing.T) { 70 // Only run this on linux + amd64 machines to simplify the test (it has different output based on the os/arch) 71 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { 72 t.Skip("skipping test because it is only for linux/amd64") 73 } 74 75 pkg.Commit = "abc123" 76 pkg.Version = "v1.2.3" 77 defer func() { 78 pkg.Commit = "" 79 pkg.Version = "" 80 }() 81 82 ctx := context.Background() 83 p := NewTestPorter(t) 84 defer p.Close() 85 86 opts := VersionOpts{System: true} 87 opts.RawFormat = string(printer.FormatJson) 88 err := opts.Validate() 89 require.Nil(t, err) 90 require.NoError(t, p.PrintVersion(ctx, opts)) 91 92 gotOutput := p.TestConfig.TestContext.GetOutput() 93 test.CompareGoldenFile(t, "testdata/version/version-output.json", gotOutput) 94 } 95 96 func TestPrintDebugInfoPlainTextVersion(t *testing.T) { 97 // Only run this on linux + amd64 machines to simplify the test (it has different output based on the os/arch) 98 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" { 99 t.Skip("skipping test because it is only for linux/amd64") 100 } 101 102 pkg.Commit = "abc123" 103 pkg.Version = "v1.2.3" 104 defer func() { 105 pkg.Commit = "" 106 pkg.Version = "" 107 }() 108 109 ctx := context.Background() 110 p := NewTestPorter(t) 111 defer p.Close() 112 113 opts := VersionOpts{System: true} 114 err := opts.Validate() 115 require.Nil(t, err) 116 require.NoError(t, p.PrintVersion(ctx, opts)) 117 118 gotOutput := p.TestConfig.TestContext.GetOutput() 119 test.CompareGoldenFile(t, "testdata/version/version-output.txt", gotOutput) 120 }