-
Notifications
You must be signed in to change notification settings - Fork 2
Detect unused variables in tests #97
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
Conversation
build.savant
Outdated
| testngVersion = "7.8.0" | ||
|
|
||
| project(group: "org.primeframework", name: "prime-mvc", version: "5.5.1", licenses: ["ApacheV2_0"]) { | ||
| project(group: "org.primeframework", name: "prime-mvc", version: "5.6.1", licenses: ["ApacheV2_0"]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we bump the minor version, assuming we want to reset the patch to 0 to make this 5.6.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes - 709459f
| * @throws IOException If the template could not be loaded, parsed or executed. | ||
| */ | ||
| public static String processTemplateWithMap(Path path, Map<String, Object> values, boolean createMissingTemplates) | ||
| public static String processTemplateWithMap(Path path, DetectionMap values, boolean createMissingTemplates) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we are not using createMissingTemplates and just always creating the new file, so we could delete this arg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep - b791241
| Set<Object> unusedVariables = values.getUnusedVariables("_", | ||
| "actual"); | ||
| if (!unusedVariables.isEmpty()) { | ||
| throw new IllegalArgumentException("Variables %s are not used in the [%s] template. If it's acceptable for the variable to not be used, wrap it in an Optional".formatted(unusedVariables.stream() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion:
Unused values [%s] found in the [%s] template. If it's acceptable for the variable to be unused, wrap it in an Optional."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed as f5963f6
|
|
||
| // if an Optional is used in a template variable, we don't want it to be considered an unused value | ||
| // and we want to evaluate it so FreeMarker does not have to | ||
| if (value instanceof Optional<?> opt && opt.isPresent()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the use case for passing in a parameter to BodyTools that we don't expect in the template? Should we just fail to force the test author to use the correct template?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple cases, like data driven tests. Rather than force the tests to really be re-written. my first offer is, if the test author wraps the value in Optional, then this doesn't complain if that value isn't used since it might be used on the next permutation. I think that's a decent compromise. It still catches cruft. but if you deliberately say "no i want this" by using Optional, it leaves you alone.
# Conflicts: # build.savant
When a JSON FTL is specified, detect which variables are sent to render the template vs. which are used.
This eliminates a rather large amount of cruft in applications using this for tests.