From 2b97896e506a1aca07a62335bd93a4c8e61000e0 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Wed, 24 Feb 2021 12:20:30 +0100 Subject: [PATCH 1/6] Use new registry to obtain parsers. --- pom.xml | 8 +++- .../hm/hafner/grading/AutoGradingAction.java | 47 +++++++++---------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pom.xml b/pom.xml index d472016e..c01baca6 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ edu.hm.hafner codingstyle-pom - 1.6.0 + 2.0.2 @@ -21,6 +21,10 @@ repo.jenkins-ci.org https://repo.jenkins-ci.org/public/ + + incrementals.jenkins-ci.org + https://repo.jenkins-ci.org/incrementals/ + @@ -39,7 +43,7 @@ edu.hm.hafner analysis-model - 9.8.0 + 10.0.0-rc3954.8c8ecf529a7e diff --git a/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java b/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java index 546e19b1..2ebf583b 100644 --- a/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java +++ b/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java @@ -5,7 +5,6 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.StringUtils; @@ -15,10 +14,8 @@ import edu.hm.hafner.analysis.Report; import edu.hm.hafner.analysis.Report.IssueFilterBuilder; import edu.hm.hafner.analysis.Severity; -import edu.hm.hafner.analysis.parser.FindBugsParser; -import edu.hm.hafner.analysis.parser.FindBugsParser.PriorityProperty; -import edu.hm.hafner.analysis.parser.checkstyle.CheckStyleParser; -import edu.hm.hafner.analysis.parser.pmd.PmdParser; +import edu.hm.hafner.analysis.registry.ParserDescriptor; +import edu.hm.hafner.analysis.registry.ParserRegistry; import edu.hm.hafner.grading.github.GitHubPullRequestWriter; import de.tobiasmichael.me.Util.JacocoParser; @@ -33,6 +30,9 @@ @SuppressWarnings("checkstyle:ClassDataAbstractionCoupling") public class AutoGradingAction { private static final String JACOCO_RESULTS = "target/site/jacoco/jacoco.xml"; + private static final String CHECKSTYLE = "checkstyle"; + private static final String PMD = "pmd"; + private static final String SPOTBUGS = "spotbugs"; /** * Public entry point, calls the action. @@ -64,22 +64,18 @@ void run() { List pitReports = new PitReportFinder().find(); score.addPitScores(new PitReportSupplier(pitReports)); - List analysisReports = new ArrayList<>(); + ParserRegistry registry = new ParserRegistry(); - Report checkStyleReport = parse(configuration, new CheckStyleParser(), "target/checkstyle-result.xml"); - analysisReports.add(createAnalysisScore(analysisConfiguration, "CheckStyle", "checkstyle", - checkStyleReport)); - - Report pmdReport = parse(configuration, new PmdParser(), "target/pmd.xml"); - analysisReports.add(createAnalysisScore(analysisConfiguration, "PMD", "pmd", - pmdReport)); - - Report spotBugsReport = parse(configuration, new FindBugsParser(PriorityProperty.RANK), - "target/spotbugsXml.xml"); - analysisReports.add(createAnalysisScore(analysisConfiguration, "SpotBugs", "spotbugs", - spotBugsReport)); - - score.addAnalysisScores(new AnalysisReportSupplier(analysisReports)); + String[] tools = {CHECKSTYLE, PMD, SPOTBUGS}; + List analysisReports = new ArrayList<>(); + List analysisScores = new ArrayList<>(); + for (String tool : tools) { + ParserDescriptor parser = registry.get(tool); + Report report = parse(configuration, parser); + analysisReports.add(report); + analysisScores.add(createAnalysisScore(analysisConfiguration, parser.getName(), parser.getId(), report)); + } + score.addAnalysisScores(new AnalysisReportSupplier(analysisScores)); if (Files.isReadable(Paths.get(JACOCO_RESULTS))) { JacocoReport coverageReport = new JacocoParser().parse(read(JACOCO_RESULTS)); @@ -90,23 +86,26 @@ void run() { } GradingReport results = new GradingReport(); - GitHubPullRequestWriter pullRequestWriter = new GitHubPullRequestWriter(); String files = createAffectedFiles(configuration); pullRequestWriter.addComment(getChecksName(), results.getHeader(), results.getSummary(score) + files, - results.getDetails(score, testReports), - Arrays.asList(pmdReport, checkStyleReport, spotBugsReport)); + results.getDetails(score, testReports), analysisReports); } private String createAffectedFiles(final GradingConfiguration configuration) { String analysisPattern = configuration.getAnalysisPattern(); - if (StringUtils.isNotBlank(analysisPattern) && !StringUtils.equals(analysisPattern, GradingConfiguration.ALL_FILES)) { + if (StringUtils.isNotBlank(analysisPattern) && !StringUtils.equals(analysisPattern, + GradingConfiguration.ALL_FILES)) { return "\n" + new ReportFinder().renderLinks("./", "regex:" + analysisPattern); } return StringUtils.EMPTY; + } + private Report parse(final GradingConfiguration configuration, final ParserDescriptor descriptor) { + return filterAnalysisReport(descriptor.createParser().parse(read(descriptor.getPattern())), + configuration.getAnalysisPattern()); } private Report parse(final GradingConfiguration configuration, From 891e0eaf511871c84378a7aa3f2c7adb103e69e9 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Mon, 1 Mar 2021 20:43:30 +0100 Subject: [PATCH 2/6] Fix some warnings. --- pom.xml | 4 ++-- .../java/edu/hm/hafner/grading/AnalysisReportSupplier.java | 2 -- .../java/edu/hm/hafner/grading/GradingConfigurationTest.java | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index c01baca6..64a6025d 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ edu.hm.hafner autograding-model - 1.2.0 + 1.2.1-SNAPSHOT edu.hm.hafner @@ -49,7 +49,7 @@ org.kohsuke github-api - 1.122 + 1.123 diff --git a/src/main/java/edu/hm/hafner/grading/AnalysisReportSupplier.java b/src/main/java/edu/hm/hafner/grading/AnalysisReportSupplier.java index fb01361c..afc92093 100644 --- a/src/main/java/edu/hm/hafner/grading/AnalysisReportSupplier.java +++ b/src/main/java/edu/hm/hafner/grading/AnalysisReportSupplier.java @@ -1,10 +1,8 @@ package edu.hm.hafner.grading; import java.util.List; -import java.util.stream.Collectors; import edu.hm.hafner.analysis.Report; -import edu.hm.hafner.analysis.parser.violations.PitAdapter; /** * Provides static analysis scores by converting corresponding {@link Report} instances. diff --git a/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java b/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java index 09349915..9dba178d 100644 --- a/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java +++ b/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java @@ -39,8 +39,7 @@ void shouldReturnProvidedTestPattern() { @Test void shouldReturnProvidedAnalysisPattern() { - assertThat(new GradingConfiguration( - "{\n" + assertThat(new GradingConfiguration("{\n" + " \"analysis\": {\n" + " \"pattern\": \"File.*\",\n" + " \"maxScore\": 100,\n" From 963c303ea3e6fcdcc835d4bb894e79d3f4c6413c Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Mon, 1 Mar 2021 23:32:30 +0100 Subject: [PATCH 3/6] Use new parser registry to obtain analysis parsers. --- pom.xml | 6 +-- .../hm/hafner/grading/AutoGradingAction.java | 54 ++++++++++++++----- .../hafner/grading/GradingConfiguration.java | 22 ++++---- .../hm/hafner/grading/PitReportFinder.java | 3 +- .../hm/hafner/grading/TestReportFinder.java | 2 +- .../grading/GradingConfigurationTest.java | 20 +++---- 6 files changed, 70 insertions(+), 37 deletions(-) diff --git a/pom.xml b/pom.xml index a7235123..ef720352 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ edu.hm.hafner codingstyle-pom - 2.0.2 + 2.0.3 @@ -31,7 +31,7 @@ edu.hm.hafner autograding-model - 1.2.1-SNAPSHOT + 1.3.0 edu.hm.hafner @@ -43,7 +43,7 @@ edu.hm.hafner analysis-model - 10.0.0-rc3954.8c8ecf529a7e + 10.0.0-rc3980.fd5b09344558 diff --git a/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java b/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java index 2ebf583b..61d7e84b 100644 --- a/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java +++ b/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java @@ -5,6 +5,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.apache.commons.lang3.StringUtils; @@ -50,32 +51,61 @@ void run() { JacksonFacade jackson = new JacksonFacade(); - System.out.println("Test Configuration: " + jackson.toJson(score.getTestConfiguration())); - System.out.println("Code Coverage Configuration: " + jackson.toJson(score.getCoverageConfiguration())); - System.out.println("PIT Mutation Coverage Configuration: " + jackson.toJson(score.getPitConfiguration())); - AnalysisConfiguration analysisConfiguration = score.getAnalysisConfiguration(); - System.out.println("Static Analysis Configuration: " + jackson.toJson(analysisConfiguration)); + System.out.println("------------------------------------------------------------------"); + System.out.println("------------------------ Configuration ---------------------------"); + System.out.println("------------------------------------------------------------------"); + System.out.println("-> Test Configuration: " + jackson.toJson(score.getTestConfiguration())); + System.out.println("-> Code Coverage Configuration: " + jackson.toJson(score.getCoverageConfiguration())); + System.out.println("-> PIT Mutation Coverage Configuration: " + jackson.toJson(score.getPitConfiguration())); + System.out.println("-> Static Analysis Configuration: " + jackson.toJson(score.getAnalysisConfiguration())); GradingConfiguration configuration = new GradingConfiguration(jsonConfiguration); - List testReports = new TestReportFinder().find(configuration.getTestPattern()); + System.out.println("=================================================================="); + List testReports = new TestReportFinder().find(configuration.getTestPattern()); score.addTestScores(new TestReportSupplier(testReports)); - + System.out.println("=================================================================="); List pitReports = new PitReportFinder().find(); score.addPitScores(new PitReportSupplier(pitReports)); - + System.out.println("=================================================================="); + if (Files.isReadable(Paths.get(JACOCO_RESULTS))) { + JacocoReport coverageReport = new JacocoParser().parse(new FileReaderFactory(Paths.get(JACOCO_RESULTS))); + score.addCoverageScores(new CoverageReportSupplier(coverageReport)); + } + else { + System.out.println("No JaCoCo coverage result files found!"); + } + System.out.println("=================================================================="); + ReportFinder reportFinder = new ReportFinder(); ParserRegistry registry = new ParserRegistry(); String[] tools = {CHECKSTYLE, PMD, SPOTBUGS}; List analysisReports = new ArrayList<>(); List analysisScores = new ArrayList<>(); + for (String tool : tools) { ParserDescriptor parser = registry.get(tool); - Report report = parse(configuration, parser); - analysisReports.add(report); - analysisScores.add(createAnalysisScore(analysisConfiguration, parser.getName(), parser.getId(), report)); + List files = reportFinder.find("target", "glob:" + parser.getPattern()); + System.out.format("Searching for '%s' results matching file name pattern %s%n", parser.getName(), parser.getPattern()); + + if (files.size() == 0) { + System.out.println("No matching report result files found!"); + } + else { + Collections.sort(files); + + for (Path file : files) { + Report allIssues = parser.createParser().parse(new FileReaderFactory(file)); + Report filteredIssues = filterAnalysisReport(allIssues, configuration.getAnalysisPattern()); + System.out.format("- %s : %d warnings (from total %d)%n", file, filteredIssues.size(), allIssues.size()); + analysisReports.add(filteredIssues); + analysisScores.add(createAnalysisScore(score.getAnalysisConfiguration(), parser.getName(), + parser.getId(), filteredIssues)); + } + } } score.addAnalysisScores(new AnalysisReportSupplier(analysisScores)); + System.out.println("=================================================================="); if (Files.isReadable(Paths.get(JACOCO_RESULTS))) { JacocoReport coverageReport = new JacocoParser().parse(read(JACOCO_RESULTS)); @@ -97,7 +127,7 @@ void run() { private String createAffectedFiles(final GradingConfiguration configuration) { String analysisPattern = configuration.getAnalysisPattern(); if (StringUtils.isNotBlank(analysisPattern) && !StringUtils.equals(analysisPattern, - GradingConfiguration.ALL_FILES)) { + GradingConfiguration.INCLUDE_ALL_FILES)) { return "\n" + new ReportFinder().renderLinks("./", "regex:" + analysisPattern); } return StringUtils.EMPTY; diff --git a/src/main/java/edu/hm/hafner/grading/GradingConfiguration.java b/src/main/java/edu/hm/hafner/grading/GradingConfiguration.java index a6fbb191..8fe138a1 100644 --- a/src/main/java/edu/hm/hafner/grading/GradingConfiguration.java +++ b/src/main/java/edu/hm/hafner/grading/GradingConfiguration.java @@ -12,8 +12,8 @@ * @author Ullrich Hafner */ public class GradingConfiguration { - static final String SUREFIRE_REPORT_PATTERN = "glob:./target/surefire-reports/*.xml"; - static final String ALL_FILES = ".*"; + static final String SUREFIRE_DEFAULT_PATTERN = "./target/surefire-reports/*.xml"; + static final String INCLUDE_ALL_FILES = ".*"; private final String testPattern; private final String analysisPattern; @@ -22,20 +22,20 @@ public class GradingConfiguration { ObjectMapper mapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - testPattern = asString(configuration, mapper, "tests", SUREFIRE_REPORT_PATTERN); - System.out.println("Using test file pattern: " + testPattern); + testPattern = asString(configuration, mapper, "tests", "pattern", SUREFIRE_DEFAULT_PATTERN); + System.out.println("-> Using test files pattern: " + testPattern); - analysisPattern = asString(configuration, mapper, "analysis", ALL_FILES); - System.out.println("Using analysis file pattern: " + analysisPattern); + analysisPattern = asString(configuration, mapper, "analysis", "fileFilter", INCLUDE_ALL_FILES); + System.out.println("-> Using file name filter (include) for static analysis: " + analysisPattern); } private String asString(final String configuration, final ObjectMapper mapper, final String type, - final String defaultValue) { + final String propertyName, final String defaultValue) { try { ObjectNode node = mapper.readValue(configuration, ObjectNode.class); - JsonNode tests = node.get(type); - if (tests != null) { - JsonNode pattern = tests.get("pattern"); + JsonNode typeNode = node.get(type); + if (typeNode != null) { + JsonNode pattern = typeNode.get(propertyName); if (pattern != null) { return pattern.asText(defaultValue); } @@ -48,7 +48,7 @@ private String asString(final String configuration, final ObjectMapper mapper, f } public String getTestPattern() { - return testPattern; + return "glob:" + testPattern; } public String getAnalysisPattern() { diff --git a/src/main/java/edu/hm/hafner/grading/PitReportFinder.java b/src/main/java/edu/hm/hafner/grading/PitReportFinder.java index dfb24a5a..13ac6323 100644 --- a/src/main/java/edu/hm/hafner/grading/PitReportFinder.java +++ b/src/main/java/edu/hm/hafner/grading/PitReportFinder.java @@ -29,7 +29,8 @@ public List find() { } System.out.println("Reading PIT results: "); - System.out.println(reportFiles); + Collections.sort(reportFiles); + reportFiles.forEach(file -> System.out.format("- %s%n", file)); PitAdapter parser = new PitAdapter(); return reportFiles.stream() diff --git a/src/main/java/edu/hm/hafner/grading/TestReportFinder.java b/src/main/java/edu/hm/hafner/grading/TestReportFinder.java index 749d7c1e..27f32fab 100644 --- a/src/main/java/edu/hm/hafner/grading/TestReportFinder.java +++ b/src/main/java/edu/hm/hafner/grading/TestReportFinder.java @@ -35,7 +35,7 @@ public List find(final String pattern) { Collections.sort(reportFiles); System.out.println("Reading test results: "); - System.out.println(reportFiles); + reportFiles.forEach(file -> System.out.format("- %s%n", file)); JUnitAdapter parser = new JUnitAdapter(); return reportFiles.stream() diff --git a/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java b/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java index 9dba178d..6c822e6a 100644 --- a/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java +++ b/src/test/java/edu/hm/hafner/grading/GradingConfigurationTest.java @@ -10,38 +10,40 @@ * @author Ullrich Hafner */ class GradingConfigurationTest { + private static final String DEFAULT_PATTERN = "glob:" + GradingConfiguration.SUREFIRE_DEFAULT_PATTERN; + @Test void shouldReturnDefaultPattern() { assertThat(new GradingConfiguration( "{ \"tests\": {\"maxScore\":5,\"failureImpact\":1,\"passedImpact\":2,\"skippedImpact\":3}}") - .getTestPattern()).isEqualTo(GradingConfiguration.SUREFIRE_REPORT_PATTERN); + .getTestPattern()).isEqualTo(DEFAULT_PATTERN); assertThat(new GradingConfiguration("{ \"tests\": {}}").getTestPattern()).isEqualTo( - GradingConfiguration.SUREFIRE_REPORT_PATTERN); + DEFAULT_PATTERN); assertThat(new GradingConfiguration("{}").getTestPattern()).isEqualTo( - GradingConfiguration.SUREFIRE_REPORT_PATTERN); + DEFAULT_PATTERN); assertThat(new GradingConfiguration("").getTestPattern()).isEqualTo( - GradingConfiguration.SUREFIRE_REPORT_PATTERN); + DEFAULT_PATTERN); assertThat(new GradingConfiguration("<[+").getTestPattern()).isEqualTo( - GradingConfiguration.SUREFIRE_REPORT_PATTERN); + DEFAULT_PATTERN); assertThat(new GradingConfiguration("<[+").getTestPattern()).isEqualTo( - GradingConfiguration.SUREFIRE_REPORT_PATTERN); + DEFAULT_PATTERN); - assertThat(new GradingConfiguration("<[+").getAnalysisPattern()).isEqualTo(GradingConfiguration.ALL_FILES); + assertThat(new GradingConfiguration("<[+").getAnalysisPattern()).isEqualTo(GradingConfiguration.INCLUDE_ALL_FILES); } @Test void shouldReturnProvidedTestPattern() { assertThat(new GradingConfiguration( "{ \"tests\": {\"pattern\":\"*/*.xml\", \"maxScore\":5,\"failureImpact\":1,\"passedImpact\":2,\"skippedImpact\":3}}") - .getTestPattern()).isEqualTo("*/*.xml"); + .getTestPattern()).isEqualTo("glob:*/*.xml"); } @Test void shouldReturnProvidedAnalysisPattern() { assertThat(new GradingConfiguration("{\n" + " \"analysis\": {\n" - + " \"pattern\": \"File.*\",\n" + + " \"fileFilter\": \"File.*\",\n" + " \"maxScore\": 100,\n" + " \"errorImpact\": -5,\n" + " \"highImpact\": -3,\n" From abee728c76f20e03f62a6cfd8065d8fbfeba9432 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Tue, 2 Mar 2021 10:18:05 +0100 Subject: [PATCH 4/6] Use JDK 11 in builds. --- .github/workflows/ci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee4ba8c4..6c5ec290 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,11 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2.3.4 + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11 - name: Build and deploy run: mvn -ntp clean verify jib:dockerBuild - name: Test setup From 87b5f1954f4335b26beb9283121404b6e125aa60 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Tue, 2 Mar 2021 10:19:17 +0100 Subject: [PATCH 5/6] Refactoring. --- .../hm/hafner/grading/AutoGradingAction.java | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java b/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java index 61d7e84b..82d055f0 100644 --- a/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java +++ b/src/main/java/edu/hm/hafner/grading/AutoGradingAction.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; @@ -11,7 +12,6 @@ import org.apache.commons.lang3.StringUtils; import edu.hm.hafner.analysis.FileReaderFactory; -import edu.hm.hafner.analysis.IssueParser; import edu.hm.hafner.analysis.Report; import edu.hm.hafner.analysis.Report.IssueFilterBuilder; import edu.hm.hafner.analysis.Severity; @@ -107,14 +107,6 @@ void run() { score.addAnalysisScores(new AnalysisReportSupplier(analysisScores)); System.out.println("=================================================================="); - if (Files.isReadable(Paths.get(JACOCO_RESULTS))) { - JacocoReport coverageReport = new JacocoParser().parse(read(JACOCO_RESULTS)); - score.addCoverageScores(new CoverageReportSupplier(coverageReport)); - } - else { - System.out.println("No JaCoCo coverage result files found!"); - } - GradingReport results = new GradingReport(); GitHubPullRequestWriter pullRequestWriter = new GitHubPullRequestWriter(); @@ -133,16 +125,6 @@ private String createAffectedFiles(final GradingConfiguration configuration) { return StringUtils.EMPTY; } - private Report parse(final GradingConfiguration configuration, final ParserDescriptor descriptor) { - return filterAnalysisReport(descriptor.createParser().parse(read(descriptor.getPattern())), - configuration.getAnalysisPattern()); - } - - private Report parse(final GradingConfiguration configuration, - final IssueParser parser, final String filePattern) { - return filterAnalysisReport(parser.parse(read(filePattern)), configuration.getAnalysisPattern()); - } - private Report filterAnalysisReport(final Report checkStyleReport, final String analysisPattern) { IssueFilterBuilder builder = new IssueFilterBuilder(); builder.setIncludeFileNameFilter(analysisPattern); @@ -167,10 +149,6 @@ private String getChecksName() { return StringUtils.defaultIfBlank(System.getenv("CHECKS_NAME"), "Autograding results"); } - private String getRepository() { - return StringUtils.defaultIfBlank(System.getenv("GITHUB_REPOSITORY"), "Autograding results"); - } - private String getConfiguration() { String configuration = System.getenv("CONFIG"); if (StringUtils.isBlank(configuration)) { @@ -195,7 +173,4 @@ private String readDefaultConfiguration() { } } - private static FileReaderFactory read(final String s) { - return new FileReaderFactory(Paths.get(s)); - } } From 77c7718d1265a6e1489f5592e3876c7d1440f684 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Tue, 2 Mar 2021 10:40:02 +0100 Subject: [PATCH 6/6] Add constructor for tests. --- .../edu/hm/hafner/grading/ReportFinder.java | 23 +++++++++++++++---- .../hm/hafner/grading/ReportFinderTest.java | 8 +++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/edu/hm/hafner/grading/ReportFinder.java b/src/main/java/edu/hm/hafner/grading/ReportFinder.java index 5d532d38..76daa6ff 100644 --- a/src/main/java/edu/hm/hafner/grading/ReportFinder.java +++ b/src/main/java/edu/hm/hafner/grading/ReportFinder.java @@ -17,6 +17,7 @@ import org.apache.commons.lang3.StringUtils; import edu.hm.hafner.analysis.Report; +import edu.hm.hafner.util.VisibleForTesting; /** * Base class that finds files in the workspace and parses these files with a parser that returns a {@link Report} @@ -25,6 +26,20 @@ * @author Ullrich Hafner */ class ReportFinder { + private final String repository; + private final String branch; + + ReportFinder() { + this(StringUtils.defaultString(System.getenv("GITHUB_REPOSITORY")), + StringUtils.remove(StringUtils.defaultString(System.getenv("GITHUB_REF")), "refs/heads/")); + } + + @VisibleForTesting + ReportFinder(final String repository, final String branch) { + this.repository = repository; + this.branch = branch; + } + /** * Returns the paths that match the specified pattern. * @@ -51,13 +66,11 @@ protected List find(final String directory, final String pattern) { public String renderLinks(final String directory, final String pattern) { String result = "### Analyzed files\n\n"; - String repo = StringUtils.defaultString(System.getenv("GITHUB_REPOSITORY")); - String ref = StringUtils.remove(StringUtils.defaultString(System.getenv("GITHUB_REF")), "refs/heads/"); return find(directory, pattern).stream() .map(file -> String.format("- [%s](https://github.com/%s/blob/%s/%s)", StringUtils.substringAfterLast(file.toString(), "/"), - repo, - ref, + repository, + branch, file)).collect(Collectors.joining("\n", result, "\n")); } @@ -67,7 +80,7 @@ private static class PathMatcherFileVisitor extends SimpleFileVisitor { PathMatcherFileVisitor(final String syntaxAndPattern) { try { - this.pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern); + pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern); } catch (IllegalArgumentException exception) { throw new IllegalArgumentException("Pattern not valid for FileSystem.getPathMatcher: " + syntaxAndPattern); diff --git a/src/test/java/edu/hm/hafner/grading/ReportFinderTest.java b/src/test/java/edu/hm/hafner/grading/ReportFinderTest.java index 5d9e753d..1932a7d2 100644 --- a/src/test/java/edu/hm/hafner/grading/ReportFinderTest.java +++ b/src/test/java/edu/hm/hafner/grading/ReportFinderTest.java @@ -30,13 +30,13 @@ void shouldFindSources() { @Test void shouldCreateAffectedFilesReport() { - ReportFinder finder = new ReportFinder(); + ReportFinder finder = new ReportFinder("uhafner/autograding-github-action", "master"); assertThat(finder.renderLinks("src/main/java/", "regex:.*Jacoco.*\\.java")) .contains("# Analyzed files", - "- [JacocoParser.java](https://github.com//blob//src/main/java/de/tobiasmichael/me/Util/JacocoParser.java)", - "- [JacocoCounter.java](https://github.com//blob//src/main/java/de/tobiasmichael/me/Util/JacocoCounter.java)", - "- [JacocoReport.java](https://github.com//blob//src/main/java/de/tobiasmichael/me/Util/JacocoReport.java)"); + "- [JacocoParser.java](https://github.com/uhafner/autograding-github-action/blob/master/src/main/java/de/tobiasmichael/me/Util/JacocoParser.java)", + "- [JacocoCounter.java](https://github.com/uhafner/autograding-github-action/blob/master/src/main/java/de/tobiasmichael/me/Util/JacocoCounter.java)", + "- [JacocoReport.java](https://github.com/uhafner/autograding-github-action/blob/master/src/main/java/de/tobiasmichael/me/Util/JacocoReport.java)"); } }