github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/version/info_test.go (about)

     1  // +build unit
     2  
     3  package version_test
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/blang/semver"
     9  	"github.com/olli-ai/jx/v2/pkg/version"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var InputNonEmpty = map[string]string{
    14  	"version":      "2.2.2",
    15  	"revision":     "abcdef",
    16  	"buildDate":    "20200526-19:28:09",
    17  	"goVersion":    "1.14.8",
    18  	"gitTreeState": "dirty",
    19  }
    20  
    21  var OutputNonEmpty = InputNonEmpty
    22  
    23  var InputEmpty = map[string]string{
    24  	"version":      "",
    25  	"revision":     "",
    26  	"buildDate":    "",
    27  	"goVersion":    "",
    28  	"gitTreeState": "",
    29  }
    30  
    31  var OutputEmpty = map[string]string{
    32  	"version":      version.TestVersion,
    33  	"revision":     version.TestRevision,
    34  	"buildDate":    version.TestBuildDate,
    35  	"goVersion":    version.TestGoVersion,
    36  	"gitTreeState": version.TestTreeState,
    37  }
    38  
    39  var getterTests = []struct {
    40  	description string
    41  	in          map[string]string
    42  	out         map[string]string
    43  }{
    44  	{
    45  		"Case 1: build passes a value",
    46  		InputNonEmpty,
    47  		OutputNonEmpty,
    48  	},
    49  	{
    50  		"Case 2: No value passed during build",
    51  		InputEmpty,
    52  		OutputEmpty,
    53  	},
    54  }
    55  
    56  func TestGetters(t *testing.T) {
    57  	for _, tt := range getterTests {
    58  		t.Run(tt.description, func(t *testing.T) {
    59  			version.Map["version"] = tt.in["version"]
    60  			version.Map["revision"] = tt.in["revision"]
    61  			version.Map["buildDate"] = tt.in["buildDate"]
    62  			version.Map["goVersion"] = tt.in["goVersion"]
    63  			version.Map["gitTreeState"] = tt.in["gitTreeState"]
    64  
    65  			result := version.GetVersion()
    66  			assert.Equal(t, tt.out["version"], result)
    67  
    68  			result = version.GetRevision()
    69  			assert.Equal(t, tt.out["revision"], result)
    70  
    71  			result = version.GetBuildDate()
    72  			assert.Equal(t, tt.out["buildDate"], result)
    73  
    74  			result = version.GetGoVersion()
    75  			assert.Equal(t, tt.out["goVersion"], result)
    76  
    77  			result = version.GetTreeState()
    78  			assert.Equal(t, tt.out["gitTreeState"], result)
    79  		})
    80  	}
    81  }
    82  
    83  // TODO refactor to encapsulate
    84  func TestGetSemverVersisonWithStandardVersion(t *testing.T) {
    85  	version.Map["version"] = "1.2.1"
    86  	result, err := version.GetSemverVersion()
    87  	expectedResult := semver.Version{Major: 1, Minor: 2, Patch: 1}
    88  	assert.NoError(t, err, "GetSemverVersion should exit without failure")
    89  	assert.Exactly(t, expectedResult, result)
    90  }
    91  
    92  // TODO refactor to encapsulate
    93  func TestGetSemverVersisonWithNonStandardVersion(t *testing.T) {
    94  	version.Map["version"] = "1.3.153-dev+7a8285f4"
    95  	result, err := version.GetSemverVersion()
    96  
    97  	prVersions := []semver.PRVersion{{VersionStr: "dev"}}
    98  	builds := []string{"7a8285f4"}
    99  	expectedResult := semver.Version{Major: 1, Minor: 3, Patch: 153, Pre: prVersions, Build: builds}
   100  	assert.NoError(t, err, "GetSemverVersion should exit without failure")
   101  	assert.Exactly(t, expectedResult, result)
   102  }