This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
235 lines (175 loc) · 5.6 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"os"
"fmt"
"flag"
"errors"
"github.com/wsxiaoys/terminal"
"github.com/shiena/ansicolor"
"gopkg.in/fatih/set.v0"
)
var Version = "1.1.0"
var SettingFiles = []string{ "noel.json", "noel.yml", "noel.yaml" }
func getChangedPackages() ([]string, error) {
return getChangedRootDirs(1)
}
func contains(col []string, val string) bool {
for _, x := range(col) {
if x == val {
return true
}
}
return false
}
func printOk(w terminal.TerminalWriter, msg string) {
w.Color("g").
Print(msg).
Print("\n").
Reset()
}
func printSkip(w terminal.TerminalWriter, msg string) {
w.Color("b").
Print(msg).
Print("\n").
Reset()
}
func printError(w terminal.TerminalWriter, err error, msg string) {
w.Color("r").
Print("\n").
Print(err).
Print("\n\n").
Print(msg).
Print("\n").
Reset()
}
func getAllPackages(pkgs *Conf) []string {
allPkgs := pkgs.Manual
for _, pkg := range(pkgs.Automatic) {
allPkgs = append(allPkgs, pkg)
}
return allPkgs
}
func getTestPkgs(hackPkgs []string, changedPkgs []string, pkgs *Conf) []string {
testPkgs := set.New()
for _, pkg := range(changedPkgs) {
testPkgs.Add(pkg)
}
for _, pkg := range(hackPkgs) {
if pkg == "<all>" {
allPkgs := getAllPackages(pkgs)
for _, pkgByConf := range(allPkgs) {
testPkgs.Add(pkgByConf)
}
continue
}
testPkgs.Add(pkg)
}
return set.StringSlice(testPkgs)
}
func main() {
var install bool
var timeout int
flag.BoolVar(&install, "install", false, "Install package")
flag.IntVar(&timeout, "timeout", 60 * 10, "Install timeout")
flag.Parse()
stdout := terminal.TerminalWriter { ansicolor.NewAnsiColorWriter(os.Stdout) }
fmt.Println("Start Noel v" + Version + " [Chocolatey Packages Test Runner]\n")
// ----------------------------------------------------
fmt.Println("Check environment:")
if err := checkEnv(); err != nil {
printError(stdout, err, "Failed")
os.Exit(1)
} else {
printOk(stdout, "\nSucceeded\n")
}
// ----------------------------------------------------
stdout.Print("Load settings: ")
var pkgs *Conf
for _, path := range(SettingFiles) {
var err error
if pkgs, err = LoadConf(path); err == nil {
break
}
}
if pkgs == nil {
printError(stdout, errors.New("Can't find settings"), "Failed")
os.Exit(1)
} else {
printOk(stdout, "Succeeded")
}
// ----------------------------------------------------
fmt.Print("Detect hacks of commit messages: ")
hackPkgs, err := getPackageNamesOfCommitMessage()
if err != nil {
printError(stdout, err, "Failed")
os.Exit(1)
} else {
printOk(stdout, "Succeeded")
}
// ----------------------------------------------------
fmt.Println("\nHack packages:")
for _, pkg := range(hackPkgs) {
fmt.Println(" " + pkg)
}
fmt.Printf("\n %d packages\n\n", len(hackPkgs))
// ----------------------------------------------------
fmt.Print("Detect package changes: ")
changedPkgs, err := getChangedPackages()
if err != nil {
printError(stdout, err, "Failed")
os.Exit(1)
} else {
printOk(stdout, "Succeeded")
}
// ----------------------------------------------------
fmt.Println("\nChanged packages:")
for _, pkg := range(changedPkgs) {
fmt.Println(" " + pkg)
}
fmt.Printf("\n %d packages", len(changedPkgs))
if len(changedPkgs) == 0 && len(hackPkgs) == 0 {
printSkip(stdout, "\n\nNo changed\n\n")
return
}
// ----------------------------------------------------
fmt.Println("\n\nStart tests:\n")
testPkgs := getTestPkgs(hackPkgs, changedPkgs, pkgs)
for _, pkg := range(testPkgs) {
fmt.Print("Test for [" + pkg + "]")
data := TestData {
Name: pkg,
Install: install,
Timeout: timeout,
}
if contains(pkgs.WithoutInstall, pkg) {
data.Install = false
}
if !data.Install {
fmt.Print(" without install: ")
} else {
fmt.Print(": ")
}
if contains(pkgs.Manual, pkg) {
fmt.Println("Manual tests")
if err := TestManual(data); err != nil {
printError(stdout, err, "Failed")
os.Exit(1)
} else {
printOk(stdout, "Succeeded\n")
}
continue
}
if contains(pkgs.Automatic, pkg) {
fmt.Println("Automatic tests")
if err := TestAutomatic(data); err != nil {
printError(stdout, err, "Failed")
os.Exit(1)
} else {
printOk(stdout, "Succeeded\n")
}
continue
}
printSkip(stdout, "Skip")
}
printOk(stdout, "\nAll test succeeded")
}