From 14efa00144de2917885b50e62c78536826d92b04 Mon Sep 17 00:00:00 2001 From: jmnote Date: Fri, 29 Dec 2023 09:27:51 +0000 Subject: [PATCH] checks --- hack/go-licenses.sh | 15 +++++++++++++++ hack/test-cover.sh | 20 +++++++++++++++++++ main.go | 16 ++++++++++----- main_test.go | 47 +++++++++++++++++++++++++++++++-------------- 4 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 hack/go-licenses.sh create mode 100644 hack/test-cover.sh diff --git a/hack/go-licenses.sh b/hack/go-licenses.sh new file mode 100644 index 0000000..1becef1 --- /dev/null +++ b/hack/go-licenses.sh @@ -0,0 +1,15 @@ +#!/bin/bash +cd $(dirname $0)/.. + +which go-licenses || go install github.com/google/go-licenses@v1.6.0 + +echo go-licenses report +go-licenses report . | tee docs/go-licenses.csv + +echo go-licenses check +go-licenses check . +if [[ $? != 0 ]]; then + echo "❌ FAIL" + exit 1 +fi +echo "✔️ OK" diff --git a/hack/test-cover.sh b/hack/test-cover.sh new file mode 100644 index 0000000..cd111c3 --- /dev/null +++ b/hack/test-cover.sh @@ -0,0 +1,20 @@ +#!/bin/bash +MIN_COVER=50.0 + +cd $(dirname $0)/.. +export PS4='[$(basename $0):$LINENO] ' +set -x + +go test ./... -v -failfast -race -coverprofile /tmp/coverprofile +if [[ $? != 0 ]]; then + echo "❌ FAIL - test failed" + exit 1 +fi + +COVER=$(go tool cover -func /tmp/coverprofile | tail -1 | grep -oP [0-9.]+) +rm -f /tmp/coverprofile +if [[ $COVER < $MIN_COVER ]]; then + echo "⚠️ WARN - total COVER: ${COVER}% (<${MIN_COVER}%)" + exit +fi +echo "✔️ OK - total COVER: ${COVER}% (>=${MIN_COVER}%)" diff --git a/main.go b/main.go index 099ef0c..cf1d286 100644 --- a/main.go +++ b/main.go @@ -27,15 +27,18 @@ func loadConfig() (Config, error) { return cfg, nil } -func setupRouter(cfg Config) *gin.Engine { +func setupRouter(cfg Config) (*gin.Engine, error) { gin.SetMode(gin.ReleaseMode) router := gin.Default() - router.SetTrustedProxies(cfg.Proxies) + err := router.SetTrustedProxies(cfg.Proxies) + if err != nil { + return nil, fmt.Errorf("setTrustedProxies err: %w", err) + } router.ForwardedByClientIP = true router.GET("/", func(c *gin.Context) { c.String(http.StatusOK, c.ClientIP()+"\n") }) - return router + return router, nil } func run() error { @@ -44,8 +47,11 @@ func run() error { return fmt.Errorf("loadConfig err: %w", err) } log.Println("IP App started...") - r := setupRouter(cfg) - return r.Run(cfg.Addr) + router, err := setupRouter(cfg) + if err != nil { + return fmt.Errorf("setupRouter err: %w", err) + } + return router.Run(cfg.Addr) } func main() { diff --git a/main_test.go b/main_test.go index 0487ae3..16ed2bc 100644 --- a/main_test.go +++ b/main_test.go @@ -23,24 +23,43 @@ func TestLoadConfig(t *testing.T) { } func TestSetupRouter(t *testing.T) { - router := setupRouter(Config{}) - w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/", nil) - router.ServeHTTP(w, req) - require.Equal(t, 200, w.Code) - require.Equal(t, "\n", w.Body.String()) + t.Run("error", func(t *testing.T) { + router, err := setupRouter(Config{Proxies: []string{"hello", "world"}}) + require.EqualError(t, err, "setTrustedProxies err: invalid IP address: hello") + require.Nil(t, router) + }) + t.Run("ok", func(t *testing.T) { + router, err := setupRouter(Config{}) + require.NoError(t, err) + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/", nil) + router.ServeHTTP(w, req) + require.Equal(t, 200, w.Code) + require.Equal(t, "\n", w.Body.String()) + }) } func TestRun(t *testing.T) { - os.Clearenv() - os.Setenv("APP_DEBUG", "hello") - err := run() - require.EqualError(t, err, `loadConfig err: process err: envconfig.Process: assigning APP_DEBUG to Debug: converting 'hello' to type bool. details: strconv.ParseBool: parsing "hello": invalid syntax`) + t.Run("error APP_DEBUG", func(t *testing.T) { + os.Clearenv() + os.Setenv("APP_DEBUG", "hello") + err := run() + require.EqualError(t, err, `loadConfig err: process err: envconfig.Process: assigning APP_DEBUG to Debug: converting 'hello' to type bool. details: strconv.ParseBool: parsing "hello": invalid syntax`) + }) - os.Clearenv() - os.Setenv("APP_ADDR", "hello") - err = run() - require.EqualError(t, err, "listen tcp: address hello: missing port in address") + t.Run("error APP_ADDR", func(t *testing.T) { + os.Clearenv() + os.Setenv("APP_ADDR", "hello") + err := run() + require.EqualError(t, err, "listen tcp: address hello: missing port in address") + }) + + t.Run("error APP_PROXIES", func(t *testing.T) { + os.Clearenv() + os.Setenv("APP_PROXIES", "hello") + err := run() + require.EqualError(t, err, "setupRouter err: setTrustedProxies err: invalid IP address: hello") + }) } func TestMain(t *testing.T) {