Skip to content

Commit

Permalink
Fix MalformedInputException in checkJUnitDependencies (#1932)
Browse files Browse the repository at this point in the history
Fix `MalformedInputException` when checking non-utf8 files for correct junit dependencies.
  • Loading branch information
CRogers authored Oct 4, 2021
1 parent 15e9384 commit 29ba499
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
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,8 @@ private boolean hasDep(Set<ResolvedComponentResult> deps, Predicate<ModuleVersio
}

private boolean sourceSetMentionsJUnit4(SourceSet ss) {
return !ss.getAllSource()
// getAllJava() includes groovy sources too
return !ss.getAllJava()
.filter(file -> fileContainsSubstring(
file,
l -> l.contains("org.junit.Test")
Expand All @@ -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<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) {
throw new RuntimeException("Unable to check file for junit dependencies: " + file, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.palantir.baseline


import nebula.test.IntegrationSpec
import nebula.test.functional.ExecutionResult
import spock.lang.Unroll
Expand Down Expand Up @@ -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')
}
}

0 comments on commit 29ba499

Please sign in to comment.