Skip to content

Commit

Permalink
fixes #37 "Java code parse errors should not fail the Maven plugin ex…
Browse files Browse the repository at this point in the history
…ecution"
  • Loading branch information
janScheible committed Oct 19, 2022
1 parent 1af11cb commit c61ac43
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ The code changes in a local Git working tree are inspected and not covered metho
This is useful for local development iterations before pushing the changes to a remote repository and starting a CI build.

```DOS .bat
mvn com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:clean-jacoco-reports^
mvn com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports^
jacoco:prepare-agent^
surefire:test^
jacoco:report^
com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:perform
com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform
```

By also adding `jacoco:prepare-agent-integration`, `failsafe:integration-test` and `jacoco:report-integration` integration tests are also considered (method coverages of all JaCoCo reports are merged).
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1-SNAPSHOT</version>

<packaging>pom</packaging>
<name>com.scheible::test-gap-analysis</name>
Expand Down
6 changes: 3 additions & 3 deletions test-gap-analysis-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1-SNAPSHOT</version>
</parent>

<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-maven-plugin</artifactId>
<version>1.2.0</version>
<version>1.2.1-SNAPSHOT</version>

<packaging>maven-plugin</packaging>
<name>com.scheible::test-gap-analysis maven-plugin</name>
Expand All @@ -21,7 +21,7 @@

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<test-gap-analysis.version>1.2.0</test-gap-analysis.version>
<test-gap-analysis.version>1.2.1-SNAPSHOT</test-gap-analysis.version>
</properties>

<dependencies>
Expand Down
16 changes: 8 additions & 8 deletions test-gap-analysis/nbactions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,22 @@
<actionName>CUSTOM-Test Gap Analysis of Working Tree Changes</actionName>
<displayName>Test Gap Analysis of Working Tree Changes</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:perform</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform</goal>
</goals>
</action>
<action>
<actionName>CUSTOM-Test Gap Analysis with Reference Commit</actionName>
<displayName>Test Gap Analysis with Reference Commit</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:perform</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform</goal>
</goals>
<properties>
<test-gap-analysis.reference-commit-hash>30640f01d7c116ca0d75ce9285357e5a8053009e</test-gap-analysis.reference-commit-hash>
Expand All @@ -245,11 +245,11 @@
<actionName>CUSTOM-Test Gap Analysis with Previous Tag RegEx</actionName>
<displayName>Test Gap Analysis with Previous Tag RegEx</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:perform</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform</goal>
</goals>
<properties>
<test-gap-analysis.previous-tag-regex>[0-9]+\.[0-9]+\.[0-9]+</test-gap-analysis.previous-tag-regex>
Expand All @@ -259,11 +259,11 @@
<actionName>CUSTOM-Debug Coverage Resolution</actionName>
<displayName>Debug Coverage Resolution</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.0:debug-coverage-resolution</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:debug-coverage-resolution</goal>
</goals>
</action>
</actions>
4 changes: 2 additions & 2 deletions test-gap-analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1-SNAPSHOT</version>
</parent>

<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis</artifactId>
<version>1.2.0</version>
<version>1.2.1-SNAPSHOT</version>

