Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(tests): assert.Error -> require.Error (other workflow/ dirs) #13402

Merged
merged 1 commit into from
Aug 10, 2024

Conversation

Joibel
Copy link
Member

@Joibel Joibel commented Jul 26, 2024

This is part of a series of test tidies started by #13365.

The aim is to enable the testifylint golangci-lint checker.

This commit converts assert.Error checks into require.Error for the part of the codebase, as per #13270 (comment)

In some places checks have been coalesced - in particular the pattern

if assert.Error() {
    assert.Contains(..., "message")
}

is now

require.ErrorContains(..., "message")

Getting this wrong and missing the Contains is still valid Go, so that's a mistake I may have made.

Signed-off-by: Alan Clucas alan@clucas.org

Copy link
Member

@terrytangyuan terrytangyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we combine these PRs into one?

Base automatically changed from testifylint-automated-fix to main July 27, 2024 16:05
@agilgur5 agilgur5 changed the title chore(tests): assert.Error->require.Error (workflow/other directory) refactor(tests): assert.Error -> require.Error (other workflow/ dirs) Jul 27, 2024
@Joibel Joibel force-pushed the testifylint-require-workflow-other branch from 173e074 to f5a1a53 Compare July 27, 2024 18:20
@Joibel
Copy link
Member Author

Joibel commented Jul 27, 2024

Can we combine these PRs into one?

I've split these to make them easier to digest - it's otherwise a heavy but dull change to over 100 files. It's @agilgur5 and my preference to work on smaller commits. These are arbitary splits, each file could be independent or it could be a mega commit.

I can pop them into one mega commit if they're not through when I get back from vacation.

@agilgur5
Copy link
Member

it's otherwise a heavy but dull change to over 100 files

I think closer to ~150, it's quite a lot of files. Most of the changes are straight replace, but not quite all of them (in particular the if assert.NoError ones (the if is no longer necessary when using require). So some files need a more careful double-check

This is part of a series of test tidies started by #13365.

The aim is to enable the testifylint golangci-lint checker.

This commit converts assert.Error checks into require.Error for the
part of the codebase, as per #13270 (comment)

In some places checks have been coaleced - in particular the pattern

```go
if assert.Error() {
    assert.Contains(..., "message")
}
```

is now
```go
require.ErrorContains(..., "message")
```

Getting this wrong and missing the Contains is still valid go, so
that's a mistake I may have made.

Signed-off-by: Alan Clucas <alan@clucas.org>
@Joibel Joibel force-pushed the testifylint-require-workflow-other branch from f5a1a53 to 32f195f Compare July 27, 2024 19:46
@agilgur5
Copy link
Member

it's otherwise a heavy but dull change to over 100 files

I think closer to ~150

Correction, after fixing what seems to be a GH rebase bug, this did end up smaller than that, ~99 on the dot I think

Copy link
Member

@agilgur5 agilgur5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there could be some potential further simplifications in a few places

if !assert.NoError(t, err) {
t.Fatal(err)
}
require.NoError(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's some inconsistent new lines in the changes here, so not sure which one you were going for; adding a new line to all or no new line for all

Comment on lines 266 to 267
if !assert.True(t, ok) {
t.Fatal("tmplBase is not a WorkflowTemplate")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this also be simplified?

workflow/artifacts/http/clients_test.go Show resolved Hide resolved
Comment on lines +1380 to 1384
if assert.Len(t, wf.Status.Nodes, 2) {
assert.Equal(t, wfv1.NodeSucceeded, wf.Status.Nodes["1"].Phase)
assert.Equal(t, wfv1.NodeRunning, wf.Status.Nodes["continue-on-failed-workflow-2"].Phase)
assert.Len(t, podsToDelete, 4)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if assert.Len(t, wf.Status.Nodes, 2) {
assert.Equal(t, wfv1.NodeSucceeded, wf.Status.Nodes["1"].Phase)
assert.Equal(t, wfv1.NodeRunning, wf.Status.Nodes["continue-on-failed-workflow-2"].Phase)
assert.Len(t, podsToDelete, 4)
}
require.Len(t, wf.Status.Nodes, 2)
assert.Equal(t, wfv1.NodeSucceeded, wf.Status.Nodes["1"].Phase)
assert.Equal(t, wfv1.NodeRunning, wf.Status.Nodes["continue-on-failed-workflow-2"].Phase)
assert.Len(t, podsToDelete, 4)

this can also be simplified as a require if I'm not mistaken

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see also my comment on the other PR: #13401 (comment)

these are not assert.Error changes though, so may make sense as a separate PR. as it would be another indentation change though, it may make sense to consolidate for diff simplicity

@@ -602,7 +599,7 @@ func TestApplySubmitOpts(t *testing.T) {
},
}
err := ApplySubmitOpts(wf, &wfv1.SubmitOpts{Parameters: []string{"a=81861780812"}})
assert.NoError(t, err)
require.NoError(t, err)
parameters := wf.Spec.Arguments.Parameters
if assert.Len(t, parameters, 1) {
Copy link
Member

@agilgur5 agilgur5 Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@@ -106,16 +107,16 @@ spec:
`

err = yaml.Unmarshal([]byte(cronWfInstanceIdString), &cronWf)
assert.NoError(t, err)
require.NoError(t, err)
wf = ConvertCronWorkflowToWorkflow(&cronWf)
if assert.Contains(t, wf.GetLabels(), LabelKeyControllerInstanceID) {
Copy link
Member

@agilgur5 agilgur5 Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}
require.Error(t, err)
argoError, ok := err.(errors.ArgoError)
if assert.True(t, ok) {
Copy link
Member

@agilgur5 agilgur5 Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

I also wonder if this is further simplifiable with the Error* helpers

}
require.Error(t, err)
argoError, ok := err.(errors.ArgoError)
if assert.True(t, ok) {
Copy link
Member

@agilgur5 agilgur5 Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@@ -115,7 +116,7 @@ func TestLoadToStream(t *testing.T) {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the panic up here also be replaced by a require.NoError? there's a couple panics in the tests that seem like they should perhaps be a require.NoError from a quick glance

@@ -62,11 +63,11 @@ func TestArtifactDriver_WithServiceKey_DownloadDirectory_Subdir(t *testing.T) {

// ensure container exists
containerClient, err := driver.newAzureContainerClient()
assert.NoError(t, err)
require.NoError(t, err)
_, err = containerClient.Create(context.Background(), nil)
var responseError *azcore.ResponseError
if err != nil && !(errors.As(err, &responseError) && responseError.ErrorCode == "ContainerAlreadyExists") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if this might be able to be rewritten as assertions. maybe not though

@Joibel
Copy link
Member Author

Joibel commented Jul 30, 2024

Feel free to apply changes as you see fit @agilgur5

Copy link
Member

@agilgur5 agilgur5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. leaving the non-error if assert.* -> require.* changes to a separate PR per #13402 (comment). same for the panic -> require etc changes

@agilgur5 agilgur5 merged commit 24de3c6 into main Aug 10, 2024
28 checks passed
@agilgur5 agilgur5 deleted the testifylint-require-workflow-other branch August 10, 2024 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants