2022-12-02 02:08:48 +03:00
|
|
|
// Copyright 2021 OpenSSF Scorecard Authors
|
2021-06-10 23:24:33 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// 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.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCsvWriter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
out string
|
2021-10-29 19:07:46 +03:00
|
|
|
oldRepos []RepoFormat
|
|
|
|
newRepos []RepoFormat
|
2021-06-10 23:24:33 +03:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Basic",
|
2021-10-29 19:07:46 +03:00
|
|
|
oldRepos: []RepoFormat{
|
2021-06-10 23:24:33 +03:00
|
|
|
{
|
2021-10-29 19:07:46 +03:00
|
|
|
Repo: "github.com/owner1/repo1",
|
|
|
|
Metadata: []string{"meta1"},
|
2021-06-10 23:24:33 +03:00
|
|
|
},
|
2023-05-25 00:43:36 +03:00
|
|
|
{
|
|
|
|
Repo: "gitlab.com/owner3/repo3",
|
|
|
|
Metadata: []string{"meta3"},
|
|
|
|
},
|
2021-06-10 23:24:33 +03:00
|
|
|
},
|
2021-10-29 19:07:46 +03:00
|
|
|
newRepos: []RepoFormat{
|
2021-06-10 23:24:33 +03:00
|
|
|
{
|
2021-10-29 19:07:46 +03:00
|
|
|
Repo: "github.com/owner2/repo2",
|
|
|
|
Metadata: []string{"meta2"},
|
2021-06-10 23:24:33 +03:00
|
|
|
},
|
2023-05-25 00:43:36 +03:00
|
|
|
{
|
|
|
|
Repo: "gitlab.com/owner4/repo4",
|
|
|
|
Metadata: []string{"meta4"},
|
|
|
|
},
|
2021-06-10 23:24:33 +03:00
|
|
|
},
|
|
|
|
out: `repo,metadata
|
|
|
|
github.com/owner1/repo1,meta1
|
|
|
|
github.com/owner2/repo2,meta2
|
2023-05-25 00:43:36 +03:00
|
|
|
gitlab.com/owner3/repo3,meta3
|
|
|
|
gitlab.com/owner4/repo4,meta4
|
2021-06-10 23:24:33 +03:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
testcase := testcase
|
|
|
|
t.Run(testcase.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var buf bytes.Buffer
|
2021-10-29 19:07:46 +03:00
|
|
|
err := SortAndAppendTo(&buf, testcase.oldRepos, testcase.newRepos)
|
2021-06-10 23:24:33 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error while running testcase: %v", err)
|
|
|
|
}
|
|
|
|
if buf.String() != testcase.out {
|
|
|
|
t.Errorf("\nexpected: \n%s \ngot: \n%s", testcase.out, buf.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|