Skip to content

Commit

Permalink
Merge pull request #23859 from aloubyansky/quarkus-unit-test-target-dir
Browse files Browse the repository at this point in the history
Set target dir in QuarkusUnitTest
  • Loading branch information
geoand authored Feb 23, 2022
2 parents 9b885d0 + 8300b95 commit 7b574cd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,12 @@ void configFiles(BuildProducer<HotDeploymentWatchedFileBuildItem> watchedFiles,
SmallRyeOpenApiConfig openApiConfig,
LaunchModeBuildItem launchMode,
OutputTargetBuildItem outputTargetBuildItem) throws IOException {

// Add any aditional directories if configured
if (launchMode.getLaunchMode().isDevOrTest() && openApiConfig.additionalDocsDirectory.isPresent()) {
List<Path> additionalStaticDocuments = openApiConfig.additionalDocsDirectory.get();
for (Path path : additionalStaticDocuments) {
// Scan all yaml and json files
List<String> filesInDir = getResourceFiles(ClassPathUtils.toResourceName(path),
outputTargetBuildItem.getOutputDirectory());
List<String> filesInDir = getResourceFiles(path, outputTargetBuildItem.getOutputDirectory());
for (String possibleFile : filesInDir) {
watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(possibleFile));
}
Expand Down Expand Up @@ -795,7 +793,7 @@ private List<Result> findStaticModels(SmallRyeOpenApiConfig openApiConfig, Path
for (Path path : additionalStaticDocuments) {
// Scan all yaml and json files
try {
List<String> filesInDir = getResourceFiles(ClassPathUtils.toResourceName(path), target);
List<String> filesInDir = getResourceFiles(path, target);
for (String possibleModelFile : filesInDir) {
addStaticModelIfExist(results, possibleModelFile);
}
Expand Down Expand Up @@ -829,32 +827,31 @@ private void addStaticModelIfExist(List<Result> results, Format format, String p
}
}

private List<String> getResourceFiles(String pathName, Path target) throws IOException {
private List<String> getResourceFiles(Path resourcePath, Path target) throws IOException {
final String resourceName = ClassPathUtils.toResourceName(resourcePath);
List<String> filenames = new ArrayList<>();
if (target == null) {
// Here we are resolving the resource dir relative to the classes dir and if it does not exist, we fallback to locating the resource dir on the classpath.
// Although the classes dir should already be on the classpath.
// In a QuarkusUnitTest the module's classes dir and the test application root could be different directories, is this code here for that reason?
final Path targetResourceDir = target == null ? null : target.resolve("classes").resolve(resourcePath);
if (targetResourceDir != null && Files.exists(targetResourceDir)) {
try (Stream<Path> paths = Files.list(targetResourceDir)) {
return paths.map((t) -> {
return resourceName + "/" + t.getFileName().toString();
}).collect(Collectors.toList());
}
} else {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try (InputStream inputStream = cl.getResourceAsStream(pathName)) {
try (InputStream inputStream = cl.getResourceAsStream(resourceName)) {
if (inputStream != null) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String resource;
while ((resource = br.readLine()) != null) {
filenames.add(pathName + "/" + resource);
filenames.add(resourceName + "/" + resource);
}
}
}
}
} else {
Path classes = target.resolve("classes");
if (classes != null) {
Path path = classes.resolve(pathName);
if (Files.exists(path)) {
try (Stream<Path> paths = Files.list(path)) {
return paths.map((t) -> {
return pathName + "/" + t.getFileName().toString();
}).collect(Collectors.toList());
}
}
}
}
return filenames;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,24 @@ private static boolean isInTestDir(URL resource) {
private static Path toPath(URL resource) {
return ClassPathUtils.toLocalPath(resource);
}

/**
* Returns the build directory of the project given its base dir and the test classes dir.
*
* @param projectRoot project dir
* @param testClassLocation test dir
*
* @return project build dir
*/
public static Path getProjectBuildDir(Path projectRoot, Path testClassLocation) {
Path outputDir;
try {
// this should work for both maven and gradle
outputDir = projectRoot.resolve(projectRoot.relativize(testClassLocation).getName(0));
} catch (Exception e) {
// this shouldn't happen since testClassLocation is usually found under the project dir
outputDir = projectRoot;
}
return outputDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -540,6 +541,8 @@ public boolean test(String s) {
.setMode(QuarkusBootstrap.Mode.TEST)
.addExcludedPath(testLocation)
.setProjectRoot(testLocation)
.setTargetDirectory(
PathTestHelper.getProjectBuildDir(Paths.get("").normalize().toAbsolutePath(), testLocation))
.setFlatClassPath(flatClassPath)
.setForcedDependencies(forcedDependencies);
for (JavaArchive dependency : additionalDependencies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,7 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend
}

final Path projectRoot = Paths.get("").normalize().toAbsolutePath();
Path outputDir;
try {
// this should work for both maven and gradle
outputDir = projectRoot.resolve(projectRoot.relativize(testClassLocation).getName(0));
} catch (Exception e) {
// this shouldn't happen since testClassLocation is usually found under the project dir
outputDir = projectRoot;
}
final Path outputDir = PathTestHelper.getProjectBuildDir(projectRoot, testClassLocation);

rootBuilder.add(appClassLocation);
final Path appResourcesLocation = PathTestHelper.getResourcesForClassesDirOrNull(appClassLocation, "main");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,7 @@ static ArtifactLauncher.InitContext.DevServicesLaunchResult handleDevServices(Ex

final Path projectRoot = Paths.get("").normalize().toAbsolutePath();
runnerBuilder.setProjectRoot(projectRoot);
Path outputDir;
try {
// this should work for both maven and gradle
outputDir = projectRoot.resolve(projectRoot.relativize(testClassLocation).getName(0));
} catch (Exception e) {
// this shouldn't happen since testClassLocation is usually found under the project dir
outputDir = projectRoot;
}
runnerBuilder.setTargetDirectory(outputDir);
runnerBuilder.setTargetDirectory(PathTestHelper.getProjectBuildDir(projectRoot, testClassLocation));

rootBuilder.add(appClassLocation);
final Path appResourcesLocation = PathTestHelper.getResourcesForClassesDirOrNull(appClassLocation, "main");
Expand Down

0 comments on commit 7b574cd

Please sign in to comment.