Skip to content

Commit

Permalink
✨ scdiff: improve compare usability (#3573)
Browse files Browse the repository at this point in the history
* 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 <sschrock@google.com>

* continue on first diff, to highlight all differences.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* tests for date fallback.

Signed-off-by: Spencer Schrock <sschrock@google.com>

---------

Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock authored Oct 16, 2023
1 parent b9bbb82 commit 63fff3c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cmd/internal/scdiff/app/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pkg

import (
"encoding/json"
"errors"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"path"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 63fff3c

Please sign in to comment.