Skip to content

Commit

Permalink
ci: add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
davidramiro committed Aug 31, 2024
1 parent 463976a commit b49ce78
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 109 deletions.
1 change: 0 additions & 1 deletion .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: goreleaser

on:
push:
tags:
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: go-lint
on:
push:
branches:
- master
pull_request:
branches:
- master

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
8 changes: 6 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ linters:
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
- testableexamples # checks if examples are testable (have an expected output)
- testifylint # checks usage of github.com/stretchr/testify
- testpackage # makes you use a separate _test package

- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- unconvert # removes unnecessary type conversions
- unparam # reports unused function parameters
Expand All @@ -289,6 +289,7 @@ linters:
- whitespace # detects leading and trailing whitespace

## you may want to enable
#- testpackage # makes you use a separate _test package
#- decorder # checks declaration order and count of types, constants, variables and functions
#- exhaustruct # [highly recommend to enable] checks if all structure fields are initialized
#- gci # controls golang package import order and makes it always deterministic
Expand Down Expand Up @@ -339,7 +340,7 @@ issues:
- source: "(noinspection|TODO)"
linters: [ godot ]
- source: "//noinspection"
linters: [ gocritic ]
linters: [ gocritic, gosec ]
- path: "_test\\.go"
linters:
- bodyclose
Expand All @@ -351,3 +352,6 @@ issues:
- wrapcheck
- text: 'shadow: declaration of "(ctx|err)" shadows declaration at'
linters: [ govet ]
# avoid false positives for tainted input on command execution, user input is only a number
- path: "internal/adapters/converter/magick.go"
linters: [ gosec ]
20 changes: 12 additions & 8 deletions internal/adapters/converter/magick.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"github.com/rs/zerolog/log"
"hsbot/internal/adapters/file"
"os/exec"
"path/filepath"
"strings"

"github.com/rs/zerolog/log"
)

const MaxPower = 100
Expand Down Expand Up @@ -53,22 +54,19 @@ func (m *MagickConverter) Scale(ctx context.Context, imageURL string, power floa
return nil, err
}

size := MaxPower - (power / PowerFactor)
outFile := fmt.Sprintf("%sliq%s", strings.TrimSuffix(path, extension), extension)
dimensions := fmt.Sprintf("%d%%x%d%%", int(size), int(size))
command := createCommand(m.magickBinary, power, path, outFile)

defer file.RemoveTempFile(path)
defer file.RemoveTempFile(outFile)

args := append(m.magickBinary, path, "-liquid-rescale", dimensions, outFile)

log.Debug().Strs("args", args).
Str("dimensions", dimensions).
log.Debug().
Strs("command", command).
Str("outFile", outFile).
Str("path", path).
Msg("scaling image")

