test: Fix capturing the timestamp warning (#13426)

This commit is contained in:
Joshua Powers 2023-06-13 06:15:49 -06:00 committed by GitHub
parent a77127dc45
commit 57eecb31e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 3 deletions

View File

@ -654,6 +654,15 @@ func TestParseTimestamp(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Ensure any one-time warnings are printed for each test
once = sync.Once{}
// Ensure the warnings are captured and not to stdout
var buf bytes.Buffer
backup := log.Writer()
log.SetOutput(&buf)
defer log.SetOutput(backup)
var loc *time.Location var loc *time.Location
if tt.location != "" { if tt.location != "" {
var err error var err error
@ -725,6 +734,15 @@ func TestParseTimestampInvalid(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
// Ensure any one-time warnings are printed for each test
once = sync.Once{}
// Ensure the warnings are captured and not to stdout
var buf bytes.Buffer
backup := log.Writer()
log.SetOutput(&buf)
defer log.SetOutput(backup)
_, err := ParseTimestamp(tt.format, tt.timestamp, nil) _, err := ParseTimestamp(tt.format, tt.timestamp, nil)
require.ErrorContains(t, err, tt.expected) require.ErrorContains(t, err, tt.expected)
}) })
@ -732,15 +750,18 @@ func TestParseTimestampInvalid(t *testing.T) {
} }
func TestTimestampAbbrevWarning(t *testing.T) { func TestTimestampAbbrevWarning(t *testing.T) {
// Ensure any one-time warnings are printed for each test
once = sync.Once{}
// Ensure the warnings are captured and not to stdout
var buf bytes.Buffer var buf bytes.Buffer
backup := log.Writer() backup := log.Writer()
log.SetOutput(&buf) log.SetOutput(&buf)
defer log.SetOutput(backup) defer log.SetOutput(backup)
once = sync.Once{} ts, err := ParseTimestamp("RFC1123", "Mon, 02 Jan 2006 15:04:05 EST", nil)
ts, err := ParseTimestamp("RFC1123", "Mon, 02 Jan 2006 15:04:05 MST", nil)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, 1136239445, ts.Unix()) require.EqualValues(t, 1136232245, ts.Unix())
require.Contains(t, buf.String(), "Your config is using abbreviated timezones and parsing was changed in v1.27.0") require.Contains(t, buf.String(), "Your config is using abbreviated timezones and parsing was changed in v1.27.0")
} }