Steve Mitcham
Steve Mitcham

Reputation: 5313

Gradle get file collection of parent directories

I'm trying to configure a task that will generate a single report from a collection of binary junit test results, but I'm unable to create a FileCollection containing the paths in which all the result files are located.

My task is defined like this

task aggregateTestREports(type: org.gradle.api.tasks.tests.TestSupport) {
   destinationDir = reportDir
   testResultsDirs = ???
}

Where the ??? is the part that I'm not getting to work.

I can use the following code to get a list of the output.bin files in our build structure, but I need to transform this into the list of the directories the files are in.

fileTree(dir: '.', includes: ['**/test-results/binary/test/output.bin'])

I've tried creating a custom class from the base class like this and passing the results of that line to the testOutputBinFiles parameter and calculating the files dynamically

class AggregateTestReport extends TestReport {
   @Input
   def testOutputBinFiles 

   def getTestResultDirs() {
      def parents = []
      testOutputBinFiles.each {
         File file -> parents << file.parentFile.absoluteFile
      }
      parents
   }
}

but this gives me an error that the return value is not compatible with a FileCollection

The docs for FileCollection indicate that the only way to get a new one is to use the files() function, but it isn't available from within the custom class to get a new file.

Upvotes: 0

Views: 1530

Answers (2)

Steve Mitcham
Steve Mitcham

Reputation: 5313

Due to an issue with the way the tests in my project are written taking on the dependency that is introduced by dnault's answer causes our tests to fail. Ultimately as we fix the issue in our project that causes this, his solution will work so I accepted that answer. For completeness, my stopgap solution ended up being

def resultPaths(FileCollection testOutputBinFiles) {
  def parents = []
  testOutputBinFiles.each {
     File file -> parents << file.parentFile.absoluteFile
  }
  parents
}

task aggregateTestReports(type: org.gradle.api.tasks.testing.TestReport) {
  destinationDir = reportDir
  reportOn resultPaths(fileTree(dir: '.', includes: ['**/test-results/binary/test/output.bin']))
}

Upvotes: 2

dnault
dnault

Reputation: 8889

You can declare a task of type TestReport with a 'reportOn' property listing the Test tasks to aggregate:

task allTests(type: TestReport) {
    destinationDir = file("${buildDir}/reports/allTests")
    reportOn test, myCustomTestTask
}

This approach is outlined in section 7.3.3 of "Gradle in Action" by Benjamin Muschko (highly recommended reading).

Upvotes: 1

Related Questions