Skip to content

Commit 9428df4

Browse files
committed
Fix tests
1 parent deac6da commit 9428df4

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/github/gh-cli/command"
99
"github.com/github/gh-cli/update"
1010
"github.com/github/gh-cli/utils"
11-
"github.com/mattn/go-isatty"
1211
"github.com/mgutz/ansi"
12+
"github.com/mitchellh/go-homedir"
1313
)
1414

1515
var updaterEnabled = ""
@@ -45,8 +45,9 @@ func main() {
4545
}
4646

4747
func shouldCheckForUpdate() bool {
48-
errFd := os.Stderr.Fd()
49-
return updaterEnabled != "" && (isatty.IsTerminal(errFd) || isatty.IsCygwinTerminal(errFd))
48+
// errFd := os.Stderr.Fd()
49+
// return updaterEnabled != "" && (isatty.IsTerminal(errFd) || isatty.IsCygwinTerminal(errFd))
50+
return true
5051
}
5152

5253
func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
@@ -60,5 +61,9 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
6061
}
6162

6263
repo := updaterEnabled
63-
return update.CheckForUpdate(client, repo, currentVersion)
64+
stateFilePath, err := homedir.Expand("~/.config/gh/state.yml")
65+
if err != nil {
66+
return nil, err
67+
}
68+
return update.CheckForUpdate(client, stateFilePath, repo, currentVersion)
6469
}

update/update.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77

88
"github.com/github/gh-cli/api"
99
"github.com/hashicorp/go-version"
10-
"github.com/mitchellh/go-homedir"
1110
"gopkg.in/yaml.v3"
1211
)
1312

1413
// ReleaseInfo stores information about a release
1514
type ReleaseInfo struct {
16-
Version string
17-
URL string
15+
Version string `json:"tag_name"`
16+
URL string `json:"html_url"`
1817
}
1918

2019
type StateEntry struct {
@@ -23,21 +22,22 @@ type StateEntry struct {
2322
}
2423

2524
// CheckForUpdate checks whether this software has had a newer relase on GitHub
26-
func CheckForUpdate(client *api.Client, repo, currentVersion string) (*ReleaseInfo, error) {
27-
latestRelease, err := getLatestReleaseInfo(client, repo, currentVersion)
25+
func CheckForUpdate(client *api.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) {
26+
latestRelease, err := getLatestReleaseInfo(client, stateFilePath, repo, currentVersion)
2827
if err != nil {
2928
return nil, err
3029
}
3130

31+
fmt.Printf("🌭 %+v -> %+v\n", latestRelease.Version, currentVersion)
3232
if versionGreaterThan(latestRelease.Version, currentVersion) {
3333
return latestRelease, nil
3434
}
3535

3636
return nil, nil
3737
}
3838

39-
func getLatestReleaseInfo(client *api.Client, repo, currentVersion string) (*ReleaseInfo, error) {
40-
stateEntry, err := getStateEntry()
39+
func getLatestReleaseInfo(client *api.Client, stateFilePath, repo, currentVersion string) (*ReleaseInfo, error) {
40+
stateEntry, err := getStateEntry(stateFilePath)
4141
if err != nil {
4242
return nil, err
4343
}
@@ -52,26 +52,23 @@ func getLatestReleaseInfo(client *api.Client, repo, currentVersion string) (*Rel
5252
if err != nil {
5353
return nil, err
5454
}
55+
fmt.Printf("🌭 latestRelease: %+v\n", latestRelease)
5556

56-
err = setStateEntry(time.Now(), latestRelease)
57+
err = setStateEntry(stateFilePath, time.Now(), latestRelease)
5758
if err != nil {
5859
return nil, err
5960
}
6061

6162
return &latestRelease, nil
6263
}
6364

64-
func stateFileName() string {
65-
f, _ := homedir.Expand("~/.config/gh/state.yml")
66-
return f
67-
}
68-
69-
func getStateEntry() (*StateEntry, error) {
70-
f := stateFileName()
71-
content, err := ioutil.ReadFile(f)
65+
func getStateEntry(stateFilePath string) (*StateEntry, error) {
66+
content, err := ioutil.ReadFile(stateFilePath)
7267
if err != nil {
68+
// State files doesn't exist, so create one with default values.
69+
lastWeek := time.Now().Add(-time.Hour * 24 * 7)
7370
data := StateEntry{
74-
CheckedForUpdateAt: time.Now(),
71+
CheckedForUpdateAt: lastWeek,
7572
LatestRelease: ReleaseInfo{
7673
Version: "v0.0.0",
7774
URL: "<?>",
@@ -81,7 +78,7 @@ func getStateEntry() (*StateEntry, error) {
8178
if err != nil {
8279
return nil, err
8380
}
84-
ioutil.WriteFile(f, content, 0600)
81+
ioutil.WriteFile(stateFilePath, content, 0600)
8582
}
8683

8784
var stateEntry StateEntry
@@ -93,7 +90,7 @@ func getStateEntry() (*StateEntry, error) {
9390
return &stateEntry, nil
9491
}
9592

96-
func setStateEntry(t time.Time, r ReleaseInfo) error {
93+
func setStateEntry(stateFilePath string, t time.Time, r ReleaseInfo) error {
9794
data := StateEntry{
9895
CheckedForUpdateAt: t,
9996
LatestRelease: r,
@@ -102,7 +99,7 @@ func setStateEntry(t time.Time, r ReleaseInfo) error {
10299
if err != nil {
103100
return err
104101
}
105-
ioutil.WriteFile(stateFileName(), content, 0600)
102+
ioutil.WriteFile(stateFilePath, content, 0600)
106103

107104
return nil
108105
}

update/update_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package update
33
import (
44
"bytes"
55
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"os"
69
"testing"
710

811
"github.com/github/gh-cli/api"
@@ -55,7 +58,7 @@ func TestCheckForUpdate(t *testing.T) {
5558
"html_url": "%s"
5659
}`, s.LatestVersion, s.LatestURL)))
5760

58-
rel, err := CheckForUpdate(client, "OWNER/REPO", s.CurrentVersion)
61+
rel, err := CheckForUpdate(client, tempFilePath(), "OWNER/REPO", s.CurrentVersion)
5962
if err != nil {
6063
t.Fatal(err)
6164
}
@@ -87,3 +90,13 @@ func TestCheckForUpdate(t *testing.T) {
8790
})
8891
}
8992
}
93+
94+
func tempFilePath() string {
95+
file, err := ioutil.TempFile("", "")
96+
if err != nil {
97+
log.Fatal(err)
98+
}
99+
os.Remove(file.Name())
100+
fmt.Printf("🌭 %+v\n", file.Name())
101+
return file.Name()
102+
}

0 commit comments

Comments
 (0)