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

Set target dir in QuarkusUnitTest #23859

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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