From 77c7718d1265a6e1489f5592e3876c7d1440f684 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Tue, 2 Mar 2021 10:40:02 +0100 Subject: [PATCH] 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)"); } }