From d465b7f5d5852f35f9539a7c6d6451e9b899f4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 25 Jan 2021 13:46:30 +0100 Subject: [PATCH] Freeze time in `issue view` test --- pkg/cmd/issue/view/view.go | 8 ++++---- pkg/cmd/issue/view/view_test.go | 35 +++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/issue/view/view.go b/pkg/cmd/issue/view/view.go index 24867b32b..917bdd0a2 100644 --- a/pkg/cmd/issue/view/view.go +++ b/pkg/cmd/issue/view/view.go @@ -9,7 +9,6 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/api" - "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/pkg/cmd/issue/shared" issueShared "github.com/cli/cli/pkg/cmd/issue/shared" @@ -23,20 +22,21 @@ import ( type ViewOptions struct { HttpClient func() (*http.Client, error) - Config func() (config.Config, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) SelectorArg string WebMode bool Comments bool + + Now func() time.Time } func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command { opts := &ViewOptions{ IO: f.IOStreams, HttpClient: f.HttpClient, - Config: f.Config, + Now: time.Now, } cmd := &cobra.Command{ @@ -141,7 +141,7 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error { func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error { out := opts.IO.Out - now := time.Now() + now := opts.Now() ago := now.Sub(issue.CreatedAt) cs := opts.IO.ColorScheme() diff --git a/pkg/cmd/issue/view/view_test.go b/pkg/cmd/issue/view/view_test.go index c908aa6c5..1cfc0db88 100644 --- a/pkg/cmd/issue/view/view_test.go +++ b/pkg/cmd/issue/view/view_test.go @@ -7,6 +7,7 @@ import ( "net/http" "os/exec" "testing" + "time" "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" @@ -251,20 +252,38 @@ func TestIssueView_tty_Preview(t *testing.T) { } for name, tc := range tests { t.Run(name, func(t *testing.T) { - http := &httpmock.Registry{} - defer http.Verify(t) + io, _, stdout, stderr := iostreams.Test() + io.SetStdoutTTY(true) + io.SetStdinTTY(true) + io.SetStderrTTY(true) - http.Register(httpmock.GraphQL(`query IssueByNumber\b`), httpmock.FileResponse(tc.fixture)) + httpReg := &httpmock.Registry{} + defer httpReg.Verify(t) - output, err := runCommand(http, true, "123") - if err != nil { - t.Errorf("error running `issue view`: %v", err) + httpReg.Register(httpmock.GraphQL(`query IssueByNumber\b`), httpmock.FileResponse(tc.fixture)) + + opts := ViewOptions{ + IO: io, + Now: func() time.Time { + t, _ := time.Parse(time.RFC822, "03 Nov 20 15:04 UTC") + return t + }, + HttpClient: func() (*http.Client, error) { + return &http.Client{Transport: httpReg}, nil + }, + BaseRepo: func() (ghrepo.Interface, error) { + return ghrepo.New("OWNER", "REPO"), nil + }, + SelectorArg: "123", } - assert.Equal(t, "", output.Stderr()) + err := viewRun(&opts) + assert.NoError(t, err) + + assert.Equal(t, "", stderr.String()) //nolint:staticcheck // prefer exact matchers over ExpectLines - test.ExpectLines(t, output.String(), tc.expectedOutputs...) + test.ExpectLines(t, stdout.String(), tc.expectedOutputs...) }) } }