Skip to content

Commit 10aeb28

Browse files
authored
Add tests for action handlers (#11)
Add tests for action handlers Update dependencies
1 parent 6b09a76 commit 10aeb28

File tree

18 files changed

+431
-61
lines changed

18 files changed

+431
-61
lines changed

.mockery.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ recursive: false
44
dir: "{{.InterfaceDir}}/mocks"
55
outpkg: "mocks"
66
filename: "mock_{{.InterfaceName}}.go"
7+
mock-build-tags: "test"
78
packages:
89
github.com/outcatcher/hipapu/app:
10+
github.com/outcatcher/hipapu/cmd/handlers:
911

1012
# compatibility warnings
1113
issue-845-fix: true

Taskfile.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tasks:
1414

1515
mock:
1616
desc: Generates mocks
17-
cmd: docker run -v "$PWD":/src -w /src vektra/mockery
17+
cmd: docker run -v "$PWD":/src -w /src vektra/mockery:v2.53
1818

1919
test:
2020
desc: Run tests
@@ -27,4 +27,4 @@ tasks:
2727

2828
build:
2929
desc: Build binary
30-
cmd: go build -o ./build/hihapu ./cmd/main.go
30+
cmd: go build -o ./build/hipapu ./cmd/main.go

app/mocks/mock_cfg.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/mocks/mock_localFiles.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/mocks/mock_remoteClient.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/handlers/actions.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
// Copyright (C) 2025 Anton Kachurin
22
package handlers
33

4+
import (
5+
"context"
6+
"fmt"
7+
8+
"github.com/outcatcher/hipapu/app"
9+
"github.com/outcatcher/hipapu/internal/config"
10+
"github.com/urfave/cli/v3"
11+
)
12+
413
// DefaultCommandName - name of default command.
514
const DefaultCommandName = commandNameList
615

16+
type application interface {
17+
List() []config.Installation
18+
Add(remoteURL, localPath string) error
19+
Synchronize(ctx context.Context) error
20+
}
21+
722
// ActionHandlers handle CLI actions.
823
type ActionHandlers struct {
924
filePath, repoPath, configPath string
25+
26+
app application
27+
}
28+
29+
// Before is a before function for the command handlers.
30+
func (h *ActionHandlers) Before(ctx context.Context, _ *cli.Command) (context.Context, error) {
31+
application, err := app.New(h.configPath)
32+
if err != nil {
33+
return ctx, fmt.Errorf("failed to init app: %w", err)
34+
}
35+
36+
h.app = application
37+
38+
return ctx, nil
1039
}

cmd/handlers/add.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import (
55
"context"
66
"fmt"
77

8-
"github.com/outcatcher/hipapu/app"
98
"github.com/urfave/cli/v3"
109
)
1110

1211
const commandNameAdd = "add"
1312

14-
// AddCommand handle 'add' subcommand.
15-
func (h *ActionHandlers) AddCommand() *cli.Command {
13+
// CommandAdd handle 'add' subcommand.
14+
func (h *ActionHandlers) CommandAdd() *cli.Command {
1615
return &cli.Command{
1716
Name: commandNameAdd,
1817
Usage: "Adds package to the watchlist",
@@ -42,17 +41,12 @@ func (h *ActionHandlers) AddCommand() *cli.Command {
4241
}
4342
}
4443

45-
func (h *ActionHandlers) addHandler(context.Context, *cli.Command) error {
46-
application, err := app.New(h.configPath)
47-
if err != nil {
48-
return fmt.Errorf("failed to start app: %w", err)
49-
}
50-
51-
if err := application.Add(h.repoPath, h.filePath); err != nil {
44+
func (h *ActionHandlers) addHandler(_ context.Context, cmd *cli.Command) error {
45+
if err := h.app.Add(h.repoPath, h.filePath); err != nil {
5246
return fmt.Errorf("error during installation addition: %w", err)
5347
}
5448

55-
println("Added!")
49+
_, _ = fmt.Fprintln(cmd.Writer, "Added!")
5650

5751
return nil
5852
}

cmd/handlers/add_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2025 Anton Kachurin
2+
3+
//go:build test
4+
// +build test
5+
6+
package handlers //nolint:testpackage // don't want to make useless tests overly complex
7+
8+
import (
9+
"io"
10+
"testing"
11+
12+
"github.com/outcatcher/hipapu/cmd/handlers/mocks"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestAdd(t *testing.T) {
17+
t.Parallel()
18+
19+
ctx := t.Context()
20+
21+
const (
22+
expectedFilePath = "test-file-path-add"
23+
expectedRepoPath = "test-repo-path-add"
24+
)
25+
26+
appMock := mocks.NewMockapplication(t)
27+
appMock.On("Add", expectedRepoPath, expectedFilePath).Once().Return(nil)
28+
29+
hdl := &ActionHandlers{
30+
filePath: expectedFilePath,
31+
repoPath: expectedRepoPath,
32+
configPath: "test-config-path-add",
33+
app: appMock,
34+
}
35+
36+
addCmd := hdl.CommandAdd()
37+
addCmd.Writer = io.Discard
38+
39+
require.NoError(t, addCmd.Action(ctx, addCmd))
40+
}

cmd/handlers/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright (C) 2025 Anton Kachurin
2+
13
package handlers
24

35
import (
@@ -6,8 +8,8 @@ import (
68
"github.com/urfave/cli/v3"
79
)
810

9-
// ConfigFlag - handle '--config' flag.
10-
func (h *ActionHandlers) ConfigFlag() *cli.StringFlag {
11+
// FlagConfig - handle '--config' flag.
12+
func (h *ActionHandlers) FlagConfig() *cli.StringFlag {
1113
return &cli.StringFlag{
1214
Name: "config",
1315
Usage: "Configuration file path",
@@ -16,8 +18,6 @@ func (h *ActionHandlers) ConfigFlag() *cli.StringFlag {
1618
Value: defaultConfigPath(),
1719
Destination: &h.configPath,
1820
Aliases: []string{"c"},
19-
TakesFile: true,
20-
OnlyOnce: true,
2121
}
2222
}
2323

cmd/handlers/config_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (C) 2025 Anton Kachurin
2+
3+
//go:build test
4+
// +build test
5+
6+
package handlers //nolint:testpackage // don't want to make useless tests overly complex
7+
8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestConfig(t *testing.T) {
15+
t.Parallel()
16+
17+
const expectedConfigPath = "test-config-path-add"
18+
19+
hdl := &ActionHandlers{}
20+
21+
require.NoError(t, hdl.FlagConfig().Set("", expectedConfigPath))
22+
23+
require.Equal(t, expectedConfigPath, hdl.configPath)
24+
}

0 commit comments

Comments
 (0)