cmd := exec.Command(args[0], args[1:]...)
cmd := exec.Command(command[0], command[1:]...)
out, err := cmd.Output()
if err != nil {
log.Error().Bytes("magickStderr", out).Msg("magick commands failed")
Expand All @@ -79,3 +77,9 @@ func (m *MagickConverter) Scale(ctx context.Context, imageURL string, power floa

return file.GetTempFile(outFile)
}

func createCommand(baseCommands []string, power float32, inFile, outFile string) []string {
size := MaxPower - (power / PowerFactor)
dimensions := fmt.Sprintf("%d%%x%d%%", int(size), int(size))
return append(baseCommands, inFile, "-liquid-rescale", dimensions, outFile)
}
21 changes: 12 additions & 9 deletions internal/adapters/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,50 @@ package file

import (
"context"
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

func TestDownloadFile(t *testing.T) {
res, err := DownloadFile(context.Background(), "https://kore.cc/test.txt")
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, []byte("test\n"), res)
}

func TestSaveTemp(t *testing.T) {
res, err := DownloadFile(context.Background(), "https://kore.cc/test.txt")
assert.NoError(t, err)
require.NoError(t, err)

path, err := SaveTempFile(res, "txt")
assert.NoError(t, err)
require.NoError(t, err)

defer RemoveTempFile(path)

stat, err := os.Stat(path)
assert.NoError(t, err)
require.NoError(t, err)

assert.Equal(t, int64(5), stat.Size())
}

func TestGetTemp(t *testing.T) {
res, err := DownloadFile(context.Background(), "https://kore.cc/test.txt")
assert.NoError(t, err)
require.NoError(t, err)

path, err := SaveTempFile(res, "txt")
assert.NoError(t, err)
require.NoError(t, err)
defer RemoveTempFile(path)

stat, err := os.Stat(path)

assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, int64(5), stat.Size())

file, err := GetTempFile(path)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, []byte("test\n"), file)
}
21 changes: 13 additions & 8 deletions internal/adapters/handler/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ func (h *CommandHandler) Handle(ctx context.Context, b *bot.Bot, update *models.
go getOptionalImage(ctx, b, update, imageURL)
go getOptionalAudio(ctx, b, update, audioURL)

go commandHandler.Respond(ctx, h.timeout, &domain.Message{
ID: update.Message.ID,
ChatID: update.Message.Chat.ID,
Text: update.Message.Text,
ReplyToMessageID: replyToMessageID,
ImageURL: <-imageURL,
AudioURL: <-audioURL,
})
go func() {
err := commandHandler.Respond(ctx, h.timeout, &domain.Message{
ID: update.Message.ID,
ChatID: update.Message.Chat.ID,
Text: update.Message.Text,
ReplyToMessageID: replyToMessageID,
ImageURL: <-imageURL,
AudioURL: <-audioURL,
})
if err != nil {
log.Err(err).Str("command", cmd).Msg("failed to respond to command")
}
}()
}

func getOptionalImage(ctx context.Context, b *bot.Bot, update *models.Update, url chan string) {
Expand Down
20 changes: 11 additions & 9 deletions internal/core/domain/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package domain
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type MockResponder struct {
command string
}

func (m *MockResponder) Respond(ctx context.Context, message *Message) error {
func (m *MockResponder) Respond(_ context.Context, _ time.Duration, _ *Message) error {
return nil
}

Expand All @@ -24,36 +26,36 @@ func TestRegister(t *testing.T) {
mr := &MockResponder{command: "/test"}

cr.Register(mr)
assert.Equal(t, 1, len(cr.commands))
assert.Len(t, cr.commands, 1)
}

func TestGetNotRegistered(t *testing.T) {
cr := &CommandRegistry{}

_, err := cr.Get("test")
assert.Errorf(t, err, "can't fetch commands, registry not initialized")
require.Errorf(t, err, "can't fetch commands, registry not initialized")
}

func TestGetCommandNotFound(t *testing.T) {
cr := &CommandRegistry{}
mr := &MockResponder{command: "/test"}

cr.Register(mr)
assert.Equal(t, 1, len(cr.commands))
assert.Len(t, cr.commands, 1)

_, err := cr.Get("/foo")
assert.Errorf(t, err, "command not found")
require.Errorf(t, err, "command not found")
}

func TestGetCommandFound(t *testing.T) {
cr := &CommandRegistry{}
mr := &MockResponder{command: "/test"}

cr.Register(mr)
assert.Equal(t, 1, len(cr.commands))
assert.Len(t, cr.commands, 1)

cmd, err := cr.Get("/test")
assert.NoError(t, err)
require.NoError(t, err)
assert.NotNil(t, cmd)

assert.Equal(t, "/test", cmd.GetCommand())
Expand All @@ -66,11 +68,11 @@ func TestListServices(t *testing.T) {

cr.Register(mr1)
cr.Register(mr2)
assert.Equal(t, 2, len(cr.commands))
assert.Len(t, cr.commands, 2)

list := cr.ListCommands()

assert.Equal(t, 2, len(list))
assert.Len(t, list, 2)
assert.Equal(t, "/foo", list[0])
assert.Equal(t, "/bar", list[1])
}
Expand Down
Loading

0 comments on commit b49ce78

Please sign in to comment.