-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update CheckForNewVersion to use the GitHub location (#18)
* Update CheckForNewVersion to use the GitHub location Add test * Trim version number from 'v' * Remove commented io import * Add and test generateDownloadURL * Update generateDownloadURL to produce a .zip url for Windows * Reindent TestGenerateDownloadURL * Update the log message to be specific to Windows and non-Windows systems * Add new line at the EOF * Remove the redundant version compare term * Fix a bug * Add v to remote version number
- Loading branch information
Showing
2 changed files
with
189 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package datasetUtils | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"bytes" | ||
"log" | ||
) | ||
|
||
func TestFetchLatestVersion(t *testing.T) { | ||
// Create a mock HTTP server | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Return a JSON response similar to the GitHub API | ||
w.Write([]byte(`{"tag_name": "v1.0.0"}`)) | ||
})) | ||
defer server.Close() | ||
|
||
// Replace GitHubAPI with the URL of the mock server | ||
oldGitHubAPI := GitHubAPI | ||
GitHubAPI = server.URL | ||
defer func() { GitHubAPI = oldGitHubAPI }() | ||
|
||
// Create a mock HTTP client | ||
client := server.Client() | ||
|
||
// Call fetchLatestVersion | ||
version, err := fetchLatestVersion(client) | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
|
||
// Check the version number | ||
if version != "v1.0.0" { | ||
t.Errorf("Expected version v1.0.0, got %s", version) | ||
} | ||
} | ||
|
||
func TestGenerateDownloadURL(t *testing.T) { | ||
deployLocation := "https://github.com/paulscherrerinstitute/scicat-cli/releases/download" | ||
latestVersion := "0.1.0" | ||
|
||
testCases := []struct { | ||
osName string | ||
expectedURL string | ||
}{ | ||
{ | ||
osName: "Linux", | ||
expectedURL: "https://github.com/paulscherrerinstitute/scicat-cli/releases/download/v0.1.0/scicat-cli_.0.1.0_Linux_x86_64.tar.gz", | ||
}, | ||
{ | ||
osName: "Windows", | ||
expectedURL: "https://github.com/paulscherrerinstitute/scicat-cli/releases/download/v0.1.0/scicat-cli_.0.1.0_Windows_x86_64.zip", | ||
}, | ||
{ | ||
osName: "Darwin", | ||
expectedURL: "https://github.com/paulscherrerinstitute/scicat-cli/releases/download/v0.1.0/scicat-cli_.0.1.0_Darwin_x86_64.tar.gz", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
actualURL := generateDownloadURL(deployLocation, latestVersion, testCase.osName) | ||
|
||
if actualURL != testCase.expectedURL { | ||
t.Errorf("Expected URL to be %s, but got %s", testCase.expectedURL, actualURL) | ||
} | ||
} | ||
} | ||
|
||
func TestCheckForNewVersion(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
currentVersion string | ||
mockResponse string | ||
expectedLog string | ||
}{ | ||
{ | ||
name: "New version available", | ||
currentVersion: "0.9.0", | ||
mockResponse: `{"tag_name": "v1.0.0"}`, | ||
expectedLog: "You should upgrade to a newer version", | ||
}, | ||
{ | ||
name: "No new version available", | ||
currentVersion: "1.0.0", | ||
mockResponse: `{"tag_name": "v1.0.0"}`, | ||
expectedLog: "Your version of this program is up-to-date", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Create a mock HTTP server | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Return a JSON response similar to the GitHub API | ||
w.Write([]byte(tt.mockResponse)) | ||
})) | ||
defer server.Close() | ||
|
||
// Replace GitHubAPI with the URL of the mock server | ||
oldGitHubAPI := GitHubAPI | ||
GitHubAPI = server.URL | ||
defer func() { GitHubAPI = oldGitHubAPI }() | ||
|
||
// Create a mock HTTP client | ||
client := server.Client() | ||
|
||
// Call CheckForNewVersion | ||
CheckForNewVersion(client, "test", tt.currentVersion, false) | ||
|
||
// Check the log output | ||
logOutput := getLogOutput() | ||
if !strings.Contains(logOutput, tt.expectedLog) { | ||
t.Errorf("Expected log message not found: %s", logOutput) | ||
} | ||
|
||
// Clear the log buffer after each test | ||
buf.Reset() | ||
}) | ||
} | ||
} | ||
|
||
var buf bytes.Buffer | ||
|
||
func init() { | ||
// Redirect the output of the logger to buf | ||
log.SetOutput(&buf) | ||
} | ||
|
||
func getLogOutput() string { | ||
return buf.String() | ||
} |