This is an automated email from the ASF dual-hosted git repository. squakez pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit b54296e0af822093db62327f24c831146f0a0590 Author: Pasquale Congiusti <[email protected]> AuthorDate: Wed Nov 25 13:22:52 2026 +0100 CAMEL-27543: check directory not empty --- .../dsl/jbang/run/export/ProjectExporter.java | 26 +++++++++++++++-- .../dsl/jbang/run/export/ProjectExporterTest.java | 34 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java index ea76f0a81611..abff0a117dc8 100644 --- a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java +++ b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java @@ -17,11 +17,13 @@ package org.apache.camel.dsl.jbang.run.export; import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Properties; +import java.util.stream.Stream; import org.apache.camel.dsl.jbang.run.RunPipeline; import org.apache.camel.dsl.jbang.run.common.CamelJBangConstants; @@ -90,7 +92,14 @@ public class ProjectExporter { * @return 0 when the project was exported, or the exit code of the failure */ public int export(ExportRequest request) throws Exception { - Path baseDir = prepare(request); + Path baseDir; + try { + baseDir = prepare(request); + } catch (FileNotFoundException ex) { + // as in export(Path, ExportRequest): on stderr, and exit 66 + printer().printErr(ex.getMessage()); + return RunPipeline.FILE_NOT_FOUND; + } if (request.runtime == null) { printer().printErr("The runtime option must be specified"); @@ -129,6 +138,9 @@ public class ProjectExporter { if (Files.isDirectory(first)) { baseDir = first; ExportHelper.dirToFiles(name, request.files); + if (request.files.isEmpty()) { + throw new FileNotFoundException("No files found in directory: " + name); + } } } } @@ -254,7 +266,7 @@ public class ProjectExporter { return context.getPrinter(); } - private static void verifyExportFiles(List<String> files) throws FileNotFoundException { + private static void verifyExportFiles(List<String> files) throws IOException { for (var fn : files) { if (fn.indexOf(':') < 0 || fn.startsWith("file:")) { if (fn.startsWith("file:")) { @@ -269,10 +281,20 @@ public class ProjectExporter { if (!Files.exists(Paths.get(fn))) { throw new FileNotFoundException("Path does not exist: " + fn); } + Path path = Paths.get(fn); + if (Files.isDirectory(path) && isEmptyDirectory(path)) { + throw new FileNotFoundException("No files found in directory: " + fn); + } } } } + private static boolean isEmptyDirectory(Path dir) throws IOException { + try (Stream<Path> entries = Files.list(dir)) { + return entries.findAny().isEmpty(); + } + } + private static void doLoadAndInitProfileProperties(ExportRequest request, Path path) throws Exception { if (Files.exists(path)) { Properties props = new CamelCaseOrderedProperties(); diff --git a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java index 71f3e4a4dfc6..8f62181750aa 100644 --- a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java +++ b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java @@ -745,6 +745,40 @@ class ProjectExporterTest { assertThat(resolved).isEmpty(); } + @Test + void failsOnEmptyDirectory() throws Exception { + Path empty = Files.createDirectories(dir.resolve("empty")); + ExportRequest request = new ExportRequest(); + request.files.add(empty + "/*"); + request.runtime = RuntimeType.main; + + StringPrinter printer = new StringPrinter(); + int exit = new ProjectExporter(new ExportContext().withPrinter(printer).withDependencyResolver(resolver)) + .export(request); + + assertThat(exit).isEqualTo(RunPipeline.FILE_NOT_FOUND); + assertThat(printer.getStderr()).contains("No files found in directory"); + assertThat(printer.getStdout()).isEmpty(); + assertThat(resolved).isEmpty(); + } + + @Test + void failsOnEmptyDirectoryWithoutWildcard() throws Exception { + Path empty = Files.createDirectories(dir.resolve("empty")); + ExportRequest request = new ExportRequest(); + request.files.add(empty.toString()); + request.runtime = RuntimeType.main; + + StringPrinter printer = new StringPrinter(); + int exit = new ProjectExporter(new ExportContext().withPrinter(printer).withDependencyResolver(resolver)) + .export(request); + + assertThat(exit).isEqualTo(RunPipeline.FILE_NOT_FOUND); + assertThat(printer.getStderr()).contains("No files found in directory"); + assertThat(printer.getStdout()).isEmpty(); + assertThat(resolved).isEmpty(); + } + private ExportContext exportContext() { return new ExportContext().withPrinter(new StringPrinter()).withDependencyResolver(resolver); }
