Skip to content
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

Fix MalformedInputException in checkJUnitDependencies #1932

Merged
merged 6 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1932.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: Fix `MalformedInputException` when checking non-utf8 files for correct
junit dependencies.
links:
- https://github.com/palantir/gradle-baseline/pull/1932
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.palantir.baseline.plugins.BaselineTesting;
import com.palantir.baseline.util.VersionUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -164,7 +163,7 @@ private boolean hasDep(Set<ResolvedComponentResult> deps, Predicate<ModuleVersio
}

private boolean sourceSetMentionsJUnit4(SourceSet ss) {
return !ss.getAllSource()
return !ss.getAllJava()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a small comment here that getAllJava is also including groovy files.

.filter(file -> fileContainsSubstring(
file,
l -> l.contains("org.junit.Test")
Expand All @@ -174,18 +173,16 @@ private boolean sourceSetMentionsJUnit4(SourceSet ss) {
}

private boolean sourceSetMentionsJUnit5Api(SourceSet ss) {
return !ss.getAllSource()
return !ss.getAllJava()
.filter(file -> fileContainsSubstring(file, l -> l.contains("org.junit.jupiter.api.")))
.isEmpty();
}

private boolean fileContainsSubstring(File file, Predicate<String> substring) {
try (Stream<String> lines = Files.lines(file.toPath())) {
boolean hit = lines.anyMatch(substring::test);
getProject().getLogger().debug("[{}] {}", hit ? "hit" : "miss", file);
return hit;
} catch (IOException e) {
throw new RuntimeException("Unable to check file " + file, e);
return lines.anyMatch(substring);
} catch (Exception e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MalformedInputException is thrown as an UncheckedIOException.

throw new RuntimeException("Unable to check file for junit dependencies: " + file, e);
}
}

Expand Down