-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation.go
More file actions
114 lines (101 loc) · 2.91 KB
/
Copy pathvalidation.go
File metadata and controls
114 lines (101 loc) · 2.91 KB
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
package testy
import (
"errors"
"fmt"
"os"
"path/filepath"
)
type ValidationError struct {
Field string
Message string
}
func (e ValidationError) Error() string {
return fmt.Sprintf("validation error: %s - %s", e.Field, e.Message)
}
func (c *Config) Validate() error {
var validationErrors []ValidationError
if c.Handler == nil && c.GRPCAddr == "" {
validationErrors = append(validationErrors, ValidationError{
Field: "Handler",
Message: "http.Handler or gRPC Server is required",
})
}
if c.Handler != nil && c.GRPCAddr != "" {
validationErrors = append(validationErrors, ValidationError{
Field: "Handler",
Message: "Only one of http.Handler or gRPC Server can be provided",
})
}
if c.CasesDir == "" {
validationErrors = append(validationErrors, ValidationError{
Field: "CasesDir",
Message: "CasesDir is required",
})
} else {
if info, err := os.Stat(c.CasesDir); err != nil {
validationErrors = append(validationErrors, ValidationError{
Field: "CasesDir",
Message: fmt.Sprintf("directory does not exist: %s", c.CasesDir),
})
} else if !info.IsDir() {
validationErrors = append(validationErrors, ValidationError{
Field: "CasesDir",
Message: fmt.Sprintf("path is not a directory: %s", c.CasesDir),
})
}
}
if c.FixturesDir != "" {
if info, err := os.Stat(c.FixturesDir); err != nil {
validationErrors = append(validationErrors, ValidationError{
Field: "FixturesDir",
Message: fmt.Sprintf("directory does not exist: %s", c.FixturesDir),
})
} else if !info.IsDir() {
validationErrors = append(validationErrors, ValidationError{
Field: "FixturesDir",
Message: fmt.Sprintf("path is not a directory: %s", c.FixturesDir),
})
}
}
if c.FixturesDir != "" && c.ConnStr == "" {
validationErrors = append(validationErrors, ValidationError{
Field: "ConnStr",
Message: "database connection string is required when FixturesDir is provided",
})
}
if c.ConnStr != "" && len(c.DBType) == 0 {
validationErrors = append(validationErrors, ValidationError{
Field: "DBType",
Message: "DBType must be specified when ConnStr is provided",
})
}
if c.JUnitReport != "" {
dir := filepath.Dir(c.JUnitReport)
if dir != "" && dir != "." {
if info, err := os.Stat(dir); err != nil {
if err := os.MkdirAll(dir, 0755); err != nil {
validationErrors = append(validationErrors, ValidationError{
Field: "JUnitReport",
Message: fmt.Sprintf("cannot create report directory: %s", err),
})
}
} else if !info.IsDir() {
validationErrors = append(validationErrors, ValidationError{
Field: "JUnitReport",
Message: fmt.Sprintf("parent path is not a directory: %s", dir),
})
}
}
}
if len(validationErrors) > 0 {
var errMsg string
for i, ve := range validationErrors {
if i > 0 {
errMsg += "; "
}
errMsg += ve.Error()
}
return errors.New(errMsg)
}
return nil
}