-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from everdrone/feat/new_update_available
Add new version update check
- Loading branch information
Showing
13 changed files
with
281 additions
and
32 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,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
tu "github.com/everdrone/grab/testutils" | ||
) | ||
|
||
func TestConfigCmd(t *testing.T) { | ||
t.Run("prints help message", func(tc *testing.T) { | ||
c, got, err := tu.ExecuteCommand(RootCmd, "config") | ||
|
||
if err != nil { | ||
tc.Fatal(err) | ||
} | ||
|
||
if c.Name() != ConfigCmd.Name() { | ||
tc.Fatalf("got: '%s', want: '%s'", c.Name(), ConfigCmd.Name()) | ||
} | ||
|
||
if !strings.HasPrefix(got, ConfigCmd.Short) { | ||
tc.Errorf("got: '%s', want: '%s'", got, ConfigCmd.Short) | ||
} | ||
}) | ||
} |
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
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,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
tu "github.com/everdrone/grab/testutils" | ||
) | ||
|
||
func TestRootCmd(t *testing.T) { | ||
t.Run("prints help message", func(tc *testing.T) { | ||
c, got, err := tu.ExecuteCommand(RootCmd, "") | ||
|
||
if err != nil { | ||
tc.Fatal(err) | ||
} | ||
|
||
if c.Name() != RootCmd.Name() { | ||
tc.Fatalf("got: '%s', want: '%s'", c.Name(), RootCmd.Name()) | ||
} | ||
|
||
if !strings.HasPrefix(got, RootCmd.Short) { | ||
tc.Errorf("got: '%s', want: '%s'", got, RootCmd.Short) | ||
} | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,34 +1,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/everdrone/grab/internal/config" | ||
tu "github.com/everdrone/grab/testutils" | ||
) | ||
|
||
func TestVersionCmd(t *testing.T) { | ||
t.Run("version", func(t *testing.T) { | ||
cmdName := "version" | ||
|
||
c, got, err := tu.ExecuteCommand(RootCmd, cmdName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := fmt.Sprintf("%s v%s %s/%s (%s)\n", | ||
"grab", | ||
config.Version, | ||
"unknown", | ||
"unknown", | ||
"unknown", | ||
) | ||
if c.Name() != cmdName { | ||
t.Fatalf("got: '%s', want: '%s'", c.Name(), cmdName) | ||
} | ||
if got != want { | ||
t.Errorf("got: %s, want: %s", got, want) | ||
} | ||
}) | ||
config.CommitHash = "abcdef0123456789" | ||
config.BuildOS = runtime.GOOS | ||
config.BuildArch = runtime.GOARCH | ||
|
||
tests := []struct { | ||
name string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
want string | ||
}{ | ||
{ | ||
name: "no updates", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"tag_name": "v` + config.Version + `"}`)) | ||
}, | ||
want: "grab v" + config.Version + " " + config.BuildOS + "/" + config.BuildArch + " (" + config.CommitHash[:7] + ")\n", | ||
}, | ||
{ | ||
name: "newer version", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"tag_name": "v987.654.321"}`)) | ||
}, | ||
want: "grab v" + config.Version + " " + config.BuildOS + "/" + config.BuildArch + " (" + config.CommitHash[:7] + ")\n\n" + | ||
"New version available " + config.Version + " → 987.654.321\n" + | ||
"https://github.com/everdrone/grab/releases/latest\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(tc *testing.T) { | ||
// start the test server | ||
ts := httptest.NewServer(http.HandlerFunc(tt.handler)) | ||
|
||
config.LatestReleaseURL = ts.URL | ||
|
||
c, got, err := tu.ExecuteCommand(RootCmd, "version") | ||
if err != nil { | ||
tc.Fatal(err) | ||
} | ||
|
||
if c.Name() != VersionCmd.Name() { | ||
tc.Fatalf("got: %s, want: %s", c.Name(), VersionCmd.Name()) | ||
} | ||
|
||
if got != tt.want { | ||
tc.Errorf("got: %s, want: %s", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
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,54 @@ | ||
package update | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/everdrone/grab/internal/config" | ||
"github.com/everdrone/grab/internal/net" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
func CheckForUpdates() (string, error) { | ||
resp, err := net.Fetch(config.LatestReleaseURL, &net.FetchOptions{ | ||
Headers: map[string]string{ | ||
"Accept": "application/vnd.github+json", | ||
}, | ||
Timeout: 1000, | ||
Retries: 1, | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var decoded map[string]interface{} | ||
if err = json.Unmarshal([]byte(resp), &decoded); err != nil { | ||
return "", err | ||
} | ||
|
||
tagName := decoded["tag_name"] | ||
if tagName == "" { | ||
return "", fmt.Errorf("no tag name") | ||
} | ||
|
||
if latest, ok := tagName.(string); ok { | ||
if latest[0] != 'v' { | ||
latest = "v" + latest | ||
} | ||
|
||
if !semver.IsValid(latest) { | ||
return "", fmt.Errorf("invalid version: %s", latest) | ||
} | ||
|
||
current := "v" + config.Version | ||
|
||
if semver.Compare(latest, current) == 1 { | ||
return latest[1:], nil | ||
} | ||
} else { | ||
return "", fmt.Errorf("invalid tag name") | ||
} | ||
|
||
return "", nil | ||
} |
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,103 @@ | ||
package update | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/everdrone/grab/internal/config" | ||
) | ||
|
||
func TestCheckForUpdates(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
handler func(w http.ResponseWriter, r *http.Request) | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no updates", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"tag_name": "v` + config.Version + `"}`)) | ||
}, | ||
want: "", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid semver", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"tag_name": "newVersion"}`)) | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid response", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"something": "else"}`)) | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid json", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"something`)) | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "empty tag name", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"tag_name": ""}`)) | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "network error", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "request times out", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
time.Sleep(time.Millisecond * 1500) | ||
|
||
w.Write([]byte(`{"tag_name": "v` + config.Version + `"}`)) | ||
}, | ||
want: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "update available", | ||
handler: func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte(`{"tag_name": "v987.654.321"}`)) | ||
}, | ||
want: "987.654.321", | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(tc *testing.T) { | ||
// start the test server | ||
ts := httptest.NewServer(http.HandlerFunc(tt.handler)) | ||
|
||
config.LatestReleaseURL = ts.URL | ||
|
||
got, err := CheckForUpdates() | ||
if (err != nil) != tt.wantErr { | ||
tc.Errorf("got error: '%v', want error: '%v'", err, tt.wantErr) | ||
} | ||
if got != tt.want { | ||
tc.Errorf("got: '%s', want: '%s'", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |