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

Skip checking binary files for possible JUnit dependencies #886

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions changelog/@unreleased/pr-886.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: fix
fix:
description: |-
Treat MalformedInputException as binary files that are not necessary to
scan for old JUnit dependencies.

Rethrow any non-IOExeception (such as UncheckedIOException wrapping
MalformedInputException) with more useful error message indicating a
file could not be scanned (e.g. ).
links:
- https://github.com/palantir/gradle-baseline/pull/886
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package com.palantir.baseline.tasks;

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.palantir.baseline.plugins.BaselineTesting;
import java.io.File;
import java.io.IOException;
import java.nio.charset.MalformedInputException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand Down Expand Up @@ -149,15 +151,24 @@ private boolean sourceSetMentionsJUnit5Api(SourceSet ss) {
}

private boolean fileContainsSubstring(File file, Predicate<String> substring) {
try (Stream<String> lines = Files.lines(file.toPath())) {
Path path = file.toPath();
try (Stream<String> lines = Files.lines(path)) {
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);
} catch (Exception e) {
if (isCausedByBinaryFile(e)) {
getLogger().warn("Unable to check binary file {} due to {}", path, e.getMessage());
return false;
}
throw new RuntimeException("Unable to check file " + path, e);
}
}

private static boolean isCausedByBinaryFile(Exception exception) {
return Throwables.getCausalChain(exception).stream().anyMatch(MalformedInputException.class::isInstance);
}

private static boolean isJunitJupiter(ModuleVersionIdentifier dep) {
return "org.junit.jupiter".equals(dep.getGroup()) && "junit-jupiter".equals(dep.getName());
}
Expand Down