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

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


The following commit(s) were added to refs/heads/main by this push:
     new cbc3a78bbd Fix Move files select "Do Nothing" #5790 (#5815)
cbc3a78bbd is described below

commit cbc3a78bbd3431de52e3cc483664d28c753ba617
Author: lance <[email protected]>
AuthorDate: Thu Oct 23 19:18:12 2025 +0800

    Fix Move files select "Do Nothing" #5790 (#5815)
    
    * Move files: Selecting Do Nothing when destination ecists prevents move 
and may create a folder names as the target file
    
    Signed-off-by: lance <[email protected]>
    
    * fix checkstyle AvoidStarImport
    
    Signed-off-by: lance <[email protected]>
    
    * Fix movefiles if file is exist
    
    Signed-off-by: lance <[email protected]>
    
    ---------
    
    Signed-off-by: lance <[email protected]>
---
 .../actions/movefiles/ActionMoveFiles.java         | 26 +++++++++++++++++++++-
 .../WorkflowActionMoveFilesLocalTest.java          | 21 +++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git 
a/plugins/actions/movefiles/src/main/java/org/apache/hop/workflow/actions/movefiles/ActionMoveFiles.java
 
b/plugins/actions/movefiles/src/main/java/org/apache/hop/workflow/actions/movefiles/ActionMoveFiles.java
index 815d03b4b3..5f9ff00b3b 100644
--- 
a/plugins/actions/movefiles/src/main/java/org/apache/hop/workflow/actions/movefiles/ActionMoveFiles.java
+++ 
b/plugins/actions/movefiles/src/main/java/org/apache/hop/workflow/actions/movefiles/ActionMoveFiles.java
@@ -26,6 +26,7 @@ import java.util.regex.Pattern;
 import org.apache.commons.vfs2.AllFileSelector;
 import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSelectInfo;
+import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileType;
 import org.apache.hop.core.Const;
 import org.apache.hop.core.ICheckResult;
@@ -612,7 +613,7 @@ public class ActionMoveFiles extends ActionBase implements 
Cloneable, IAction {
               FileObject destinationfile =
                   HopVfs.getFileObject(destinationfilenamefull, 
getVariables());
 
-              destinationfile.createFolder();
+              createFolderIfNotExists(destinationfilefolder);
 
               entrystatus =
                   moveFile(
@@ -1019,6 +1020,8 @@ public class ActionMoveFiles extends ActionBase 
implements Cloneable, IAction {
         } else if (ifFileExists.equals("fail")) {
           // Update Errors
           updateErrors();
+        } else if (ifFileExists.equals(CONST_DO_NOTHING)) {
+          retval = true;
         }
       }
     } catch (Exception e) {
@@ -1561,4 +1564,25 @@ public class ActionMoveFiles extends ActionBase 
implements Cloneable, IAction {
   public boolean isEvaluation() {
     return true;
   }
+
+  /**
+   * Ensures that the given FileObject represents an existing folder.
+   *
+   * @param folder the FileObject representing the target folder
+   * @throws FileSystemException if the path exists as a file or the folder 
creation fails
+   */
+  private void createFolderIfNotExists(FileObject folder) throws 
FileSystemException {
+    // If the path already exists, it's a folder (directory)
+    if (folder.exists() && folder.getType().hasChildren()) {
+      return;
+    }
+
+    // Ensure parent folder exists before creating this one
+    FileObject parent = folder.getParent();
+    if (parent != null && !parent.exists()) {
+      parent.createFolder();
+    }
+
+    folder.createFolder();
+  }
 }
diff --git 
a/plugins/actions/movefiles/src/test/java/org/apache/hop/workflow/actions/movefiles/WorkflowActionMoveFilesLocalTest.java
 
b/plugins/actions/movefiles/src/test/java/org/apache/hop/workflow/actions/movefiles/WorkflowActionMoveFilesLocalTest.java
index bbe5b6ae10..4282ac80d5 100644
--- 
a/plugins/actions/movefiles/src/test/java/org/apache/hop/workflow/actions/movefiles/WorkflowActionMoveFilesLocalTest.java
+++ 
b/plugins/actions/movefiles/src/test/java/org/apache/hop/workflow/actions/movefiles/WorkflowActionMoveFilesLocalTest.java
@@ -106,6 +106,7 @@ class WorkflowActionMoveFilesLocalTest {
     action.sourceFileFolder = new String[] {sourceFile.toString()};
     action.destinationFileFolder = new String[] {destFile.toString()};
     action.setDestinationIsAFile(true);
+    action.setIfFileExists("fail");
 
     Result result = action.execute(new Result(), 0);
     assertFalse(result.isResult(), "Move should not succeed when destination 
exists");
@@ -154,6 +155,26 @@ class WorkflowActionMoveFilesLocalTest {
     assertTrue(Files.exists(destFile), "File should be moved");
   }
 
+  @Test
+  void testMoveWithOverwriteFileExist() throws IOException, HopException {
+    Path sourceFile = createTestFilePath(sourceFolder, "test.txt");
+    Path destFile = createTestFilePath(destinationFolder, "test.txt");
+
+    action.sourceFileFolder = new String[] {sourceFile.toString()};
+    action.destinationFileFolder = new String[] {destinationFolder.toString()};
+    action.setDestinationIsAFile(true);
+    action.setIfFileExists("overwrite_file");
+
+    Result result = action.execute(new Result(), 0);
+    assertTrue(result.getResult(), "Move with overwrite should succeed");
+    assertFalse(Files.exists(sourceFile), "Source file should not exist");
+    assertTrue(Files.exists(destFile), "Destination file should exist");
+    assertEquals(
+        TEST_FILE_CONTENT,
+        Files.readString(destFile, StandardCharsets.UTF_8),
+        "File content should match source");
+  }
+
   private Path createTestFilePath(File folder, String filename) throws 
IOException {
     Path filePath = folder.toPath().resolve(filename);
     Files.writeString(filePath, TEST_FILE_CONTENT, StandardCharsets.UTF_8);

Reply via email to