Skip to content

Additional resource directories are not patched into the module during testing. #227

@big-andy-coates

Description

@big-andy-coates

The org.gradle.api.tasks.SourceSetOutput object allows other parts of the system to register additional directories that contain generated resources via the dir methods.

For example, given the following in the build script:

val generatedResourcesDir = "generated/resources/test";

sourceSets {
    test {
        // Add additional output directory for generated resources.
        // See org.gradle.api.tasks.SourceSetOutput for more info.
        output.dir(layout.buildDirectory.dir(generatedResourcesDir))
    }
}

val generateResources = tasks.register("generateResources") {
    doLast {
        val outputFile = layout.buildDirectory.file("$generatedResourcesDir/generated-resource.txt")
        outputFile.get().asFile.parentFile.mkdirs()
        outputFile.get().asFile.writeText("some content")

        println("Resource file generated at: ${outputFile.get().asFile.absolutePath}")
    }
}

tasks.test {
    dependsOn(generateResources)
}

Then a unit test should be able to load the resource:

  @Test
  fun testGeneratedResource() {
      val resource = object: Any() {}.javaClass.getResourceAsStream("/generated-resource.txt")
      if (resource == null) {
          throw RuntimeException("Couldn't load generated resource")
      }
  }

See java docs for more info.

At the moment, this gradle-modules-plugin does not patch these additional resource directories into the module-under-test when running unit tests, causing unit tests to fail when attempting to load resources.

These additional resource directories are not included in the SourceSetOutput.getClassesDirs() or SourceSetOutput.getResourcesDir() that are accessed by the TestTask class in this plugin. Instead, they can be accessed via SourceSetOutput.getDirs(). TestTask needs to be enhanced to include these additional resource directories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions