-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
63 lines (51 loc) · 1.33 KB
/
utils.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
)
// parseVapefile reads a given Vapefile and returns the contents.
func parseVapefile(file string) (SmokeTests, error) {
raw, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
var tests SmokeTests
err = json.Unmarshal(raw, &tests)
if err != nil {
return nil, err
}
for _, test := range tests {
if test.URI == "" || test.ExpectedStatusCode == 0 {
return nil, fmt.Errorf("Each test should have at least a uri and status_code:\n %v", string(raw))
}
}
return tests, nil
}
// parseBaseURL tests a given URL is valid.
func parseBaseURL(baseURL string) (*url.URL, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
if u.Scheme != "http" && u.Scheme != "https" {
return nil, errors.New("invalid protocol scheme")
}
return u, nil
}
// formatResult returns a readable string summarizing the result
func formatResult(result SmokeTestResult) string {
message := fmt.Sprintf("[%d:%d] %s", result.Test.ExpectedStatusCode, result.ActualStatusCode, result.Test.URI)
if result.Test.Content != "" {
message = fmt.Sprintf("%s %s", message, result.Test.Content)
}
colour := 32
icon := '✓'
if !result.Passed() {
icon = '✘'
colour = 31
}
return fmt.Sprintf("\033[%dm%c %s\033[0m", colour, icon, message)
}