github.com/jfrerich/mattermost-server@v5.8.0-rc2+incompatible/testlib/helper.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package testlib 5 6 import ( 7 "flag" 8 "os" 9 "testing" 10 11 "github.com/mattermost/mattermost-server/mlog" 12 "github.com/mattermost/mattermost-server/model" 13 "github.com/mattermost/mattermost-server/store" 14 "github.com/mattermost/mattermost-server/store/sqlstore" 15 "github.com/mattermost/mattermost-server/store/storetest" 16 "github.com/mattermost/mattermost-server/utils" 17 ) 18 19 type MainHelper struct { 20 Settings *model.SqlSettings 21 Store store.Store 22 SqlSupplier *sqlstore.SqlSupplier 23 ClusterInterface *FakeClusterInterface 24 25 status int 26 } 27 28 func NewMainHelper() *MainHelper { 29 flag.Parse() 30 31 // Setup a global logger to catch tests logging outside of app context 32 // The global logger will be stomped by apps initalizing but that's fine for testing. 33 // Ideally this won't happen. 34 mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{ 35 EnableConsole: true, 36 ConsoleJson: true, 37 ConsoleLevel: "error", 38 EnableFile: false, 39 })) 40 41 utils.TranslationsPreInit() 42 43 settings := storetest.MakeSqlSettings(model.DATABASE_DRIVER_MYSQL) 44 45 clusterInterface := &FakeClusterInterface{} 46 sqlSupplier := sqlstore.NewSqlSupplier(*settings, nil) 47 testStore := &TestStore{ 48 store.NewLayeredStore(sqlSupplier, nil, clusterInterface), 49 } 50 51 return &MainHelper{ 52 Settings: settings, 53 Store: testStore, 54 SqlSupplier: sqlSupplier, 55 ClusterInterface: clusterInterface, 56 } 57 } 58 59 func (h *MainHelper) Main(m *testing.M) { 60 h.status = m.Run() 61 } 62 63 func (h *MainHelper) Close() error { 64 storetest.CleanupSqlSettings(h.Settings) 65 66 os.Exit(h.status) 67 68 return nil 69 }