Skip to content

Commit

Permalink
cli: use N/A as placeholder for old CREATED dates
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
imjasonh committed Sep 19, 2022
1 parent 06cbd26 commit 90db6b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cli/command/image/formatter_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *historyContext) CreatedSince() string {
return c.CreatedAt()
}
if c.h.Created <= epoch {
return ""
return "N/A"
}
created := units.HumanDuration(time.Now().UTC().Sub(time.Unix(c.h.Created, 0)))
return created + " ago"
Expand Down
21 changes: 10 additions & 11 deletions cli/command/image/formatter_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/pkg/stringid"
"gotest.tools/v3/assert"
"gotest.tools/v3/skip"
)

type historyCase struct {
Expand Down Expand Up @@ -52,8 +51,8 @@ func TestHistoryContext_ID(t *testing.T) {
}

func TestHistoryContext_CreatedSince(t *testing.T) {
skip.If(t, notUTCTimezone, "expected output requires UTC timezone")
dateStr := "2009-11-10T23:00:00Z"
longerAgo := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
dateStr := longerAgo.Local().Format(time.RFC3339)
var ctx historyContext
cases := []historyCase{
{
Expand All @@ -65,7 +64,7 @@ func TestHistoryContext_CreatedSince(t *testing.T) {
},
{
historyContext{
h: image.HistoryResponseItem{Created: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()},
h: image.HistoryResponseItem{Created: longerAgo.Unix()},
trunc: false,
human: false,
}, dateStr, ctx.CreatedSince,
Expand All @@ -76,23 +75,23 @@ func TestHistoryContext_CreatedSince(t *testing.T) {
h: image.HistoryResponseItem{Created: 0},
trunc: false,
human: true,
}, "", ctx.CreatedSince,
}, "N/A", ctx.CreatedSince,
},
{
// A time before the year 2000 is not displayed.
historyContext{
h: image.HistoryResponseItem{Created: time.Date(1980, time.November, 10, 10, 23, 0, 0, time.UTC).Unix()},
trunc: false,
human: true,
}, "", ctx.CreatedSince,
}, "N/A", ctx.CreatedSince,
},
{
// A time after 2000 is displayed.
historyContext{
h: image.HistoryResponseItem{Created: time.Date(2005, time.September, 27, 4, 37, 0, 0, time.UTC).Unix()},
h: image.HistoryResponseItem{Created: time.Now().AddDate(-11, 0, 0).Unix()},
trunc: false,
human: true,
}, "", ctx.CreatedSince,
}, "11 years ago", ctx.CreatedSince,
},
}

Expand All @@ -102,7 +101,7 @@ func TestHistoryContext_CreatedSince(t *testing.T) {
if strings.Contains(v, ",") {
test.CompareMultipleValues(t, v, c.expValue)
} else if v != c.expValue {
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
t.Fatalf("Expected %q, was %q\n", c.expValue, v)
}
}
}
Expand Down Expand Up @@ -219,15 +218,15 @@ imageID1 24 hours ago /bin/bash ls && npm i && npm run test && karma -c karm
imageID2 24 hours ago /bin/bash echo 183MB Hi
imageID3 24 hours ago /bin/bash ls 183MB Hi
imageID4 24 hours ago /bin/bash grep 183MB Hi
imageID5 /bin/bash echo 183MB Hi
imageID5 N/A /bin/bash echo 183MB Hi
imageID6 17 years ago /bin/bash echo 183MB Hi
`
const expectedTrunc = `IMAGE CREATED CREATED BY SIZE COMMENT
imageID1 24 hours ago /bin/bash ls && npm i && npm run test && kar… 183MB Hi
imageID2 24 hours ago /bin/bash echo 183MB Hi
imageID3 24 hours ago /bin/bash ls 183MB Hi
imageID4 24 hours ago /bin/bash grep 183MB Hi
imageID5 /bin/bash echo 183MB Hi
imageID5 N/A /bin/bash echo 183MB Hi
imageID6 17 years ago /bin/bash echo 183MB Hi
`

Expand Down

0 comments on commit 90db6b8

Please sign in to comment.