Skip to content

Commit 51e5976

Browse files
committed
move mock to its own package
1 parent ab1469f commit 51e5976

File tree

6 files changed

+24
-17
lines changed

6 files changed

+24
-17
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Generated files
2-
agent/agentcontainers/containers_mock.go linguist-generated=true
2+
agent/agentcontainers/acmock/acmock.go linguist-generated=true
33
coderd/apidoc/docs.go linguist-generated=true
44
docs/reference/api/*.md linguist-generated=true
55
docs/reference/cli/*.md linguist-generated=true

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,8 @@ GEN_FILES := \
563563
site/e2e/provisionerGenerated.ts \
564564
examples/examples.gen.json \
565565
$(TAILNETTEST_MOCKS) \
566-
coderd/database/pubsub/psmock/psmock.go
566+
coderd/database/pubsub/psmock/psmock.go \
567+
agent/agentcontainers/acmock/acmock.go
567568

568569

569570
# all gen targets should be added here and to gen/mark-fresh
@@ -629,6 +630,9 @@ coderd/database/dbmock/dbmock.go: coderd/database/db.go coderd/database/querier.
629630
coderd/database/pubsub/psmock/psmock.go: coderd/database/pubsub/pubsub.go
630631
go generate ./coderd/database/pubsub/psmock
631632

633+
agent/agentcontainers/acmock/acmock.go: agent/agentcontainers/containers.go
634+
go generate ./agent/agentcontainers/acmock/
635+
632636
$(TAILNETTEST_MOCKS): tailnet/coordinator.go tailnet/service.go
633637
go generate ./tailnet/tailnettest/
634638

agent/agentcontainers/containers_mock.go renamed to agent/agentcontainers/acmock/acmock.go

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

agent/agentcontainers/acmock/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package acmock contains a mock implementation of agentcontainers.Lister for use in tests.
2+
package acmock
3+
4+
//go:generate mockgen -destination ./acmock.go -package acmock .. Lister

agent/agentcontainers/containers.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package agentcontainers
22

3-
//go:generate mockgen -destination ./containers_mock.go -package agentcontainers . Lister
4-
53
import (
64
"context"
75
"errors"
@@ -36,6 +34,9 @@ type devcontainersHandler struct {
3634
mtime time.Time
3735
}
3836

37+
// Option is a functional option for devcontainersHandler.
38+
type Option func(*devcontainersHandler)
39+
3940
// WithLister sets the agentcontainers.Lister implementation to use.
4041
// The default implementation uses the Docker CLI to list containers.
4142
func WithLister(cl Lister) Option {
@@ -44,9 +45,6 @@ func WithLister(cl Lister) Option {
4445
}
4546
}
4647

47-
// Option is a functional option for devcontainersHandler.
48-
type Option func(*devcontainersHandler)
49-
5048
// New returns a new devcontainersHandler with the given options applied.
5149
func New(options ...Option) http.Handler {
5250
ch := &devcontainersHandler{}

agent/agentcontainers/containers_internal_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515
"go.uber.org/mock/gomock"
1616

17+
"github.com/coder/coder/v2/agent/agentcontainers/acmock"
1718
"github.com/coder/coder/v2/codersdk"
1819
"github.com/coder/coder/v2/testutil"
1920
"github.com/coder/quartz"
@@ -100,15 +101,15 @@ func TestContainersHandler(t *testing.T) {
100101
// relative age of the cached data
101102
cacheAge time.Duration
102103
// function to set up expectations for the mock
103-
setupMock func(*MockLister)
104+
setupMock func(*acmock.MockLister)
104105
// expected result
105106
expected codersdk.WorkspaceAgentListContainersResponse
106107
// expected error
107108
expectedErr string
108109
}{
109110
{
110111
name: "no cache",
111-
setupMock: func(mcl *MockLister) {
112+
setupMock: func(mcl *acmock.MockLister) {
112113
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(fakeCt), nil).AnyTimes()
113114
},
114115
expected: makeResponse(fakeCt),
@@ -118,7 +119,7 @@ func TestContainersHandler(t *testing.T) {
118119
cacheData: makeResponse(),
119120
cacheAge: 2 * time.Second,
120121
cacheDur: time.Second,
121-
setupMock: func(mcl *MockLister) {
122+
setupMock: func(mcl *acmock.MockLister) {
122123
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(fakeCt), nil).AnyTimes()
123124
},
124125
expected: makeResponse(fakeCt),
@@ -132,7 +133,7 @@ func TestContainersHandler(t *testing.T) {
132133
},
133134
{
134135
name: "lister error",
135-
setupMock: func(mcl *MockLister) {
136+
setupMock: func(mcl *acmock.MockLister) {
136137
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(), assert.AnError).AnyTimes()
137138
},
138139
expectedErr: assert.AnError.Error(),
@@ -142,7 +143,7 @@ func TestContainersHandler(t *testing.T) {
142143
cacheAge: 2 * time.Second,
143144
cacheData: makeResponse(fakeCt),
144145
cacheDur: time.Second,
145-
setupMock: func(mcl *MockLister) {
146+
setupMock: func(mcl *acmock.MockLister) {
146147
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(fakeCt2), nil).AnyTimes()
147148
},
148149
expected: makeResponse(fakeCt2),
@@ -155,7 +156,7 @@ func TestContainersHandler(t *testing.T) {
155156
ctx = testutil.Context(t, testutil.WaitShort)
156157
clk = quartz.NewMock(t)
157158
ctrl = gomock.NewController(t)
158-
mockLister = NewMockLister(ctrl)
159+
mockLister = acmock.NewMockLister(ctrl)
159160
now = time.Now().UTC()
160161
ch = devcontainersHandler{
161162
cacheDuration: tc.cacheDur,

0 commit comments

Comments
 (0)