Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ scdiff: improve compare usability #3573

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}