fix some linter issues
Signed-off-by: s3rj1k <evasive.gyron@gmail.com>
This commit is contained in:
parent
b06ff17030
commit
c9278e7960
6 changed files with 71 additions and 17 deletions
10
captcha.go
10
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())
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
3
hsva.go
3
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
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestHSVAInterface(t *testing.T) {
|
||||
func TestHSVAInterface(_ *testing.T) {
|
||||
var _ color.Color = hsva{}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue