Skip to content

Commit

Permalink
Mock current time in issue view tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiand391 committed Jan 25, 2021
1 parent 6e2c1b3 commit 8ef5cfe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/cmd/issue/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ func printRawIssuePreview(out io.Writer, issue *api.Issue) error {
return nil
}

var now = func() time.Time {
return time.Now()
}

func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error {
out := opts.IO.Out
now := time.Now()
ago := now.Sub(issue.CreatedAt)
currentTime := now()
ago := currentTime.Sub(issue.CreatedAt)
cs := opts.IO.ColorScheme()

// Header (Title and State)
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/issue/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os/exec"
"testing"
"time"

"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghrepo"
Expand Down Expand Up @@ -251,6 +252,8 @@ func TestIssueView_tty_Preview(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
parsed, _ := time.Parse(time.RFC3339, "2021-01-23T04:06:55Z")
now = func() time.Time { return parsed }
http := &httpmock.Registry{}
defer http.Verify(t)

Expand Down

0 comments on commit 8ef5cfe

Please sign in to comment.