-
Notifications
You must be signed in to change notification settings - Fork 4
/
generic.go
187 lines (149 loc) · 4.3 KB
/
generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package test
import (
"fmt"
"os"
"strings"
"sync"
"github.com/ottemo/commerce/app"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/env/ini"
// using standard set of packages
_ "github.com/ottemo/commerce/basebuild"
"github.com/ottemo/commerce/db"
)
// Package global variables
var (
startAppFlag bool
startAppMutex sync.RWMutex
)
// SwitchToTestIniSection switches ini config to use value from test section instead of general
func SwitchToTestIniSection() error {
if err := os.Setenv(ini.ConstEnvironmentIniSection, ini.ConstTestSectionName); err != nil {
return err
}
return nil
}
// UpdateWorkingDirectory modifies current working directory to be same for all packages
func UpdateWorkingDirectory() error {
// was specified environment variable
if value := os.Getenv("OTTEMO_PATH"); value != "" {
return os.Chdir(value)
}
// for Ottemo internal packages
workingDirectory, _ := os.Getwd()
if value := strings.Index(workingDirectory, "/src/github.com/ottemo/commerce"); value > 0 {
return os.Chdir(workingDirectory[0:value])
}
// for other packages
goPathList := strings.Split(os.Getenv("GOPATH"), ";")
for _, currentPath := range goPathList {
if currentPath == "" {
currentPath = "."
}
_, err := os.Stat(currentPath + "/src/github.com/ottemo/commerce")
if os.IsExist(err) {
return os.Chdir(currentPath)
}
}
return nil
}
// CheckTestIniDefaults prepares database to be used for tests
func CheckTestIniDefaults() error {
// we need to init iniConfig before check
err := app.Init()
if err != nil {
return err
}
// checking default test mode values
iniConfig := env.GetIniConfig()
if err := iniConfig.SetWorkingSection(ini.ConstTestSectionName); err != nil {
return err
}
changesMade := false
// checking test ini section for sqlite
if iniConfig.GetSectionValue(ini.ConstTestSectionName, "db.sqlite3.uri", "") == "" {
if err := iniConfig.SetValue("db.sqlite3.uri", "ottemo_test.db"); err != nil {
return err
}
changesMade = true
}
// checking test ini section for mongodb
if iniConfig.GetSectionValue(ini.ConstTestSectionName, "mongodb.uri", "") == "" {
uriValue := strings.Trim(iniConfig.GetValue("mongodb.uri", "mongodb://localhost:27017/ottemo"), "/") + "_test"
if err := iniConfig.SetValue("mongodb.uri", uriValue); err != nil {
return err
}
changesMade = true
}
if iniConfig.GetSectionValue(ini.ConstTestSectionName, "mongodb.db", "") == "" {
dbValue := iniConfig.GetValue("mongodb.db", "ottemo") + "_test"
if err := iniConfig.SetValue("mongodb.db", dbValue); err != nil {
return err
}
changesMade = true
}
// checking test ini section for mysql
if iniConfig.GetSectionValue(ini.ConstTestSectionName, "db.mysql.uri", "") == "" {
uriValue := iniConfig.GetValue("db.mysql.uri", "/")
uriValue = uriValue[0 : strings.LastIndex(uriValue, "/")+1]
if err := iniConfig.SetValue("db.mysql.uri", uriValue); err != nil {
return err
}
changesMade = true
}
if iniConfig.GetSectionValue(ini.ConstTestSectionName, "db.mysql.db", "") == "" {
dbValue := iniConfig.GetValue("db.mysql.db", "ottemo") + "_test"
if err := iniConfig.SetValue("db.mysql.db", dbValue); err != nil {
return err
}
changesMade = true
}
// if ini default values were updated
if changesMade {
err = app.End()
if err != nil {
return err
}
err = app.Init()
if err != nil {
return err
}
}
envConfig := env.GetConfig()
if err := envConfig.SetValue(app.ConstConfigPathMailPort, nil); err != nil {
// Ignoring error dispatching because of purpose of this block is for testing
fmt.Println("338d3826-6421-4475-9ff6-29c9eeb7b05d - Check log file. Non critical error:", err)
}
return nil
}
// StartAppInTestingMode starts application in "test mode" (you should use that function for your package test)
func StartAppInTestingMode() error {
var readyChannel = make(chan int, 1)
db.RegisterOnDatabaseStart(func() error {
readyChannel <- 1
return nil
})
startAppMutex.Lock()
defer startAppMutex.Unlock()
if !startAppFlag {
err := UpdateWorkingDirectory()
if err != nil {
return err
}
err = SwitchToTestIniSection()
if err != nil {
return err
}
err = CheckTestIniDefaults()
if err != nil {
return err
}
err = app.Start()
if err != nil {
return err
}
startAppFlag = true
}
<-readyChannel
return nil
}