Lint rule for using wrapper classes for testify suite.Suite (#2661)

Also fixup some missed instances of `suite.Suite`

----

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [x] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #2373

#### Test Plan

- [x] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2023-02-27 12:22:46 -08:00 committed by GitHub
parent 3a2203ded5
commit 896a9cfeba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 30 deletions

View File

@ -34,6 +34,10 @@ linters-settings:
- '\bpath\.(Ext|Base|Dir|Join)' - '\bpath\.(Ext|Base|Dir|Join)'
# Don't allow the typo m356 to be used in place of m365. # Don't allow the typo m356 to be used in place of m365.
- '[Mm]356' - '[Mm]356'
# Don't allow use of testify suite directly. Use one of the wrappers from
# tester/suite.go instead. Use an ignore lower down to exclude packages
# that result in import cycles if they try to use the wrapper.
- 'suite\.Suite(# tests should use one of the Suite wrappers in tester package )?'
lll: lll:
line-length: 120 line-length: 120
revive: revive:
@ -115,4 +119,22 @@ issues:
- gofumpt - gofumpt
- misspell - misspell
- errcheck - errcheck
- path: internal/tester/suite.go
linters:
- forbidigo
text: "suite.Suite"
# account package creates an import cycle with tester package.
- path: pkg/account
linters:
- forbidigo
text: "suite.Suite"
# storage package creates an import cycle with tester package.
- path: pkg/storage
linters:
- forbidigo
text: "suite.Suite"
# Not yet updated. Unclear if it should be updated.
- path: pkg/repository/loadtest
linters:
- forbidigo
text: "suite.Suite"

View File

@ -13,11 +13,11 @@ import (
) )
type BetaUnitSuite struct { type BetaUnitSuite struct {
suite.Suite tester.Suite
} }
func TestBetaUnitSuite(t *testing.T) { func TestBetaUnitSuite(t *testing.T) {
suite.Run(t, new(BetaUnitSuite)) suite.Run(t, &BetaUnitSuite{Suite: tester.NewUnitSuite(t)})
} }
func (suite *BetaUnitSuite) TestBetaService_Adapter() { func (suite *BetaUnitSuite) TestBetaService_Adapter() {

View File

@ -1,24 +1,24 @@
package api package api
import ( import (
"reflect"
"testing" "testing"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/alcionai/corso/src/internal/tester"
) )
type UsersUnitSuite struct { type UsersUnitSuite struct {
suite.Suite tester.Suite
} }
func TestUsersUnitSuite(t *testing.T) { func TestUsersUnitSuite(t *testing.T) {
suite.Run(t, new(UsersUnitSuite)) suite.Run(t, &UsersUnitSuite{Suite: tester.NewUnitSuite(t)})
} }
func (suite *UsersUnitSuite) TestValidateUser() { func (suite *UsersUnitSuite) TestValidateUser() {
t := suite.T()
name := "testuser" name := "testuser"
email := "testuser@foo.com" email := "testuser@foo.com"
id := "testID" id := "testID"
@ -28,20 +28,20 @@ func (suite *UsersUnitSuite) TestValidateUser() {
user.SetId(&id) user.SetId(&id)
tests := []struct { tests := []struct {
name string name string
args interface{} args interface{}
want models.Userable want models.Userable
wantErr bool errCheck assert.ErrorAssertionFunc
}{ }{
{ {
name: "Invalid type", name: "Invalid type",
args: string("invalid type"), args: string("invalid type"),
wantErr: true, errCheck: assert.Error,
}, },
{ {
name: "No ID", name: "No ID",
args: models.NewUser(), args: models.NewUser(),
wantErr: true, errCheck: assert.Error,
}, },
{ {
name: "No user principal name", name: "No user principal name",
@ -50,24 +50,23 @@ func (suite *UsersUnitSuite) TestValidateUser() {
u.SetId(&id) u.SetId(&id)
return u return u
}(), }(),
wantErr: true, errCheck: assert.Error,
}, },
{ {
name: "Valid User", name: "Valid User",
args: user, args: user,
want: user, want: user,
errCheck: assert.NoError,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { suite.Run(tt.name, func() {
t := suite.T()
got, err := validateUser(tt.args) got, err := validateUser(tt.args)
if (err != nil) != tt.wantErr { tt.errCheck(t, err)
t.Errorf("parseUser() error = %v, wantErr %v", err, tt.wantErr)
return assert.Equal(t, tt.want, got)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseUser() = %v, want %v", got, tt.want)
}
}) })
} }
} }