Skip to content

Commit 2b70e82

Browse files
committed
better time stub
1 parent 0d45dd8 commit 2b70e82

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

pkg/cmd/gist/list/list.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ type ListOptions struct {
1717
IO *iostreams.IOStreams
1818
HttpClient func() (*http.Client, error)
1919

20-
Since func(t time.Time) time.Duration
21-
2220
Limit int
2321
Visibility string // all, secret, public
2422
}
@@ -27,9 +25,6 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
2725
opts := &ListOptions{
2826
IO: f.IOStreams,
2927
HttpClient: f.HttpClient,
30-
Since: func(t time.Time) time.Duration {
31-
return time.Since(t)
32-
},
3328
}
3429

3530
cmd := &cobra.Command{
@@ -109,7 +104,7 @@ func listRun(opts *ListOptions) error {
109104
tp.AddField(utils.Pluralize(fileCount, "file"), nil, nil)
110105
tp.AddField(visibility, nil, visColor)
111106
if tp.IsTTY() {
112-
updatedAt := utils.FuzzyAgo(opts.Since(gist.UpdatedAt))
107+
updatedAt := utils.FuzzyAgo(time.Since(gist.UpdatedAt))
113108
tp.AddField(updatedAt, nil, cs.Gray)
114109
} else {
115110
tp.AddField(gist.UpdatedAt.String(), nil, nil)

pkg/cmd/gist/list/list_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ func TestNewCmdList(t *testing.T) {
8989

9090
func Test_listRun(t *testing.T) {
9191
tests := []struct {
92-
name string
93-
opts *ListOptions
94-
wantOut string
95-
stubs func(*httpmock.Registry)
96-
nontty bool
92+
name string
93+
opts *ListOptions
94+
wantOut string
95+
stubs func(*httpmock.Registry)
96+
nontty bool
97+
updatedAt *time.Time
9798
}{
9899
{
99100
name: "no gists",
@@ -126,20 +127,28 @@ func Test_listRun(t *testing.T) {
126127
wantOut: "1234567890 cool.txt 1 file public about 6 hours ago\n",
127128
},
128129
{
129-
name: "nontty output",
130-
opts: &ListOptions{},
131-
wantOut: "1234567890\tcool.txt\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n4567890123\t\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n2345678901\ttea leaves thwart those who court catastrophe\t2 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n3456789012\tshort desc\t11 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n",
132-
nontty: true,
130+
name: "nontty output",
131+
opts: &ListOptions{},
132+
updatedAt: &time.Time{},
133+
wantOut: "1234567890\tcool.txt\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n4567890123\t\t1 file\tpublic\t0001-01-01 00:00:00 +0000 UTC\n2345678901\ttea leaves thwart those who court catastrophe\t2 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n3456789012\tshort desc\t11 files\tsecret\t0001-01-01 00:00:00 +0000 UTC\n",
134+
nontty: true,
133135
},
134136
}
135137

136138
for _, tt := range tests {
139+
sixHoursAgo, _ := time.ParseDuration("-6h")
140+
updatedAt := time.Now().Add(sixHoursAgo)
141+
if tt.updatedAt != nil {
142+
updatedAt = *tt.updatedAt
143+
}
144+
137145
reg := &httpmock.Registry{}
138146
if tt.stubs == nil {
139147
reg.Register(httpmock.REST("GET", "gists"),
140148
httpmock.JSONResponse([]shared.Gist{
141149
{
142150
ID: "1234567890",
151+
UpdatedAt: updatedAt,
143152
Description: "",
144153
Files: map[string]*shared.GistFile{
145154
"cool.txt": {},
@@ -148,6 +157,7 @@ func Test_listRun(t *testing.T) {
148157
},
149158
{
150159
ID: "4567890123",
160+
UpdatedAt: updatedAt,
151161
Description: "",
152162
Files: map[string]*shared.GistFile{
153163
"gistfile0.txt": {},
@@ -156,6 +166,7 @@ func Test_listRun(t *testing.T) {
156166
},
157167
{
158168
ID: "2345678901",
169+
UpdatedAt: updatedAt,
159170
Description: "tea leaves thwart those who court catastrophe",
160171
Files: map[string]*shared.GistFile{
161172
"gistfile0.txt": {},
@@ -165,6 +176,7 @@ func Test_listRun(t *testing.T) {
165176
},
166177
{
167178
ID: "3456789012",
179+
UpdatedAt: updatedAt,
168180
Description: "short desc",
169181
Files: map[string]*shared.GistFile{
170182
"gistfile0.txt": {},
@@ -190,11 +202,6 @@ func Test_listRun(t *testing.T) {
190202
return &http.Client{Transport: reg}, nil
191203
}
192204

193-
tt.opts.Since = func(t time.Time) time.Duration {
194-
d, _ := time.ParseDuration("6h")
195-
return d
196-
}
197-
198205
io, _, stdout, _ := iostreams.Test()
199206
io.SetStdoutTTY(!tt.nontty)
200207
tt.opts.IO = io

0 commit comments

Comments
 (0)