Skip to content

Commit 1231ddd

Browse files
committed
Add test
1 parent de98dbd commit 1231ddd

File tree

7 files changed

+149
-91
lines changed

7 files changed

+149
-91
lines changed

api/client.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/ioutil"
99
"net/http"
10+
"path"
1011
)
1112

1213
// ClientOption represents an argument to NewClient
@@ -98,6 +99,39 @@ func (c Client) GraphQL(query string, variables map[string]interface{}, data int
9899
return handleResponse(resp, data)
99100
}
100101

102+
// REST performs a REST request and parses the response.
103+
func (c Client) REST(method string, p string, data interface{}) error {
104+
url := path.Join("https://api.github.com/", p)
105+
req, err := http.NewRequest(method, url, nil)
106+
if err != nil {
107+
return err
108+
}
109+
req.Header.Set("Content-Type", "application/json; charset=utf-8")
110+
111+
resp, err := c.http.Do(req)
112+
if err != nil {
113+
return err
114+
}
115+
defer resp.Body.Close()
116+
117+
success := resp.StatusCode >= 200 && resp.StatusCode < 300
118+
if !success {
119+
return handleHTTPError(resp)
120+
}
121+
122+
body, err := ioutil.ReadAll(resp.Body)
123+
if err != nil {
124+
return err
125+
}
126+
127+
err = json.Unmarshal(body, &data)
128+
if err != nil {
129+
return err
130+
}
131+
132+
return nil
133+
}
134+
101135
func handleResponse(resp *http.Response, data interface{}) error {
102136
success := resp.StatusCode >= 200 && resp.StatusCode < 300
103137

command/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ var BuildDate = "YYYY-MM-DD"
2121

2222
func init() {
2323
RootCmd.Version = fmt.Sprintf("%s (%s)", strings.TrimPrefix(Version, "v"), BuildDate)
24-
RootCmd.AddCommand(versionCmd)
24+
RootCmd.AddCommand(versionCmd)
2525

26-
RootCmd.PersistentFlags().StringP("repo", "R", "", "Current GitHub repository")
26+
RootCmd.PersistentFlags().StringP("repo", "R", "", "Current GitHub repository")
2727
RootCmd.PersistentFlags().Bool("help", false, "Show help for command")
2828
RootCmd.Flags().Bool("version", false, "Print gh version")
2929
// TODO:
@@ -66,6 +66,10 @@ var initContext = func() context.Context {
6666
return ctx
6767
}
6868

69+
func BasicClient() (*api.Client, error) {
70+
return apiClientForContext(initContext())
71+
}
72+
6973
func contextForCommand(cmd *cobra.Command) context.Context {
7074
ctx := initContext()
7175
if repo, err := cmd.Flags().GetString("repo"); err == nil && repo != "" {

main.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,31 @@ import (
1010
)
1111

1212
func main() {
13-
isProduction := os.Getenv("APP_ENV") != "production"
14-
update.RunWhileCheckingForUpdate(isProduction, func() {
15-
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
16-
fmt.Fprintln(os.Stderr, err)
17-
_, isFlagError := err.(command.FlagError)
18-
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
19-
fmt.Fprintln(os.Stderr, cmd.UsageString())
20-
}
21-
os.Exit(1)
13+
alertMsgChan := make(chan *string)
14+
go updateInBackground(alertMsgChan)
15+
16+
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
17+
fmt.Fprintln(os.Stderr, err)
18+
_, isFlagError := err.(command.FlagError)
19+
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
20+
fmt.Fprintln(os.Stderr, cmd.UsageString())
2221
}
23-
})
22+
os.Exit(1)
23+
}
24+
25+
alertMsg := <-alertMsgChan
26+
if alertMsg != nil {
27+
fmt.Fprintf(os.Stderr, *alertMsg)
28+
}
29+
}
30+
31+
func updateInBackground(alertMsgChan chan *string) {
32+
client, err := command.BasicClient()
33+
if err != nil {
34+
alertMsgChan <- nil
35+
return
36+
}
37+
38+
alertMsg := update.UpdateMessage(client)
39+
alertMsgChan <- alertMsg
2440
}

test/fixtures/latestRelease.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"tag_name": "v1.0.0",
3+
"html_url": "https://www.spacejam.com/archive/spacejam/movie/jam.htm"
4+
}

update/checkForUpdate.go

Lines changed: 0 additions & 79 deletions
This file was deleted.

update/update.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package update
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/github/gh-cli/api"
8+
"github.com/github/gh-cli/command"
9+
"github.com/github/gh-cli/utils"
10+
)
11+
12+
const nwo = "github/homebrew-gh"
13+
14+
type releaseInfo struct {
15+
Version string `json:"tag_name"`
16+
URL string `json:"html_url"`
17+
}
18+
19+
func UpdateMessage(client *api.Client) *string {
20+
latestRelease, err := getLatestRelease(client)
21+
if err != nil {
22+
fmt.Fprintf(os.Stderr, "%+v", err)
23+
return nil
24+
}
25+
26+
updateAvailable := latestRelease.Version != command.Version
27+
if updateAvailable {
28+
alertMsg := fmt.Sprintf(utils.Cyan(`
29+
A new version of gh is available! %s → %s
30+
Changelog: %s
31+
Run 'brew upgrade gh' to update!`)+"\n\n", command.Version, latestRelease.Version, latestRelease.URL)
32+
return &alertMsg
33+
}
34+
35+
return nil
36+
}
37+
38+
func getLatestRelease(client *api.Client) (*releaseInfo, error) {
39+
path := fmt.Sprintf("repos/%s/releases/latest", nwo)
40+
var r releaseInfo
41+
err := client.REST("GET", path, &r)
42+
return &r, err
43+
}

update/update_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package update
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
9+
"github.com/github/gh-cli/api"
10+
"github.com/github/gh-cli/command"
11+
)
12+
13+
func TestRunWhileCheckingForUpdate(t *testing.T) {
14+
originalVersion := command.Version
15+
command.Version = "v0.0.0"
16+
defer func() {
17+
command.Version = originalVersion
18+
}()
19+
20+
http := &api.FakeHTTP{}
21+
jsonFile, _ := os.Open("../test/fixtures/latestRelease.json")
22+
defer jsonFile.Close()
23+
http.StubResponse(200, jsonFile)
24+
25+
client := api.NewClient(api.ReplaceTripper(http))
26+
alertMsg := *UpdateMessage(client)
27+
fmt.Printf("🌭 %+v\n", alertMsg)
28+
29+
if !strings.Contains(alertMsg, command.Version) {
30+
t.Errorf("expected: \"%v\" to contain \"%v\"", alertMsg, command.Version)
31+
}
32+
33+
if !strings.Contains(alertMsg, "v1.0.0") {
34+
t.Errorf("expected: \"%v\" to contain \"%v\"", alertMsg, "v1.0.0")
35+
}
36+
}

0 commit comments

Comments
 (0)