Skip to content

withFileFromClassPath fails when path to classpath resource contains spaces #263

@lbialy

Description

@lbialy

Hello,

How to replicate:

  1. create directory with spaces in path
mkdir ~/dir\ with\ spaces
  1. move some project using testcontainers-java to that directory
  2. put a file to copy into container in resources directory (touch src/test/resources/any_file in this example)
  3. use container loading files from class path, ie:
new GenericContainer(
        new ImageFromDockerfile()
                .withDockerfileFromBuilder(builder -> {
                        builder
                                .from("alpine:3.2")
                                .copy("any_file", "any_file")
                                .build(); 
                        })
                )
                .withFileFromClasspath("any_file", "any_file")
)

This will throw exception:

1 error
Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image name from org.testcontainers.images.builder.ImageFromDockerfile@61ae0d43
Caused by: java.lang.RuntimeException: Can't get size from ~/dir%20with%20spaces/your-project/target/test-classes/any_file
Caused by: java.nio.file.NoSuchFileException: ~/dir%20with%20spaces/your-project/target/test-classes/any_file

Issue is related to urlencoded spaces in path (%20) when using

URL resource = ClasspathTrait.class.getClassLoader().getResource(resourcePath);
String resourceFilePath = new File(resource.getFile()).getAbsolutePath();

in default implementation of withFileFromClasspath. Calling .toURI() instead of .getFile() on resource should fix this issue while not breaking anything - I'm using this workaround and seems to be fine. No PR though, sorry, simply no time to do it.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions