mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
add some tests (#239)
* add some tests * remove template --------- Co-authored-by: n0vad3v <n0vad3v@riseup.net>
This commit is contained in:
parent
eea4537f22
commit
13281d929b
10
.github/workflows/CI.yaml
vendored
10
.github/workflows/CI.yaml
vendored
@ -1,5 +1,13 @@
|
|||||||
name: CI check on every PR
|
name: CI check on every PR
|
||||||
on:
|
on:
|
||||||
|
push:
|
||||||
|
paths-ignore:
|
||||||
|
- '**.md'
|
||||||
|
- 'Makefile'
|
||||||
|
- 'config.json'
|
||||||
|
branches:
|
||||||
|
- 'master'
|
||||||
|
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
@ -10,7 +18,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
ci:
|
ci:
|
||||||
name: CI check on every push
|
name: CI check on every push and PR
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
|
4
Makefile
4
Makefile
@ -31,8 +31,8 @@ static-check: install-staticcheck
|
|||||||
tools/bin/staticcheck -checks all,-ST1000 ./...
|
tools/bin/staticcheck -checks all,-ST1000 ./...
|
||||||
GO111MODULE=on tools/bin/golangci-lint run -v $$($(PACKAGE_DIRECTORIES)) --config .golangci.yml
|
GO111MODULE=on tools/bin/golangci-lint run -v $$($(PACKAGE_DIRECTORIES)) --config .golangci.yml
|
||||||
|
|
||||||
test: static-check
|
test:
|
||||||
go test -v -coverprofile=coverage.txt -covermode=atomic
|
go test -v -coverprofile=coverage.txt -covermode=atomic ./...
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf builds prefetch remote-raw exhaust tools coverage.txt
|
rm -rf builds prefetch remote-raw exhaust tools coverage.txt
|
||||||
|
@ -51,7 +51,7 @@ WantedBy=multi-user.target`
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configPath string
|
ConfigPath string
|
||||||
Jobs int
|
Jobs int
|
||||||
DumpSystemd bool
|
DumpSystemd bool
|
||||||
DumpConfig bool
|
DumpConfig bool
|
||||||
@ -75,26 +75,24 @@ type jsonFile struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to webp")
|
||||||
flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
|
flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
|
||||||
flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json")
|
flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json")
|
||||||
flag.BoolVar(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
|
flag.BoolVar(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
|
||||||
flag.BoolVar(&ShowVersion, "V", false, "Show version information.")
|
flag.BoolVar(&ShowVersion, "V", false, "Show version information.")
|
||||||
flag.Parse()
|
|
||||||
Config = loadConfig()
|
|
||||||
switchProxyMode()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() (config jsonFile) {
|
func LoadConfig() {
|
||||||
jsonObject, err := os.Open(configPath)
|
jsonObject, err := os.Open(ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
decoder := json.NewDecoder(jsonObject)
|
decoder := json.NewDecoder(jsonObject)
|
||||||
_ = decoder.Decode(&config)
|
_ = decoder.Decode(&Config)
|
||||||
_ = jsonObject.Close()
|
_ = jsonObject.Close()
|
||||||
return config
|
switchProxyMode()
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExtraParams struct {
|
type ExtraParams struct {
|
||||||
|
39
config/config_test.go
Normal file
39
config/config_test.go
Normal file
@ -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)
|
||||||
|
}
|
5
go.mod
5
go.mod
@ -10,11 +10,13 @@ require (
|
|||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/schollz/progressbar/v3 v3.13.1
|
github.com/schollz/progressbar/v3 v3.13.1
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
|
github.com/stretchr/testify v1.8.4
|
||||||
github.com/valyala/fasthttp v1.47.0
|
github.com/valyala/fasthttp v1.47.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
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/google/uuid v1.3.0 // indirect
|
||||||
github.com/klauspost/compress v1.16.3 // indirect
|
github.com/klauspost/compress v1.16.3 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // 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/mattn/go-runewidth v0.0.14 // indirect
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||||
github.com/philhofer/fwd v1.1.2 // 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/rivo/uniseg v0.2.0 // indirect
|
||||||
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
|
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
|
||||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // 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/tinylib/msgp v1.1.8 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
github.com/valyala/tcplisten 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/sys v0.8.0 // indirect
|
||||||
golang.org/x/term v0.6.0 // indirect
|
golang.org/x/term v0.6.0 // indirect
|
||||||
golang.org/x/text v0.8.0 // indirect
|
golang.org/x/text v0.8.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
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 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
|
||||||
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
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/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/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 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
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/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 h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
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/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 h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
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-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/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 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/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.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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
52
helper/helper_test.go
Normal file
52
helper/helper_test.go
Normal file
@ -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"))
|
||||||
|
})
|
||||||
|
}
|
1
helper/test.txt
Normal file
1
helper/test.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
not an image
|
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -44,6 +45,9 @@ func setupLogger() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
// main init is the last one to be called
|
||||||
|
flag.Parse()
|
||||||
|
config.LoadConfig()
|
||||||
setupLogger()
|
setupLogger()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user