diff --git a/changelog/@unreleased/pr-1932.v2.yml b/changelog/@unreleased/pr-1932.v2.yml new file mode 100644 index 000000000..7990b1f5c --- /dev/null +++ b/changelog/@unreleased/pr-1932.v2.yml @@ -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 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckJUnitDependencies.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckJUnitDependencies.java index 7263541be..9e7018061 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckJUnitDependencies.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/tasks/CheckJUnitDependencies.java @@ -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; @@ -164,7 +163,8 @@ private boolean hasDep(Set deps, Predicate fileContainsSubstring( file, l -> l.contains("org.junit.Test") @@ -174,18 +174,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 substring) { try (Stream 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) { + throw new RuntimeException("Unable to check file for junit dependencies: " + file, e); } } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy index 99dd2f896..f3312ed40 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineTestingIntegrationTest.groovy @@ -16,6 +16,7 @@ package com.palantir.baseline + import nebula.test.IntegrationSpec import nebula.test.functional.ExecutionResult import spock.lang.Unroll @@ -209,4 +210,16 @@ class BaselineTestingIntegrationTest extends IntegrationSpec { def result4 = runTasksSuccessfully('test', '-Drecreate=true') result4.wasExecuted(':test') } + + def 'does not crash with non-utf8 resources'() { + when: + buildFile << standardBuildFile + file('src/test/resources/some-binary').newOutputStream().withCloseable { + // Invalid unicode sequence identifier + it.write([0xA0, 0xA1] as byte[]) + } + + then: + runTasksSuccessfully('checkJUnitDependencies') + } }