-
Notifications
You must be signed in to change notification settings - Fork 135
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
new JUnit5SuiteMisuse error-prone check #843
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
580ee4d
New JUnit5SuiteMisuse errorprone check
iamdanfox e0fffe1
Comments and more tests
iamdanfox d807d5d
Add generated changelog entries
iamdanfox 82779ef
Actually compile
iamdanfox 8499790
Always link to palantir/gradle-baseline README
iamdanfox df29d5e
README
iamdanfox 7e7da7d
Checkstyle
iamdanfox be6dc4f
Less crazy types
iamdanfox 60659c7
Merge branch 'develop' into dfox/detect-suite-misuse
iamdanfox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/JUnit5SuiteMisuse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.matchers.AnnotationMatcherUtils; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matchers; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.AnnotationTree; | ||
import com.sun.source.tree.ClassTree; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.tools.javac.code.Type; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "JUnit5SuiteMisuse", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = BugPattern.LinkType.CUSTOM, | ||
severity = BugPattern.SeverityLevel.ERROR, | ||
summary = "Referencing JUnit5 tests from JUnit4 Suites will silently not work") | ||
public final class JUnit5SuiteMisuse extends BugChecker | ||
implements BugChecker.ClassTreeMatcher, BugChecker.AnnotationTreeMatcher { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
// We remember classes and validate them later because error-prone doesn't let us arbitrarily explore classes we | ||
// discover when reading the @SuiteClasses annotation. | ||
private static final Set<Type.ClassType> knownJUnit5TestClasses = new HashSet<>(); | ||
private static final Set<Type.ClassType> referencedBySuites = new HashSet<>(); | ||
|
||
@Override | ||
public Description matchClass(ClassTree tree, VisitorState state) { | ||
if (!JUnit5RuleUsage.hasJunit5TestCases.matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
Type.ClassType type = ASTHelpers.getType(tree); | ||
knownJUnit5TestClasses.add(type); // accumulate these so we can check them when visiting suiteclasses | ||
|
||
if (referencedBySuites.contains(type)) { | ||
return buildDescription(tree) | ||
.setMessage("Class uses JUnit5 tests but is referenced by a JUnit4 SuiteClasses") | ||
.build(); | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
|
||
@Override | ||
public Description matchAnnotation(AnnotationTree tree, VisitorState state) { | ||
if (!Matchers.isSameType("org.junit.runners.Suite.SuiteClasses").matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
for (JCTree.JCFieldAccess field : getReferencedClasses(tree, state)) { | ||
Type.ClassType classType = (Type.ClassType) field.selected.type; | ||
referencedBySuites.add(classType); | ||
|
||
if (knownJUnit5TestClasses.contains(classType)) { | ||
return buildDescription(tree) | ||
.setMessage("Don't reference JUnit5 test classes from JUnit4 SuiteClasses annotation") | ||
.build(); | ||
} | ||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
|
||
private static List<JCTree.JCFieldAccess> getReferencedClasses(AnnotationTree tree, VisitorState state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ExpressionTree value = AnnotationMatcherUtils.getArgument(tree, "value"); | ||
|
||
if (value == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
if (value instanceof JCTree.JCFieldAccess) { | ||
return Collections.singletonList((JCTree.JCFieldAccess) value); | ||
} | ||
|
||
if (value instanceof JCTree.JCNewArray) { | ||
ImmutableList.Builder<JCTree.JCFieldAccess> list = ImmutableList.builder(); | ||
for (JCTree.JCExpression elem : ((JCTree.JCNewArray) value).elems) { | ||
list.add((JCTree.JCFieldAccess) elem); | ||
} | ||
return list.build(); | ||
} | ||
|
||
throw new UnsupportedOperationException(String.format( | ||
"Unable to get referenced classes for %s of type %s", | ||
state.getSourceForNode(tree), | ||
value.getClass())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,8 @@ | |
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "StringBuilderConstantParameters", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unrelated but nice |
||
linkType = BugPattern.LinkType.CUSTOM, | ||
severity = SeverityLevel.WARNING, | ||
summary = "StringBuilder with a constant number of parameters should be replaced by simple concatenation") | ||
public final class StringBuilderConstantParameters | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...ine-error-prone/src/test/java/com/palantir/baseline/errorprone/JUnit5SuiteMisuseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class JUnit5SuiteMisuseTest { | ||
|
||
private CompilationTestHelper compilationHelper; | ||
|
||
@Before | ||
public void before() { | ||
compilationHelper = CompilationTestHelper.newInstance(JUnit5SuiteMisuse.class, getClass()); | ||
} | ||
|
||
@Test | ||
public void multiple_junit4_references_pass() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" @RunWith(Suite.class)", | ||
" @Suite.SuiteClasses({FooTest.class, BarTest.class})", | ||
" public static class MySuite {}", | ||
"", | ||
" public static class FooTest {", | ||
" @org.junit.Test public void my_test() {}", | ||
" }", | ||
"", | ||
" public static class BarTest {", | ||
" @org.junit.Test public void my_test() {}", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void single_junit4_reference_passes() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" @RunWith(Suite.class)", | ||
" @Suite.SuiteClasses(FooTest.class)", | ||
" public static class MySuite {}", | ||
"", | ||
" public static class FooTest {", | ||
" @org.junit.Test public void my_test() {}", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void single_junit4_reference_passes_different_order() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" public static class FooTest {", | ||
" @org.junit.Test public void my_test() {}", | ||
" }", | ||
"", | ||
" @RunWith(Suite.class)", | ||
" @Suite.SuiteClasses(FooTest.class)", | ||
" public static class MySuite {}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void multiple_junit5_references_fail() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" @RunWith(Suite.class)", | ||
" @Suite.SuiteClasses({FooTest.class, BarTest.class})", | ||
" public static class MySuite {}", | ||
"", | ||
" // BUG: Diagnostic contains: JUnit5SuiteMisuse", | ||
" public static class FooTest {", | ||
" @org.junit.jupiter.api.Test public void my_test() {}", | ||
" }", | ||
"", | ||
" // BUG: Diagnostic contains: JUnit5SuiteMisuse", | ||
" public static class BarTest {", | ||
" @org.junit.jupiter.api.Test public void my_test() {}", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void multiple_junit5_references_fail_different_order() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" public static class FooTest {", | ||
" @org.junit.jupiter.api.Test public void my_test() {}", | ||
" }", | ||
"", | ||
" public static class BarTest {", | ||
" @org.junit.jupiter.api.Test public void my_test() {}", | ||
" }", | ||
"", | ||
" @RunWith(Suite.class)", | ||
" // BUG: Diagnostic contains: JUnit5SuiteMisuse", | ||
" @Suite.SuiteClasses({FooTest.class, BarTest.class})", | ||
" public static class MySuite {}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void single_junit5_reference_fails() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" @RunWith(Suite.class)", | ||
" @Suite.SuiteClasses(FooTest.class)", | ||
" public static class MySuite {}", | ||
"", | ||
" // BUG: Diagnostic contains: JUnit5SuiteMisuse", | ||
" public static class FooTest {", | ||
" @org.junit.jupiter.api.Test public void my_test() {}", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void single_junit5_reference_fails_different_order() { | ||
compilationHelper.addSourceLines( | ||
"Container.java", | ||
"import org.junit.runner.RunWith;", | ||
"import org.junit.runners.Suite;", | ||
"class Container {", | ||
" public static class FooTest {", | ||
" @org.junit.jupiter.api.Test public void my_test() {}", | ||
" }", | ||
"", | ||
" @RunWith(Suite.class)", | ||
" // BUG: Diagnostic contains: JUnit5SuiteMisuse", | ||
" @Suite.SuiteClasses(FooTest.class)", | ||
" public static class MySuite {}", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This seems pretty weird, but I can't think of another alternative.
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.
Agreed - I tried a bunch of things before landing on this. You can traverse up your class hierarchy nice and happily, and you can do whatever you like inside a file, I think they enforce that you can't go spelunking around arbitrary classes to ensure things are performant.