added some tests
This commit is contained in:
parent
0e4ba3dd9f
commit
76876e012a
2
Makefile
2
Makefile
@ -18,7 +18,7 @@ build: generate
|
|||||||
env GOARCH=amd64 GOOS=linux go build -o dist/${BIN_NAME}_linux_amd64
|
env GOARCH=amd64 GOOS=linux go build -o dist/${BIN_NAME}_linux_amd64
|
||||||
|
|
||||||
test: generate
|
test: generate
|
||||||
go test ./...
|
go test ./... -cover
|
||||||
|
|
||||||
watch:
|
watch:
|
||||||
modd
|
modd
|
||||||
|
|||||||
@ -68,12 +68,12 @@ func init() {
|
|||||||
viper.AddConfigPath(fmt.Sprintf("/etc%c%s", os.PathSeparator, dirName))
|
viper.AddConfigPath(fmt.Sprintf("/etc%c%s", os.PathSeparator, dirName))
|
||||||
viper.SetConfigName("config.toml")
|
viper.SetConfigName("config.toml")
|
||||||
viper.SetEnvPrefix("hasshelper")
|
viper.SetEnvPrefix("hasshelper")
|
||||||
viper.Set("version", gitVersion)
|
|
||||||
viper.SetDefault("deployment", "prod")
|
|
||||||
viper.SetDefault("loglevel", "info")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
viper.Set("version", gitVersion)
|
||||||
|
viper.SetDefault("deployment", "prod")
|
||||||
|
viper.SetDefault("loglevel", "info")
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
if cfgFile != "" {
|
if cfgFile != "" {
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
|
|||||||
@ -1,15 +1,39 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitConfig(t *testing.T) {
|
func TestInitConfig(t *testing.T) {
|
||||||
|
viper.Reset()
|
||||||
viper.Set("deployment", "testing")
|
viper.Set("deployment", "testing")
|
||||||
viper.Set("image_dir", "/tmp")
|
viper.Set("image_dir", "/tmp")
|
||||||
viper.Set("webserver_port", "8080")
|
viper.Set("webserver_port", "8080")
|
||||||
|
|
||||||
initConfig()
|
initConfig()
|
||||||
|
assert.Equal(t, gitVersion, viper.GetString("version"), "config version mismatch")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInitConfigMissingImageDir(t *testing.T) {
|
||||||
|
if os.Getenv("TEST_MISSING_INPUT") == "1" {
|
||||||
|
viper.Reset()
|
||||||
|
viper.Set("deployment", "testing")
|
||||||
|
viper.Set("version", "vTest")
|
||||||
|
viper.Set("webserver_port", "8080")
|
||||||
|
|
||||||
|
initConfig()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmd := exec.Command(os.Args[0], "-test.run=TestInitConfigMissingImageDir")
|
||||||
|
cmd.Env = append(os.Environ(), "TEST_MISSING_INPUT=1")
|
||||||
|
err := cmd.Run()
|
||||||
|
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Fatalf("process ran with err %v, want exit status 1", err)
|
||||||
}
|
}
|
||||||
|
|||||||
3
go.mod
3
go.mod
@ -9,6 +9,7 @@ require (
|
|||||||
github.com/rs/zerolog v1.27.0
|
github.com/rs/zerolog v1.27.0
|
||||||
github.com/spf13/cobra v1.5.0
|
github.com/spf13/cobra v1.5.0
|
||||||
github.com/spf13/viper v1.12.0
|
github.com/spf13/viper v1.12.0
|
||||||
|
github.com/stretchr/testify v1.8.0
|
||||||
golang.org/x/net v0.0.0-20220812174116-3211cb980234
|
golang.org/x/net v0.0.0-20220812174116-3211cb980234
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,6 +25,7 @@ require (
|
|||||||
github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743 // indirect
|
github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743 // indirect
|
||||||
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec // indirect
|
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec // indirect
|
||||||
github.com/daaku/go.zipexe v1.0.1 // indirect
|
github.com/daaku/go.zipexe v1.0.1 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.0 // indirect
|
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||||
github.com/emirpasic/gods v1.18.1 // indirect
|
github.com/emirpasic/gods v1.18.1 // indirect
|
||||||
github.com/fatih/color v1.13.0 // indirect
|
github.com/fatih/color v1.13.0 // indirect
|
||||||
@ -46,6 +48,7 @@ require (
|
|||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.3 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.3 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rjeczalik/notify v0.0.0-20181126183243-629144ba06a1 // indirect
|
github.com/rjeczalik/notify v0.0.0-20181126183243-629144ba06a1 // indirect
|
||||||
github.com/sergi/go-diff v1.2.0 // indirect
|
github.com/sergi/go-diff v1.2.0 // indirect
|
||||||
github.com/spf13/afero v1.9.2 // indirect
|
github.com/spf13/afero v1.9.2 // indirect
|
||||||
|
|||||||
@ -19,22 +19,23 @@ func imageHandler() http.Handler {
|
|||||||
func chooseRandomFile(ctx context.Context, imageDir string) (filepath string, err error) {
|
func chooseRandomFile(ctx context.Context, imageDir string) (filepath string, err error) {
|
||||||
validEntries := make([]string, 0)
|
validEntries := make([]string, 0)
|
||||||
log := config.logger.With().Str("imageDir", imageDir).Logger()
|
log := config.logger.With().Str("imageDir", imageDir).Logger()
|
||||||
err = fs.WalkDir(os.DirFS(imageDir), ".", func(path string, d fs.DirEntry, err error) error {
|
err = fs.WalkDir(
|
||||||
if err != nil {
|
os.DirFS(imageDir), ".", func(path string, d fs.DirEntry, err error) error {
|
||||||
log.Error().Err(err).Msg("Error walking directory")
|
if err != nil {
|
||||||
return err
|
log.Error().Err(err).Msg("Error walking directory")
|
||||||
}
|
return err
|
||||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
}
|
||||||
log.Error().Err(ctxErr).Msg("Context closed while walking directory")
|
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||||
return ctxErr
|
log.Error().Err(ctxErr).Msg("Context closed while walking directory")
|
||||||
}
|
return ctxErr
|
||||||
if d.Type().IsRegular() {
|
}
|
||||||
filename := fmt.Sprintf("%s%c%s", imageDir, os.PathSeparator, d.Name())
|
if d.Type().IsRegular() {
|
||||||
log.Debug().Str("filename", filename).Msg("file scan")
|
filename := fmt.Sprintf("%s%c%s", imageDir, os.PathSeparator, d.Name())
|
||||||
validEntries = append(validEntries, filename)
|
log.Debug().Str("filename", filename).Msg("file scan")
|
||||||
}
|
validEntries = append(validEntries, filename)
|
||||||
return nil
|
}
|
||||||
})
|
return nil
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user