Skip to content

Commit

Permalink
Make checkForNewVersion function not stop the code (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kavir1698 authored Apr 8, 2024
1 parent 06457f6 commit 817002b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 48 deletions.
5 changes: 1 addition & 4 deletions cmd/datasetArchiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func main() {
}

// check for program version only if running interactively
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}
datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})

if *testenvFlag {
APIServer = TEST_API_SERVER
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetCleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ func main() {
}

// check for program version only if running interactively
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}
datasetUtils.CheckForNewVersion(client, APP, VERSION, !*nonInteractiveFlag, datasetUtils.StdinUserInput{})
datasetUtils.CheckForServiceAvailability(client, *testenvFlag, true)

//}
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetGetProposal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func main() {
}

// check for program version only if running interactively
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}
datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})

if *testenvFlag {
APIServer = TEST_API_SERVER
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetIngestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ func main() {
}

// check for program version only if running interactively
err := datasetUtils.CheckForNewVersion(client, APP, VERSION, !*noninteractiveFlag, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}
datasetUtils.CheckForNewVersion(client, APP, VERSION, !*noninteractiveFlag, datasetUtils.StdinUserInput{})
datasetUtils.CheckForServiceAvailability(client, *testenvFlag, *autoarchiveFlag)

//}
Expand Down
5 changes: 1 addition & 4 deletions cmd/datasetRetriever/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func main() {
return
}

err := datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})
if err != nil {
log.Fatalf("Error checking for new version: %v", err)
}
datasetUtils.CheckForNewVersion(client, APP, VERSION, true, datasetUtils.StdinUserInput{})

var env string
if *testenvFlag {
Expand Down
21 changes: 10 additions & 11 deletions datasetUtils/checkForNewVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ func generateDownloadURL(deployLocation, latestVersion, osName string) string {
return fmt.Sprintf("%s/v%s/scicat-cli_.%s_%s_x86_64.tar.gz", deployLocation, latestVersion, latestVersion, strings.Title(osName))
}

func CheckForNewVersion(client *http.Client, APP string, VERSION string, interactiveFlag bool, userInput UserInput) error {
func CheckForNewVersion(client *http.Client, APP string, VERSION string, interactiveFlag bool, userInput UserInput) {
// avoid checking for new version in test mode
if os.Getenv("TEST_MODE") == "true" {
return nil
return
}
latestVersion, err := fetchLatestVersion(client)
if err != nil {
log.Printf("Can not find info about latest version for this program: %s\n", err)
return err
log.Printf("Warning: Can not find info about latest version for this program: %s\n", err)
return
}

latestVersion = strings.TrimPrefix(latestVersion, "v")
_, err = strconv.Atoi(strings.Split(latestVersion, ".")[0])
if err != nil {
log.Fatalf("Illegal latest version number:%v", latestVersion)
log.Printf("Warning: Illegal latest version number:%v\n", latestVersion)
}
_, err = strconv.Atoi(strings.Split(VERSION, ".")[0])
if err != nil {
log.Fatalf("Illegal version number:%v", VERSION)
log.Printf("Warning: Illegal version number:%v\n", VERSION)
}
log.Printf("Latest version: %s", latestVersion)

Expand Down Expand Up @@ -109,13 +109,12 @@ func CheckForNewVersion(client *http.Client, APP string, VERSION string, interac
log.Print("Do you want to continue with current version (y/N) ? ")
continueyn, err := userInput.ReadLine()
if err != nil {
return fmt.Errorf("failed to read user input: %v", err)
}
if strings.TrimSpace(continueyn) != "y" {
return fmt.Errorf("Execution stopped, please update the program now.")
log.Printf("Warning: Failed to read user input: %v\n", err)
} else if strings.TrimSpace(continueyn) != "y" {
log.Println("Warning: Execution stopped, please update the program now.")
}
}
return nil
return
}

// UserInput is an interface that defines a method to read a line of input. We use this so that we can test interactive mode.
Expand Down
22 changes: 5 additions & 17 deletions datasetUtils/checkForNewVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"bytes"
"log"
"errors"
)

func TestFetchLatestVersion(t *testing.T) {
Expand Down Expand Up @@ -83,7 +82,6 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion string
mockResponse string
expectedLog string
expectedError error
interactiveFlag bool
userInput string
}{
Expand All @@ -92,7 +90,6 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "You should upgrade to a newer version",
expectedError: nil,
interactiveFlag: false,
userInput: "y\n",
},
Expand All @@ -101,7 +98,6 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion: "1.0.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "Your version of this program is up-to-date",
expectedError: nil,
interactiveFlag: false,
userInput: "y\n",
},
Expand All @@ -110,16 +106,14 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "You should upgrade to a newer version",
expectedError: nil,
interactiveFlag: true,
userInput: "y\n",
},
{
name: "New version available, interactive mode, no upgrade",
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v1.0.0"}`,
expectedLog: "Execution stopped, please update the program now.",
expectedError: errors.New("Execution stopped, please update the program now."),
expectedLog: "Warning: Execution stopped, please update the program now.",
interactiveFlag: true,
userInput: "n\n",
},
Expand All @@ -128,7 +122,6 @@ func TestCheckForNewVersion(t *testing.T) {
currentVersion: "0.9.0",
mockResponse: `{"tag_name": "v0.9.1"}`,
expectedLog: "You should upgrade to a newer version",
expectedError: nil,
interactiveFlag: true,
userInput: "y\n",
},
Expand All @@ -152,17 +145,12 @@ func TestCheckForNewVersion(t *testing.T) {
client := server.Client()

// Call CheckForNewVersion
err := CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput})
if err != nil && err.Error() != tt.expectedError.Error() {
t.Errorf("got error %v, want %v", err, tt.expectedLog)
}
CheckForNewVersion(client, "test", tt.currentVersion, tt.interactiveFlag, MockUserInput{Input: tt.userInput})

// Check the log output
if tt.userInput == "y\n" {
logOutput := getLogOutput()
if !strings.Contains(logOutput, tt.expectedLog) {
t.Errorf("Expected log message not found: %s", logOutput)
}
logOutput := getLogOutput()
if !strings.Contains(logOutput, tt.expectedLog) {
t.Errorf("Expected log message not found: %s", logOutput)
}

// Clear the log buffer after each test
Expand Down

0 comments on commit 817002b

Please sign in to comment.