gomega annotation formatting #68
Replies: 14 comments
-
I've seen people do this wrong in Kubernetes (kubernetes/kubernetes#115234). |
Beta Was this translation helpful? Give feedback.
-
Thanks for raising it @pohly. I wonder what is the benefit for enforcing this new rule. The assertion output will be the same and I'm not sure the readability is much better than using the WDYT? |
Beta Was this translation helpful? Give feedback.
-
My thinking was that But I am not sure anymore whether Gomega will call
Even if Gomega passes through a single string, I prefer to have it done consistently, and the documentation for Gomega describes it without |
Beta Was this translation helpful? Give feedback.
-
Found it: https://github.com/onsi/gomega/blob/master/internal/assertion.go#L77-L87 func (assertion *Assertion) buildDescription(optionalDescription ...interface{}) string {
switch len(optionalDescription) {
case 0:
return ""
case 1:
if describe, ok := optionalDescription[0].(func() string); ok {
return describe() + "\n"
}
}
return fmt.Sprintf(optionalDescription[0].(string), optionalDescription[1:]...) + "\n"
} |
Beta Was this translation helpful? Give feedback.
-
As using Does the linter support severity levels? I've not investigated how those work, but perhaps they can be used to distinguish between issues that must be fixed and those that only get called out without causing linting to fail? |
Beta Was this translation helpful? Give feedback.
-
The golangci-lint |
Beta Was this translation helpful? Give feedback.
-
There's actually one advantage of using But I see that as a workaround for a deficiency in linting: ideally, the same check should also be applied when using Gomega's formatting ( |
Beta Was this translation helpful? Give feedback.
-
Sound interesting. I don't want to copy the govet (?) logic to ginkgolinter. In theory, we can export "fact" about this function call and let govet handle it. But I almost 100% sure that ginkgolinter will run later. Need to check this approach. |
Beta Was this translation helpful? Give feedback.
-
And another one (from Kubernetes):
Including s2 in the format arguments is redundant. |
Beta Was this translation helpful? Give feedback.
-
I was thinking the same, but it could be problematic because the format string itself is hidden behind the |
Beta Was this translation helpful? Give feedback.
-
Oh. got it - this is why the printf linter can't find it. It looks for functions with 2nd last parameter of type string, and here it's interface{}. I'm not sure I want to focus on parsing and understanding the assertion description in this linter at this point. I implemented the original idea at #50, but not sure I want to merge it. I want to hear more voices. How often do you find this pattern? |
Beta Was this translation helpful? Give feedback.
-
Could the implementation of the printf linter be reused? Just a thought. I understand that this could get complicated.
That makes sense. After verifying that passing a single string is okay, I am not convinced myself either. It's subjective. More important is the printf checking, because that could find real problems that are unambiguously wrong.
Some of those are false positives, but eyeballing the results confirms that many are legitimate. |
Beta Was this translation helpful? Give feedback.
-
Looking at the I couldn't find a way to reuse this linter in ginkgolinter. If you have an idea how to do that - I'll be glad to hear. |
Beta Was this translation helpful? Give feedback.
-
No good idea. We could ask @onsi to add this to gomega:
My expectation (to be verified!) is that using this will trigger the format checking, while being more efficient than calling Then the linter could enforce that the annotation is either:
For "string + args" it could point towards Still feels like a linter workaround, though 😢 |
Beta Was this translation helpful? Give feedback.
-
gomega annotations support printf-formatting and thus should not be passed the result of
fmt.Sprintf
The linter could complain about:
That should be
Bonus points if it supports code suggestions and thus automatic fixing of existing code 😄
Beta Was this translation helpful? Give feedback.
All reactions