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
|
||||
|
||||
test: generate
|
||||
go test ./...
|
||||
go test ./... -cover
|
||||
|
||||
watch:
|
||||
modd
|
||||
|
||||
@ -68,12 +68,12 @@ func init() {
|
||||
viper.AddConfigPath(fmt.Sprintf("/etc%c%s", os.PathSeparator, dirName))
|
||||
viper.SetConfigName("config.toml")
|
||||
viper.SetEnvPrefix("hasshelper")
|
||||
viper.Set("version", gitVersion)
|
||||
viper.SetDefault("deployment", "prod")
|
||||
viper.SetDefault("loglevel", "info")
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
viper.Set("version", gitVersion)
|
||||
viper.SetDefault("deployment", "prod")
|
||||
viper.SetDefault("loglevel", "info")
|
||||
viper.AutomaticEnv()
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
|
||||
@ -1,15 +1,39 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestInitConfig(t *testing.T) {
|
||||
viper.Reset()
|
||||
viper.Set("deployment", "testing")
|
||||
viper.Set("image_dir", "/tmp")
|
||||
viper.Set("webserver_port", "8080")
|
||||
|
||||
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/spf13/cobra v1.5.0
|
||||
github.com/spf13/viper v1.12.0
|
||||
github.com/stretchr/testify v1.8.0
|
||||
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/termlog v0.0.0-20210222042314-a1eec763abec // 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/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
@ -46,6 +48,7 @@ require (
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // 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/sergi/go-diff v1.2.0 // 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) {
|
||||
validEntries := make([]string, 0)
|
||||
log := config.logger.With().Str("imageDir", imageDir).Logger()
|
||||
err = fs.WalkDir(os.DirFS(imageDir), ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
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")
|
||||
return ctxErr
|
||||
}
|
||||
if d.Type().IsRegular() {
|
||||
filename := fmt.Sprintf("%s%c%s", imageDir, os.PathSeparator, d.Name())
|
||||
log.Debug().Str("filename", filename).Msg("file scan")
|
||||
validEntries = append(validEntries, filename)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
err = fs.WalkDir(
|
||||
os.DirFS(imageDir), ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
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")
|
||||
return ctxErr
|
||||
}
|
||||
if d.Type().IsRegular() {
|
||||
filename := fmt.Sprintf("%s%c%s", imageDir, os.PathSeparator, d.Name())
|
||||
log.Debug().Str("filename", filename).Msg("file scan")
|
||||
validEntries = append(validEntries, filename)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user