From b3ab76818cb2fdb7815644e0824ba8f895b18e10 Mon Sep 17 00:00:00 2001 From: satotake Date: Thu, 25 Aug 2022 00:01:29 +0900 Subject: [PATCH] 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 --- pkg/cmd/issue/list/fixtures/issueList.json | 8 ++--- pkg/cmd/issue/list/list.go | 4 +++ pkg/cmd/issue/list/list_test.go | 23 +++++++------- pkg/cmd/pr/list/fixtures/prList.json | 6 ++-- pkg/cmd/pr/list/list.go | 5 +++- pkg/cmd/pr/list/list_test.go | 35 +++++++++++----------- 6 files changed, 45 insertions(+), 36 deletions(-) diff --git a/pkg/cmd/issue/list/fixtures/issueList.json b/pkg/cmd/issue/list/fixtures/issueList.json index 8af96da1a..526695533 100644 --- a/pkg/cmd/issue/list/fixtures/issueList.json +++ b/pkg/cmd/issue/list/fixtures/issueList.json @@ -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 @@ } } } -} \ No newline at end of file +} diff --git a/pkg/cmd/issue/list/list.go b/pkg/cmd/issue/list/list.go index 99774d718..6ff04e9b5 100644 --- a/pkg/cmd/issue/list/list.go +++ b/pkg/cmd/issue/list/list.go @@ -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 diff --git a/pkg/cmd/issue/list/list_test.go b/pkg/cmd/issue/list/list_test.go index 645c83f01..15df64eaf 100644 --- a/pkg/cmd/issue/list/list_test.go +++ b/pkg/cmd/issue/list/list_test.go @@ -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()) } diff --git a/pkg/cmd/pr/list/fixtures/prList.json b/pkg/cmd/pr/list/fixtures/prList.json index eb2b5d31f..ae247b14b 100644 --- a/pkg/cmd/pr/list/fixtures/prList.json +++ b/pkg/cmd/pr/list/fixtures/prList.json @@ -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" } diff --git a/pkg/cmd/pr/list/list.go b/pkg/cmd/pr/list/list.go index ff0f93788..258a70b51 100644 --- a/pkg/cmd/pr/list/list.go +++ b/pkg/cmd/pr/list/list.go @@ -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))) diff --git a/pkg/cmd/pr/list/list_test.go b/pkg/cmd/pr/list/list_test.go index 318bad63d..bc661f104 100644 --- a/pkg/cmd/pr/list/list_test.go +++ b/pkg/cmd/pr/list/list_test.go @@ -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) {