From de83df12d272966d8ab18b4cb42721473113097d Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Mon, 19 Sep 2022 14:54:18 +0400 Subject: [PATCH] Stub out time.Now for search tests (#6299) --- pkg/cmd/search/repos/repos.go | 12 ++++++++---- pkg/cmd/search/repos/repos_test.go | 4 +++- pkg/cmd/search/shared/shared.go | 10 +++++++--- pkg/cmd/search/shared/shared_test.go | 7 ++++--- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/search/repos/repos.go b/pkg/cmd/search/repos/repos.go index 412b36cbb..23e5bfbef 100644 --- a/pkg/cmd/search/repos/repos.go +++ b/pkg/cmd/search/repos/repos.go @@ -20,6 +20,7 @@ type ReposOptions struct { Browser browser.Browser Exporter cmdutil.Exporter IO *iostreams.IOStreams + Now time.Time Query search.Query Searcher search.Searcher WebMode bool @@ -148,10 +149,14 @@ func reposRun(opts *ReposOptions) error { if len(result.Items) == 0 { return cmdutil.NewNoResultsError("no repositories matched your search") } - return displayResults(io, result) + + return displayResults(io, opts.Now, result) } -func displayResults(io *iostreams.IOStreams, results search.RepositoriesResult) error { +func displayResults(io *iostreams.IOStreams, now time.Time, results search.RepositoriesResult) error { + if now.IsZero() { + now = time.Now() + } cs := io.ColorScheme() tp := utils.NewTablePrinter(io) for _, repo := range results.Items { @@ -172,13 +177,12 @@ func displayResults(io *iostreams.IOStreams, results search.RepositoriesResult) tp.AddField(text.RemoveExcessiveWhitespace(description), nil, nil) tp.AddField(info, nil, infoColor) if tp.IsTTY() { - tp.AddField(text.FuzzyAgoAbbr(time.Now(), repo.UpdatedAt), nil, cs.Gray) + tp.AddField(text.FuzzyAgoAbbr(now, repo.UpdatedAt), nil, cs.Gray) } else { tp.AddField(repo.UpdatedAt.Format(time.RFC3339), nil, nil) } tp.EndRow() } - if io.IsStdoutTTY() { header := fmt.Sprintf("Showing %d of %d repositories\n\n", len(results.Items), results.Total) fmt.Fprintf(io.Out, "\n%s", header) diff --git a/pkg/cmd/search/repos/repos_test.go b/pkg/cmd/search/repos/repos_test.go index 010ee84cc..d0410b196 100644 --- a/pkg/cmd/search/repos/repos_test.go +++ b/pkg/cmd/search/repos/repos_test.go @@ -149,6 +149,8 @@ func TestNewCmdRepos(t *testing.T) { } func TestReposRun(t *testing.T) { + var now = time.Date(2022, 2, 28, 12, 30, 0, 0, time.UTC) + var updatedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) var query = search.Query{ Keywords: []string{"cli"}, Kind: "repositories", @@ -158,7 +160,6 @@ func TestReposRun(t *testing.T) { Topic: []string{"golang"}, }, } - var updatedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) tests := []struct { errMsg string name string @@ -270,6 +271,7 @@ func TestReposRun(t *testing.T) { ios.SetStdoutTTY(tt.tty) ios.SetStderrTTY(tt.tty) tt.opts.IO = ios + tt.opts.Now = now t.Run(tt.name, func(t *testing.T) { err := reposRun(tt.opts) if tt.wantErr { diff --git a/pkg/cmd/search/shared/shared.go b/pkg/cmd/search/shared/shared.go index 7531cfedf..f670548b3 100644 --- a/pkg/cmd/search/shared/shared.go +++ b/pkg/cmd/search/shared/shared.go @@ -31,6 +31,7 @@ type IssuesOptions struct { Entity EntityType Exporter cmdutil.Exporter IO *iostreams.IOStreams + Now time.Time Query search.Query Searcher search.Searcher WebMode bool @@ -88,10 +89,13 @@ func SearchIssues(opts *IssuesOptions) error { return cmdutil.NewNoResultsError(msg) } - return displayIssueResults(io, opts.Entity, result) + return displayIssueResults(io, opts.Now, opts.Entity, result) } -func displayIssueResults(io *iostreams.IOStreams, et EntityType, results search.IssuesResult) error { +func displayIssueResults(io *iostreams.IOStreams, now time.Time, et EntityType, results search.IssuesResult) error { + if now.IsZero() { + now = time.Now() + } cs := io.ColorScheme() tp := utils.NewTablePrinter(io) for _, issue := range results.Items { @@ -120,7 +124,7 @@ func displayIssueResults(io *iostreams.IOStreams, et EntityType, results search. tp.AddField(text.RemoveExcessiveWhitespace(issue.Title), nil, nil) tp.AddField(listIssueLabels(&issue, cs, tp.IsTTY()), nil, nil) if tp.IsTTY() { - tp.AddField(text.FuzzyAgo(time.Now(), issue.UpdatedAt), nil, cs.Gray) + tp.AddField(text.FuzzyAgo(now, issue.UpdatedAt), nil, cs.Gray) } else { tp.AddField(issue.UpdatedAt.String(), nil, nil) } diff --git a/pkg/cmd/search/shared/shared_test.go b/pkg/cmd/search/shared/shared_test.go index 66c9c9cc2..fe6c8c57b 100644 --- a/pkg/cmd/search/shared/shared_test.go +++ b/pkg/cmd/search/shared/shared_test.go @@ -23,7 +23,9 @@ func TestSearcher(t *testing.T) { } func TestSearchIssues(t *testing.T) { - query := search.Query{ + var now = time.Date(2022, 2, 28, 12, 30, 0, 0, time.UTC) + var updatedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) + var query = search.Query{ Keywords: []string{"keyword"}, Kind: "issues", Limit: 30, @@ -33,8 +35,6 @@ func TestSearchIssues(t *testing.T) { Is: []string{"public", "locked"}, }, } - - var updatedAt = time.Date(2021, 2, 28, 12, 30, 0, 0, time.UTC) tests := []struct { errMsg string name string @@ -193,6 +193,7 @@ func TestSearchIssues(t *testing.T) { ios.SetStdoutTTY(tt.tty) ios.SetStderrTTY(tt.tty) tt.opts.IO = ios + tt.opts.Now = now t.Run(tt.name, func(t *testing.T) { err := SearchIssues(tt.opts) if tt.wantErr {