Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
big-andy-coates opened this issue Jan 17, 2024 · 0 comments · Fixed by #228
Closed

Comments

@big-andy-coates
Copy link
Collaborator

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.

big-andy-coates added a commit to big-andy-coates/gradle-modules-plugin that referenced this issue Jan 17, 2024
fixes: java9-modularity#227

Enhance `TestTask` to patch-in additional resource directories containing generated resources, as added via `org.gradle.api.tasks.SourceSetOutput.dir()` methods, when running unit tests, so that all resources are accessible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant