Skip to content

Commit ae272b1

Browse files
committed
sftp: Replace mockClosableClient with mockery
1 parent 97c7a37 commit ae272b1

File tree

2 files changed

+8
-45
lines changed

2 files changed

+8
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Use context from `testing.T` introduced in Go 1.24.
1515
- Define more sentinel errors for more ergonomic error checking.
1616
- Use typed expectations consistently for added type safety.
17+
- Replace sftp `mockClosableClient` type with mockery.
1718
### Fixed
1819
- Use walrus assignment where possible.
1920
- Use the `any` keyword where possible.

backend/sftp/concurrency_test.go

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ package sftp
33
import (
44
"fmt"
55
"io"
6-
"os"
76
"reflect"
87
"sync"
98
"testing"
109
"time"
1110

12-
_sftp "github.com/pkg/sftp"
1311
"github.com/stretchr/testify/suite"
1412
"golang.org/x/crypto/ssh"
1513

14+
"github.com/c2fo/vfs/v7/backend/sftp/mocks"
1615
"github.com/c2fo/vfs/v7/utils/authority"
1716
)
1817

@@ -272,9 +271,9 @@ func (s *SFTPConcurrencyTestSuite) TestConnectTimeout() {
272271
func (s *SFTPConcurrencyTestSuite) TestTimerLogicValidation() {
273272
s.Run("Timer closes valid client", func() {
274273
// Create a mock client that tracks Close() calls
275-
mockClient := &mockClosableClient{
276-
closeCalled: make(chan bool, 1),
277-
}
274+
mockClient := mocks.NewClient(s.T())
275+
closeCalled := make(chan bool, 1)
276+
mockClient.EXPECT().Close().Run(func() { closeCalled <- true }).Return(nil).Once()
278277
mockConn := &mockCloser{closeCalled: make(chan bool, 1)}
279278

280279
fs := &FileSystem{
@@ -294,7 +293,7 @@ func (s *SFTPConcurrencyTestSuite) TestTimerLogicValidation() {
294293

295294
// Wait for timer to fire and Close to be called
296295
select {
297-
case <-mockClient.closeCalled:
296+
case <-closeCalled:
298297
// Success - Close was called on valid client
299298
case <-time.After(2 * time.Second):
300299
s.Fail("Timer should have closed the valid client")
@@ -317,7 +316,7 @@ func (s *SFTPConcurrencyTestSuite) TestTimerLogicValidation() {
317316

318317
s.Run("Timer handles typed-nil client safely", func() {
319318
// Create a typed-nil client (this is what happens when client creation fails)
320-
var typedNilClient Client = (*mockClosableClient)(nil)
319+
var typedNilClient Client = (*mocks.Client)(nil)
321320

322321
fs := &FileSystem{
323322
sftpclient: typedNilClient,
@@ -352,10 +351,7 @@ func (s *SFTPConcurrencyTestSuite) TestTimerLogicValidation() {
352351
s.Run("Timer does not call Close on typed-nil client", func() {
353352
// This test validates that Close() is NOT called on typed-nil
354353
// (which would panic since the receiver is nil)
355-
closableNil := &mockClosableClient{
356-
closeCalled: make(chan bool, 1),
357-
}
358-
var typedNilClient Client = (*mockClosableClient)(nil)
354+
var typedNilClient Client = (*mocks.Client)(nil)
359355

360356
fs := &FileSystem{
361357
sftpclient: typedNilClient,
@@ -390,43 +386,9 @@ func (s *SFTPConcurrencyTestSuite) TestTimerLogicValidation() {
390386

391387
// The key validation: no panic should have occurred
392388
s.False(panicOccurred, "Timer should handle typed-nil without panic")
393-
394-
// Verify Close was never called (channel should be empty)
395-
select {
396-
case <-closableNil.closeCalled:
397-
s.Fail("Close should NOT be called on typed-nil client")
398-
default:
399-
// Success - Close was not called
400-
}
401389
})
402390
}
403391

404-
// mockClosableClient implements Client interface for testing timer behavior
405-
type mockClosableClient struct {
406-
closeCalled chan bool
407-
}
408-
409-
func (m *mockClosableClient) Close() error {
410-
if m.closeCalled != nil {
411-
m.closeCalled <- true
412-
}
413-
return nil
414-
}
415-
416-
func (m *mockClosableClient) Chmod(path string, mode os.FileMode) error { return nil }
417-
func (m *mockClosableClient) Chtimes(path string, atime, mtime time.Time) error {
418-
return nil
419-
}
420-
func (m *mockClosableClient) Create(path string) (*_sftp.File, error) { return nil, nil }
421-
func (m *mockClosableClient) MkdirAll(path string) error { return nil }
422-
func (m *mockClosableClient) OpenFile(path string, f int) (*_sftp.File, error) {
423-
return nil, nil
424-
}
425-
func (m *mockClosableClient) ReadDir(p string) ([]os.FileInfo, error) { return nil, nil }
426-
func (m *mockClosableClient) Remove(path string) error { return nil }
427-
func (m *mockClosableClient) Rename(oldname, newname string) error { return nil }
428-
func (m *mockClosableClient) Stat(p string) (os.FileInfo, error) { return nil, nil }
429-
430392
// mockCloser implements io.Closer for testing
431393
type mockCloser struct {
432394
closeCalled chan bool

0 commit comments

Comments
 (0)