From 76876e012a4b73a8a4be6ef76c825cec568342eb Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sat, 8 Oct 2022 00:00:53 -0500 Subject: [PATCH] added some tests --- Makefile | 2 +- cmd/root.go | 6 +++--- cmd/root_test.go | 24 ++++++++++++++++++++++++ go.mod | 3 +++ web/handlers.go | 33 +++++++++++++++++---------------- 5 files changed, 48 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 24ae18d..ab27789 100644 --- a/Makefile +++ b/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 diff --git a/cmd/root.go b/cmd/root.go index 0abfd43..8f067c0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) diff --git a/cmd/root_test.go b/cmd/root_test.go index 89abf88..0f512a0 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -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) } diff --git a/go.mod b/go.mod index ea7a2a4..fc6cf82 100644 --- a/go.mod +++ b/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 diff --git a/web/handlers.go b/web/handlers.go index 146beac..103e06a 100644 --- a/web/handlers.go +++ b/web/handlers.go @@ -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 }