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

Remove testify/assert from Validator Tests #158

Merged
merged 8 commits into from
Apr 14, 2020
Merged

Conversation

alrs
Copy link
Contributor

@alrs alrs commented Apr 8, 2020

Hi, @urso.

I've dipped my toe in the water of removing assert, as mentioned in #156.

This removes testify/assert from only one test file in the ucfg package, and it brings in go-spew to improve the readability of test failures.

If this approach looks good to you, I'm happy to keep going, preferably one entire package per PR.

@@ -310,7 +310,9 @@ func TestValidationPass(t *testing.T) {
for i, test := range tests {
t.Run(fmt.Sprintf("Test config (%v): %#v", i, test), func(t *testing.T) {
err := c.Unpack(test)
assert.NoError(t, err)
if err != nil {
t.Fatalf("test:%v error:%v", spew.Sdump(test), err)
Copy link

Choose a reason for hiding this comment

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

The error would be cause because c and test are "incompatible". Can you try to print c as well? Also curious how the output will look like (e.g. add a failing test).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link

Choose a reason for hiding this comment

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

Cool, I like that. Thanks!

@@ -632,7 +636,11 @@ func TestValidateRequiredFailing(t *testing.T) {

t.Logf("Unpack returned error: %v", err)
err = err.(Error).Reason()
assert.Equal(t, test.err, err)
logTmpl := "expected:%q got:%q"
Copy link

Choose a reason for hiding this comment

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

I don't think we always need to log. Appropos want vs. got, I like to use go-cmp in my own projects. Asserting becomes this then:

if diff := cmp.Diff(want, got); diff != "" {
	t.Errorf("mismatch (-want +got):\n%s", diff)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Non-error test logging only happens when go test is given the -v option, does that change your concern at all?

Copy link

Choose a reason for hiding this comment

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

We run most tests with -v and the output is quite verbose already. Just noticed that the go-ucfg travis file does not use -c, but I will add it to be consistent with other projects (need to update the travis file because I'm adding go modules support anyways).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

assert.True(t, err != nil)
if err == nil {
t.Fatalf("test:%v error:%v", spew.Sdump(test), err)
}
Copy link

Choose a reason for hiding this comment

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

We have this pattern in 3 places now, maybe more? Consider to provide a helper function like this (also guarntees that the message is and keeps unified between tests in the future):

func mustUnpack(t *testing.T, cfg *Config, to interface{}) {
   if err := c.Unpack(to); err != nil {
       ...
   }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm keeping that in mind, for now I've removed some redundancy with the validateErrorTemplate variable.

Copy link

Choose a reason for hiding this comment

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

This makes the C programmer in me sad. Fortunately Printf is more secure in go. :)

For Printf style function I'd prefer to not use const or vars for 'templates'. Having the format string always with the Printf functions helps linters (e.g. go vet) to detect errors early. This is why I'm prefering a function here (can also be a function for logging/failing only), as this will also fix arguments we accept and force developers to follow up if they do any changes to the format string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did this in two functions, mustUnpack() and mustFailUnpack().

@urso urso added in progress Team:Elastic-Agent Label for the Agent team labels Apr 9, 2020
@codecov-io
Copy link

codecov-io commented Apr 10, 2020

Codecov Report

Merging #158 into master will increase coverage by 0.10%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #158      +/-   ##
==========================================
+ Coverage   78.63%   78.73%   +0.10%     
==========================================
  Files          24       24              
  Lines        2822     2822              
==========================================
+ Hits         2219     2222       +3     
+ Misses        440      437       -3     
  Partials      163      163              
Impacted Files Coverage Δ
variables.go 80.28% <0.00%> (+0.35%) ⬆️
merge.go 80.24% <0.00%> (+0.61%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a138d3e...3305326. Read the comment docs.

@alrs alrs changed the title Remove testify/assert WIP Remove testify/assert from Validator Test Apr 13, 2020
@alrs alrs changed the title Remove testify/assert from Validator Test Remove testify/assert from Validator Tests Apr 13, 2020
ucfg: quieter logs in TestValidateNonzeroFailing()
@urso
Copy link

urso commented Apr 14, 2020

Thank you. LGTM. Can we merge it as is, or is it still in progress from your point of view?

@alrs
Copy link
Contributor Author

alrs commented Apr 14, 2020

Thank you. LGTM. Can we merge it as is, or is it still in progress from your point of view?

This can merge, I removed "WIP" from the title.

@urso urso merged commit 59731a8 into elastic:master Apr 14, 2020
@urso
Copy link

urso commented Apr 14, 2020

Merged. Thank you.

@urso urso added review and removed in progress labels Apr 14, 2020
@zube zube bot added in progress and removed review labels Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in progress Team:Elastic-Agent Label for the Agent team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants