[MM-61087] Fix errcheck linter issues in channels/jobs/helper_test.go (#30643)

Co-authored-by: Claude <noreply@anthropic.com>
Этот коммит содержится в:
Ben Schumacher
2025-04-28 15:02:10 +02:00
коммит произвёл GitHub
родитель 6a228877b7
Коммит eea39a5f23
2 изменённых файлов: 43 добавлений и 68 удалений

Просмотреть файл

@@ -107,7 +107,6 @@ issues:
channels/app/team_test.go|\ channels/app/team_test.go|\
channels/app/upload.go|\ channels/app/upload.go|\
channels/jobs/batch_worker_test.go|\ channels/jobs/batch_worker_test.go|\
channels/jobs/helper_test.go|\
channels/jobs/hosted_purchase_screening/worker.go|\ channels/jobs/hosted_purchase_screening/worker.go|\
channels/store/localcachelayer/channel_layer.go|\ channels/store/localcachelayer/channel_layer.go|\
channels/store/localcachelayer/channel_layer_test.go|\ channels/store/localcachelayer/channel_layer_test.go|\

Просмотреть файл

@@ -7,11 +7,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"sync"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog" "github.com/mattermost/mattermost/server/public/shared/mlog"
@@ -20,7 +20,6 @@ import (
"github.com/mattermost/mattermost/server/v8/channels/jobs" "github.com/mattermost/mattermost/server/v8/channels/jobs"
"github.com/mattermost/mattermost/server/v8/channels/store" "github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/config" "github.com/mattermost/mattermost/server/v8/config"
"github.com/stretchr/testify/require"
) )
type TestHelper struct { type TestHelper struct {
@@ -37,16 +36,15 @@ type TestHelper struct {
IncludeCacheLayer bool IncludeCacheLayer bool
ConfigStore *config.Store ConfigStore *config.Store
t testing.TB
tempWorkspace string tempWorkspace string
oldWatcherPollingInterval int oldWatcherPollingInterval int
} }
func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer bool, func setupTestHelper(t testing.TB, dbStore store.Store, enterprise bool, includeCacheLayer bool,
updateCfg func(cfg *model.Config), options []app.Option) *TestHelper { updateCfg func(cfg *model.Config), options []app.Option) *TestHelper {
tempWorkspace, err := os.MkdirTemp("", "jobstest") tempWorkspace, err := os.MkdirTemp("", "jobstest")
if err != nil { require.NoError(t, err)
panic(err)
}
configStore := config.NewTestMemoryStore() configStore := config.NewTestMemoryStore()
memoryConfig := configStore.Get() memoryConfig := configStore.Get()
@@ -63,7 +61,8 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo
updateCfg(memoryConfig) updateCfg(memoryConfig)
} }
configStore.Set(memoryConfig) _, _, err = configStore.Set(memoryConfig)
require.NoError(t, err)
buffer := &mlog.Buffer{} buffer := &mlog.Buffer{}
@@ -75,22 +74,20 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo
options = append(options, app.StoreOverride(dbStore)) options = append(options, app.StoreOverride(dbStore))
} }
testLogger, _ := mlog.NewLogger() testLogger, err := mlog.NewLogger()
logCfg, _ := config.MloggerConfigFromLoggerConfig(&memoryConfig.LogSettings, nil, config.GetLogFileLocation) require.NoError(t, err)
if errCfg := testLogger.ConfigureTargets(logCfg, nil); errCfg != nil { logCfg, err := config.MloggerConfigFromLoggerConfig(&memoryConfig.LogSettings, nil, config.GetLogFileLocation)
panic("failed to configure test logger: " + errCfg.Error()) require.NoError(t, err)
} err = testLogger.ConfigureTargets(logCfg, nil)
if errW := mlog.AddWriterTarget(testLogger, buffer, true, mlog.StdAll...); errW != nil { require.NoError(t, err, "failed to configure test logger")
panic("failed to add writer target to test logger: " + errW.Error()) err = mlog.AddWriterTarget(testLogger, buffer, true, mlog.StdAll...)
} require.NoError(t, err, "failed to add writer target to test logger")
// lock logger config so server init cannot override it during testing. // lock logger config so server init cannot override it during testing.
testLogger.LockConfiguration() testLogger.LockConfiguration()
options = append(options, app.SetLogger(testLogger)) options = append(options, app.SetLogger(testLogger))
s, err := app.NewServer(options...) s, err := app.NewServer(options...)
if err != nil { require.NoError(t, err)
panic(err)
}
th := &TestHelper{ th := &TestHelper{
App: app.New(app.ServerConnector(s.Channels())), App: app.New(app.ServerConnector(s.Channels())),
@@ -100,14 +97,14 @@ func setupTestHelper(dbStore store.Store, enterprise bool, includeCacheLayer boo
TestLogger: testLogger, TestLogger: testLogger,
IncludeCacheLayer: includeCacheLayer, IncludeCacheLayer: includeCacheLayer,
ConfigStore: configStore, ConfigStore: configStore,
t: t,
tempWorkspace: tempWorkspace,
} }
prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = "localhost:0" }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = "localhost:0" })
serverErr := th.Server.Start() err = th.Server.Start()
if serverErr != nil { require.NoError(t, err)
panic(serverErr)
}
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress }) th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
@@ -133,43 +130,28 @@ func SetupWithUpdateCfg(tb testing.TB, updateCfg func(cfg *model.Config), option
dbStore.MarkSystemRanUnitTests() dbStore.MarkSystemRanUnitTests()
mainHelper.PreloadMigrations() mainHelper.PreloadMigrations()
th := setupTestHelper(dbStore, false, true, updateCfg, options) th := setupTestHelper(tb, dbStore, false, true, updateCfg, options)
th.oldWatcherPollingInterval = oldWatcherPollingInterval th.oldWatcherPollingInterval = oldWatcherPollingInterval
return th return th
} }
var initBasicOnce sync.Once
var userCache struct {
SystemAdminUser *model.User
BasicUser *model.User
BasicUser2 *model.User
}
func (th *TestHelper) InitBasic() *TestHelper { func (th *TestHelper) InitBasic() *TestHelper {
// create users once and cache them because password hashing is slow th.SystemAdminUser = th.CreateUser()
initBasicOnce.Do(func() { _, appErr := th.App.UpdateUserRoles(th.Context, th.SystemAdminUser.Id, model.SystemUserRoleId+" "+model.SystemAdminRoleId, false)
th.SystemAdminUser = th.CreateUser() require.Nil(th.t, appErr)
th.App.UpdateUserRoles(th.Context, th.SystemAdminUser.Id, model.SystemUserRoleId+" "+model.SystemAdminRoleId, false) th.SystemAdminUser, appErr = th.App.GetUser(th.SystemAdminUser.Id)
th.SystemAdminUser, _ = th.App.GetUser(th.SystemAdminUser.Id) require.Nil(th.t, appErr)
userCache.SystemAdminUser = th.SystemAdminUser.DeepCopy()
th.BasicUser = th.CreateUser() th.BasicUser = th.CreateUser()
th.BasicUser, _ = th.App.GetUser(th.BasicUser.Id) th.BasicUser, appErr = th.App.GetUser(th.BasicUser.Id)
userCache.BasicUser = th.BasicUser.DeepCopy() require.Nil(th.t, appErr)
th.BasicUser2 = th.CreateUser() th.BasicUser2 = th.CreateUser()
th.BasicUser2, _ = th.App.GetUser(th.BasicUser2.Id) th.BasicUser2, appErr = th.App.GetUser(th.BasicUser2.Id)
userCache.BasicUser2 = th.BasicUser2.DeepCopy() require.Nil(th.t, appErr)
})
// restore cached users
th.SystemAdminUser = userCache.SystemAdminUser.DeepCopy()
th.BasicUser = userCache.BasicUser.DeepCopy()
th.BasicUser2 = userCache.BasicUser2.DeepCopy()
users := []*model.User{th.SystemAdminUser, th.BasicUser, th.BasicUser2}
mainHelper.GetSQLStore().User().InsertUsers(users)
th.BasicTeam = th.CreateTeam() th.BasicTeam = th.CreateTeam()
return th return th
} }
@@ -182,10 +164,8 @@ func (th *TestHelper) CreateTeam() *model.Team {
Type: model.TeamOpen, Type: model.TeamOpen,
} }
var err *model.AppError team, err := th.App.CreateTeam(th.Context, team)
if team, err = th.App.CreateTeam(th.Context, team); err != nil { require.Nil(th.t, err)
panic(err)
}
return team return team
} }
@@ -206,14 +186,11 @@ func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User {
var err *model.AppError var err *model.AppError
if guest { if guest {
if user, err = th.App.CreateGuest(th.Context, user); err != nil { user, err = th.App.CreateGuest(th.Context, user)
panic(err)
}
} else { } else {
if user, err = th.App.CreateUser(th.Context, user); err != nil { user, err = th.App.CreateUser(th.Context, user)
panic(err)
}
} }
require.Nil(th.t, err)
return user return user
} }
@@ -236,9 +213,11 @@ func (th *TestHelper) ShutdownApp() {
func (th *TestHelper) TearDown() { func (th *TestHelper) TearDown() {
if th.IncludeCacheLayer { if th.IncludeCacheLayer {
// Clean all the caches // Clean all the caches
th.App.Srv().InvalidateAllCaches() appErr := th.App.Srv().InvalidateAllCaches()
require.Nil(th.t, appErr)
} }
th.ShutdownApp() th.ShutdownApp()
if th.tempWorkspace != "" { if th.tempWorkspace != "" {
os.RemoveAll(th.tempWorkspace) os.RemoveAll(th.tempWorkspace)
} }
@@ -257,10 +236,7 @@ func (th *TestHelper) SetupBatchWorker(t *testing.T, worker *jobs.BatchWorker) *
jobData := make(model.StringMap) jobData := make(model.StringMap)
jobData["batch_number"] = "1" jobData["batch_number"] = "1"
job, appErr := th.Server.Jobs.CreateJob(th.Context, jobId, jobData) job, appErr := th.Server.Jobs.CreateJob(th.Context, jobId, jobData)
require.Nil(t, appErr)
if appErr != nil {
panic(appErr)
}
done := make(chan bool) done := make(chan bool)
go func() { go func() {
@@ -342,8 +318,8 @@ func (th *TestHelper) checkJobStatus(t *testing.T, jobId string, status string)
require.Eventuallyf(t, func() bool { require.Eventuallyf(t, func() bool {
// it's ok if there's an error, it might take awhile for the job to finish. // it's ok if there's an error, it might take awhile for the job to finish.
job, err := th.Server.Jobs.GetJob(th.Context, jobId) job, appErr := th.Server.Jobs.GetJob(th.Context, jobId)
assert.Nil(t, err) assert.Nil(th.t, appErr)
if jobId == job.Id { if jobId == job.Id {
return job.Status == status return job.Status == status
} }