2022-12-02 02:08:48 +03:00
|
|
|
// Copyright 2021 OpenSSF Scorecard Authors
|
2021-05-15 00:32:23 +03:00
|
|
|
//
|
2021-05-24 08:01:56 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2021-05-15 00:32:23 +03:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
2021-05-24 08:01:56 +03:00
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-05-15 00:32:23 +03:00
|
|
|
|
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-27 19:04:00 +03:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2024-04-13 00:51:50 +03:00
|
|
|
sce "github.com/ossf/scorecard/v5/errors"
|
2021-05-15 00:32:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type outcome struct {
|
|
|
|
expectedErr error
|
2021-10-29 19:07:46 +03:00
|
|
|
repo RepoFormat
|
2021-05-15 00:32:23 +03:00
|
|
|
hasError bool
|
|
|
|
}
|
|
|
|
|
2022-09-08 14:32:52 +03:00
|
|
|
//nolint:gocognit
|
2021-05-15 00:32:23 +03:00
|
|
|
func TestCsvIterator(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-10-21 23:08:56 +03:00
|
|
|
|
2021-05-15 00:32:23 +03:00
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
outcomes []outcome
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Basic",
|
|
|
|
filename: "testdata/basic.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-25 00:43:36 +03:00
|
|
|
{
|
|
|
|
name: "BasicGitlabOnly",
|
|
|
|
filename: "testdata/basic-gitlab-only.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "gitlab.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "gitlab.com/owner3/path1/repo2",
|
|
|
|
Metadata: []string{"meta"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BasicWithGitlab",
|
|
|
|
filename: "testdata/basic-with-gitlab.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "gitlab.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "gitlab.com/owner3/path1/repo2",
|
|
|
|
Metadata: []string{"meta"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-19 03:33:45 +03:00
|
|
|
{
|
|
|
|
name: "Comment",
|
|
|
|
filename: "testdata/comment.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
2021-07-19 03:33:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
2021-07-19 03:33:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
2021-07-19 03:33:45 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-15 00:32:23 +03:00
|
|
|
{
|
|
|
|
name: "FailingURLs",
|
|
|
|
filename: "testdata/failing_urls.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: true,
|
2024-04-18 08:49:12 +03:00
|
|
|
expectedErr: sce.ErrInvalidURL,
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: true,
|
2024-04-18 08:49:12 +03:00
|
|
|
expectedErr: sce.ErrInvalidURL,
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: true,
|
2024-04-18 08:49:12 +03:00
|
|
|
expectedErr: sce.ErrInvalidURL,
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "EmptyRows",
|
|
|
|
filename: "testdata/empty_row.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ExtraColumns",
|
|
|
|
filename: "testdata/extra_column.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
2021-10-29 19:07:46 +03:00
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
2021-05-15 00:32:23 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-09-14 04:57:31 +03:00
|
|
|
{
|
|
|
|
name: "Ignore First Header Row",
|
|
|
|
filename: "testdata/ignore_header.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// will error due to GitHub URL sanity check
|
|
|
|
hasError: true,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "repo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "No Header Row",
|
|
|
|
filename: "testdata/no_header.csv",
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Only Header Row",
|
|
|
|
filename: "testdata/only_header.csv",
|
|
|
|
outcomes: []outcome{},
|
|
|
|
},
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
testcase := testcase
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
testFile, err := os.OpenFile(testcase.filename, os.O_RDONLY, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to open %s: %v", testcase.filename, err)
|
|
|
|
}
|
|
|
|
defer testFile.Close()
|
|
|
|
|
2021-05-20 04:00:44 +03:00
|
|
|
testReader, err := MakeIteratorFrom(testFile)
|
2021-05-15 00:32:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create reader: %v", err)
|
|
|
|
}
|
|
|
|
for _, outcome := range testcase.outcomes {
|
|
|
|
if !testReader.HasNext() {
|
|
|
|
t.Error("expected outcome, got EOF")
|
|
|
|
}
|
|
|
|
repoURL, err := testReader.Next()
|
|
|
|
if (err != nil) != outcome.hasError {
|
|
|
|
t.Errorf("expected hasError: %t, got: %v", outcome.hasError, err)
|
|
|
|
}
|
2021-10-21 23:08:56 +03:00
|
|
|
|
|
|
|
if !outcome.hasError {
|
2021-10-29 19:07:46 +03:00
|
|
|
if !cmp.Equal(outcome.repo, repoURL) {
|
|
|
|
t.Errorf("expected equal, got diff: %s", cmp.Diff(outcome.repo, repoURL))
|
2021-10-21 23:08:56 +03:00
|
|
|
}
|
2021-05-15 00:32:23 +03:00
|
|
|
}
|
|
|
|
if outcome.hasError && outcome.expectedErr != nil && !errors.Is(err, outcome.expectedErr) {
|
|
|
|
t.Errorf("expected error: %v, got %v", outcome.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if testReader.HasNext() {
|
|
|
|
t.Error("actual reader has more repos than expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 14:32:52 +03:00
|
|
|
|
|
|
|
//nolint:gocognit
|
|
|
|
func TestNestedIterator(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
filenames []string
|
|
|
|
outcomes []outcome
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Multiple files",
|
|
|
|
filenames: []string{"testdata/basic.csv", "testdata/split_file.csv"},
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner4/repo4",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner5/repo5",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Empty first file",
|
|
|
|
filenames: []string{"testdata/split_file_empty.csv", "testdata/basic.csv"},
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one file",
|
|
|
|
filenames: []string{"testdata/basic.csv"},
|
|
|
|
outcomes: []outcome{
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner2/repo2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
hasError: false,
|
|
|
|
repo: RepoFormat{
|
|
|
|
Repo: "github.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
testcase := testcase
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var iters []Iterator
|
|
|
|
for _, file := range testcase.filenames {
|
|
|
|
testFile, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to open %s: %v", file, err)
|
|
|
|
}
|
|
|
|
defer testFile.Close()
|
|
|
|
iter, err := MakeIteratorFrom(testFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create reader: %v", err)
|
|
|
|
}
|
|
|
|
iters = append(iters, iter)
|
|
|
|
}
|
|
|
|
|
|
|
|
testReader, err := MakeNestedIterator(iters)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create reader: %v", err)
|
|
|
|
}
|
|
|
|
for _, outcome := range testcase.outcomes {
|
|
|
|
if !testReader.HasNext() {
|
|
|
|
t.Error("expected outcome, got EOF")
|
|
|
|
}
|
|
|
|
repoURL, err := testReader.Next()
|
|
|
|
if (err != nil) != outcome.hasError {
|
|
|
|
t.Errorf("expected hasError: %t, got: %v", outcome.hasError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !outcome.hasError {
|
|
|
|
if !cmp.Equal(outcome.repo, repoURL) {
|
|
|
|
t.Errorf("expected equal, got diff: %s", cmp.Diff(outcome.repo, repoURL))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if outcome.hasError && outcome.expectedErr != nil && !errors.Is(err, outcome.expectedErr) {
|
|
|
|
t.Errorf("expected error: %v, got %v", outcome.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if testReader.HasNext() {
|
|
|
|
t.Error("actual reader has more repos than expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|