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 4af16efa597 CAMEL-21082 - do not delete existing file sin target 
folder when (#15159)
4af16efa597 is described below

commit 4af16efa597039c14930b15d0c7d2480115b1588
Author: AurĂ©lien Pupier <[email protected]>
AuthorDate: Sat Aug 17 08:49:28 2024 +0200

    CAMEL-21082 - do not delete existing file sin target folder when (#15159)
    
    creating a new Camel integration files with Camel JBang init
    
    Signed-off-by: AurĂ©lien Pupier <[email protected]>
---
 .../modules/ROOT/pages/camel-jbang.adoc            |  6 --
 .../apache/camel/dsl/jbang/core/commands/Init.java |  5 +-
 .../camel/dsl/jbang/core/commands/InitTest.java    | 88 ++++++++++++++++++++++
 3 files changed, 90 insertions(+), 9 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 452bd843d67..6b3c07fbb50 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -144,8 +144,6 @@ named _foobar_ you can do:
 camel init foo.java --directory=foobar
 ----
 
-IMPORTANT: When using `--directory` then Camel will automatically clean this 
directory if already exists.
-
 === Running Routes from multiple files
 
 You can run more than 1 file, for example to run two YAML files you can do:
@@ -820,8 +818,6 @@ you would do:
 camel init 
https://github.com/apache/camel-kamelets-examples/tree/main/jbang/dependency-injection
 --directory=myproject
 ----
 
-IMPORTANT: When using `--directory` then Camel will automatically clean this 
directory if already exists.
-
 You can also run in dev mode, to hot-deploy on source code changes.
 
 [source,bash]
@@ -867,8 +863,6 @@ you would do:
 camel init https://gist.github.com/davsclaus/477ddff5cdeb1ae03619aa544ce47e92 
--directory=foobar
 ----
 
-IMPORTANT: When using `--directory` then Camel will automatically clean this 
directory if already exists.
-
 === Using a specific Camel version
 
 You can specify which Camel version to run as shown:
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
index 2c03f571d75..80bf49c3f9f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Init.java
@@ -54,7 +54,8 @@ public class Init extends CamelCommand {
 
     @Option(names = {
             "--dir",
-            "--directory" }, description = "Directory where the project will 
be saved", defaultValue = ".")
+            "--directory" }, description = "Directory relative path where the 
new Camel integration will be saved",
+            defaultValue = ".")
     private String directory;
 
     @Option(names = { "--from-kamelet" },
@@ -146,8 +147,6 @@ public class Init extends CamelCommand {
 
         if (!directory.equals(".")) {
             File dir = new File(directory);
-            CommandHelper.cleanExportDir(directory);
-            // ensure target dir is created after clean
             dir.mkdirs();
         }
         File target = new File(directory, file);
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
new file mode 100644
index 00000000000..215be781fee
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/InitTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.camel.util.FileUtil;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import picocli.CommandLine;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class InitTest {
+
+    private File workingDir;
+
+    @BeforeEach
+    public void beforeEach() throws IOException {
+        workingDir = Files.createTempDirectory("camel-init").toFile();
+    }
+
+    @AfterEach
+    public void afterEach() {
+        FileUtil.removeDir(workingDir);
+    }
+
+    @Test
+    void initBasicYaml() throws Exception {
+        Init initCommand = new Init(new CamelJBangMain());
+        CommandLine.populateCommand(initCommand, "my.camel.yaml");
+
+        int exit = initCommand.doCall();
+
+        assertEquals(0, exit);
+        File f = new File("my.camel.yaml");
+        assertTrue(f.exists(), "Yaml file not created: " + f);
+        f.delete();
+    }
+
+    @Test
+    void initYamlInDirectory() throws Exception {
+        Init initCommand = new Init(new CamelJBangMain());
+        CommandLine.populateCommand(initCommand, "my.camel.yaml", "--dir=" + 
workingDir);
+
+        int exit = initCommand.doCall();
+
+        assertEquals(0, exit);
+        File f = new File(workingDir, "my.camel.yaml");
+        assertTrue(f.exists(), "Yaml file not created: " + f);
+    }
+
+    @Test
+    void initYamlInDirectoryWithExistingFiles() throws Exception {
+        Path existingFileInTargetDirectory = Files.createFile(new 
File(workingDir, "anotherfile.txt").toPath());
+        assertTrue(existingFileInTargetDirectory.toFile().exists(), "Cannot 
create file to setup context");
+
+        Init initCommand = new Init(new CamelJBangMain());
+        CommandLine.populateCommand(initCommand, "my.camel.yaml", "--dir=" + 
workingDir);
+
+        int exit = initCommand.doCall();
+
+        assertEquals(0, exit);
+        File f = new File(workingDir, "my.camel.yaml");
+        assertTrue(f.exists(), "Yaml file not created: " + f);
+
+        assertTrue(existingFileInTargetDirectory.toFile().exists(), "The file 
in the target folder has been deleted");
+    }
+}

Reply via email to