This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 4eee4ec76168 CAMEL-26930: camel-jbang - camel run expands a wildcard 
the shell did not, such as '*' or the Windows command prompt (#29965)
4eee4ec76168 is described below

commit 4eee4ec761681023b4f3e9aad5be92ec66e87252
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 09:59:43 2026 +0100

    CAMEL-26930: camel-jbang - camel run expands a wildcard the shell did not, 
such as '*' or the Windows command prompt (#29965)
    
    Found while testing `--exclude` for item 6 of 
[CAMEL-26930](https://issues.apache.org/jira/browse/CAMEL-26930), and filed 
with its other run issues (item 7).
    
    ## Problem
    
    `camel run *` works because a Unix shell expands the `*` before the CLI 
sees it. A shell that passes the wildcard on, such as the Windows command 
prompt, or a quoted `'*'`, gave the CLI the `*` itself:
    
    ```
    $ camel run '*'                    ERROR: No routes found in: *             
                    exit 1
    $ camel run '*.camel.yaml'         ERROR: File not found: Source file 
'*.camel.yaml' does not exist   exit 66
    $ camel run 'sub/*.yaml'           ERROR: File not found: Source file 
'sub/*.yaml' does not exist     exit 66
    ```
    
    ## Change
    
    `Run.expandWildcards` expands a local file name with `*` or `?` in its name 
(not in its folder) to the files and folders it matches, sorted, as a shell 
does: a hidden file matches only a wildcard that starts with a dot, and a 
folder is left out later as with `camel run *`. A wildcard that matches nothing 
exits with 66, "No file matches: *.xml". A `file:` resource pattern and a 
remote file, such as `github:...:jbang/*`, are left as given.
    
    On the launcher, in a folder with `a.camel.yaml`, `skip.camel.yaml` and 
`sub/`:
    
    ```
    camel run '*'                                              a skip
    camel run '*.camel.yaml' --exclude=skip.camel.yaml         a
    camel run 'sub/*.yaml'                                     sub/b sub/skip2
    camel run '*.xml'                                          ERROR: No file 
matches: *.xml   exit 66
    ```
    
    ## Tests
    
    - `RunWildcardTest` (new): `*` is the files of the folder, sorted, without 
a hidden file; `*.yaml` and `?`; other files, `github:` and `file:` are left as 
given; no match exits 66.
    - camel-jbang-core: 3073 + 264 pass (`DependencyUpdateTest` flaked once and 
passed on the rerun).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 50 ++++++++++++
 .../dsl/jbang/core/commands/RunWildcardTest.java   | 92 ++++++++++++++++++++++
 2 files changed, 142 insertions(+)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 8062ef890a96..2ba1369d5658 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -25,11 +25,13 @@ import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
+import java.nio.file.PathMatcher;
 import java.nio.file.Paths;
 import java.util.*;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Stream;
 
 import org.aesh.command.CommandDefinition;
 import org.aesh.command.CommandLifecycle;
@@ -382,6 +384,7 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
             return runExample();
         }
 
+        expandWildcards();
         checkFilesExist();
         skipCitrusTests();
         printRunConfiguration();
@@ -396,6 +399,53 @@ public class Run extends CamelCommand implements 
CommandLifecycle {
         printConfigurationValues("Running integration with the following 
configuration:");
     }
 
+    /**
+     * Expands a local file name with a wildcard, such as {@code *} or {@code 
routes/*.yaml}, to the files it matches,
+     * as a Unix shell does with {@code camel run *}. A shell that does not, 
such as the Windows command prompt, or a
+     * quoted {@code '*'}, passed the wildcard on, and camel run failed with 
"No routes found in: *" (CAMEL-26930). As
+     * in a shell, a hidden file matches only a wildcard that starts with a 
dot. A wildcard that matches nothing exits
+     * with code 66.
+     */
+    void expandWildcards() {
+        if (files == null || files.isEmpty()) {
+            return;
+        }
+        List<String> answer = new ArrayList<>();
+        for (String file : files) {
+            // a local file, not file: (a resource pattern) nor a remote one; 
a single letter is a drive on Windows
+            String scheme = getScheme(file);
+            boolean local = scheme == null || scheme.length() == 1;
+            String name = FileUtil.stripPath(file);
+            String dir = FileUtil.onlyPath(file);
+            boolean wildcard = local && (name.contains("*") || 
name.contains("?"))
+                    && (dir == null || !dir.contains("*") && 
!dir.contains("?"));
+            if (!wildcard) {
+                answer.add(file);
+                continue;
+            }
+            Path folder = Paths.get(dir != null ? dir : ".");
+            List<String> matches = new ArrayList<>();
+            if (Files.isDirectory(folder)) {
+                PathMatcher matcher = 
folder.getFileSystem().getPathMatcher("glob:" + name);
+                try (Stream<Path> list = Files.list(folder)) {
+                    list.map(Path::getFileName)
+                            .filter(p -> matcher.matches(p) && 
(name.startsWith(".") || !p.toString().startsWith(".")))
+                            .map(p -> dir != null ? dir + "/" + p : 
p.toString())
+                            .sorted()
+                            .forEach(matches::add);
+                } catch (IOException e) {
+                    throw CliException.failure("Cannot list the files of " + 
folder + ": " + e.getMessage());
+                }
+            }
+            if (matches.isEmpty()) {
+                throw CliException.fileNotFound("No file matches: " + file);
+            }
+            answer.addAll(matches);
+        }
+        files.clear();
+        files.addAll(answer);
+    }
+
     /**
      * Fails a local file that does not exist with exit code 66, before 
anything is prepared or printed. Remote files
      * (such as github: and https:) and wildcards are checked when they are 
read.
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunWildcardTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunWildcardTest.java
new file mode 100644
index 000000000000..7dbc11a47fbd
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/RunWildcardTest.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.camel.dsl.jbang.core.common.CliException;
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
+import org.apache.camel.dsl.jbang.core.common.CommandLineTestHelper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * camel run expands a wildcard that the shell passed on, such as a quoted '*' 
or the Windows command prompt, as a Unix
+ * shell does (CAMEL-26930). It failed with "No routes found in: *".
+ */
+class RunWildcardTest {
+
+    @TempDir
+    Path dir;
+
+    private static Run run(String... args) {
+        Run command = new Run(new CamelJBangMain());
+        CommandLineTestHelper.parse(command, args);
+        return command;
+    }
+
+    @Test
+    void aStarIsTheFilesOfTheFolder() throws Exception {
+        Files.writeString(dir.resolve("b.camel.yaml"), "- route:\n");
+        Files.writeString(dir.resolve("a.camel.yaml"), "- route:\n");
+        Files.writeString(dir.resolve("application.properties"), "");
+        Files.writeString(dir.resolve(".hidden.yaml"), "");
+
+        Run run = run(dir + "/*");
+        run.expandWildcards();
+
+        // sorted as a shell does, without the hidden file
+        assertThat(run.files).containsExactly(dir + "/a.camel.yaml", dir + 
"/application.properties",
+                dir + "/b.camel.yaml");
+    }
+
+    @Test
+    void aPatternIsTheFilesItMatches() throws Exception {
+        Files.writeString(dir.resolve("a.camel.yaml"), "- route:\n");
+        Files.writeString(dir.resolve("b.camel.xml"), "<routes/>");
+
+        Run run = run(dir + "/*.yaml", dir + "/b.camel.xm?");
+        run.expandWildcards();
+
+        assertThat(run.files).containsExactly(dir + "/a.camel.yaml", dir + 
"/b.camel.xml");
+    }
+
+    @Test
+    void otherFilesAreLeftAsGiven() throws Exception {
+        Files.writeString(dir.resolve("a.camel.yaml"), "- route:\n");
+
+        Run run = run(dir + "/a.camel.yaml", 
"github:apache:camel-kamelets-examples:jbang/*", "file:" + dir + "/*");
+        run.expandWildcards();
+
+        assertThat(run.files).containsExactly(dir + "/a.camel.yaml", 
"github:apache:camel-kamelets-examples:jbang/*",
+                "file:" + dir + "/*");
+    }
+
+    @Test
+    void aWildcardThatMatchesNothingIsAFileNotFound() {
+        Run run = run(dir + "/*.yaml");
+
+        assertThatThrownBy(run::expandWildcards)
+                .isInstanceOfSatisfying(CliException.class,
+                        e -> 
assertThat(e.getExitCode()).isEqualTo(CliExitCode.FILE_NOT_FOUND))
+                .hasMessage("No file matches: " + dir + "/*.yaml");
+    }
+}

Reply via email to