-
Notifications
You must be signed in to change notification settings - Fork 201
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
[Approach PR] [Not ready for review] - Adding support for mocks in test cleanup #107
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,7 @@ | |
import java.util.Set; | ||
import javax.annotation.Nullable; | ||
import javax.lang.model.element.ElementKind; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.JSONParser; | ||
import org.json.simple.parser.ParseException; | ||
|
@@ -186,6 +187,8 @@ enum API { | |
*/ | ||
private ImmutableMultimap<String, PiranhaMethodRecord> configMethodProperties; | ||
|
||
private ImmutableMultimap<String, PiranhaMethodRecord> configTestMethodProperties; | ||
|
||
private final HashSet<String> handledAnnotations = new HashSet<>(); | ||
private String linkURL = PIRANHA_DEFAULT_URL; | ||
|
||
|
@@ -221,6 +224,7 @@ void init(ErrorProneFlags flags) throws PiranhaConfigurationException { | |
// No configuration present at all, disable Piranha checker | ||
disabled = true; | ||
configMethodProperties = ImmutableMultimap.of(); | ||
configTestMethodProperties = ImmutableMultimap.of(); | ||
return; | ||
} | ||
|
||
|
@@ -274,6 +278,19 @@ void init(ErrorProneFlags flags) throws PiranhaConfigurationException { | |
builder.put(methodRecord.getMethodName(), methodRecord); | ||
} | ||
configMethodProperties = builder.build(); | ||
|
||
// Add test method configuration | ||
Set<Map<String, Object>> testMethodProperties = new HashSet<>(); | ||
if (propertiesJson.get("testMethodProperties") != null) { | ||
testMethodProperties.addAll((List<Map<String, Object>>) propertiesJson.get("testMethodProperties")); | ||
} | ||
ImmutableMultimap.Builder<String, PiranhaMethodRecord> testPropertiesBuilder = new ImmutableMultimap.Builder<>(); | ||
for (Map<String, Object> testMethodProperty : testMethodProperties) { | ||
PiranhaMethodRecord methodRecord = PiranhaMethodRecord.parseFromJSONPropertyEntryMap(testMethodProperty, isArgumentIndexOptional); | ||
testPropertiesBuilder.put(methodRecord.getMethodName(), methodRecord); | ||
} | ||
configTestMethodProperties = testPropertiesBuilder.build(); | ||
|
||
} catch (IOException fnfe) { | ||
throw new PiranhaConfigurationException( | ||
"Error reading config file " + Paths.get(configFile).toAbsolutePath() + " : " + fnfe); | ||
|
@@ -293,6 +310,7 @@ void init(ErrorProneFlags flags) throws PiranhaConfigurationException { | |
// Already in the right format, re-throw | ||
throw pce; | ||
} catch (Exception e) { | ||
e.getStackTrace(); | ||
throw new PiranhaConfigurationException("Some other exception thrown while parsing config"); | ||
} | ||
} else { | ||
|
@@ -881,6 +899,45 @@ private boolean isCheckedXPFlagName(ExpressionTree tree) { | |
public Description matchMethod(MethodTree tree, VisitorState state) { | ||
if (disabled) return Description.NO_MATCH; | ||
|
||
if (!configTestMethodProperties.isEmpty() && tree != null && tree.getBody() != null && tree.getBody().getStatements() != null) { | ||
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. @mkr-plse - This entire logic is very specific and I'm not happy with this approach. |
||
List<? extends StatementTree> bt = tree.getBody().getStatements(); | ||
for (StatementTree st : bt) { | ||
if (st != null && st.getKind().equals(Kind.EXPRESSION_STATEMENT)) { | ||
ExpressionStatementTree est = (ExpressionStatementTree) st; | ||
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.
So, the code will be:
|
||
if (est != null && est.getExpression() instanceof MethodInvocationTree) { | ||
MethodInvocationTree mit = (MethodInvocationTree) est.getExpression(); | ||
if (mit != null && mit.getKind().equals(Kind.METHOD_INVOCATION)) { | ||
ExpressionTree exp = ((MemberSelectTree) mit.getMethodSelect()).getExpression(); | ||
if (exp != null && exp.getKind().equals(Kind.METHOD_INVOCATION)) { | ||
ExpressionTree exp2 = ((MethodInvocationTree)exp).getMethodSelect(); | ||
if (exp2 != null && exp2.getKind().equals(Kind.MEMBER_SELECT)) { | ||
String methodName = ((MemberSelectTree) exp2).getIdentifier().toString(); | ||
if (configTestMethodProperties.containsKey(methodName)) { | ||
ImmutableCollection<PiranhaMethodRecord> methodRecords = configTestMethodProperties.get(methodName); | ||
for (PiranhaMethodRecord methodRecord : methodRecords) { | ||
Optional<Integer> optionalArgumentIdx = methodRecord.getArgumentIdx(); | ||
if (optionalArgumentIdx.isPresent()) { | ||
Value value = evalExpr(((MethodInvocationTree) exp).getArguments().get(optionalArgumentIdx.get()), state); | ||
if (value == Value.TRUE || value == Value.FALSE) { | ||
Description.Builder builder = buildDescription(est); | ||
SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); | ||
fixBuilder.delete(est); | ||
decrementAllSymbolUsages(est, state, fixBuilder); | ||
builder.addFix(fixBuilder.build()); | ||
endPos = state.getEndPosition(est); | ||
return builder.build(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (String name : handledAnnotations) { | ||
AnnotationTree at = | ||
ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), name); | ||
|
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 will act as the configuration for the test methods that needs to be cleanup. Structure is the same as
methodProperties
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.
Just change the flagType for the method. So,
No separate
testMethodProperties
.