Repository: cli/cli
Pull request #8454 (2023-12-13 13:50:15)

Fix flaky tests coupled to relative dates
## Description

This fixes https://github.com/cli/cli/issues/8452

The issue here is that the tableprinter was being provided `time.Now` rather than the injected now. There's more I'd like to look at around how we handle time in our tests since there are a few different approaches in the codebase right now but I don't want to tackle that right now.

However, since at least one of these tests will start failing from January 7th, it seems better to get this is in and get it out in a patch version before Christmas.

### Reviewer Notes

This fix can be checked by changing the system clock to the year 2025, seeing that the tests pass, then checking out the previous commit and seeing them fail.


Patch:
diff --git a/pkg/cmd/cache/list/list.go b/pkg/cmd/cache/list/list.go
index ab5d662b5..6920c2faa 100644
--- a/pkg/cmd/cache/list/list.go
+++ b/pkg/cmd/cache/list/list.go
@@ -120,8 +120,8 @@ func listRun(opts *ListOptions) error {
 		tp.AddField(opts.IO.ColorScheme().Cyan(fmt.Sprintf("%d", cache.Id)))
 		tp.AddField(cache.Key)
 		tp.AddField(humanFileSize(cache.SizeInBytes))
-		tp.AddTimeField(time.Now(), cache.CreatedAt, opts.IO.ColorScheme().Gray)
-		tp.AddTimeField(time.Now(), cache.LastAccessedAt, opts.IO.ColorScheme().Gray)
+		tp.AddTimeField(opts.Now, cache.CreatedAt, opts.IO.ColorScheme().Gray)
+		tp.AddTimeField(opts.Now, cache.LastAccessedAt, opts.IO.ColorScheme().Gray)
 		tp.EndRow()
 	}
 
diff --git a/pkg/cmd/run/list/list.go b/pkg/cmd/run/list/list.go
index 0913babf3..1d9dad6b6 100644
--- a/pkg/cmd/run/list/list.go
+++ b/pkg/cmd/run/list/list.go
@@ -158,7 +158,7 @@ func listRun(opts *ListOptions) error {
 		tp.AddField(string(run.Event))
 		tp.AddField(fmt.Sprintf("%d", run.ID), tableprinter.WithColor(cs.Cyan))
 		tp.AddField(run.Duration(opts.now).String())
-		tp.AddTimeField(time.Now(), run.StartedTime(), cs.Gray)
+		tp.AddTimeField(opts.now, run.StartedTime(), cs.Gray)
 		tp.EndRow()
 	}
 


Test patch:
diff --git a/pkg/cmd/run/list/list_test.go b/pkg/cmd/run/list/list_test.go
index 1c3062804..762cc8cb7 100644
--- a/pkg/cmd/run/list/list_test.go
+++ b/pkg/cmd/run/list/list_test.go
@@ -169,16 +169,16 @@ func TestListRun(t *testing.T) {
 			},
 			wantOut: heredoc.Doc(`
 				STATUS  TITLE        WORKFLOW  BRANCH  EVENT  ID    ELAPSED  AGE
-				X       cool commit  CI        trunk   push   1     4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   2     4m34s    about 2 years ago
-				✓       cool commit  CI        trunk   push   3     4m34s    about 2 years ago
-				X       cool commit  CI        trunk   push   4     4m34s    about 2 years ago
-				X       cool commit  CI        trunk   push   1234  4m34s    about 2 years ago
-				-       cool commit  CI        trunk   push   6     4m34s    about 2 years ago
-				-       cool commit  CI        trunk   push   7     4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   8     4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   9     4m34s    about 2 years ago
-				X       cool commit  CI        trunk   push   10    4m34s    about 2 years ago
+				X       cool commit  CI        trunk   push   1     4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   2     4m34s    about 4 minutes ago
+				✓       cool commit  CI        trunk   push   3     4m34s    about 4 minutes ago
+				X       cool commit  CI        trunk   push   4     4m34s    about 4 minutes ago
+				X       cool commit  CI        trunk   push   1234  4m34s    about 4 minutes ago
+				-       cool commit  CI        trunk   push   6     4m34s    about 4 minutes ago
+				-       cool commit  CI        trunk   push   7     4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   8     4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   9     4m34s    about 4 minutes ago
+				X       cool commit  CI        trunk   push   10    4m34s    about 4 minutes ago
 			`),
 		},
 		{
@@ -249,107 +249,107 @@ func TestListRun(t *testing.T) {
 			},
 			wantOut: heredoc.Doc(`
 				STATUS  TITLE        WORKFLOW  BRANCH  EVENT  ID   ELAPSED  AGE
-				*       cool commit  CI        trunk   push   0    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   1    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   2    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   3    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   4    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   5    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   6    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   7    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   8    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   9    4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   10   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   11   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   12   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   13   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   14   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   15   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   16   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   17   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   18   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   19   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   20   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   21   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   22   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   23   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   24   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   25   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   26   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   27   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   28   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   29   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   30   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   31   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   32   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   33   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   34   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   35   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   36   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   37   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   38   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   39   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   40   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   41   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   42   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   43   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   44   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   45   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   46   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   47   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   48   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   49   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   50   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   51   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   52   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   53   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   54   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   55   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   56   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   57   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   58   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   59   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   60   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   61   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   62   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   63   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   64   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   65   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   66   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   67   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   68   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   69   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   70   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   71   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   72   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   73   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   74   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   75   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   76   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   77   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   78   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   79   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   80   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   81   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   82   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   83   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   84   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   85   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   86   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   87   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   88   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   89   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   90   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   91   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   92   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   93   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   94   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   95   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   96   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   97   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   98   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   99   4m34s    about 2 years ago
-				*       cool commit  CI        trunk   push   100  4m34s    about 2 years ago
+				*       cool commit  CI        trunk   push   0    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   1    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   2    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   3    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   4    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   5    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   6    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   7    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   8    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   9    4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   10   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   11   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   12   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   13   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   14   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   15   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   16   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   17   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   18   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   19   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   20   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   21   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   22   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   23   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   24   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   25   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   26   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   27   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   28   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   29   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   30   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   31   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   32   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   33   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   34   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   35   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   36   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   37   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   38   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   39   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   40   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   41   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   42   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   43   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   44   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   45   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   46   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   47   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   48   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   49   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   50   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   51   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   52   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   53   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   54   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   55   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   56   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   57   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   58   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   59   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   60   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   61   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   62   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   63   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   64   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   65   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   66   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   67   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   68   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   69   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   70   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   71   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   72   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   73   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   74   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   75   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   76   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   77   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   78   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   79   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   80   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   81   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   82   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   83   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   84   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   85   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   86   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   87   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   88   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   89   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   90   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   91   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   92   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   93   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   94   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   95   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   96   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   97   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   98   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   99   4m34s    about 4 minutes ago
+				*       cool commit  CI        trunk   push   100  4m34s    about 4 minutes ago
 			`),
 		},
 		{
@@ -386,9 +386,9 @@ func TestListRun(t *testing.T) {
 			},
 			wantOut: heredoc.Doc(`
 				STATUS  TITLE        WORKFLOW    BRANCH  EVENT  ID    ELAPSED  AGE
-				*       cool commit  a workflow  trunk   push   2     4m34s    about 2 years ago
-				✓       cool commit  a workflow  trunk   push   3     4m34s    about 2 years ago
-				X       cool commit  a workflow  trunk   push   1234  4m34s    about 2 years ago
+				*       cool commit  a workflow  trunk   push   2     4m34s    about 4 minute...
+				✓       cool commit  a workflow  trunk   push   3     4m34s    about 4 minute...
+				X       cool commit  a workflow  trunk   push   1234  4m34s    about 4 minute...
 			`),
 		},
 		{