mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge branch 'jmks-improve-test-coverage'
This commit is contained in:
commit
6593827a99
@ -5,55 +5,75 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/wtf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModuleValidator struct{}
|
type ModuleValidator struct{}
|
||||||
|
|
||||||
|
type widgetError struct {
|
||||||
|
name string
|
||||||
|
validationErrors []cfg.Validatable
|
||||||
|
}
|
||||||
|
|
||||||
func NewModuleValidator() *ModuleValidator {
|
func NewModuleValidator() *ModuleValidator {
|
||||||
val := &ModuleValidator{}
|
return &ModuleValidator{}
|
||||||
return val
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate rolls through all the enabled widgets and looks for configuration errors.
|
// Validate rolls through all the enabled widgets and looks for configuration errors.
|
||||||
// If it finds any it stringifies them, writes them to the console, and kills the app gracefully
|
// If it finds any it stringifies them, writes them to the console, and kills the app gracefully
|
||||||
func (val *ModuleValidator) Validate(widgets []wtf.Wtfable) {
|
func (val *ModuleValidator) Validate(widgets []wtf.Wtfable) {
|
||||||
var errStr string
|
validationErrors := validate(widgets)
|
||||||
hasErrors := false
|
|
||||||
|
|
||||||
|
if len(validationErrors) > 0 {
|
||||||
|
fmt.Println()
|
||||||
|
for _, error := range validationErrors {
|
||||||
|
for _, message := range error.errorMessages() {
|
||||||
|
fmt.Println(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func validate(widgets []wtf.Wtfable) (widgetErrors []widgetError) {
|
||||||
for _, widget := range widgets {
|
for _, widget := range widgets {
|
||||||
var widgetErrStr string
|
error := widgetError{name: widget.Name()}
|
||||||
|
|
||||||
for _, val := range widget.CommonSettings().Validations() {
|
for _, val := range widget.CommonSettings().Validations() {
|
||||||
if val.HasError() {
|
if val.HasError() {
|
||||||
hasErrors = true
|
error.validationErrors = append(error.validationErrors, val)
|
||||||
widgetErrStr += fmt.Sprintf(" - %s\t%s %v\n", val, aurora.Red("Error:"), val.Error())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if widgetErrStr != "" {
|
if len(error.validationErrors) > 0 {
|
||||||
errStr += fmt.Sprintf(
|
widgetErrors = append(widgetErrors, error)
|
||||||
"%s\n",
|
}
|
||||||
fmt.Sprintf(
|
}
|
||||||
|
|
||||||
|
return widgetErrors
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err widgetError) errorMessages() (messages []string) {
|
||||||
|
widgetMessage := fmt.Sprintf(
|
||||||
"%s in %s configuration",
|
"%s in %s configuration",
|
||||||
aurora.Red("Errors"),
|
aurora.Red("Errors"),
|
||||||
aurora.Yellow(
|
aurora.Yellow(
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"%s.position",
|
"%s.position",
|
||||||
widget.Name(),
|
err.name,
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
messages = append(messages, widgetMessage)
|
||||||
|
|
||||||
errStr += widgetErrStr + "\n"
|
for _, e := range err.validationErrors {
|
||||||
}
|
configMessage := fmt.Sprintf(" - %s\t%s %v", e.String(), aurora.Red("Error:"), e.Error())
|
||||||
|
|
||||||
|
messages = append(messages, configMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasErrors {
|
return messages
|
||||||
fmt.Println()
|
|
||||||
fmt.Println(errStr)
|
|
||||||
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
102
app/module_validator_test.go
Normal file
102
app/module_validator_test.go
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/logrusorgru/aurora"
|
||||||
|
"github.com/olebedev/config"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/wtfutil/wtf/wtf"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
valid = `
|
||||||
|
wtf:
|
||||||
|
mods:
|
||||||
|
clocks:
|
||||||
|
enabled: true
|
||||||
|
position:
|
||||||
|
top: 0
|
||||||
|
left: 0
|
||||||
|
height: 1
|
||||||
|
width: 1
|
||||||
|
refreshInterval: 30`
|
||||||
|
|
||||||
|
invalid = `
|
||||||
|
wtf:
|
||||||
|
mods:
|
||||||
|
clocks:
|
||||||
|
enabled: true
|
||||||
|
position:
|
||||||
|
top: abc
|
||||||
|
left: 0
|
||||||
|
height: 1
|
||||||
|
width: 1
|
||||||
|
refreshInterval: 30`
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_NewModuleValidator(t *testing.T) {
|
||||||
|
assert.IsType(t, &ModuleValidator{}, NewModuleValidator())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_validate(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
moduleName string
|
||||||
|
config *config.Config
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid config",
|
||||||
|
moduleName: "clocks",
|
||||||
|
config: func() *config.Config {
|
||||||
|
cfg, _ := config.ParseYaml(valid)
|
||||||
|
return cfg
|
||||||
|
}(),
|
||||||
|
expected: []string{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid config",
|
||||||
|
moduleName: "clocks",
|
||||||
|
config: func() *config.Config {
|
||||||
|
cfg, _ := config.ParseYaml(invalid)
|
||||||
|
return cfg
|
||||||
|
}(),
|
||||||
|
expected: []string{
|
||||||
|
fmt.Sprintf("%s in %s configuration", aurora.Red("Errors"), aurora.Yellow("clocks.position")),
|
||||||
|
fmt.Sprintf(
|
||||||
|
" - Invalid value for %s: 0 %s strconv.ParseInt: parsing \"abc\": invalid syntax",
|
||||||
|
aurora.Yellow("top"),
|
||||||
|
aurora.Red("Error:"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
widget := MakeWidget(nil, nil, tt.moduleName, tt.config)
|
||||||
|
|
||||||
|
if widget == nil {
|
||||||
|
t.Logf("Failed to create widget %s", tt.moduleName)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
errs := validate([]wtf.Wtfable{widget})
|
||||||
|
|
||||||
|
if len(tt.expected) == 0 {
|
||||||
|
assert.Empty(t, errs)
|
||||||
|
} else {
|
||||||
|
assert.NotEmpty(t, errs)
|
||||||
|
|
||||||
|
var actual []string
|
||||||
|
for _, err := range errs {
|
||||||
|
actual = append(actual, err.errorMessages()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, tt.expected, actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -29,7 +29,7 @@ const (
|
|||||||
|
|
||||||
// CreateFile creates the named file in the config directory, if it does not already exist.
|
// CreateFile creates the named file in the config directory, if it does not already exist.
|
||||||
// If the file exists it does not recreate it.
|
// If the file exists it does not recreate it.
|
||||||
// If successful, eturns the absolute path to the file
|
// If successful, returns the absolute path to the file
|
||||||
// If unsuccessful, returns an error
|
// If unsuccessful, returns an error
|
||||||
func CreateFile(fileName string) (string, error) {
|
func CreateFile(fileName string) (string, error) {
|
||||||
configDir, err := WtfConfigDir()
|
configDir, err := WtfConfigDir()
|
||||||
|
2
go.mod
2
go.mod
@ -15,8 +15,8 @@ require (
|
|||||||
github.com/briandowns/openweathermap v0.0.0-20180804155945-5f41b7c9d92d
|
github.com/briandowns/openweathermap v0.0.0-20180804155945-5f41b7c9d92d
|
||||||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
||||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
|
||||||
github.com/digitalocean/godo v1.46.0
|
|
||||||
github.com/creack/pty v1.1.11
|
github.com/creack/pty v1.1.11
|
||||||
|
github.com/digitalocean/godo v1.46.0
|
||||||
github.com/docker/distribution v2.7.1+incompatible // indirect
|
github.com/docker/distribution v2.7.1+incompatible // indirect
|
||||||
github.com/docker/docker v1.13.1
|
github.com/docker/docker v1.13.1
|
||||||
github.com/docker/docker-credential-helpers v0.6.3
|
github.com/docker/docker-credential-helpers v0.6.3
|
||||||
|
2
go.sum
2
go.sum
@ -925,8 +925,6 @@ golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 h1:B6caxRw+hozq68X2MY7jEpZh/cr4/aHLv9xU8Kkadrw=
|
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642 h1:B6caxRw+hozq68X2MY7jEpZh/cr4/aHLv9xU8Kkadrw=
|
||||||
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200817155316-9781c653f443 h1:X18bCaipMcoJGm27Nv7zr4XYPKGUy92GtqboKC2Hxaw=
|
|
||||||
golang.org/x/sys v0.0.0-20200817155316-9781c653f443/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user