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

Included tests for internal/collector/source #310

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion internal/collector/depsdev/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type depsDevSet struct {
}

func (s *depsDevSet) Namespace() signal.Namespace {
return signal.Namespace("depsdev")
return "depsdev"
}

type depsDevSource struct {
Expand Down
184 changes: 184 additions & 0 deletions internal/collector/depsdev/source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package depsdev

import (
"context"
"net/url"
"reflect"
"testing"
"time"

"github.com/golang/mock/gomock"
"go.uber.org/zap"

"github.com/ossf/criticality_score/internal/collector/signal"
mocks "github.com/ossf/criticality_score/internal/mock"
)

func Test_parseRepoURL(t *testing.T) {
tests := []struct {
name string
u *url.URL
wantProjectName string
wantProjectType string
}{
{
name: "github.com",
u: &url.URL{
Host: "github.com",
},
wantProjectName: "",
wantProjectType: "GITHUB",
},
{
name: "random",
u: &url.URL{
Host: "random",
},
wantProjectName: "",
wantProjectType: "",
},
}
for _, tt := range tests {
nathannaveen marked this conversation as resolved.
Show resolved Hide resolved
t.Run(tt.name, func(t *testing.T) {
gotProjectName, gotProjectType := parseRepoURL(tt.u)
if gotProjectName != tt.wantProjectName {
t.Errorf("parseRepoURL() gotProjectName = %v, want %v", gotProjectName, tt.wantProjectName)
}
if gotProjectType != tt.wantProjectType {
t.Errorf("parseRepoURL() gotProjectType = %v, want %v", gotProjectType, tt.wantProjectType)
}
})
}
}

func Test_depsDevSource_Get(t *testing.T) {
type args struct {
ctx context.Context
rHostName string
jobID string
}
tests := []struct { //nolint:govet
nathannaveen marked this conversation as resolved.
Show resolved Hide resolved
name string
logger *zap.Logger
dependents *dependents
args args
want signal.Set
wantErr bool
}{
{
name: "invalid url",
logger: zap.NewNop(),
dependents: &dependents{},
args: args{
ctx: context.Background(),
rHostName: "random",
},
want: &depsDevSet{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := mocks.NewMockRepo(ctrl)

r.EXPECT().URL().Return(&url.URL{Host: tt.args.rHostName}).AnyTimes()
nathannaveen marked this conversation as resolved.
Show resolved Hide resolved

c := &depsDevSource{
logger: tt.logger,
dependents: tt.dependents,
}
got, err := c.Get(tt.args.ctx, r, tt.args.jobID)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
}
})
}
}

func TestNewSource(t *testing.T) {
type args struct {
ctx context.Context
logger *zap.Logger
projectID string
datasetName string
datasetTTL time.Duration
}
test := struct { //nolint:govet
name string
args args
want signal.Source
wantErr bool
}{
name: "new client error",
args: args{
ctx: context.Background(),
logger: zap.NewNop(),
projectID: "",
datasetName: "dataset",
datasetTTL: time.Hour,
},
wantErr: true,
}
got, err := NewSource(test.args.ctx, test.args.logger, test.args.projectID, test.args.datasetName, test.args.datasetTTL)
if (err != nil) != test.wantErr {
t.Errorf("NewSource() error = %v, wantErr %v", err, test.wantErr)
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("NewSource() got = %v, want %v", got, test.want)
}
}

func Test_depsDevSource_IsSupported(t *testing.T) {
type fields struct {
logger *zap.Logger
dependents *dependents
}
tests := []struct {
name string
fields fields
rHostName string
want bool
}{
{
name: "github",
fields: fields{
logger: zap.NewNop(),
},
rHostName: "github.com",
want: true,
},
{
name: "random",
fields: fields{
logger: zap.NewNop(),
},
rHostName: "random",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &depsDevSource{
logger: tt.fields.logger,
dependents: tt.fields.dependents,
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := mocks.NewMockRepo(ctrl)

r.EXPECT().URL().Return(&url.URL{Host: tt.rHostName}).AnyTimes()

if got := c.IsSupported(r); got != tt.want {
t.Errorf("IsSupported() = %v, want %v", got, tt.want)
}
})
}
}
103 changes: 103 additions & 0 deletions internal/mock/repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.