Refactor issue list and pr list tests

following #6085

* Inject fake `Now` function to tests of `issue list` / `pr list`
* Remove `regexp` from the tests
This commit is contained in:
satotake 2022-08-25 00:01:29 +09:00
parent 6955db471d
commit b3ab76818c
6 changed files with 45 additions and 36 deletions

View file

@ -9,7 +9,7 @@
"number": 1,
"title": "number won",
"url": "https://wow.com",
"updatedAt": "2011-01-26T19:01:12Z",
"updatedAt": "2022-08-24T22:01:12Z",
"labels": {
"nodes": [
{
@ -23,7 +23,7 @@
"number": 2,
"title": "number too",
"url": "https://wow.com",
"updatedAt": "2011-01-26T19:01:12Z",
"updatedAt": "2022-07-20T19:01:12Z",
"labels": {
"nodes": [
{
@ -37,7 +37,7 @@
"number": 4,
"title": "number fore",
"url": "https://wow.com",
"updatedAt": "2011-01-26T19:01:12Z",
"updatedAt": "2020-01-26T19:01:12Z",
"labels": {
"nodes": [
{
@ -51,4 +51,4 @@
}
}
}
}
}

View file

@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"strings"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
@ -39,6 +40,8 @@ type ListOptions struct {
Mention string
Milestone string
Search string
Now func() time.Time
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
@ -47,6 +50,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
HttpClient: f.HttpClient,
Config: f.Config,
Browser: f.Browser,
Now: time.Now,
}
var appAuthor string

View file

@ -4,8 +4,8 @@ import (
"bytes"
"io"
"net/http"
"regexp"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/browser"
@ -40,7 +40,14 @@ func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, err
},
}
cmd := NewCmdList(factory, nil)
fakeNow := func() time.Time {
return time.Date(2022, time.August, 24, 23, 50, 0, 0, time.UTC)
}
cmd := NewCmdList(factory, func(opts *ListOptions) error {
opts.Now = fakeNow
return listRun(opts)
})
argv, err := shlex.Split(cli)
if err != nil {
@ -93,18 +100,14 @@ func TestIssueList_tty(t *testing.T) {
t.Errorf("error running command `issue list`: %v", err)
}
out := output.String()
timeRE := regexp.MustCompile(`\d+ years`)
out = timeRE.ReplaceAllString(out, "X years")
assert.Equal(t, heredoc.Doc(`
Showing 3 of 3 open issues in OWNER/REPO
#1 number won label about X years ago
#2 number too label about X years ago
#4 number fore label about X years ago
`), out)
#1 number won label less than a minute ago
#2 number too label about 1 month ago
#4 number fore label about 2 years ago
`), output.String())
assert.Equal(t, ``, output.Stderr())
}

View file

@ -8,7 +8,7 @@
"number": 32,
"title": "New feature",
"url": "https://github.com/monalisa/hello/pull/32",
"createtedAt": "2011-01-26T19:01:12Z",
"createdAt": "2022-08-24T20:01:12Z",
"headRefName": "feature",
"state": "OPEN",
"isDraft": true
@ -17,7 +17,7 @@
"number": 29,
"title": "Fixed bad bug",
"url": "https://github.com/monalisa/hello/pull/29",
"createtedAt": "2011-01-26T19:01:12Z",
"createdAt": "2022-07-20T19:01:12Z",
"headRefName": "bug-fix",
"state": "OPEN",
"isDraft": false,
@ -31,7 +31,7 @@
"state": "MERGED",
"isDraft": false,
"title": "Improve documentation",
"createtedAt": "2011-01-26T19:01:12Z",
"createdAt": "2020-01-26T19:01:12Z",
"url": "https://github.com/monalisa/hello/pull/28",
"headRefName": "docs"
}

View file

@ -37,6 +37,8 @@ type ListOptions struct {
Assignee string
Search string
Draft *bool
Now func() time.Time
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
@ -44,6 +46,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
IO: f.IOStreams,
HttpClient: f.HttpClient,
Browser: f.Browser,
Now: time.Now,
}
var appAuthor string
@ -201,7 +204,7 @@ func listRun(opts *ListOptions) error {
if table.IsTTY() {
prNum = "#" + prNum
}
now := time.Now()
now := opts.Now()
ago := now.Sub(pr.CreatedAt)
table.AddField(prNum, nil, cs.ColorFromString(shared.ColorForPRState(pr)))

View file

@ -4,9 +4,9 @@ import (
"bytes"
"io"
"net/http"
"regexp"
"strings"
"testing"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/browser"
@ -38,7 +38,14 @@ func runCommand(rt http.RoundTripper, isTTY bool, cli string) (*test.CmdOut, err
},
}
cmd := NewCmdList(factory, nil)
fakeNow := func() time.Time {
return time.Date(2022, time.August, 24, 23, 50, 0, 0, time.UTC)
}
cmd := NewCmdList(factory, func(opts *ListOptions) error {
opts.Now = fakeNow
return listRun(opts)
})
argv, err := shlex.Split(cli)
if err != nil {
@ -73,18 +80,14 @@ func TestPRList(t *testing.T) {
t.Fatal(err)
}
out := output.String()
timeRE := regexp.MustCompile(`\d+ years`)
out = timeRE.ReplaceAllString(out, "X years")
assert.Equal(t, heredoc.Doc(`
Showing 3 of 3 open pull requests in OWNER/REPO
#32 New feature feature about X years ago
#29 Fixed bad bug hubot:bug-fix about X years ago
#28 Improve documentation docs about X years ago
`), out)
#32 New feature feature about 3 hours ago
#29 Fixed bad bug hubot:bug-fix about 1 month ago
#28 Improve documentation docs about 2 years ago
`), output.String())
assert.Equal(t, ``, output.Stderr())
}
@ -101,14 +104,10 @@ func TestPRList_nontty(t *testing.T) {
assert.Equal(t, "", output.Stderr())
out := output.String()
timeRE := regexp.MustCompile(`\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d .\d\d\d\d UTC`)
out = timeRE.ReplaceAllString(out, "XXXX-XX-XX XX:XX:XX +XXXX UTC")
assert.Equal(t, `32 New feature feature DRAFT XXXX-XX-XX XX:XX:XX +XXXX UTC
29 Fixed bad bug hubot:bug-fix OPEN XXXX-XX-XX XX:XX:XX +XXXX UTC
28 Improve documentation docs MERGED XXXX-XX-XX XX:XX:XX +XXXX UTC
`, out)
assert.Equal(t, `32 New feature feature DRAFT 2022-08-24 20:01:12 +0000 UTC
29 Fixed bad bug hubot:bug-fix OPEN 2022-07-20 19:01:12 +0000 UTC
28 Improve documentation docs MERGED 2020-01-26 19:01:12 +0000 UTC
`, output.String())
}
func TestPRList_filtering(t *testing.T) {