Skip to content

Commit

Permalink
Add constructor for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Mar 2, 2021
1 parent 87b5f19 commit 77c7718
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
23 changes: 18 additions & 5 deletions src/main/java/edu/hm/hafner/grading/ReportFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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.
*
Expand All @@ -51,13 +66,11 @@ protected List<Path> 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"));
}

Expand All @@ -67,7 +80,7 @@ private static class PathMatcherFileVisitor extends SimpleFileVisitor<Path> {

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);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/edu/hm/hafner/grading/ReportFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)");

}
}

0 comments on commit 77c7718

Please sign in to comment.