Skip to content

Commit

Permalink
[#53] Make dependency exclusion on classpath configurable
Browse files Browse the repository at this point in the history
Closes #53.
  • Loading branch information
szpak committed Nov 14, 2017
1 parent 3945e31 commit a78e764
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.2.4 - Unreleased

- Make dependency exclusion on classpath configurable - [#53]
- PIT 1.2.4 by default
- Switch build to Gradle 4.3.1 - nicer executed tasks displayng with `--console=verbose`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PitestPlugin implements Plugin<Project> {
public final static String PITEST_CONFIGURATION_NAME = 'pitest'

private final static List<String> DYNAMIC_LIBRARY_EXTENSIONS = ['so', 'dll', 'dylib']
private final static List<String> FILE_EXTENSIONS_TO_FILTER_FROM_CLASSPATH = ['pom', 'xml', 'orbit'] + DYNAMIC_LIBRARY_EXTENSIONS
private final static List<String> DEFAULT_FILE_EXTENSIONS_TO_FILTER_FROM_CLASSPATH = ['pom'] + DYNAMIC_LIBRARY_EXTENSIONS

private final static Logger log = Logging.getLogger(PitestPlugin)

Expand Down Expand Up @@ -81,6 +81,7 @@ class PitestPlugin implements Plugin<Project> {
extension.pitestVersion = DEFAULT_PITEST_VERSION
extension.testSourceSets = [project.sourceSets.test] as Set
extension.mainSourceSets = [project.sourceSets.main] as Set
extension.fileExtensionsToFilter = DEFAULT_FILE_EXTENSIONS_TO_FILTER_FROM_CLASSPATH
}

private void configureTaskDefault(PitestTask task) {
Expand All @@ -90,7 +91,7 @@ class PitestPlugin implements Plugin<Project> {

FileCollection combinedTaskClasspath = new UnionFileCollection(testRuntimeClasspath)
FileCollection filteredCombinedTaskClasspath = combinedTaskClasspath.filter { File file ->
!FILE_EXTENSIONS_TO_FILTER_FROM_CLASSPATH.find { file.name.endsWith(".$it") }
!extension.fileExtensionsToFilter.find { file.name.endsWith(".$it") }
}

return filteredCombinedTaskClasspath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ class PitestPluginExtension {
@Incubating
List<String> features

/**
* File extensions which should be filtered from a classpath.
*
* PIT fails on not Java specific file passed on a classpath (e.g. native libraries). Native libraries ('*.so', '*.dll', '*.dylib')
* and '*.pom' files are filtered by default, but a developer can add extra extensions to the list:
* <pre>
* pitest {
* fileExtensionsToFilter += ['xml', 'orbit']
* }
* </pre>
*
* Rationale: https://github.com/szpak/gradle-pitest-plugin/issues/53
*
* This feature is specific to the Gradle plugin.
*
* @since 1.2.4
*/
@Incubating
List<String> fileExtensionsToFilter

void setReportDir(String reportDirAsString) {
this.reportDir = new File(reportDirAsString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class PitestPluginClasspathFilteringSpec extends BasicProjectBuilderSpec {
private PitestTask task

@Issue('https://github.com/szpak/gradle-pitest-plugin/issues/52')
def "should filter dynamic library '#libFileName'"() {
def "should filter dynamic library '#libFileName' by default"() {
given:
File libFile = new File(tmpProjectDir.root, libFileName)
and:
task = getJustOnePitestTaskOrFail()
expect:
!task.createTaskArgumentMap()['classPath'].contains(libFile.path)
where:
libFileName << ['lib.so', 'win.dll', 'dyn.dylib', "my.xml", "strange.orbit"] //TODO: Add test with more than one element
libFileName << ['lib.so', 'win.dll', 'dyn.dylib'] //TODO: Add test with more than one element
}

def "should filter .pom file"() {
def "should filter .pom file by default"() {
given:
File pomFile = addFileWithFileNameAsCompileDependencyAndReturnAsFile('foo.pom')
and:
Expand All @@ -42,7 +42,7 @@ class PitestPluginClasspathFilteringSpec extends BasicProjectBuilderSpec {
!task.createTaskArgumentMap()['classPath'].contains(pomFile.path)
}

def "should not filer regular dependency '#depFileName'"() {
def "should not filer regular dependency '#depFileName' by default"() {
given:
File depFile = addFileWithFileNameAsCompileDependencyAndReturnAsFile(depFileName)
and:
Expand All @@ -53,7 +53,7 @@ class PitestPluginClasspathFilteringSpec extends BasicProjectBuilderSpec {
depFileName << ['foo.jar', 'foo.zip']
}

def "should not filer source set directory"() {
def "should not filer source set directory by default"() {
given:
File testClassesDir = new File(new File(new File(new File(tmpProjectDir.root, 'build'), 'classes'), 'java'), 'test')
and:
Expand All @@ -74,6 +74,59 @@ class PitestPluginClasspathFilteringSpec extends BasicProjectBuilderSpec {
!task.createTaskArgumentMap()['classPath'].contains(libDepFile.path)
}

def "should filter user defined extensions"() {
given:
File depFile = addFileWithFileNameAsCompileDependencyAndReturnAsFile('file.extra')
and:
project.pitest.fileExtensionsToFilter = ['extra']
and:
task = getJustOnePitestTaskOrFail()
expect:
!task.createTaskArgumentMap()['classPath'].contains(depFile.path)
}

def "should allow to override extensions filtered by default"() {
given:
File depFile = addFileWithFileNameAsCompileDependencyAndReturnAsFile('needed.so')
and:
project.pitest.fileExtensionsToFilter = ['extra']
and:
task = getJustOnePitestTaskOrFail()
expect:
task.createTaskArgumentMap()['classPath'].contains(depFile.path)
}

def "should allow to provide extra extensions in addition to default ones"() {
given:
File libDepFile = addFileWithFileNameAsCompileDependencyAndReturnAsFile('default.so')
File extraDepFile = addFileWithFileNameAsCompileDependencyAndReturnAsFile('file.extra')
and:
project.pitest.fileExtensionsToFilter += ['extra']
and:
task = getJustOnePitestTaskOrFail()
expect:
String resolvedPitClasspath = task.createTaskArgumentMap()['classPath']
!resolvedPitClasspath.contains(libDepFile.path)
!resolvedPitClasspath.contains(extraDepFile.path)
}

def "should not fail on fileExtensionsToFilter set to null"() {
given:
project.pitest.fileExtensionsToFilter = null
and:
task = getJustOnePitestTaskOrFail()
when:
String resolvedPitClasspath = forceClasspathResolutionInPluginAndReturnIt()
then:
noExceptionThrown()
and:
resolvedPitClasspath.contains('main')
}

private String forceClasspathResolutionInPluginAndReturnIt() {
return task.createTaskArgumentMap()['classPath']
}

private File addFileWithFileNameAsCompileDependencyAndReturnAsFile(String depFileName) {
File depFile = new File(tmpProjectDir.root, depFileName)
project.dependencies.add('compile', project.files(depFile))
Expand Down

0 comments on commit a78e764

Please sign in to comment.