Skip to content

Commit 673e84b

Browse files
committed
Use typed expectations
1 parent f7a03c4 commit 673e84b

File tree

13 files changed

+232
-240
lines changed

13 files changed

+232
-240
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Tests
9+
- Use typed expectations consistently for added type safety.
810

911
## [v7.10.0] - 2025-10-07
1012
### Security

backend/ftp/location_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (lt *locationTestSuite) TestList() {
5151
}
5252
authorityStr := "host.com"
5353
locPath := "/dir1/"
54-
lt.client.On("List", locPath).Return(entries, nil).Once()
54+
lt.client.EXPECT().List(locPath).Return(entries, nil).Once()
5555

5656
loc, err := lt.ftpfs.NewLocation(authorityStr, locPath)
5757
lt.Require().NoError(err)
@@ -63,13 +63,13 @@ func (lt *locationTestSuite) TestList() {
6363
}
6464

6565
// file not found (location doesn't exist)
66-
lt.client.On("List", locPath).Return([]*_ftp.Entry{}, errors.New("some error")).Once()
66+
lt.client.EXPECT().List(locPath).Return([]*_ftp.Entry{}, errors.New("some error")).Once()
6767
fileList, err = loc.List()
6868
lt.Require().Error(err, "should return error")
6969
lt.Empty(fileList, "Should return no files on error")
7070

7171
// file not found (location doesn't exist)
72-
lt.client.On("List", locPath).Return([]*_ftp.Entry{}, errors.New("550")).Once()
72+
lt.client.EXPECT().List(locPath).Return([]*_ftp.Entry{}, errors.New("550")).Once()
7373
fileList, err = loc.List()
7474
lt.Require().NoError(err, "Shouldn't return an error on file not found.")
7575
lt.Empty(fileList, "Should return no files on file not found")
@@ -287,7 +287,7 @@ func (lt *locationTestSuite) TestListByRegex() {
287287
}
288288
authorityStr := "host.com"
289289
locPath := "/dir1/"
290-
lt.client.On("List", locPath).Return(entries, nil).Once()
290+
lt.client.EXPECT().List(locPath).Return(entries, nil).Once()
291291
loc, err := lt.ftpfs.NewLocation(authorityStr, locPath)
292292
lt.Require().NoError(err)
293293

@@ -301,7 +301,7 @@ func (lt *locationTestSuite) TestListByRegex() {
301301

302302
// ListByRegex Returns List error
303303
listErr := errors.New("some list error")
304-
lt.client.On("List", locPath).Return(nil, listErr).Once()
304+
lt.client.EXPECT().List(locPath).Return(nil, listErr).Once()
305305
fileList, err = loc.ListByRegex(fileTypeRegex)
306306
lt.Require().Error(err, "error is expected")
307307
lt.Require().ErrorIs(err, listErr, "error is right kind of error")
@@ -423,7 +423,7 @@ func (lt *locationTestSuite) TestExists() {
423423
Time: time.Now().UTC(),
424424
},
425425
}
426-
lt.client.On("List", locPath).Return(entries, nil).Once()
426+
lt.client.EXPECT().List(locPath).Return(entries, nil).Once()
427427
loc, err := lt.ftpfs.NewLocation(authorityStr, locPath)
428428
lt.Require().NoError(err)
429429
exists, err := loc.Exists()
@@ -440,15 +440,15 @@ func (lt *locationTestSuite) TestExists() {
440440
Time: time.Now().UTC(),
441441
},
442442
}
443-
lt.client.On("List", "/my/").Return(entries, nil).Once()
443+
lt.client.EXPECT().List("/my/").Return(entries, nil).Once()
444444
loc, err = lt.ftpfs.NewLocation(authorityStr, locPath)
445445
lt.Require().NoError(err)
446446
exists, err = loc.Exists()
447447
lt.Require().NoError(err, "No error expected from Exists")
448448
lt.False(exists, "Call to Exists expected to return false.")
449449

450450
// some error calling list
451-
lt.client.On("List", "/my/").Return(entries, errors.New("some error")).Once()
451+
lt.client.EXPECT().List("/my/").Return(entries, errors.New("some error")).Once()
452452
loc, err = lt.ftpfs.NewLocation(authorityStr, locPath)
453453
lt.Require().NoError(err)
454454
exists, err = loc.Exists()
@@ -470,7 +470,7 @@ func (lt *locationTestSuite) TestExists() {
470470
Time: time.Now().UTC(),
471471
},
472472
}
473-
lt.client.On("List", "/my/").Return(entries, nil).Once()
473+
lt.client.EXPECT().List("/my/").Return(entries, nil).Once()
474474
loc, err = lt.ftpfs.NewLocation(authorityStr, locPath)
475475
lt.Require().NoError(err)
476476
exists, err = loc.Exists()
@@ -543,7 +543,7 @@ func (lt *locationTestSuite) TestDeleteFile() {
543543
// error deleting
544544
dataConnGetterFunc = getDataConn
545545
loc.(*Location).fileSystem.dataconn = nil
546-
lt.client.On("Delete", "/old/filename.txt").Return(os.ErrNotExist).Once()
546+
lt.client.EXPECT().Delete("/old/filename.txt").Return(os.ErrNotExist).Once()
547547
err = loc.DeleteFile("filename.txt")
548548
lt.Require().Error(err, "failed delete")
549549
lt.Require().ErrorIs(err, os.ErrNotExist, "error should be right kind of error")

backend/os/file_test.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ func (s *osFileTest) TestCopyToLocation() {
181181
otherFile := mocks.NewFile(s.T())
182182

183183
// Expected behavior
184-
otherFile.On("Write", mock.Anything).Return(len(expectedText), nil)
185-
otherFile.On("Close").Return(nil)
186-
otherFs.On("NewFile", mock.Anything, mock.Anything).Return(otherFile, nil)
184+
otherFile.EXPECT().Write(mock.Anything).Return(len(expectedText), nil)
185+
otherFile.EXPECT().Close().Return(nil)
186+
otherFs.EXPECT().NewFile(mock.Anything, mock.Anything).Return(otherFile, nil)
187187

188188
location := Location{name: "/some/path", fileSystem: otherFs}
189189

@@ -203,12 +203,12 @@ func (s *osFileTest) TestCopyToFile() {
203203
location := Location{name: "/some/path", fileSystem: otherFs}
204204

205205
// Expected behavior
206-
otherFile.On("Write", []uint8(expectedText)).Return(len(expectedText), nil)
207-
otherFile.On("Close").Return(nil)
208-
otherFile.On("Name").Return("other.txt")
209-
otherFile.On("Location").Return(vfs.Location(&location))
206+
otherFile.EXPECT().Write([]uint8(expectedText)).Return(len(expectedText), nil)
207+
otherFile.EXPECT().Close().Return(nil)
208+
otherFile.EXPECT().Name().Return("other.txt")
209+
otherFile.EXPECT().Location().Return(vfs.Location(&location))
210210

211-
otherFs.On("NewFile", "", "/some/path/other.txt").Return(otherFile, nil)
211+
otherFs.EXPECT().NewFile("", "/some/path/other.txt").Return(otherFile, nil)
212212

213213
err := s.testFile.CopyToFile(otherFile)
214214
s.Require().NoError(err)
@@ -222,12 +222,12 @@ func (s *osFileTest) TestEmptyCopyToFile() {
222222
location := Location{name: "/some/path", fileSystem: otherFs}
223223

224224
// Expected behavior
225-
otherFile.On("Write", []uint8(expectedText)).Return(len(expectedText), nil)
226-
otherFile.On("Close").Return(nil)
227-
otherFile.On("Name").Return("other.txt")
228-
otherFile.On("Location").Return(vfs.Location(&location))
225+
otherFile.EXPECT().Write([]uint8(expectedText)).Return(len(expectedText), nil)
226+
otherFile.EXPECT().Close().Return(nil)
227+
otherFile.EXPECT().Name().Return("other.txt")
228+
otherFile.EXPECT().Location().Return(vfs.Location(&location))
229229

230-
otherFs.On("NewFile", "", "/some/path/other.txt").Return(otherFile, nil)
230+
otherFs.EXPECT().NewFile("", "/some/path/other.txt").Return(otherFile, nil)
231231

232232
emptyFile, err := s.tmploc.NewFile("test_files/empty.txt")
233233
s.Require().NoError(err, "No file was opened")
@@ -242,9 +242,9 @@ func (s *osFileTest) TestCopyToLocationIgnoreExtraSeparator() {
242242
otherFile := mocks.NewFile(s.T())
243243

244244
// Expected behavior
245-
otherFile.On("Write", mock.Anything).Return(len(expectedText), nil)
246-
otherFile.On("Close").Return(nil)
247-
otherFs.On("NewFile", "", "/some/path/test.txt").Return(otherFile, nil)
245+
otherFile.EXPECT().Write(mock.Anything).Return(len(expectedText), nil)
246+
otherFile.EXPECT().Close().Return(nil)
247+
otherFs.EXPECT().NewFile("", "/some/path/test.txt").Return(otherFile, nil)
248248

249249
// Add trailing slash
250250
location := Location{name: "/some/path/", fileSystem: otherFs}
@@ -295,19 +295,19 @@ func (s *osFileTest) TestMoveToLocation() {
295295
s.Require().NoError(err)
296296

297297
// Expected behavior
298-
mockfs.On("Scheme").Return("mock")
298+
mockfs.EXPECT().Scheme().Return("mock")
299299
fsMockFile := mocks.NewFile(s.T())
300-
fsMockFile.On("Write", mock.Anything).Return(10, nil)
301-
fsMockFile.On("Close").Return(nil)
302-
mockfs.On("NewFile", mock.Anything, mock.Anything).Return(fsMockFile, nil)
303-
mockLocation.On("FileSystem").Return(mockfs)
304-
mockLocation.On("Authority").Return(auth)
305-
mockLocation.On("Path").Return("/some/path/to/")
300+
fsMockFile.EXPECT().Write(mock.Anything).Return(10, nil)
301+
fsMockFile.EXPECT().Close().Return(nil)
302+
mockfs.EXPECT().NewFile(mock.Anything, mock.Anything).Return(fsMockFile, nil)
303+
mockLocation.EXPECT().FileSystem().Return(mockfs)
304+
mockLocation.EXPECT().Authority().Return(auth)
305+
mockLocation.EXPECT().Path().Return("/some/path/to/")
306306
mockFile := mocks.NewFile(s.T())
307-
mockFile.On("Location").Return(mockLocation, nil)
308-
mockFile.On("Name").Return("/some/path/to/move.txt")
309-
mockFile.On("Location").Return(mockLocation, nil)
310-
mockLocation.On("NewFile", mock.Anything).Return(mockFile, nil)
307+
mockFile.EXPECT().Location().Return(mockLocation)
308+
mockFile.EXPECT().Name().Return("/some/path/to/move.txt")
309+
mockFile.EXPECT().Location().Return(mockLocation)
310+
mockLocation.EXPECT().NewFile(mock.Anything).Return(mockFile, nil)
311311

312312
_, err = movedFile.MoveToLocation(mockLocation)
313313
s.Require().NoError(err)
@@ -425,17 +425,17 @@ func (s *osFileTest) TestMoveToFile() {
425425
s.Require().NoError(err)
426426

427427
// Expected behavior
428-
mockfs.On("Scheme").Return("mock")
428+
mockfs.EXPECT().Scheme().Return("mock")
429429
fsMockFile := mocks.NewFile(s.T())
430-
fsMockFile.On("Write", mock.Anything).Return(13, nil)
431-
fsMockFile.On("Close").Return(nil)
432-
mockfs.On("NewFile", mock.Anything, mock.Anything).Return(fsMockFile, nil)
433-
mockLocation.On("FileSystem").Return(mockfs)
434-
mockLocation.On("Authority").Return(auth)
435-
mockLocation.On("Path").Return("/some/path/to/")
436-
mockFile.On("Location").Return(mockLocation, nil)
437-
mockFile.On("Name").Return("/some/path/to/file.txt")
438-
mockFile.On("Location").Return(mockLocation, nil)
430+
fsMockFile.EXPECT().Write(mock.Anything).Return(13, nil)
431+
fsMockFile.EXPECT().Close().Return(nil)
432+
mockfs.EXPECT().NewFile(mock.Anything, mock.Anything).Return(fsMockFile, nil)
433+
mockLocation.EXPECT().FileSystem().Return(mockfs)
434+
mockLocation.EXPECT().Authority().Return(auth)
435+
mockLocation.EXPECT().Path().Return("/some/path/to/")
436+
mockFile.EXPECT().Location().Return(mockLocation)
437+
mockFile.EXPECT().Name().Return("/some/path/to/file.txt")
438+
mockFile.EXPECT().Location().Return(mockLocation)
439439

440440
s.Require().NoError(file2.MoveToFile(mockFile))
441441
}

backend/s3/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ would have to be cast as s3.FileSystem to use the following:
5252
// to pass specific client, for instance a mock client
5353
s3MockClient := mocks.NewClient(t)
5454
s3MockClient.EXPECT().
55-
GetObject(matchContext, mock.AnythingOfType("*s3.GetObjectInput")).
55+
GetObject(matchContext, mock.IsType((*s3.GetObjectInput)(nil))).
5656
Return(&s3.GetObjectOutput{
5757
Body: nopCloser{bytes.NewBufferString("Hello world!")},
5858
}, nil)

0 commit comments

Comments
 (0)