Stub out time.Now for search tests (#6299)
This commit is contained in:
parent
0ecd424901
commit
de83df12d2
4 changed files with 22 additions and 11 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue