github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/version/version_test.go (about)

     1  // +build unit
     2  
     3  package version_test
     4  
     5  import (
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/jenkins-x/jx/v2/pkg/cmd/clients"
    10  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    11  	"github.com/jenkins-x/jx/v2/pkg/cmd/testhelpers"
    12  	"github.com/jenkins-x/jx/v2/pkg/cmd/version"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var versiontests = []struct {
    17  	short bool
    18  	desc  string
    19  }{
    20  	{true, "Version with short flag"},
    21  	{false, "Normal version"},
    22  }
    23  
    24  // Basic unit tests to check that all the different flags properly for jx version sub command
    25  func Test_ExecuteVersion(t *testing.T) {
    26  	for _, v := range versiontests {
    27  		t.Run(v.desc, func(t *testing.T) {
    28  			// fakeout the output for the tests
    29  			out := &testhelpers.FakeOut{}
    30  			commonOpts := opts.NewCommonOptionsWithTerm(clients.NewFactory(), os.Stdin, out, os.Stderr)
    31  
    32  			// Set batchmode to true for tests
    33  			commonOpts.BatchMode = true
    34  			command := version.NewCmdVersion(commonOpts)
    35  
    36  			switch v.short {
    37  			case true:
    38  				command.SetArgs([]string{"--short"})
    39  				err := command.Execute()
    40  				assert.NoError(t, err, "could not execute version")
    41  				assert.Contains(t, out.GetOutput(), "Version")
    42  				assert.NotContains(t, out.GetOutput(), "Commit")
    43  				assert.NotContains(t, out.GetOutput(), "Build date")
    44  				assert.NotContains(t, out.GetOutput(), "Go version")
    45  				assert.NotContains(t, out.GetOutput(), "Git tree state")
    46  			default:
    47  				err := command.Execute()
    48  				assert.NoError(t, err, "could not execute version")
    49  				assert.Contains(t, out.GetOutput(), "Version")
    50  				assert.Contains(t, out.GetOutput(), "Commit")
    51  				assert.Contains(t, out.GetOutput(), "Build date")
    52  				assert.Contains(t, out.GetOutput(), "Go version")
    53  				assert.Contains(t, out.GetOutput(), "Git tree state")
    54  			}
    55  		})
    56  	}
    57  }