Skip to content

Commit

Permalink
fix: fix test package funcs (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Aug 25, 2023
2 parents d4ce3f1 + 207e787 commit ac61976
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 135 deletions.
11 changes: 4 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ linters:
- gomnd # https://github.com/tommy-muehle/go-mnd
- ifshort # for readability
- interfacer # deprecated https://github.com/mvdan/interfacer
- interfacebloat # unnecessary
- ireturn # unnecessary
- lll # unnecessary
- maligned # deprecated https://github.com/mdempsky/maligned
- nlreturn # ignore "return with no blank line before"
Expand All @@ -33,13 +35,6 @@ linters:
linters-settings:
goimports:
local-prefixes: "github.com/kunitsucom/util.go"
# varnamelen:
# ignore-decls:
# - l *Logger
# - l *rec.Logger
# - rw http.ResponseWriter
# - w http.ResponseWriter
# - r *http.Request
wrapcheck:
ignoreSigs:
- func errors.New(text string) error
Expand All @@ -62,4 +57,6 @@ issues:
- gosec
- maintidx
- noctx
- revive
- varnamelen
- wrapcheck
2 changes: 1 addition & 1 deletion archive/zip/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type addFileToZipConfig struct {
decompressionBombLimit int64
}

func AddFileToZipWithDecompressionBombLimit(decompressionBombLimit int64) AddFileToZipOption { //nolint:ireturn
func AddFileToZipWithDecompressionBombLimit(decompressionBombLimit int64) AddFileToZipOption {
return addFileToZipOptionFunc(func(cfg *addFileToZipConfig) {
cfg.decompressionBombLimit = decompressionBombLimit
})
Expand Down
6 changes: 3 additions & 3 deletions archive/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ type zipDirOptionFunc func(*zipDirConfig)

func (f zipDirOptionFunc) apply(cfg *zipDirConfig) { f(cfg) }

func ZipDirWithWalkHandler(f func(path string, info os.FileInfo, err error) error) ZipDirOption { //nolint:ireturn
func ZipDirWithWalkHandler(f func(path string, info os.FileInfo, err error) error) ZipDirOption {
return zipDirOptionFunc(func(cfg *zipDirConfig) {
cfg.walkHandler = f
})
}

func ZipDirWithPathInZipHandler(f func(path string) string) ZipDirOption { //nolint:ireturn
func ZipDirWithPathInZipHandler(f func(path string) string) ZipDirOption {
return zipDirOptionFunc(func(cfg *zipDirConfig) {
cfg.pathInZipHandler = f
})
Expand Down Expand Up @@ -97,7 +97,7 @@ type unzipFileOptionFunc func(*unzipFileConfig)

func (f unzipFileOptionFunc) apply(cfg *unzipFileConfig) { f(cfg) }

func UnzipFileWithFileInZipHandler(f func(zipfile *zip.File, dstDir string) error) UnzipFileOption { //nolint:ireturn
func UnzipFileWithFileInZipHandler(f func(zipfile *zip.File, dstDir string) error) UnzipFileOption {
return unzipFileOptionFunc(func(cfg *unzipFileConfig) {
cfg.unzipFileFileInZipHandler = f
})
Expand Down
6 changes: 3 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ func (s *Store[T]) StopRefresher() {

// GetOrSet gets cache value T, or set the value T that returns getValue.
// If getValue does not return err, cache the value T.
func (s *Store[T]) GetOrSet(key string, getValue func() (T, error)) (T, error) { //nolint:ireturn
func (s *Store[T]) GetOrSet(key string, getValue func() (T, error)) (T, error) {
return s.GetOrSetWithTTL(key, getValue, s.defaultTTL)
}

// GetOrSet gets cache value T, or set the value T that returns getValue with TTL.
// If getValue does not return err, cache the value T.
func (s *Store[T]) GetOrSetWithTTL(key string, getValue func() (T, error), ttl time.Duration) (T, error) { //nolint:ireturn
func (s *Store[T]) GetOrSetWithTTL(key string, getValue func() (T, error), ttl time.Duration) (T, error) {
return s.getOrSet(key, getValue, ttl, time.Now())
}

func (s *Store[T]) getOrSet(key string, getValue func() (T, error), ttl time.Duration, now time.Time) (T, error) { //nolint:ireturn
func (s *Store[T]) getOrSet(key string, getValue func() (T, error), ttl time.Duration, now time.Time) (T, error) {
s.mu.Lock()
defer s.mu.Unlock()

Expand Down
2 changes: 1 addition & 1 deletion database/sql/queryer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type _Queryer struct {
SQLQueryer
}

func NewDB(db SQLQueryer) Queryer { //nolint:ireturn
func NewDB(db SQLQueryer) Queryer {
return &_Queryer{
SQLQueryer: db,
}
Expand Down
6 changes: 0 additions & 6 deletions discard/discard.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
package discard

//nolint:ireturn
func Discard(discard any) {
_ = discard
}

//nolint:ireturn
func One[T any](v1 T, _ any) T {
return v1
}

//nolint:ireturn
func Two[T1, T2 any](v1 T1, v2 T2, _ any) (T1, T2) {
return v1, v2
}

//nolint:ireturn
func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, _ any) (T1, T2, T3) {
return v1, v2, v3
}

//nolint:ireturn
func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, _ any) (T1, T2, T3, T4) {
return v1, v2, v3, v4
}

//nolint:ireturn
func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, _ any) (T1, T2, T3, T4, T5) {
return v1, v2, v3, v4, v5
}
7 changes: 5 additions & 2 deletions mime/mime_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mime_test

import (
"bytes"
"strings"
"testing"

Expand All @@ -25,7 +24,11 @@ func TestDetectContentType(t *testing.T) {

t.Run("failure", func(t *testing.T) {
t.Parallel()
r := testz.NewReadWriter(bytes.NewBuffer(nil), 0, testz.ErrTestError)
r := &testz.Reader{
ReadFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
}
if _, err := mime.DetectContentType(r); err == nil {
t.Errorf("❌: err == nil")
}
Expand Down
10 changes: 5 additions & 5 deletions must/must.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ func Must(err error) {
}
}

func One[T any](v T, err error) T { //nolint:ireturn
func One[T any](v T, err error) T {
if err != nil {
panic(err)
}

return v
}

func Two[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) { //nolint:ireturn
func Two[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) {
if err != nil {
panic(err)
}

return v1, v2
}

func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) { //nolint:ireturn
func Three[T1, T2, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) {
if err != nil {
panic(err)
}

return v1, v2, v3
}

func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) { //nolint:ireturn
func Four[T1, T2, T3, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4) {
if err != nil {
panic(err)
}

return v1, v2, v3, v4
}

func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) { //nolint:ireturn
func Five[T1, T2, T3, T4, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5) {
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions net/http/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ type headerBuilder struct {
header http.Header
}

func NewHeaderBuilder() HeaderBuilder { //nolint:ireturn
func NewHeaderBuilder() HeaderBuilder {
return &headerBuilder{
header: make(http.Header),
}
}

func (h *headerBuilder) Add(key, value string) HeaderBuilder { //nolint:ireturn
func (h *headerBuilder) Add(key, value string) HeaderBuilder {
h.header.Add(key, value)
return h
}

func (h *headerBuilder) Set(key, value string) HeaderBuilder { //nolint:ireturn
func (h *headerBuilder) Set(key, value string) HeaderBuilder {
h.header.Set(key, value)
return h
}

func (h *headerBuilder) Merge(header http.Header) HeaderBuilder { //nolint:ireturn
func (h *headerBuilder) Merge(header http.Header) HeaderBuilder {
for key, values := range header {
for _, value := range values {
h.Add(key, value)
Expand Down
17 changes: 15 additions & 2 deletions net/http/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@ func TestMiddlewares(t *testing.T) {
expect := "middleware 5 preProcess\nmiddleware 4 preProcess\nmiddleware 3 preProcess\nmiddleware 2 preProcess\nmiddleware 1 preProcess\ntest_request_body\nmiddleware 1 postProcess\nmiddleware 2 postProcess\nmiddleware 3 postProcess\nmiddleware 4 postProcess\nmiddleware 5 postProcess\n"
request := bytes.NewBufferString("test_request_body\n")
r := httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", request)
hander := mids(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := mids(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.Copy(w, r.Body)
}))

response := bytes.NewBuffer(nil)
hander.ServeHTTP(testz.NewResponseWriter(response, nil, 0, nil), r)
handler.ServeHTTP(
&testz.ResponseWriter{
WriteFunc: func(p []byte) (n int, err error) {
return response.Write(p)
},
HeaderFunc: func() http.Header {
return http.Header{}
},
WriteHeaderFunc: func(statusCode int) {
// do nothing
},
},
r,
)

actual := response.String()
if expect != actual {
Expand Down
2 changes: 1 addition & 1 deletion net/http/realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func NewXRealIPHandler(set_real_ip_from []*net.IPNet, real_ip_header string, rea
}
}

func WithClientIPAddressHeader(header string) XRealIPHandlerOption { //nolint:ireturn
func WithClientIPAddressHeader(header string) XRealIPHandlerOption {
return xRealIPHandlerOption(func(h *xRealIPHandlerConfig) {
h.clientIPAddressHeader = header
})
Expand Down
18 changes: 16 additions & 2 deletions net/http/request_body_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ func TestRequestBodyBuffer(t *testing.T) {
t.Parallel()

expect := (*bytes.Buffer)(nil)
r := httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", testz.NewReadWriter(bytes.NewBuffer(nil), 0, testz.ErrTestError))
r := httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", &testz.ReadWriter{
ReadFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
WriteFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
})
actual, err := httpz.RequestBodyBuffer(r)
if !errors.Is(err, testz.ErrTestError) {
t.Errorf("❌: err != nil: %v", err)
Expand Down Expand Up @@ -128,7 +135,14 @@ func TestNewRequestBodyBufferHandler(t *testing.T) {
httpz.WithRequestBodyBufferingSkipLimit(100),
).Middleware

r := httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", testz.NewReadWriter(bytes.NewBuffer(nil), 0, testz.ErrTestError))
r := httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", &testz.ReadWriter{
ReadFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
WriteFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
})
middleware(http.HandlerFunc(
func(rw http.ResponseWriter, r *http.Request) {
actualBuf, ok = httpz.ContextRequestBodyBuffer(r.Context())
Expand Down
10 changes: 7 additions & 3 deletions net/http/response_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package httpz_test

import (
"bytes"
"errors"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -36,9 +36,13 @@ func TestReadResponseBody(t *testing.T) {
t.Parallel()

_, err := httpz.NewBufferFromResponseBody(&http.Response{
Body: io.NopCloser(testz.NewReadWriter(bytes.NewBuffer(nil), 0, testz.ErrTestError)),
Body: io.NopCloser(&testz.ReadWriter{
ReadFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
}),
})
if err == nil {
if !errors.Is(err, testz.ErrTestError) {
t.Errorf("❌: httpz.NewBufferFromResponseBody: err == nil")
}
})
Expand Down
15 changes: 13 additions & 2 deletions net/http/response_writer_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,30 @@ func TestResponseWriterBufferHandler(t *testing.T) {
ResponseWriterBufferHandlerTestOption,
).Middleware

header := http.Header{}
middleware(http.HandlerFunc(
func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
rw.Header().Set("Test-Header", "TestString")
_, _ = io.Copy(rw, r.Body)
},
)).ServeHTTP(
testz.NewResponseWriter(bytes.NewBuffer(nil), nil, 0, testz.ErrTestError),
&testz.ResponseWriter{
WriteFunc: func(p []byte) (n int, err error) {
return 0, testz.ErrTestError
},
HeaderFunc: func() http.Header {
return header
},
WriteHeaderFunc: func(statusCode int) {
// do nothing
},
},
httptest.NewRequest(http.MethodPost, "http://util.go/net/httpz", bytes.NewBufferString("test_request_body")),
)

if expect != actual {
t.Errorf("❌: expect != actual: %s", actual)
t.Errorf("❌: expect(%s) != actual(%s)", expect, actual)
}
})
}
4 changes: 2 additions & 2 deletions slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ func Select[Source, Selected any](s []Source, generator func(index int, source S
return gen
}

func First[T any](s []T) T { //nolint:ireturn
func First[T any](s []T) T {
if len(s) == 0 {
var zero T
return zero
}
return s[0]
}

func Last[T any](s []T) T { //nolint:ireturn
func Last[T any](s []T) T {
if len(s) == 0 {
var zero T
return zero
Expand Down
5 changes: 5 additions & 0 deletions test/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package testz

import "errors"

var ErrTestError = errors.New("testz: test error")
18 changes: 18 additions & 0 deletions test/read_closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package testz

import "io"

var _ io.ReadCloser = (*ReadCloser)(nil)

type ReadCloser struct {
ReadFunc func(p []byte) (n int, err error)
CloseFunc func() error
}

func (rc *ReadCloser) Read(p []byte) (n int, err error) {
return rc.ReadFunc(p)
}

func (rc *ReadCloser) Close() error {
return rc.CloseFunc()
}
Loading

0 comments on commit ac61976

Please sign in to comment.