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 5cd9924fa53c CAMEL-26933: camel-jbang-plugin-test - camel test init 
does not overwrite a test, unless --force (#29922)
5cd9924fa53c is described below

commit 5cd9924fa53c431a10c34b440ee0df72c1ea4d89
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 09:58:44 2026 +0100

    CAMEL-26933: camel-jbang-plugin-test - camel test init does not overwrite a 
test, unless --force (#29922)
    
    Item 2 of [CAMEL-26933](https://issues.apache.org/jira/browse/CAMEL-26933).
    
    ## Problem
    
    `camel test init` (`TestInit`, which extends the Citrus `Init`) has no 
existence check and no `--force`. Running it again silently replaces an edited 
test: the Citrus `Init` writes the template with `Files.writeString`. `camel 
init` refuses with "File already exists: ... (use --force to overwrite it)".
    
    ## Change
    
    - `TestInit.call()` refuses when the test file exists: "Error: Test already 
exists: <file> (use --force to overwrite it)", exit 1. The wording follows 
`camel init`, in the `Error:` style of the Citrus init messages.
    - A new `--force` option overwrites, as before.
    - The file is worked out the way the Citrus `Init` does: its file argument 
in `resolveTargetDirectory(--directory)`. The file argument goes through a 
Citrus parameter consumer into the private field `file` of `Init`, so it is 
read from there. If a future Citrus version no longer has that field, there is 
no check, and the command behaves as before rather than failing.
    
    ## Tests
    
    - `TestPluginTest.initDoesNotOverwriteATest`: init, edit the test, init 
again (refused, the edit stays, the message is printed), then `--force` 
(overwritten). It runs the `camel test init` command line, as the other tests 
of the class do.
    - camel-jbang-plugin-test: 7 tests pass.
    
    Item 8 of the JIRA (two tests with the same base name, 
`TestPluginExporter`) waits for #29857, which changes `TestPluginExporter`.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../dsl/jbang/core/commands/test/TestInit.java     | 43 ++++++++++++++++++++++
 .../jbang/core/commands/test/TestPluginTest.java   | 20 ++++++++++
 2 files changed, 63 insertions(+)

diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestInit.java
 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestInit.java
index f068034ac829..afba569be356 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestInit.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestInit.java
@@ -19,7 +19,9 @@ package org.apache.camel.dsl.jbang.core.commands.test;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.lang.reflect.Field;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.nio.file.Path;
 
 import org.apache.camel.catalog.VersionHelper;
@@ -38,10 +40,51 @@ import picocli.CommandLine;
 @CommandLine.Command(name = "init", description = "Creates a new Citrus test")
 public class TestInit extends Init {
 
+    @CommandLine.Option(names = { "--force" }, description = "Overwrite a test 
that exists already", defaultValue = "false")
+    boolean force;
+
+    @CommandLine.Spec
+    CommandLine.Model.CommandSpec spec;
+
     public TestInit(CitrusJBangMain citrus) {
         super(citrus);
     }
 
+    @Override
+    public Integer call() throws Exception {
+        // the Citrus init writes the test over one that exists, so an edited 
test was lost (CAMEL-26933)
+        Path target = targetFile();
+        System.err.println("DEBUG26933 spec=" + (spec != null) + " target=" + 
target + " pos="
+                           + (spec != null ? spec.positionalParameters() : 
null));
+        if (target != null && Files.exists(target) && !force) {
+            printer().println("Error: Test already exists: %s (use --force to 
overwrite it)".formatted(target));
+            return 1;
+        }
+        return super.call();
+    }
+
+    /**
+     * The file the Citrus init writes: the file argument in the target 
directory, or null when it is not known.
+     */
+    Path targetFile() {
+        // the file argument goes through a parameter consumer of Citrus into 
its private field file
+        String file;
+        try {
+            Field field = Init.class.getDeclaredField("file");
+            field.setAccessible(true);
+            file = (String) field.get(this);
+        } catch (ReflectiveOperationException | RuntimeException e) {
+            // another Citrus version: no check, as before
+            return null;
+        }
+        if (file == null) {
+            return null;
+        }
+        CommandLine.Model.OptionSpec option = spec != null ? 
spec.findOption("--directory") : null;
+        String directory = option != null ? option.getValue() : null;
+        return Path.of(resolveTargetDirectory(directory)).resolve(file);
+    }
+
     @Override
     protected String resolveTargetDirectory(String directory) {
         if (directory == null || ".".equals(directory)) {
diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
index d85e7178e70f..211de06d226d 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
@@ -33,6 +33,7 @@ import org.citrusframework.spi.Resources;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 class TestPluginTest {
 
@@ -68,6 +69,25 @@ class TestPluginTest {
         
Assertions.assertTrue(targetDir.resolve("sample.citrus.it.yaml").toFile().exists());
     }
 
+    @Test
+    public void initDoesNotOverwriteATest(@TempDir Path dir) throws Exception {
+        // camel test init replaced an edited test without a word (CAMEL-26933)
+        CamelJBangMain camelJBangMain = createCamelJBangMain();
+        camelJBangMain.execute("test", "init", "edited.citrus.it.yaml", 
"--directory", dir.toString());
+        Path test = dir.resolve("edited.citrus.it.yaml");
+        Files.writeString(test, "name: my edited test\n");
+
+        camelJBangMain.execute("test", "init", "edited.citrus.it.yaml", 
"--directory", dir.toString());
+
+        Assertions.assertEquals("name: my edited test\n", 
Files.readString(test));
+        Assertions.assertTrue(printer.getOutput().contains("Test already 
exists: " + test + " (use --force to overwrite it)"),
+                printer.getOutput());
+
+        camelJBangMain.execute("test", "init", "edited.citrus.it.yaml", 
"--directory", dir.toString(), "--force");
+
+        Assertions.assertNotEquals("name: my edited test\n", 
Files.readString(test));
+    }
+
     @Test
     public void shouldInitXmlTest() {
         Path targetDir = 
CommandLineHelper.getHomeDir().toAbsolutePath().resolve(TestPlugin.TEST_DIR);

Reply via email to