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

Move internal/collector/projectrepo/repo into a different package #279

Open
nathannaveen opened this issue Dec 13, 2022 · 0 comments
Open

Comments

@nathannaveen
Copy link
Contributor

I am in the process of creating tests for internal/collector/projectrepo/resolver. I was trying to create mocks for repo:

type Repo interface {
URL() *url.URL
}
// Factory is used to obtain new instances of Repo.
type Factory interface {
// New returns a new instance of Repo for the supplied URL.
//
// If the project can not be found, the error NoRepoFound will be returned.
//
// If the project is not valid for use, or there is any other issue creating
// the Repo, Repo will be nil and an error will be returned.
New(context.Context, *url.URL) (Repo, error)
// Match returns true if this factory can create a new instance of Repo
// repository for the given repository URL.
Match(*url.URL) bool
}

Here is what mockgen created:

// Code generated by MockGen. DO NOT EDIT.
// Source: internal/collector/projectrepo/repo.go

// Package mocks is a generated GoMock package.
package mocks

import (
	context "context"
	gomock "github.com/golang/mock/gomock"
	"github.com/ossf/criticality_score/internal/collector/projectrepo"
	url "net/url"
	reflect "reflect"
)

// MockRepo is a mock of Repo interface.
type MockRepo struct {
	ctrl     *gomock.Controller
	recorder *MockRepoMockRecorder
}

// MockRepoMockRecorder is the mock recorder for MockRepo.
type MockRepoMockRecorder struct {
	mock *MockRepo
}

// NewMockRepo creates a new mock instance.
func NewMockRepo(ctrl *gomock.Controller) *MockRepo {
	mock := &MockRepo{ctrl: ctrl}
	mock.recorder = &MockRepoMockRecorder{mock}
	return mock
}

// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockRepo) EXPECT() *MockRepoMockRecorder {
	return m.recorder
}

// URL mocks base method.
func (m *MockRepo) URL() *url.URL {
	m.ctrl.T.Helper()
	ret := m.ctrl.Call(m, "URL")
	ret0, _ := ret[0].(*url.URL)
	return ret0
}

// URL indicates an expected call of URL.
func (mr *MockRepoMockRecorder) URL() *gomock.Call {
	mr.mock.ctrl.T.Helper()
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "URL", reflect.TypeOf((*MockRepo)(nil).URL))
}

// MockFactory is a mock of Factory interface.
type MockFactory struct {
	ctrl     *gomock.Controller
	recorder *MockFactoryMockRecorder
}

// MockFactoryMockRecorder is the mock recorder for MockFactory.
type MockFactoryMockRecorder struct {
	mock *MockFactory
}

// NewMockFactory creates a new mock instance.
func NewMockFactory(ctrl *gomock.Controller) *MockFactory {
	mock := &MockFactory{ctrl: ctrl}
	mock.recorder = &MockFactoryMockRecorder{mock}
	return mock
}

// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockFactory) EXPECT() *MockFactoryMockRecorder {
	return m.recorder
}

// Match mocks base method.
func (m *MockFactory) Match(arg0 *url.URL) bool {
	m.ctrl.T.Helper()
	ret := m.ctrl.Call(m, "Match", arg0)
	ret0, _ := ret[0].(bool)
	return ret0
}

// Match indicates an expected call of Match.
func (mr *MockFactoryMockRecorder) Match(arg0 interface{}) *gomock.Call {
	mr.mock.ctrl.T.Helper()
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Match", reflect.TypeOf((*MockFactory)(nil).Match), arg0)
}

// New mocks base method.
func (m *MockFactory) New(arg0 context.Context, arg1 *url.URL) (projectrepo.Repo, error) {
	m.ctrl.T.Helper()
	ret := m.ctrl.Call(m, "New", arg0, arg1)
	ret0, _ := ret[0].(projectrepo.Repo)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}

// New indicates an expected call of New.
func (mr *MockFactoryMockRecorder) New(arg0, arg1 interface{}) *gomock.Call {
	mr.mock.ctrl.T.Helper()
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "New", reflect.TypeOf((*MockFactory)(nil).New), arg0, arg1)
}

The Issue:

The mock is using the projectrepo package and my test (which is inside projectrepo) is calling the mock package. This causes an import cycle error:

package github.com/ossf/criticality_score/internal/collector/projectrepo
imports github.com/ossf/criticality_score/internal/mocks: import cycle not allowed in test

Plausible Solution:

My suggestion would be to move repo.go into a separate package within the collector package. This is similar to collector/signal/source #277.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant