diff --git a/captcha.go b/captcha.go index b01b5d8..fc4e5d6 100644 --- a/captcha.go +++ b/captcha.go @@ -22,8 +22,11 @@ import ( const charPreset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" -var rng = rand.New(rand.NewSource(time.Now().UnixNano())) -var ttfFont *truetype.Font +// nolint: gochecknoglobals +var ( + rng = rand.New(rand.NewSource(time.Now().UnixNano())) + ttfFont *truetype.Font +) // Options manage captcha generation details. type Options struct { @@ -103,6 +106,7 @@ func (data *Data) WriteGIF(w io.Writer, o *gif.Options) error { return gif.Encode(w, data.img, o) } +// nolint: gochecknoinits func init() { ttfFont, _ = freetype.ParseFont(ttf) } @@ -223,7 +227,7 @@ func drawSineCurve(img *image.NRGBA, opts *Options) { } } -func drawText(text string, img *image.NRGBA, opts *Options) error { +func drawText(text string, img *image.NRGBA, opts *Options) error { // nolint: interfacer ctx := freetype.NewContext() ctx.SetDPI(opts.FontDPI) ctx.SetClip(img.Bounds()) diff --git a/captcha_test.go b/captcha_test.go index 26b96f7..74fc4cb 100644 --- a/captcha_test.go +++ b/captcha_test.go @@ -19,6 +19,7 @@ func TestNewCaptcha(t *testing.T) { if err != nil { t.Fatal(err) } + buf := new(bytes.Buffer) err = data.WriteImage(buf) if err != nil { @@ -38,6 +39,7 @@ func TestEncodeJPG(t *testing.T) { if err != nil { t.Fatal(err) } + buf := new(bytes.Buffer) err = data.WriteJPG(buf, &jpeg.Options{Quality: 70}) if err != nil { @@ -50,25 +52,32 @@ func TestEncodeGIF(t *testing.T) { if err != nil { t.Fatal(err) } + buf := new(bytes.Buffer) - err = data.WriteGIF(buf, &gif.Options{}) + err = data.WriteGIF(buf, new(gif.Options)) if err != nil { t.Fatal(err) } } func TestNewCaptchaOptions(t *testing.T) { - New(100, 34, func(options *Options) { + _, err := New(100, 34, func(options *Options) { options.BackgroundColor = color.Opaque options.CharPreset = "1234567890" options.CurveNumber = 0 options.TextLength = 6 options.Palette = palette.WebSafe }) + if err != nil { + t.Fatal(err) + } - NewMathExpr(100, 34, func(options *Options) { + _, err = NewMathExpr(100, 34, func(options *Options) { options.BackgroundColor = color.Black }) + if err != nil { + t.Fatal(err) + } } func TestNewMathExpr(t *testing.T) { @@ -132,19 +141,21 @@ func TestLoadFontFromReader(t *testing.T) { } func TestMaxColor(t *testing.T) { - var result uint32 - result = maxColor() + result := maxColor() if result != 0 { t.Fatalf("Expect max color to be 0, got %v", result) } + result = maxColor(1) if result != 1 { t.Fatalf("Expect max color to be 1, got %v", result) } + result = maxColor(52428, 65535) if result != 255 { t.Fatalf("Expect max color to be 255, got %v", result) } + var rng = rand.New(rand.NewSource(0)) for i := 0; i < 10; i++ { result = maxColor(rng.Uint32(), rng.Uint32(), rng.Uint32()) @@ -155,19 +166,21 @@ func TestMaxColor(t *testing.T) { } func TestMinColor(t *testing.T) { - var result uint32 - result = minColor() + result := minColor() if result != 255 { t.Fatalf("Expect min color to be 255, got %v", result) } + result = minColor(1) if result != 1 { t.Fatalf("Expect min color to be 1, got %v", result) } + result = minColor(52428, 65535) if result != 204 { t.Fatalf("Expect min color to be 1, got %v", result) } + var rng = rand.New(rand.NewSource(0)) for i := 0; i < 10; i++ { result = minColor(rng.Uint32(), rng.Uint32(), rng.Uint32()) diff --git a/example/basic/main.go b/example/basic/main.go index d7f8790..ddd400e 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -12,6 +12,7 @@ func main() { http.HandleFunc("/", indexHandle) http.HandleFunc("/captcha-default", captchaHandle) http.HandleFunc("/captcha-math", mathHandle) + fmt.Println("Server start at port 8080") err := http.ListenAndServe(":8080", nil) if err != nil { @@ -23,9 +24,16 @@ func indexHandle(w http.ResponseWriter, _ *http.Request) { doc, err := template.ParseFiles("index.html") if err != nil { fmt.Fprint(w, err.Error()) + + return + } + + err = doc.Execute(w, nil) + if err != nil { + fmt.Println(err.Error()) + return } - doc.Execute(w, nil) } func captchaHandle(w http.ResponseWriter, _ *http.Request) { @@ -33,9 +41,16 @@ func captchaHandle(w http.ResponseWriter, _ *http.Request) { if err != nil { fmt.Fprint(w, nil) fmt.Println(err.Error()) + + return + } + + err = img.WriteImage(w) + if err != nil { + fmt.Println(err.Error()) + return } - img.WriteImage(w) } func mathHandle(w http.ResponseWriter, _ *http.Request) { @@ -45,5 +60,11 @@ func mathHandle(w http.ResponseWriter, _ *http.Request) { fmt.Println(err.Error()) return } - img.WriteImage(w) + + err = img.WriteImage(w) + if err != nil { + fmt.Println(err.Error()) + + return + } } diff --git a/example/load-font/main.go b/example/load-font/main.go index ba5820e..cf4113c 100644 --- a/example/load-font/main.go +++ b/example/load-font/main.go @@ -17,6 +17,7 @@ func main() { http.HandleFunc("/", indexHandle) http.HandleFunc("/captcha", captchaHandle) + fmt.Println("Server start at port 8080") err = http.ListenAndServe(":8080", nil) if err != nil { @@ -28,9 +29,16 @@ func indexHandle(w http.ResponseWriter, _ *http.Request) { doc, err := template.ParseFiles("index.html") if err != nil { fmt.Fprint(w, err.Error()) + + return + } + + err = doc.Execute(w, nil) + if err != nil { + fmt.Println(err.Error()) + return } - doc.Execute(w, nil) } func captchaHandle(w http.ResponseWriter, _ *http.Request) { @@ -40,7 +48,14 @@ func captchaHandle(w http.ResponseWriter, _ *http.Request) { if err != nil { fmt.Fprint(w, nil) fmt.Println(err.Error()) + + return + } + + err = img.WriteImage(w) + if err != nil { + fmt.Println(err.Error()) + return } - img.WriteImage(w) } diff --git a/hsva.go b/hsva.go index 3035419..42ecdd6 100644 --- a/hsva.go +++ b/hsva.go @@ -39,5 +39,6 @@ func (c hsva) RGBA() (r, g, b, a uint32) { b |= b << 8 a = uint32(c.a) a |= a << 8 - return + + return r, g, b, a } diff --git a/hsva_test.go b/hsva_test.go index 2f4e353..d7cbd92 100644 --- a/hsva_test.go +++ b/hsva_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func TestHSVAInterface(t *testing.T) { +func TestHSVAInterface(_ *testing.T) { var _ color.Color = hsva{} }