From 63fff3c84090a64a81dca399eea2b8d6817b343e Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Mon, 16 Oct 2023 16:05:12 -0700 Subject: [PATCH] :sparkles: scdiff: improve `compare` usability (#3573) * fallback to cron style when parsing dates. The cron output was never updated in #2712. In the interim, support both formats. Signed-off-by: Spencer Schrock * continue on first diff, to highlight all differences. Signed-off-by: Spencer Schrock * tests for date fallback. Signed-off-by: Spencer Schrock --------- Signed-off-by: Spencer Schrock --- cmd/internal/scdiff/app/compare.go | 7 ++++- pkg/json.go | 5 ++++ pkg/json_test.go | 41 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/cmd/internal/scdiff/app/compare.go b/cmd/internal/scdiff/app/compare.go index 8ff1117ffcc..b0ff869802e 100644 --- a/cmd/internal/scdiff/app/compare.go +++ b/cmd/internal/scdiff/app/compare.go @@ -68,6 +68,8 @@ func compareReaders(x, y io.Reader, output io.Writer) error { xs.Buffer(nil, maxResultSize) ys := bufio.NewScanner(y) ys.Buffer(nil, maxResultSize) + + var differs bool for { if shouldContinue, err := advanceScanners(xs, ys); err != nil { return err @@ -82,9 +84,12 @@ func compareReaders(x, y io.Reader, output io.Writer) error { // go-cmp says its not production ready. Is this a valid usage? // it certainly helps with readability. fmt.Fprintf(output, "%s\n", cmp.Diff(xResult, yResult)) - return errResultsDiffer + differs = true } } + if differs { + return errResultsDiffer + } return nil } diff --git a/pkg/json.go b/pkg/json.go index 1382cc9f086..a1fab73f4a6 100644 --- a/pkg/json.go +++ b/pkg/json.go @@ -16,6 +16,7 @@ package pkg import ( "encoding/json" + "errors" "fmt" "io" "time" @@ -185,7 +186,11 @@ func ExperimentalFromJSON2(r io.Reader) (result ScorecardResult, score float64, return ScorecardResult{}, 0, fmt.Errorf("decode json: %w", err) } + var parseErr *time.ParseError date, err := time.Parse(time.RFC3339, jsr.Date) + if errors.As(err, &parseErr) { + date, err = time.Parse("2006-01-02", jsr.Date) + } if err != nil { return ScorecardResult{}, 0, fmt.Errorf("parse scorecard analysis time: %w", err) } diff --git a/pkg/json_test.go b/pkg/json_test.go index b9836bb8323..285a0694018 100644 --- a/pkg/json_test.go +++ b/pkg/json_test.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "path" + "strings" "testing" "time" @@ -490,3 +491,43 @@ func TestJSONOutput(t *testing.T) { }) } } + +func TestExperimentalFromJSON2_time(t *testing.T) { + t.Parallel() + //nolint:lll,govet // result strings are long + tests := []struct { + name string + result string + want time.Time + wantErr bool + }{ + { + name: "main RFC3339 format", + result: `{"date":"2006-01-02T15:04:05+00:00","repo":{"name":"github.com/foo/bar","commit":"HEAD"},"score":-1.0,"metadata":null}`, + want: time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC), + }, + { + name: "backup 2006-01-02 format", + result: `{"date":"2023-09-26","repo":{"name":"github.com/foo/bar","commit":"HEAD"},"score":-1.0,"metadata":null}`, + want: time.Date(2023, time.September, 26, 0, 0, 0, 0, time.UTC), + }, + { + name: "not RFC3339 or 2006-01-02 format", + result: `{"date":"January 1, 2023","repo":{"name":"github.com/foo/bar","commit":"HEAD"},"score":-1.0,"metadata":null}`, + wantErr: true, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + got, _, err := ExperimentalFromJSON2(strings.NewReader(tt.result)) + if tt.wantErr != (err != nil) { + t.Fatalf("got: %v, wantedErr: %v", err, tt.wantErr) + } + if !got.Date.Equal(tt.want) { + t.Errorf("got: %v, wanted: %v", got.Date, tt.want) + } + }) + } +}