<packaging>jar</packaging>
<name>com.scheible::test-gap-analysis library</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public AnalysisResult perform(GitChangeSet changeSet, Set<InstrumentedMethod> in
// all methods of new or changed files in the new state compared to the old state
Set<MethodCompareWrapper> previousStateMethods = changeSet.getChanges().stream()
.filter(change -> !change.isDeletion())
.flatMap(change -> this.javaParser.getMethods(change.getCurrentContent().get()).stream())
.flatMap(change -> this.javaParser
.getMethods(change.getCurrentContent().get(), change.getRelativePath()).stream())
.map(MethodCompareWrapper::new).filter(NON_GETTER_OR_SETTER_METHOD).collect(Collectors.toSet());

// all methods of changed files in the new state that already existed in the old state
Set<MethodCompareWrapper> currentStateMethods = changeSet.getChanges().stream().filter(FileChange::isChange)
.flatMap(change -> this.javaParser.getMethods(change.getPreviousContent().get()).stream())
.flatMap(change -> this.javaParser
.getMethods(change.getPreviousContent().get(), change.getRelativePath()).stream())
.map(MethodCompareWrapper::new).filter(NON_GETTER_OR_SETTER_METHOD).collect(Collectors.toSet());

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private ParseResult parseMethods(File workingDir) throws UncheckedIOException {
.collect(Collectors.toSet());

Set<ParsedMethod> methods = javaFiles.stream()
.flatMap(f -> this.javaParser.getMethods(FilesUtils.readUtf8(f)).stream())
.flatMap(f -> this.javaParser.getMethods(FilesUtils.readUtf8(f), f.getAbsolutePath()).stream())
.collect(Collectors.toSet());

return new ParseResult(methods, javaFiles.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.scheible.testgapanalysis.parser;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.Problem;
import com.github.javaparser.ast.CompilationUnit;

/**
Expand All @@ -14,14 +19,18 @@
*/
public class JavaParser {

public Set<ParsedMethod> getMethods(String code) {
protected final Logger logger = LoggerFactory.getLogger(getClass());

public Set<ParsedMethod> getMethods(String code, String path) {
ParserConfiguration configuration = new ParserConfiguration();
configuration.setLanguageLevel(LanguageLevel.BLEEDING_EDGE);
com.github.javaparser.JavaParser javaParser = new com.github.javaparser.JavaParser(configuration);

ParseResult<CompilationUnit> parserResult = javaParser.parse(code);
if (!parserResult.isSuccessful()) {
throw new ParseProblemException(parserResult.getProblems());
logger.error("Parsing methods of '{}' failed cause: {}", path, parserResult.getProblems().stream()
.map(Problem::getVerboseMessage).collect(Collectors.joining(", ")));
return Collections.emptySet();
}

MethodVisitor methodVisitor = new MethodVisitor(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ public void testLambdaParsing() throws IOException {

@Test
public void testRecordParsing() {
// records require Java 16, test-gap source code is Java 8 --> parse from string
// records requires Java 16, test-gap source code is Java 8 --> parse from string
assertThat(new JavaParser().getMethods("class Foo<J> {\n" + //
" record TestRecord<T>(String value) { \n" + //
" TestRecord(T val) {\n" + //
" this(null);\n" + //
" }\n" + //
" }\n" + //
"}")).first().satisfies(pm -> {
"}", "path")).first().satisfies(pm -> {
assertThat(pm.getScope()).containsOnly("TestRecord");
assertThat(pm.getTypeParameters()).contains(entry("T", "Object"));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.scheible.pocketsaw.api.SubModule;
import com.scheible.testgapanalysis.ExternalFunctionalities.JavaParser;
import com.scheible.testgapanalysis.ExternalFunctionalities.Slf4j;
import com.scheible.testgapanalysis.common.CommonSubModule;

/**
*
* @author sj
*/
@SubModule(uses = {JavaParser.class, CommonSubModule.class})
@SubModule(uses = {JavaParser.class, CommonSubModule.class, Slf4j.class})
public class ParserSubModule {

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static Set<ParsedMethod> parseJavaTestSource(Class<?> testClass, MethodTy

ClassWithSource classWithSource = readJavaTestSource(testClass);
JavaParser javaParser = new JavaParser();
Set<ParsedMethod> parsedMethods = javaParser.getMethods(classWithSource.source).stream()
Set<ParsedMethod> parsedMethods = javaParser
.getMethods(classWithSource.source, classWithSource.testClass.getSimpleName()).stream()
.filter(m -> (!m.getScope().isEmpty() && m.getScope().get(0).equals(testClass.getSimpleName()))
|| classWithSource.testClass.equals(classWithSource.topLevelClass))
.filter(m -> filterTypesSet.contains(m.getMethodType())).collect(Collectors.toSet());
Expand Down

0 comments on commit c61ac43

Please sign in to comment.