From e441e541c72bc0bd246719eac09c2f3ca8296188 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 13 Sep 2024 13:38:30 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 4 +++ config/config_test.go | 26 +++++++++++++++++++ config/env_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 +++ go.sum | 8 ++++++ 5 files changed, 101 insertions(+) create mode 100644 config/config_test.go create mode 100644 config/env_test.go diff --git a/config/config.go b/config/config.go index 835d7f7..33c2e54 100644 --- a/config/config.go +++ b/config/config.go @@ -4,3 +4,7 @@ type Config struct { Env Env BaseURL string } + +func (c Config) Environment() Env { + return ValidEnvOrDev(c.Env) +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..2586018 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,26 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEnvDefaultsToDev(t *testing.T) { + c := Config{} + assert.Equal(t, DEV, c.Environment()) +} + +func TestInvalidEnvReturnsDev(t *testing.T) { + c := Config{Env: Env("foobar")} + assert.Equal(t, DEV, c.Environment()) +} + +func TestValidEnvReturnsCorrect(t *testing.T) { + for _, e := range Envs { + t.Run(string(e), func(t *testing.T) { + c := Config{Env: e} + assert.Equal(t, e, c.Environment()) + }) + } +} diff --git a/config/env_test.go b/config/env_test.go new file mode 100644 index 0000000..2084c0a --- /dev/null +++ b/config/env_test.go @@ -0,0 +1,59 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEnvString(t *testing.T) { + cases := Envs[:] + cases = append(cases, Env("foobar"), Env(""), Env("42"), Env("✨")) + for _, e := range cases { + t.Run(string(e), func(t *testing.T) { + assert.Equal(t, string(e), e.String()) + }) + } +} + +func TestValidEnv(t *testing.T) { + cases := [...]struct { + e Env + exp bool + }{ + {DEV, true}, + {PROD, true}, + {QA, true}, + {TEST, true}, + {Env("foobar"), false}, + {Env(""), false}, + {Env("✨"), false}, + } + + for _, c := range cases { + t.Run(string(c.e), func(t *testing.T) { + assert.Equal(t, c.exp, ValidEnv(c.e)) + }) + } +} + +func TestValidEnvOrDev(t *testing.T) { + cases := [...]struct { + give Env + exp Env + }{ + {DEV, DEV}, + {PROD, PROD}, + {QA, QA}, + {TEST, TEST}, + {Env("foobar"), DEV}, + {Env(""), DEV}, + {Env("✨"), DEV}, + } + + for _, c := range cases { + t.Run(string(c.give), func(t *testing.T) { + assert.Equal(t, c.exp, ValidEnvOrDev(c.give)) + }) + } +} diff --git a/go.mod b/go.mod index 1704bf7..418bc6a 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-ap/fedbox v0.0.0-20240910163620-7bcedb2eb399 github.com/go-ap/processing v0.0.0-20240910151355-8284a5ce9c22 github.com/go-ap/storage-sqlite v0.0.0-20240910151457-20fa80d963aa + github.com/stretchr/testify v1.9.0 ) require ( @@ -15,6 +16,7 @@ require ( git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 // indirect git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120 // indirect git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-ap/cache v0.0.0-20240910141827-94f8ac1a9133 // indirect github.com/go-ap/errors v0.0.0-20240910140019-1e9d33cc1568 // indirect @@ -31,6 +33,7 @@ require ( github.com/ncruces/go-strftime v0.1.9 // indirect github.com/openshift/osin v1.0.2-0.20220317075346-0f4d38c6e53f // indirect github.com/pborman/uuid v1.2.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rs/xid v1.6.0 // indirect github.com/rs/zerolog v1.33.0 // indirect @@ -39,6 +42,7 @@ require ( golang.org/x/oauth2 v0.23.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a // indirect modernc.org/mathutil v1.6.0 // indirect modernc.org/memory v1.8.0 // indirect diff --git a/go.sum b/go.sum index 9637828..617a50d 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7/go.mod h1:VApG24PG5Ij github.com/carlmjohnson/be v0.23.2 h1:1QjPnPJhwGUjsD9+7h98EQlKsxnG5TV+nnEvk0wnkls= github.com/carlmjohnson/be v0.23.2/go.mod h1:KAgPUh0HpzWYZZI+IABdo80wTgY43YhbdsiLYAaSI/Q= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= @@ -75,6 +77,8 @@ github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ= github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -107,7 +111,11 @@ golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg= golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/square/go-jose.v1 v1.1.2/go.mod h1:QpYS+a4WhS+DTlyQIi6Ka7MS3SuR9a055rgXNEe6EiA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE= modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ= modernc.org/gc/v3 v3.0.0-20240801135723-a856999a2e4a h1:CfbpOLEo2IwNzJdMvE8aiRbPMxoTpgAJeyePh0SmO8M=