@@ -19,6 +19,7 @@ import (
1919 configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
2020 "github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
2121
22+ "github.com/vmware-tanzu/tanzu-cli/pkg/buildinfo"
2223 "github.com/vmware-tanzu/tanzu-cli/pkg/catalog"
2324 "github.com/vmware-tanzu/tanzu-cli/pkg/cli"
2425 "github.com/vmware-tanzu/tanzu-cli/pkg/constants"
@@ -98,6 +99,7 @@ type testCLIEnvironment struct {
9899 cacheDir string
99100 configFile string
100101 configFileNG string
102+ dataStore string
101103 envVars []string
102104}
103105
@@ -120,6 +122,9 @@ func setupTestCLIEnvironment(t *testing.T) testCLIEnvironment {
120122 os .Setenv ("TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER" , "No" )
121123 os .Setenv ("TANZU_CLI_EULA_PROMPT_ANSWER" , "Yes" )
122124
125+ tmpDataStoreFile , _ := os .CreateTemp ("" , "data-store.yaml" )
126+ os .Setenv ("TEST_CUSTOM_DATA_STORE_FILE" , tmpDataStoreFile .Name ())
127+
123128 // Start each test with the defaults of the target commands
124129 // and reset the help flag in case it was set
125130 tmcCmd .ResetCommands ()
@@ -145,12 +150,15 @@ func setupTestCLIEnvironment(t *testing.T) testCLIEnvironment {
145150 cacheDir : dir ,
146151 configFile : configFile .Name (),
147152 configFileNG : configFileNG .Name (),
153+ dataStore : tmpDataStoreFile .Name (),
154+
148155 envVars : []string {
149156 "TEST_CUSTOM_CATALOG_CACHE_DIR" ,
150157 "TANZU_CONFIG" ,
151158 "TANZU_CONFIG_NEXT_GEN" ,
152159 "TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER" ,
153160 "TANZU_CLI_EULA_PROMPT_ANSWER" ,
161+ "TEST_CUSTOM_DATA_STORE_FILE" ,
154162 },
155163 }
156164}
@@ -160,6 +168,7 @@ func tearDownTestCLIEnvironment(env testCLIEnvironment) {
160168 os .RemoveAll (env .cacheDir )
161169 os .RemoveAll (env .configFile )
162170 os .RemoveAll (env .configFileNG )
171+ os .RemoveAll (env .dataStore )
163172
164173 for _ , envVar := range env .envVars {
165174 os .Unsetenv (envVar )
@@ -895,6 +904,48 @@ func TestGlobalInit(t *testing.T) {
895904 }
896905}
897906
907+ func TestSetLastVersion (t * testing.T ) {
908+ tests := []struct {
909+ test string
910+ version string
911+ expectedVersion string
912+ }{
913+ {
914+ test : "set version to v1.2.3" ,
915+ version : "v1.2.3" ,
916+ expectedVersion : "v1.2.3" ,
917+ },
918+ }
919+
920+ originalVersion := buildinfo .Version
921+ defer func () {
922+ buildinfo .Version = originalVersion
923+ }()
924+
925+ for _ , spec := range tests {
926+ env := setupTestCLIEnvironment (t )
927+ defer tearDownTestCLIEnvironment (env )
928+
929+ t .Run (spec .test , func (t * testing.T ) {
930+ assert := assert .New (t )
931+
932+ buildinfo .Version = spec .version
933+
934+ rootCmd , err := NewRootCmd ()
935+ assert .Nil (err )
936+ // Execute any command to trigger the version update
937+ rootCmd .SetArgs ([]string {"plugin" , "list" })
938+ err = rootCmd .Execute ()
939+ assert .Nil (err )
940+
941+ // Read the data store file and make sure it contains the last executed version
942+ b , err := os .ReadFile (env .dataStore )
943+ assert .Nil (err )
944+ assert .Contains (string (b ), spec .expectedVersion )
945+ })
946+ }
947+ }
948+
898949func TestEnvVarsSet (t * testing.T ) {
899950 env := setupTestCLIEnvironment (t )
900951 defer tearDownTestCLIEnvironment (env )
0 commit comments