Skip to content

Commit

Permalink
Merge pull request quarkusio#40640 from FWest98/fw/test-component/pat…
Browse files Browse the repository at this point in the history
…hspace

Fix pathname encoding in the component test library
  • Loading branch information
geoand authored May 16, 2024
2 parents 57eb9e9 + 9c71d7a commit c97fb19
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -1197,13 +1198,20 @@ private File getTestOutputDirectory(Class<?> testClass) {
if (outputDirectory != null) {
testOutputDirectory = new File(outputDirectory);
} else {
// All below string transformations work with _URL encoded_ paths, where e.g.
// a space is replaced with %20. At the end, we feed this back to URI.create
// to make sure the encoding is dealt with properly, so we don't have to do this
// ourselves. Directly passing a URL-encoded string to the File() constructor
// does not work properly.

// org.acme.Foo -> org/acme/Foo.class
String testClassResourceName = fromClassNameToResourceName(testClass.getName());
// org/acme/Foo.class -> /some/path/to/project/target/test-classes/org/acme/Foo.class
String testPath = testClass.getClassLoader().getResource(testClassResourceName).getFile();
// /some/path/to/project/target/test-classes/org/acme/Foo.class -> /some/path/to/project/target/test-classes
String testClassesRootPath = testPath.substring(0, testPath.length() - testClassResourceName.length());
testOutputDirectory = new File(testClassesRootPath);
// org/acme/Foo.class -> file:/some/path/to/project/target/test-classes/org/acme/Foo.class
String testPath = testClass.getClassLoader().getResource(testClassResourceName).toString();
// file:/some/path/to/project/target/test-classes/org/acme/Foo.class -> file:/some/path/to/project/target/test-classes
String testClassesRootPath = testPath.substring(0, testPath.length() - testClassResourceName.length() - 1);
// resolve back to File instance
testOutputDirectory = new File(URI.create(testClassesRootPath));
}
if (!testOutputDirectory.canWrite()) {
throw new IllegalStateException("Invalid test output directory: " + testOutputDirectory);
Expand Down

0 comments on commit c97fb19

Please sign in to comment.