Skip to content

Commit

Permalink
use else instead of returns
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 committed Aug 14, 2024
1 parent fb327e0 commit 1d672e4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
9 changes: 4 additions & 5 deletions internal/hook/item_hook_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,9 @@ func TestHandleHooks(t *testing.T) {

if test.expectedError != nil {
require.EqualError(t, err, test.expectedError.Error())
return
} else {
require.NoError(t, err)
}

require.NoError(t, err)
})
}
}
Expand Down Expand Up @@ -1383,9 +1382,9 @@ func TestGetRestoreHooksFromSpec(t *testing.T) {
assert.Equal(t, tc.expected, actual)
if tc.expectedError != nil {
require.EqualError(t, err, tc.expectedError.Error())
return
} else {
require.NoError(t, err)
}
require.NoError(t, err)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func Test_newServer(t *testing.T) {
uploaderType: uploader.ResticType,
clientQPS: -1,
}, logger)
assert.Error(t, err)
require.Error(t, err)

// invalid clientBurst
factory.On("SetClientQPS", mock.Anything).Return()
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/backup_repository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ func TestGetBackupRepositoryConfig(t *testing.T) {
result, err := getBackupRepositoryConfig(context.Background(), fakeClient, test.congiName, velerov1api.DefaultNamespace, test.repoName, test.repoType, velerotest.NewLogger())

if test.expectedErr != "" {
assert.EqualError(t, err, test.expectedErr)
require.EqualError(t, err, test.expectedErr)
} else {
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, test.expectedResult, result)
}
})
Expand Down
24 changes: 12 additions & 12 deletions pkg/controller/data_download_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,15 @@ func TestOnDataDownloadFailed(t *testing.T) {
namespace := dd.Namespace
ddName := dd.Name
// Add the DataDownload object to the fake client
assert.NoError(t, r.client.Create(ctx, dd))
require.NoError(t, r.client.Create(ctx, dd))
r.OnDataDownloadFailed(ctx, namespace, ddName, fmt.Errorf("Failed to handle %v", ddName))
updatedDD := &velerov2alpha1api.DataDownload{}
if getErr {
assert.Error(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
require.Error(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
assert.NotEqual(t, velerov2alpha1api.DataDownloadPhaseFailed, updatedDD.Status.Phase)
assert.True(t, updatedDD.Status.StartTimestamp.IsZero())
} else {
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
assert.Equal(t, velerov2alpha1api.DataDownloadPhaseFailed, updatedDD.Status.Phase)
assert.True(t, updatedDD.Status.StartTimestamp.IsZero())
}
Expand All @@ -530,15 +530,15 @@ func TestOnDataDownloadCancelled(t *testing.T) {
namespace := dd.Namespace
ddName := dd.Name
// Add the DataDownload object to the fake client
assert.NoError(t, r.client.Create(ctx, dd))
require.NoError(t, r.client.Create(ctx, dd))
r.OnDataDownloadCancelled(ctx, namespace, ddName)
updatedDD := &velerov2alpha1api.DataDownload{}
if getErr {
assert.Error(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
require.Error(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
assert.NotEqual(t, velerov2alpha1api.DataDownloadPhaseFailed, updatedDD.Status.Phase)
assert.True(t, updatedDD.Status.StartTimestamp.IsZero())
} else {
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
assert.Equal(t, velerov2alpha1api.DataDownloadPhaseCanceled, updatedDD.Status.Phase)
assert.False(t, updatedDD.Status.StartTimestamp.IsZero())
assert.False(t, updatedDD.Status.CompletionTimestamp.IsZero())
Expand Down Expand Up @@ -582,15 +582,15 @@ func TestOnDataDownloadCompleted(t *testing.T) {
namespace := dd.Namespace
ddName := dd.Name
// Add the DataDownload object to the fake client
assert.NoError(t, r.client.Create(ctx, dd))
require.NoError(t, r.client.Create(ctx, dd))
r.OnDataDownloadCompleted(ctx, namespace, ddName, datapath.Result{})
updatedDD := &velerov2alpha1api.DataDownload{}
if test.isGetErr {
assert.Error(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
require.Error(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
assert.Equal(t, velerov2alpha1api.DataDownloadPhase(""), updatedDD.Status.Phase)
assert.True(t, updatedDD.Status.CompletionTimestamp.IsZero())
} else {
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: ddName, Namespace: namespace}, updatedDD))
assert.Equal(t, velerov2alpha1api.DataDownloadPhaseCompleted, updatedDD.Status.Phase)
assert.False(t, updatedDD.Status.CompletionTimestamp.IsZero())
}
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestOnDataDownloadProgress(t *testing.T) {
namespace := dd.Namespace
duName := dd.Name
// Add the DataDownload object to the fake client
assert.NoError(t, r.client.Create(context.Background(), dd))
require.NoError(t, r.client.Create(context.Background(), dd))

// Create a Progress object
progress := &uploader.Progress{
Expand All @@ -653,7 +653,7 @@ func TestOnDataDownloadProgress(t *testing.T) {
if len(test.needErrs) != 0 && !test.needErrs[0] {
// Get the updated DataDownload object from the fake client
updatedDu := &velerov2alpha1api.DataDownload{}
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
// Assert that the DataDownload object has been updated with the progress
assert.Equal(t, test.progress.TotalBytes, updatedDu.Status.Progress.TotalBytes)
assert.Equal(t, test.progress.BytesDone, updatedDu.Status.Progress.BytesDone)
Expand Down Expand Up @@ -1042,7 +1042,7 @@ func TestAttemptDataDownloadResume(t *testing.T) {
r.client.Delete(ctx, test.dd, &kbclient.DeleteOptions{})
}()

assert.NoError(t, r.client.Create(ctx, test.dd))
require.NoError(t, r.client.Create(ctx, test.dd))

dt := &duResumeTestHelper{
resumeErr: test.resumeErr,
Expand Down
16 changes: 8 additions & 8 deletions pkg/controller/data_upload_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,11 @@ func TestOnDataUploadCancelled(t *testing.T) {
namespace := du.Namespace
duName := du.Name
// Add the DataUpload object to the fake client
assert.NoError(t, r.client.Create(ctx, du))
require.NoError(t, r.client.Create(ctx, du))

r.OnDataUploadCancelled(ctx, namespace, duName)
updatedDu := &velerov2alpha1api.DataUpload{}
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
assert.Equal(t, velerov2alpha1api.DataUploadPhaseCanceled, updatedDu.Status.Phase)
assert.False(t, updatedDu.Status.CompletionTimestamp.IsZero())
assert.False(t, updatedDu.Status.StartTimestamp.IsZero())
Expand Down Expand Up @@ -672,7 +672,7 @@ func TestOnDataUploadProgress(t *testing.T) {
namespace := du.Namespace
duName := du.Name
// Add the DataUpload object to the fake client
assert.NoError(t, r.client.Create(context.Background(), du))
require.NoError(t, r.client.Create(context.Background(), du))

// Create a Progress object
progress := &uploader.Progress{
Expand All @@ -685,7 +685,7 @@ func TestOnDataUploadProgress(t *testing.T) {
if len(test.needErrs) != 0 && !test.needErrs[0] {
// Get the updated DataUpload object from the fake client
updatedDu := &velerov2alpha1api.DataUpload{}
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
// Assert that the DataUpload object has been updated with the progress
assert.Equal(t, test.progress.TotalBytes, updatedDu.Status.Progress.TotalBytes)
assert.Equal(t, test.progress.BytesDone, updatedDu.Status.Progress.BytesDone)
Expand All @@ -704,11 +704,11 @@ func TestOnDataUploadFailed(t *testing.T) {
namespace := du.Namespace
duName := du.Name
// Add the DataUpload object to the fake client
assert.NoError(t, r.client.Create(ctx, du))
require.NoError(t, r.client.Create(ctx, du))
r.snapshotExposerList = map[velerov2alpha1api.SnapshotType]exposer.SnapshotExposer{velerov2alpha1api.SnapshotTypeCSI: exposer.NewCSISnapshotExposer(r.kubeClient, r.csiSnapshotClient, velerotest.NewLogger())}
r.OnDataUploadFailed(ctx, namespace, duName, fmt.Errorf("Failed to handle %v", duName))
updatedDu := &velerov2alpha1api.DataUpload{}
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
assert.Equal(t, velerov2alpha1api.DataUploadPhaseFailed, updatedDu.Status.Phase)
assert.False(t, updatedDu.Status.CompletionTimestamp.IsZero())
assert.False(t, updatedDu.Status.StartTimestamp.IsZero())
Expand All @@ -723,11 +723,11 @@ func TestOnDataUploadCompleted(t *testing.T) {
namespace := du.Namespace
duName := du.Name
// Add the DataUpload object to the fake client
assert.NoError(t, r.client.Create(ctx, du))
require.NoError(t, r.client.Create(ctx, du))
r.snapshotExposerList = map[velerov2alpha1api.SnapshotType]exposer.SnapshotExposer{velerov2alpha1api.SnapshotTypeCSI: exposer.NewCSISnapshotExposer(r.kubeClient, r.csiSnapshotClient, velerotest.NewLogger())}
r.OnDataUploadCompleted(ctx, namespace, duName, datapath.Result{})
updatedDu := &velerov2alpha1api.DataUpload{}
assert.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
require.NoError(t, r.client.Get(ctx, types.NamespacedName{Name: duName, Namespace: namespace}, updatedDu))
assert.Equal(t, velerov2alpha1api.DataUploadPhaseCompleted, updatedDu.Status.Phase)
assert.False(t, updatedDu.Status.CompletionTimestamp.IsZero())
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/uploader/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

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

func TestValidateUploaderType(t *testing.T) {
Expand Down Expand Up @@ -36,9 +37,9 @@ func TestValidateUploaderType(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
msg, err := ValidateUploaderType(tt.input)
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
require.EqualError(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}

assert.Equal(t, tt.wantMsg, msg)
Expand Down

0 comments on commit 1d672e4

Please sign in to comment.