diff --git a/utils/cliutils/persistence_test.go b/utils/cliutils/persistence_test.go index f26fae96b..842d89d90 100644 --- a/utils/cliutils/persistence_test.go +++ b/utils/cliutils/persistence_test.go @@ -5,6 +5,8 @@ import ( "path/filepath" "testing" "time" + + "github.com/stretchr/testify/assert" ) // TestSetAndGetLatestVersionCheckTime tests setting and getting the LatestCliVersionCheckTime @@ -15,20 +17,14 @@ func TestSetAndGetLatestVersionCheckTime(t *testing.T) { // Set the timestamp timestamp := time.Now().UnixMilli() err := SetCliLatestVersionCheckTime(timestamp) - if err != nil { - t.Fatalf("Failed to set LatestCliVersionCheckTime: %v", err) - } + assert.NoError(t, err, "Failed to set LatestCliVersionCheckTime") // Get the timestamp storedTimestamp, err := GetLatestCliVersionCheckTime() - if err != nil { - t.Fatalf("Failed to get LatestCliVersionCheckTime: %v", err) - } + assert.NoError(t, err, "Failed to get LatestCliVersionCheckTime") // Assert equality - if *storedTimestamp != timestamp { - t.Fatalf("Expected %d, got %d", timestamp, *storedTimestamp) - } + assert.Equal(t, timestamp, *storedTimestamp, "Stored timestamp does not match the set timestamp") } // TestSetAndGetAiTermsVersion tests setting and getting the LatestAiTermsRevision @@ -39,20 +35,14 @@ func TestSetAndGetAiTermsVersion(t *testing.T) { // Set the AI terms version version := 42 err := SetLatestAiTermsRevision(version) - if err != nil { - t.Fatalf("Failed to set LatestAiTermsRevision: %v", err) - } + assert.NoError(t, err, "Failed to set LatestAiTermsRevision") // Get the AI terms version storedVersion, err := GetLatestAiTermsRevision() - if err != nil { - t.Fatalf("Failed to get LatestAiTermsRevision: %v", err) - } + assert.NoError(t, err, "Failed to get LatestAiTermsRevision") // Assert equality - if *storedVersion != version { - t.Fatalf("Expected %d, got %d", version, *storedVersion) - } + assert.Equal(t, version, *storedVersion, "Stored AI terms version does not match the set version") } // TestPersistenceFileCreation tests if the persistence file is created when it doesn't exist @@ -62,20 +52,14 @@ func TestPersistenceFileCreation(t *testing.T) { // Ensure the persistence file doesn't exist _, err := os.Stat(persistenceFilePath) - if !os.IsNotExist(err) { - t.Fatalf("Expected file to not exist, but it does: %v", err) - } + assert.ErrorIs(t, err, os.ErrNotExist, "Expected error to be os.ErrNotExist") // Trigger file creation by setting version check time timestamp := time.Now().UnixMilli() err = SetCliLatestVersionCheckTime(timestamp) - if err != nil { - t.Fatalf("Failed to set LatestCliVersionCheckTime: %v", err) - } + assert.NoError(t, err, "Failed to set LatestCliVersionCheckTime") // Verify the persistence file was created _, err = os.Stat(persistenceFilePath) - if os.IsNotExist(err) { - t.Fatalf("Expected file to exist, but it does not: %v", err) - } + assert.False(t, os.IsNotExist(err), "Expected file to exist, but it does not") } diff --git a/utils/cliutils/utils.go b/utils/cliutils/utils.go index ff4da947e..3f49eb0c2 100644 --- a/utils/cliutils/utils.go +++ b/utils/cliutils/utils.go @@ -694,6 +694,9 @@ func GetDetailedSummary(c *cli.Context) bool { return c.Bool("detailed-summary") || commandsummary.ShouldRecordSummary() } +// Check if the latest CLI version should be checked via the GitHub API to let the user know if a newer version is available. +// To avoid checking this for every run, we check only if the last check was more than 6 hours ago. +// Also, if the user set the JFROG_CLI_AVOID_NEW_VERSION_WARNING environment variable to true, we won't check. func shouldCheckLatestCliVersion() (shouldCheck bool, err error) { if strings.ToLower(os.Getenv(JfrogCliAvoidNewVersionWarning)) == "true" { return