Merge pull request #2843 from cli/freeze-time-in-test

Freeze time in `issue view` test
This commit is contained in:
Mislav Marohnić 2021-01-25 13:56:00 +01:00 committed by GitHub
commit a881c130c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 12 deletions

View file

@ -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()

View file

@ -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...)
})
}
}