This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix incorrect version info on check-update
Fixes #282
- Loading branch information
Showing
9 changed files
with
173 additions
and
100 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
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,47 @@ | ||
package update | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/profclems/glab/internal/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCheckUpdateCmd(s *utils.IOStreams, version string) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "check-update", | ||
Short: "Check for latest glab releases", | ||
Long: ``, | ||
Aliases: []string{"update"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return CheckUpdate(s, version, false) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func CheckUpdate(s *utils.IOStreams, version string, silentErr bool) error { | ||
latestRelease, err := GetUpdateInfo() | ||
if err != nil { | ||
if silentErr { | ||
return nil | ||
} | ||
return errors.New("could not check for update! Make sure you have a stable internet connection") | ||
} | ||
|
||
if isOlderVersion(latestRelease.Version, version) { | ||
fmt.Fprintf(s.StdOut, "%s %s → %s\n%s\n", | ||
utils.Yellow("A new version of glab has been released:"), | ||
utils.Red(version), utils.Green(latestRelease.Version), | ||
latestRelease.URL) | ||
} else { | ||
if silentErr { | ||
return nil | ||
} | ||
fmt.Fprintf(s.StdOut, "%v %v", utils.GreenCheck(), | ||
utils.Green("You are already using the latest version of glab")) | ||
} | ||
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,105 @@ | ||
package update | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/profclems/glab/internal/utils" | ||
) | ||
|
||
func TestNewCheckUpdateCmd(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("GET", `https://api.github.com/repos/profclems/glab/releases/latest`, | ||
httpmock.NewStringResponder(200, `{ | ||
"url": "https://api.github.com/repos/profclems/glab/releases/33385584", | ||
"html_url": "https://github.com/profclems/glab/releases/tag/v1.11.1", | ||
"tag_name": "v1.11.1", | ||
"name": "v1.11.1", | ||
"draft": false, | ||
"prerelease": false, | ||
"created_at": "2020-11-03T05:33:29Z", | ||
"published_at": "2020-11-03T05:39:04Z"}`)) | ||
|
||
ioStream, _, stdout, stderr := utils.IOTest() | ||
type args struct { | ||
s *utils.IOStreams | ||
version string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
stdOut string | ||
stdErr string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "same version", | ||
args: args{ | ||
s: ioStream, | ||
version: "v1.11.1", | ||
}, | ||
stdOut: "✓ You are already using the latest version of glab", | ||
stdErr: "", | ||
}, | ||
{ | ||
name: "older version", | ||
args: args{ | ||
s: ioStream, | ||
version: "v1.11.0", | ||
}, | ||
stdOut: "A new version of glab has been released: v1.11.0 → v1.11.1\nhttps://github.com/profclems/glab/releases/tag/v1.11.1\n", | ||
stdErr: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := NewCheckUpdateCmd(tt.args.s, tt.args.version).Execute() | ||
if tt.wantErr { | ||
assert.Nil(t, err) | ||
} | ||
|
||
assert.Equal(t, tt.stdOut, stdout.String()) | ||
assert.Equal(t, tt.stdErr, stderr.String()) | ||
|
||
// clean up | ||
stdout.Reset() | ||
stderr.Reset() | ||
}) | ||
} | ||
} | ||
|
||
func TestNewCheckUpdateCmd_error(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("GET", `https://api.github.com/repos/profclems/glab/releases/latest`, | ||
httpmock.NewErrorResponder(fmt.Errorf("an error expected"))) | ||
|
||
ioStream, _, stdout, stderr := utils.IOTest() | ||
|
||
err := NewCheckUpdateCmd(ioStream, "1.11.0").Execute() | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "could not check for update! Make sure you have a stable internet connection", err.Error()) | ||
assert.Equal(t, "", stdout.String()) | ||
assert.Equal(t, "", stderr.String()) | ||
} | ||
|
||
func TestNewCheckUpdateCmd_json_error(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("GET", `https://api.github.com/repos/profclems/glab/releases/latest`, | ||
httpmock.NewStringResponder(200, ``)) | ||
|
||
ioStream, _, stdout, stderr := utils.IOTest() | ||
|
||
err := NewCheckUpdateCmd(ioStream, "1.11.0").Execute() | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "could not check for update! Make sure you have a stable internet connection", err.Error()) | ||
assert.Equal(t, "", stdout.String()) | ||
assert.Equal(t, "", stderr.String()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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