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

Enable golangci-lint for tests #697

Merged
merged 2 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion backend/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
run:
tests: false
output:
format: tab
skip-dirs:
Expand Down Expand Up @@ -62,6 +61,10 @@ issues:
- text: "should have a package comment, unless it's in another file for this package"
linters:
- golint
- path: _test\.go
linters:
- gosec
- dupl
exclude-use-default: false

service:
Expand Down
3 changes: 3 additions & 0 deletions backend/_example/memory_store/accessor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ func TestMemAdmin_Get(t *testing.T) {

admins, err = ms.Admins("no-site-in-db")
assert.EqualError(t, err, "site no-site-in-db not found")
assert.Empty(t, admins)

email, err = ms.Email("no-site-in-db")
assert.EqualError(t, err, "site no-site-in-db not found")
assert.Empty(t, email)

enabled, err := ms.Enabled("site1")
assert.NoError(t, err)
Expand All @@ -61,6 +63,7 @@ func TestMemAdmin_Get(t *testing.T) {

enabled, err = ms.Enabled("no-site-in-db")
assert.EqualError(t, err, "site no-site-in-db not found")
assert.False(t, enabled)

err = ms.OnEvent("site1", admin.EvCreate)
assert.NoError(t, err)
Expand Down
7 changes: 4 additions & 3 deletions backend/_example/memory_store/accessor/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ func TestMemData_FlagReadOnlyPost(t *testing.T) {
Update: engine.FlagTrue}
val, err = b.Flag(req)
assert.NoError(t, err)
assert.True(t, val)
req = engine.FlagRequest{Locator: store.Locator{SiteID: "radio-t", URL: "url-1"}, Flag: engine.ReadOnly}
val, err = b.Flag(req)
assert.NoError(t, err)
Expand Down Expand Up @@ -546,9 +547,9 @@ func TestMemData_FlagListBlocked(t *testing.T) {
assert.NoError(t, err)

blockedList := toBlocked(vv)
var blockedIds []string
for _, x := range blockedList {
blockedIds = append(blockedIds, x.ID)
var blockedIds = make([]string, len(blockedList))
for i, x := range blockedList {
blockedIds[i] = x.ID
}
require.Equal(t, 2, len(blockedList), b.metaUsers)
assert.ElementsMatch(t, []string{"user1", "user2"}, blockedIds)
Expand Down
10 changes: 5 additions & 5 deletions backend/_example/memory_store/server/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func TestRPC_admKeyHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -30,7 +30,7 @@ func TestRPC_admKeyHndl(t *testing.T) {
}

func TestRPC_admAdminsHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -44,7 +44,7 @@ func TestRPC_admAdminsHndl(t *testing.T) {
}

func TestRPC_admEmailHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -58,7 +58,7 @@ func TestRPC_admEmailHndl(t *testing.T) {
}

func TestRPC_admEnabledHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -76,7 +76,7 @@ func TestRPC_admEnabledHndl(t *testing.T) {
}

func TestRPC_admEventHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down
22 changes: 11 additions & 11 deletions backend/_example/memory_store/server/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestRPC_createHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -33,7 +33,7 @@ func TestRPC_createHndl(t *testing.T) {
}

func TestRPC_findHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -56,7 +56,7 @@ func TestRPC_findHndl(t *testing.T) {
}

func TestRPC_getHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -80,7 +80,7 @@ func TestRPC_getHndl(t *testing.T) {
}

func TestRPC_updateHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down Expand Up @@ -108,7 +108,7 @@ func TestRPC_updateHndl(t *testing.T) {
}

func TestRPC_countHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -130,7 +130,7 @@ func TestRPC_countHndl(t *testing.T) {
}

func TestRPC_infoHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -151,7 +151,7 @@ func TestRPC_infoHndl(t *testing.T) {
}

func TestRPC_flagHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down Expand Up @@ -186,7 +186,7 @@ func TestRPC_flagHndl(t *testing.T) {
}

func TestRPC_listFlagsHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down Expand Up @@ -220,7 +220,7 @@ func TestRPC_listFlagsHndl(t *testing.T) {
}

func TestRPC_userDetailHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down Expand Up @@ -273,7 +273,7 @@ func TestRPC_userDetailHndl(t *testing.T) {
}

func TestRPC_deleteHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -296,7 +296,7 @@ func TestRPC_deleteHndl(t *testing.T) {
}

func TestRPC_closeHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down
8 changes: 4 additions & 4 deletions backend/_example/memory_store/server/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func gopherPNGBytes() []byte {
}

func TestRPC_imgLoadHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -78,7 +78,7 @@ func TestRPC_imgLoadHndl(t *testing.T) {
assert.Equal(t, gopherPNGBytes(), img)

// cleanup
err = ri.Cleanup(nil, time.Second)
err = ri.Cleanup(context.TODO(), time.Second)
assert.NoError(t, err)

// load after cleanup
Expand All @@ -89,7 +89,7 @@ func TestRPC_imgLoadHndl(t *testing.T) {
}

func TestRPC_imgCommitHndlFail(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand All @@ -99,7 +99,7 @@ func TestRPC_imgCommitHndlFail(t *testing.T) {
}

func TestRPC_imgCleanupHndl(t *testing.T) {
_, port, teardown := prepTestStore(t)
port, teardown := prepTestStore(t)
defer teardown()
api := fmt.Sprintf("http://localhost:%d/test", port)

Expand Down
6 changes: 3 additions & 3 deletions backend/_example/memory_store/server/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func waitForHTTPServerStart(port int) {
}
}

func prepTestStore(t *testing.T) (s *RPC, port int, teardown func()) {
func prepTestStore(t *testing.T) (port int, teardown func()) {
mg := accessor.NewMemData()
adm := accessor.NewMemAdminStore("secret")
img := accessor.NewMemImageStore()
s = NewRPC(mg, adm, img, &jrpc.Server{API: "/test", Logger: jrpc.NoOpLogger})
s := NewRPC(mg, adm, img, &jrpc.Server{API: "/test", Logger: jrpc.NoOpLogger})

admRec := accessor.AdminRec{
SiteID: "test-site",
Expand All @@ -68,7 +68,7 @@ func prepTestStore(t *testing.T) (s *RPC, port int, teardown func()) {

waitForHTTPServerStart(port)

return s, port, func() {
return port, func() {
require.NoError(t, s.Shutdown())
}
}
1 change: 1 addition & 0 deletions backend/app/cmd/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestCleanup_IsSpam(t *testing.T) {
}

for n, tt := range tbl {
tt := tt
checkName := fmt.Sprintf("check-%d-%s", n, tt.name)
t.Run(checkName, func(t *testing.T) {
c := store.Comment{ID: checkName, Text: tt.text, Score: tt.score}
Expand Down
4 changes: 2 additions & 2 deletions backend/app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func Test_Main(t *testing.T) {
done := make(chan struct{})
go func() {
<-done
err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
require.NoError(t, err)
e := syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
require.NoError(t, e)
}()

finished := make(chan struct{})
Expand Down
2 changes: 1 addition & 1 deletion backend/app/migrator/wordpress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ var xmlTestWP = `
<category domain="post_tag" nicename="alts"><![CDATA[alts]]></category>
<category domain="post_tag" nicename="role-playing"><![CDATA[role playing]]></category>
<category domain="category" nicename="stuff"><![CDATA[Stuff]]></category>
<category domain="post_tag" nicename="wierd-in-a-cant-quite-help-myself-way"><![CDATA[wierd in a can't quite help myself way]]></category>
<category domain="post_tag" nicename="weird-in-a-cant-quite-help-myself-way"><![CDATA[weird in a can't quite help myself way]]></category>
<wp:postmeta>
<wp:meta_key><![CDATA[_edit_last]]></wp:meta_key>
<wp:meta_value><![CDATA[2]]></wp:meta_value>
Expand Down
22 changes: 12 additions & 10 deletions backend/app/notify/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestEmailNew(t *testing.T) {
},
}
for _, d := range testSet {
d := d
t.Run(d.name, func(t *testing.T) {
email, err := NewEmail(d.emailParams, d.smtpParams)

Expand Down Expand Up @@ -152,6 +153,7 @@ func TestEmailSendClientError(t *testing.T) {
err: "can't make email writer: failed to send"},
}
for _, d := range testSet {
d := d
t.Run(d.name, func(t *testing.T) {
e := Email{smtp: d.smtp}
if d.err != "" {
Expand Down Expand Up @@ -182,8 +184,8 @@ func TestEmail_Send(t *testing.T) {
email, err := NewEmail(EmailParams{From: "[email protected]"}, SMTPParams{})
assert.NoError(t, err)
assert.NotNil(t, email)
fakeSmtp := fakeTestSMTP{}
email.smtp = &fakeSmtp
fakeSMTP := fakeTestSMTP{}
email.smtp = &fakeSMTP
email.TokenGenFn = TokenGenFn
email.UnsubscribeURL = "https://remark42.com/api/v1/email/unsubscribe"
req := Request{
Expand All @@ -192,9 +194,9 @@ func TestEmail_Send(t *testing.T) {
Email: "[email protected]",
}
assert.NoError(t, email.Send(context.TODO(), req))
assert.Equal(t, "[email protected]", fakeSmtp.readMail())
assert.Equal(t, 1, fakeSmtp.readQuitCount())
assert.Equal(t, "[email protected]", fakeSmtp.readRcpt())
assert.Equal(t, "[email protected]", fakeSMTP.readMail())
assert.Equal(t, 1, fakeSMTP.readQuitCount())
assert.Equal(t, "[email protected]", fakeSMTP.readRcpt())
// test buildMessageFromRequest separately for message text
res, err := email.buildMessageFromRequest(req, req.ForAdmin)
assert.NoError(t, err)
Expand Down Expand Up @@ -230,8 +232,8 @@ func TestEmail_SendVerification(t *testing.T) {
email, err := NewEmail(EmailParams{From: "[email protected]"}, SMTPParams{})
assert.NoError(t, err)
assert.NotNil(t, email)
fakeSmtp := fakeTestSMTP{}
email.smtp = &fakeSmtp
fakeSMTP := fakeTestSMTP{}
email.smtp = &fakeSMTP
email.TokenGenFn = TokenGenFn
req := Request{
Email: "[email protected]",
Expand All @@ -242,9 +244,9 @@ func TestEmail_SendVerification(t *testing.T) {
},
}
assert.NoError(t, email.Send(context.TODO(), req))
assert.Equal(t, "[email protected]", fakeSmtp.readMail())
assert.Equal(t, 1, fakeSmtp.readQuitCount())
assert.Equal(t, "[email protected]", fakeSmtp.readRcpt())
assert.Equal(t, "[email protected]", fakeSMTP.readMail())
assert.Equal(t, 1, fakeSMTP.readQuitCount())
assert.Equal(t, "[email protected]", fakeSMTP.readRcpt())
// test buildMessageFromRequest separately for message text
res, err := email.buildVerificationMessage(req.Verification.User, req.Email, req.Verification.Token, req.Verification.SiteID)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion backend/app/rest/api/migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ var xmlTestWP = `
<category domain="post_tag" nicename="alts"><![CDATA[alts]]></category>
<category domain="post_tag" nicename="role-playing"><![CDATA[role playing]]></category>
<category domain="category" nicename="stuff"><![CDATA[Stuff]]></category>
<category domain="post_tag" nicename="wierd-in-a-cant-quite-help-myself-way"><![CDATA[wierd in a can't quite help myself way]]></category>
<category domain="post_tag" nicename="weird-in-a-cant-quite-help-myself-way"><![CDATA[weird in a can't quite help myself way]]></category>
<wp:postmeta>
<wp:meta_key><![CDATA[_edit_last]]></wp:meta_key>
<wp:meta_value><![CDATA[2]]></wp:meta_value>
Expand Down
1 change: 1 addition & 0 deletions backend/app/rest/api/rest_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ func TestRest_Email(t *testing.T) {
}
client := http.Client{}
for _, x := range testData {
x := x
t.Run(x.description, func(t *testing.T) {
req, err := http.NewRequest(x.method, ts.URL+x.url, nil)
require.NoError(t, err)
Expand Down
Loading