From 13281d929b275de58c5a92322cc87f394f42344d Mon Sep 17 00:00:00 2001 From: Benny Date: Mon, 3 Jul 2023 11:01:13 +0200 Subject: [PATCH] add some tests (#239) * add some tests * remove template --------- Co-authored-by: n0vad3v --- .github/workflows/CI.yaml | 16 +++++++++--- Makefile | 4 +-- config.json | 2 +- config/config.go | 16 ++++++------ config/config_test.go | 39 +++++++++++++++++++++++++++++ go.mod | 5 +++- go.sum | 4 +++ helper/helper_test.go | 52 +++++++++++++++++++++++++++++++++++++++ helper/test.txt | 1 + webp-server.go | 4 +++ 10 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 config/config_test.go create mode 100644 helper/helper_test.go create mode 100644 helper/test.txt diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 1219730..cc9feb5 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -1,16 +1,24 @@ name: CI check on every PR on: + push: + paths-ignore: + - '**.md' + - 'Makefile' + - 'config.json' + branches: + - 'master' + pull_request: branches: - master paths-ignore: - - '**.md' - - 'Makefile' - - 'config.json' + - '**.md' + - 'Makefile' + - 'config.json' jobs: ci: - name: CI check on every push + name: CI check on every push and PR runs-on: ubuntu-latest steps: - name: Checkout diff --git a/Makefile b/Makefile index 11c465c..e481b4c 100644 --- a/Makefile +++ b/Makefile @@ -31,8 +31,8 @@ static-check: install-staticcheck tools/bin/staticcheck -checks all,-ST1000 ./... GO111MODULE=on tools/bin/golangci-lint run -v $$($(PACKAGE_DIRECTORIES)) --config .golangci.yml -test: static-check - go test -v -coverprofile=coverage.txt -covermode=atomic +test: + go test -v -coverprofile=coverage.txt -covermode=atomic ./... clean: rm -rf builds prefetch remote-raw exhaust tools coverage.txt diff --git a/config.json b/config.json index 63a4012..14051ee 100644 --- a/config.json +++ b/config.json @@ -7,4 +7,4 @@ "ALLOWED_TYPES": ["jpg","png","jpeg","bmp","gif"], "ENABLE_AVIF": false, "ENABLE_EXTRA_PARAMS": false -} \ No newline at end of file +} diff --git a/config/config.go b/config/config.go index 5e533fa..d3364e1 100644 --- a/config/config.go +++ b/config/config.go @@ -51,7 +51,7 @@ WantedBy=multi-user.target` ) var ( - configPath string + ConfigPath string Jobs int DumpSystemd bool DumpConfig bool @@ -75,26 +75,24 @@ type jsonFile struct { } func init() { - flag.StringVar(&configPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)") + flag.StringVar(&ConfigPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)") flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to webp") flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.") flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json") flag.BoolVar(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.") flag.BoolVar(&ShowVersion, "V", false, "Show version information.") - flag.Parse() - Config = loadConfig() - switchProxyMode() + } -func loadConfig() (config jsonFile) { - jsonObject, err := os.Open(configPath) +func LoadConfig() { + jsonObject, err := os.Open(ConfigPath) if err != nil { log.Fatal(err) } decoder := json.NewDecoder(jsonObject) - _ = decoder.Decode(&config) + _ = decoder.Decode(&Config) _ = jsonObject.Close() - return config + switchProxyMode() } type ExtraParams struct { diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..53b1a77 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,39 @@ +package config + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMain(m *testing.M) { + ConfigPath = "../config.json" + m.Run() + ConfigPath = "config.json" + Config.ImgPath = "./pics" +} + +func TestLoadConfig(t *testing.T) { + LoadConfig() + assert.Equal(t, Config.Host, "127.0.0.1") + assert.Equal(t, Config.Port, "3333") + assert.Equal(t, Config.Quality, 80) + assert.Equal(t, Config.ImgPath, "./pics") + assert.Equal(t, Config.ExhaustPath, "./exhaust") +} + +func TestExtraParamsString(t *testing.T) { + param := ExtraParams{ + Width: 100, + Height: 100, + } + assert.Equal(t, param.String(), "_width=100&height=100") + +} + +func TestSwitchProxyMode(t *testing.T) { + switchProxyMode() + assert.False(t, ProxyMode) + Config.ImgPath = "https://picsum.photos" + switchProxyMode() + assert.True(t, ProxyMode) +} diff --git a/go.mod b/go.mod index 0294839..74a6920 100644 --- a/go.mod +++ b/go.mod @@ -10,11 +10,13 @@ require ( github.com/patrickmn/go-cache v2.1.0+incompatible github.com/schollz/progressbar/v3 v3.13.1 github.com/sirupsen/logrus v1.9.3 + github.com/stretchr/testify v1.8.4 github.com/valyala/fasthttp v1.47.0 ) require ( github.com/andybalholm/brotli v1.0.5 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/uuid v1.3.0 // indirect github.com/klauspost/compress v1.16.3 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -22,10 +24,10 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/philhofer/fwd v1.1.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect - github.com/stretchr/testify v1.8.4 // indirect github.com/tinylib/msgp v1.1.8 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect @@ -34,4 +36,5 @@ require ( golang.org/x/sys v0.8.0 // indirect golang.org/x/term v0.6.0 // indirect golang.org/x/text v0.8.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a15d44f..61cda23 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,7 @@ github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1 github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= @@ -30,6 +31,7 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= @@ -126,6 +128,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U= gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/helper/helper_test.go b/helper/helper_test.go new file mode 100644 index 0000000..2a5306f --- /dev/null +++ b/helper/helper_test.go @@ -0,0 +1,52 @@ +package helper + +import ( + "testing" + "webp_server_go/config" + + "github.com/stretchr/testify/assert" +) + +func TestMain(m *testing.M) { + config.ConfigPath = "../config.json" + config.LoadConfig() + m.Run() + config.ConfigPath = "config.json" + +} + +func TestFileCount(t *testing.T) { + // test helper dir + count := FileCount("./") + assert.Equal(t, int64(3), count) +} + +func TestImageExists(t *testing.T) { + t.Run("file not exists", func(t *testing.T) { + assert.False(t, ImageExists("dgyuaikdsa")) + }) + + t.Run("file size incorrect", func(t *testing.T) { + assert.False(t, ImageExists("test.txt")) + }) + + // TODO: how to test lock? + + t.Run("test dir", func(t *testing.T) { + assert.False(t, ImageExists("/tmp")) + }) + + t.Run("test file", func(t *testing.T) { + assert.True(t, ImageExists("./helper_test.go")) + }) +} + +func TestCheckAllowedType(t *testing.T) { + t.Run("not allowed type", func(t *testing.T) { + assert.False(t, CheckAllowedType("test.txt")) + }) + + t.Run("allowed type", func(t *testing.T) { + assert.True(t, CheckAllowedType("test.jpg")) + }) +} diff --git a/helper/test.txt b/helper/test.txt new file mode 100644 index 0000000..283e5e9 --- /dev/null +++ b/helper/test.txt @@ -0,0 +1 @@ +not an image diff --git a/webp-server.go b/webp-server.go index b81b5c9..f07f374 100644 --- a/webp-server.go +++ b/webp-server.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "os" "runtime" @@ -44,6 +45,9 @@ func setupLogger() { } func init() { + // main init is the last one to be called + flag.Parse() + config.LoadConfig() setupLogger() }