github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/test/singleton/boil_main_test.go.tpl (about) 1 var flagDebugMode = flag.Bool("test.sqldebug", false, "Turns on debug mode for SQL statements") 2 var flagConfigFile = flag.String("test.config", "", "Overrides the default config") 3 4 const outputDirDepth = {{.OutputDirDepth}} 5 6 var ( 7 dbMain tester 8 ) 9 10 type tester interface { 11 setup() error 12 conn() (*sql.DB, error) 13 teardown() error 14 } 15 16 func TestMain(m *testing.M) { 17 if dbMain == nil { 18 fmt.Println("no dbMain tester interface was ready") 19 os.Exit(-1) 20 } 21 22 rand.Seed(time.Now().UnixNano()) 23 24 flag.Parse() 25 26 var err error 27 28 // Load configuration 29 err = initViper() 30 if err != nil { 31 fmt.Println("unable to load config file") 32 os.Exit(-2) 33 } 34 35 // Set DebugMode so we can see generated sql statements 36 boil.DebugMode = *flagDebugMode 37 38 if err = dbMain.setup(); err != nil { 39 fmt.Println("Unable to execute setup:", err) 40 os.Exit(-4) 41 } 42 43 conn, err := dbMain.conn() 44 if err != nil { 45 fmt.Println("failed to get connection:", err) 46 } 47 48 var code int 49 boil.SetDB(conn) 50 code = m.Run() 51 52 if err = dbMain.teardown(); err != nil { 53 fmt.Println("Unable to execute teardown:", err) 54 os.Exit(-5) 55 } 56 57 os.Exit(code) 58 } 59 60 func initViper() error { 61 if flagConfigFile != nil && *flagConfigFile != "" { 62 viper.SetConfigFile(*flagConfigFile) 63 if err := viper.ReadInConfig(); err != nil { 64 return err 65 } 66 return nil 67 } 68 69 var err error 70 71 viper.SetConfigName("sqlboiler") 72 73 configHome := os.Getenv("XDG_CONFIG_HOME") 74 homePath := os.Getenv("HOME") 75 wd, err := os.Getwd() 76 if err != nil { 77 wd = strings.Repeat("../", outputDirDepth) 78 } else { 79 wd = wd + strings.Repeat("/..", outputDirDepth) 80 } 81 82 configPaths := []string{wd} 83 if len(configHome) > 0 { 84 configPaths = append(configPaths, filepath.Join(configHome, "sqlboiler")) 85 } else { 86 configPaths = append(configPaths, filepath.Join(homePath, ".config/sqlboiler")) 87 } 88 89 for _, p := range configPaths { 90 viper.AddConfigPath(p) 91 } 92 93 // Ignore errors here, fall back to defaults and validation to provide errs 94 _ = viper.ReadInConfig() 95 viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) 96 viper.AutomaticEnv() 97 98 return nil 99 }