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

bamaer 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 7b976ba9d8 Fix#9264  Add filenames invalid regex and Delete folders 
error-limit boundary (#9357)
7b976ba9d8 is described below

commit 7b976ba9d83b35e790f2b0f1b294afb98dfe6a52
Author: Lance <[email protected]>
AuthorDate: Sun Nov 8 03:20:24 2026 +0800

    Fix#9264  Add filenames invalid regex and Delete folders error-limit 
boundary (#9357)
    
    * Fix Add filenames invalid regex and Delete folders error-limit boundary
    
    Signed-off-by: leehaut <[email protected]>
    
    * Fix Add filenames invalid regex and Delete folders error-limit boundary
    
    Signed-off-by: leehaut <[email protected]>
    
    ---------
    
    Signed-off-by: leehaut <[email protected]>
---
 .../pages/workflow/actions/addresultfilenames.adoc |   8 +-
 .../ROOT/pages/workflow/actions/deletefolders.adoc |   2 +-
 .../ActionAddResultFilenames.java                  | 203 ++++++++++-----------
 .../ActionAddResultFilenamesResultTest.java        | 163 +++++++++++++++++
 .../actions/deletefolders/ActionDeleteFolders.java | 181 +++++++++---------
 .../ActionDeleteFoldersResultTest.java             | 127 +++++++++++++
 6 files changed, 485 insertions(+), 199 deletions(-)

diff --git 
a/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/addresultfilenames.adoc
 
b/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/addresultfilenames.adoc
index 5ffe9f251a..48076fa8d5 100644
--- 
a/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/addresultfilenames.adoc
+++ 
b/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/addresultfilenames.adoc
@@ -37,7 +37,7 @@ This list of filenames can then be used in the various 
workflow actions all arou
 |Clear result filenames|This option clears the list of result files (from 
previous workflow actions) before creating a new list.
 |File/Folder|Specify the list of files or folders with wildcards (regular 
expressions) in this grid. +
 You can add a different file or folder and wildcard on each line.
-|Wildcard (RegExp)|A regular expression wildcard to include file patterns in 
the list of file names.
+|Wildcard (RegExp)|A regular expression matched against the file name 
(basename) of each file in a folder. Ignored for file entries.
 |===
 
 After specifying a folder and wildcard (regular expression), use the `Add` 
button to add your file/folder selection to the table. Use the `Delete` or 
`Edit` buttons to remove or edit selected lines in the table.
@@ -49,10 +49,10 @@ What this action passes on to the next action. See 
xref:workflow/action-results.
 [options="header", width="90%", cols="1,3"]
 |===
 |Result|Value
-|True|Every file and folder in the list exists. A folder without matching 
files adds nothing and is still true.
-|False|A file or folder in the list doesn't exist. The number of errors is the 
number of entries that don't exist. The files of the other entries are still 
added.
+|True|Every file and folder in the list exists, and the *Wildcard (RegExp)* of 
every folder is a valid regular expression. A folder without matching files 
adds nothing and is still true.
+|False|A file or folder in the list doesn't exist, or the *Wildcard (RegExp)* 
of a folder is an invalid regular expression. The number of errors is the 
number of entries that failed. The files of the other entries are still added.
 |Result files|Each file in the list is added. For a folder, the files in it 
whose whole file name, without the folder, matches *Wildcard (RegExp)* are 
added, or all its files when the wildcard is empty, and with *Include 
Subfolders* also the matching files in its subfolders. With *Clear result 
filenames*, the result files of the previous action are removed first; 
otherwise they are kept.
 |Result rows|Passed on unchanged. With *Copy previous results to args?*, the 
file or folder comes from the first field and the wildcard from the second 
field of each result row, and the list in the dialog is not used, also when 
there are no result rows. A result row with only one field adds all the files 
of that folder, as with an empty wildcard.
 |===
 
-NOTE: An invalid regular expression in *Wildcard (RegExp)* adds nothing, and 
the action is still true.
+NOTE: An invalid regular expression in *Wildcard (RegExp)* of a folder makes 
that entry fail (the action is false), and no files from that folder are added. 
A file entry ignores the wildcard.
diff --git 
a/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/deletefolders.adoc 
b/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/deletefolders.adoc
index 50a8e2745c..9e44b98ecd 100644
--- 
a/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/deletefolders.adoc
+++ 
b/docs/hop-user-manual/modules/ROOT/pages/workflow/actions/deletefolders.adoc
@@ -54,4 +54,4 @@ What this action passes on to the next action. See 
xref:workflow/action-results.
 |Lines deleted|The number of folders processed: deleted, or already missing.
 |===
 
-NOTE: With *When number of errors less than*, the action stops and is false as 
soon as the number of errors reaches *Limit*, but when the last folder brings 
it to that number, the action is still true.
+NOTE: With *When number of errors less than*, the action stops and is false as 
soon as the number of errors reaches *Limit* (strictly fewer errors than the 
limit is required for success).
diff --git 
a/plugins/actions/addresultfilenames/src/main/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenames.java
 
b/plugins/actions/addresultfilenames/src/main/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenames.java
index 21df95d807..7ddc36420b 100644
--- 
a/plugins/actions/addresultfilenames/src/main/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenames.java
+++ 
b/plugins/actions/addresultfilenames/src/main/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenames.java
@@ -170,149 +170,142 @@ public class ActionAddResultFilenames extends 
ActionBase implements Cloneable, I
       IWorkflowEngine<WorkflowMeta> parentWorkflow,
       Result result) {
 
-    boolean rcode = true;
+    String realFileFolderName = resolve(filename);
+    String realWildcard = resolve(wildcard);
     FileObject filefolder = null;
-    String realFilefoldername = resolve(filename);
-    String realwildcard = resolve(wildcard);
 
     try {
-      filefolder = HopVfs.getFileObject(realFilefoldername, getVariables());
-      if (filefolder.exists()) {
-        // the file or folder exists
-
-        if (filefolder.getType() == FileType.FILE) {
-          // Add filename to Resultfilenames ...
-          if (isDetailed()) {
-            logDetailed(
-                BaseMessages.getString(
-                    PKG, "ActionAddResultFilenames.AddingFileToResult", 
filefolder.toString()));
-          }
-          ResultFile resultFile =
-              new ResultFile(
-                  ResultFile.FILE_TYPE_GENERAL,
-                  HopVfs.getFileObject(filefolder.toString(), getVariables()),
-                  parentWorkflow.getWorkflowName(),
-                  toString());
-          result.getResultFiles().put(resultFile.getFile().toString(), 
resultFile);
-        } else {
-          FileObject[] list =
-              filefolder.findFiles(new TextFileSelector(filefolder.toString(), 
realwildcard));
-
-          for (int i = 0; i < list.length && !parentWorkflow.isStopped(); i++) 
{
-            // Add filename to Resultfilenames ...
-            if (isDetailed()) {
-              logDetailed(
-                  BaseMessages.getString(
-                      PKG, "ActionAddResultFilenames.AddingFileToResult", 
list[i].toString()));
-            }
-            ResultFile resultFile =
-                new ResultFile(
-                    ResultFile.FILE_TYPE_GENERAL,
-                    HopVfs.getFileObject(list[i].toString(), getVariables()),
-                    parentWorkflow.getWorkflowName(),
-                    toString());
-            result.getResultFiles().put(resultFile.getFile().toString(), 
resultFile);
-          }
-        }
-
-      } else {
-        // File can not be found
+      filefolder = HopVfs.getFileObject(realFileFolderName, getVariables());
+      if (!filefolder.exists()) {
         if (isBasic()) {
           logBasic(
               BaseMessages.getString(
-                  PKG, "ActionAddResultFilenames.FileCanNotbeFound", 
realFilefoldername));
+                  PKG, "ActionAddResultFilenames.FileCanNotbeFound", 
realFileFolderName));
         }
-        rcode = false;
+        return false;
+      }
+      if (filefolder.getType() == FileType.FILE) {
+        // Wildcard applies only to folders; a file entry is added as-is.
+        addToResultFiles(filefolder.toString(), parentWorkflow, result);
+      } else {
+        // Compile once so an invalid mask fails the folder entry (issue 
#9264),
+        // instead of being swallowed while selecting each file.
+        addMatchingFilesFromFolder(
+            filefolder, compileWildcard(realWildcard), parentWorkflow, result);
       }
+      return true;
     } catch (Exception e) {
-      rcode = false;
       logError(
           BaseMessages.getString(
-              PKG, "ActionAddResultFilenames.CouldNotProcess", 
realFilefoldername, e.getMessage()),
+              PKG, "ActionAddResultFilenames.CouldNotProcess", 
realFileFolderName, e.getMessage()),
           e);
+      return false;
     } finally {
-      if (filefolder != null) {
-        try {
-          filefolder.close();
-          filefolder = null;
-        } catch (IOException ex) {
-          // Ignore
-        }
-      }
+      closeQuietly(filefolder);
     }
+  }
 
-    return rcode;
+  private void addMatchingFilesFromFolder(
+      FileObject folder,
+      Pattern compiledWildcard,
+      IWorkflowEngine<WorkflowMeta> parentWorkflow,
+      Result result)
+      throws Exception {
+    FileObject[] list = folder.findFiles(new 
TextFileSelector(folder.toString(), compiledWildcard));
+    for (int i = 0; i < list.length && !parentWorkflow.isStopped(); i++) {
+      addToResultFiles(list[i].toString(), parentWorkflow, result);
+    }
   }
 
-  private class TextFileSelector implements FileSelector {
-    String fileWildcard = null;
-    String sourceFolder = null;
+  private void addToResultFiles(
+      String filePath, IWorkflowEngine<WorkflowMeta> parentWorkflow, Result 
result)
+      throws Exception {
+    if (isDetailed()) {
+      logDetailed(
+          BaseMessages.getString(PKG, 
"ActionAddResultFilenames.AddingFileToResult", filePath));
+    }
+    ResultFile resultFile =
+        new ResultFile(
+            ResultFile.FILE_TYPE_GENERAL,
+            HopVfs.getFileObject(filePath, getVariables()),
+            parentWorkflow.getWorkflowName(),
+            toString());
+    result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
+  }
 
-    public TextFileSelector(String sourcefolderin, String filewildcard) {
-      if (!Utils.isEmpty(sourcefolderin)) {
-        sourceFolder = sourcefolderin;
-      }
+  private static void closeQuietly(FileObject fileFolder) {
+    if (fileFolder == null) {
+      return;
+    }
+    try {
+      fileFolder.close();
+    } catch (IOException ex) {
+      // Ignore
+    }
+  }
 
-      if (!Utils.isEmpty(filewildcard)) {
-        fileWildcard = filewildcard;
-      }
+  /**
+   * Compile the folder file-mask regex once. An invalid expression must fail 
that folder entry
+   * instead of being swallowed while selecting files. Not used for file 
entries.
+   */
+  private static Pattern compileWildcard(String wildcard) {
+    if (Utils.isEmpty(wildcard)) {
+      return null;
+    }
+    return Pattern.compile(wildcard);
+  }
+
+  private class TextFileSelector implements FileSelector {
+    private final Pattern fileWildcard;
+    private final String sourceFolder;
+
+    TextFileSelector(String sourceFolder, Pattern fileWildcard) {
+      this.sourceFolder = sourceFolder;
+      this.fileWildcard = fileWildcard;
     }
 
     @Override
     public boolean includeFile(FileSelectInfo info) {
-      boolean returncode = false;
       try {
-        if (!info.getFile().toString().equals(sourceFolder)) {
-          // Pass over the Base folder itself
-          String shortFilename = info.getFile().getName().getBaseName();
-
-          if (info.getFile().getParent().equals(info.getBaseFolder())
-              || (!info.getFile().getParent().equals(info.getBaseFolder()) && 
includeSubFolders)) {
-            if ((info.getFile().getType() == FileType.FILE && fileWildcard == 
null)
-                || (info.getFile().getType() == FileType.FILE
-                    && fileWildcard != null
-                    && GetFileWildcard(shortFilename, fileWildcard))) {
-              returncode = true;
-            }
-          }
+        if (info.getFile().toString().equals(sourceFolder)) {
+          return false;
+        }
+        if (info.getFile().getType() != FileType.FILE) {
+          return false;
+        }
+        if (!isInScope(info)) {
+          return false;
         }
+        return matchesWildcard(info.getFile().getName().getBaseName());
       } catch (Exception e) {
         logError(
             "Error while finding files ... in ["
                 + info.getFile().toString()
                 + "]. Exception :"
                 + e.getMessage());
-        returncode = false;
+        return false;
       }
-      return returncode;
     }
 
-    @Override
-    public boolean traverseDescendents(FileSelectInfo info) {
-      return info.getDepth() == 0 || includeSubFolders;
+    private boolean isInScope(FileSelectInfo info) throws Exception {
+      if (info.getFile().getParent().equals(info.getBaseFolder())) {
+        return true;
+      }
+      return includeSubFolders;
     }
-  }
 
-  /**********************************************************
-   *
-   * @param selectedfile
-   * @param wildcard
-   * @return True if the selectedfile matches the wildcard
-   **********************************************************/
-  private boolean GetFileWildcard(String selectedfile, String wildcard) {
-    Pattern pattern = null;
-    boolean getIt = true;
-
-    if (!Utils.isEmpty(wildcard)) {
-      pattern = Pattern.compile(wildcard);
-      // First see if the file matches the regular expression!
-      if (pattern != null) {
-        Matcher matcher = pattern.matcher(selectedfile);
-        getIt = matcher.matches();
+    private boolean matchesWildcard(String shortFilename) {
+      if (fileWildcard == null) {
+        return true;
       }
+      Matcher matcher = fileWildcard.matcher(shortFilename);
+      return matcher.matches();
     }
 
-    return getIt;
+    @Override
+    public boolean traverseDescendents(FileSelectInfo info) {
+      return info.getDepth() == 0 || includeSubFolders;
+    }
   }
 
   public void setIncludeSubFolders(boolean includeSubFolders) {
@@ -346,7 +339,7 @@ public class ActionAddResultFilenames extends ActionBase 
implements Cloneable, I
                 remarks,
                 
AndValidator.putValidators(ActionValidatorUtils.notNullValidator()));
 
-    if (res == false) {
+    if (!res) {
       return;
     }
 
diff --git 
a/plugins/actions/addresultfilenames/src/test/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenamesResultTest.java
 
b/plugins/actions/addresultfilenames/src/test/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenamesResultTest.java
new file mode 100644
index 0000000000..59f2cf4cfe
--- /dev/null
+++ 
b/plugins/actions/addresultfilenames/src/test/java/org/apache/hop/workflow/actions/addresultfilenames/ActionAddResultFilenamesResultTest.java
@@ -0,0 +1,163 @@
+/*
+ * 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.hop.workflow.actions.addresultfilenames;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import org.apache.hop.core.Result;
+import org.apache.hop.core.RowMetaAndData;
+import org.apache.hop.core.logging.HopLogStore;
+import org.apache.hop.core.row.value.ValueMetaString;
+import org.apache.hop.workflow.WorkflowMeta;
+import org.apache.hop.workflow.action.ActionMeta;
+import org.apache.hop.workflow.engine.IWorkflowEngine;
+import org.apache.hop.workflow.engines.local.LocalWorkflowEngine;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/** Result behaviour for Add filenames to result (issue #9264). */
+class ActionAddResultFilenamesResultTest {
+
+  @TempDir Path testFolder;
+
+  private ActionAddResultFilenames action;
+
+  @BeforeAll
+  static void setUpBeforeClass() {
+    HopLogStore.init();
+  }
+
+  @BeforeEach
+  void setUp() {
+    IWorkflowEngine<WorkflowMeta> workflow = new LocalWorkflowEngine(new 
WorkflowMeta());
+    action = new ActionAddResultFilenames();
+    workflow.getWorkflowMeta().addAction(new ActionMeta(action));
+    action.setParentWorkflow(workflow);
+    action.setParentWorkflowMeta(mock(WorkflowMeta.class));
+    workflow.setStopped(false);
+    action.setArgFromPrevious(false);
+    action.setDeleteAllBefore(true);
+    action.setIncludeSubFolders(false);
+  }
+
+  /** #9264: invalid regex on a folder must fail the action, not succeed with 
zero result files. */
+  @Test
+  void invalidRegexWildcardOnFolderFailsTheAction() throws Exception {
+    Path folder = Files.createDirectories(testFolder.resolve("ar"));
+    Files.writeString(folder.resolve("a.txt"), "a", StandardCharsets.UTF_8);
+    Files.writeString(folder.resolve("b.log"), "b", StandardCharsets.UTF_8);
+
+    action.setArguments(List.of(new Argument(folder.toString(), "([")));
+
+    Result result = action.execute(new Result(), 0);
+
+    assertFalse(result.isResult(), "invalid regex must not succeed");
+    assertTrue(result.getNrErrors() > 0);
+    assertEquals(0, result.getResultFiles().size(), "no files must be added 
for an invalid mask");
+  }
+
+  @Test
+  void validRegexAddsMatchingFiles() throws Exception {
+    Path folder = Files.createDirectories(testFolder.resolve("ar"));
+    Files.writeString(folder.resolve("a.txt"), "a", StandardCharsets.UTF_8);
+    Files.writeString(folder.resolve("b.log"), "b", StandardCharsets.UTF_8);
+
+    action.setArguments(List.of(new Argument(folder.toString(), ".*\\.txt")));
+
+    Result result = action.execute(new Result(), 0);
+
+    assertTrue(result.isResult());
+    assertEquals(0, result.getNrErrors());
+    assertEquals(1, result.getResultFiles().size());
+    assertTrue(
+        result.getResultFiles().keySet().stream().anyMatch(k -> 
k.endsWith("a.txt")),
+        "only a.txt should match");
+  }
+
+  /**
+   * Wildcard only applies to folders. A file entry with a non-regex mask 
(e.g. {@code *.txt}) must
+   * still be added, matching Delete files and main-branch behaviour.
+   */
+  @Test
+  void fileEntryIgnoresNonRegexWildcard() throws Exception {
+    Path file = testFolder.resolve("a.txt");
+    Files.writeString(file, "a", StandardCharsets.UTF_8);
+
+    action.setArguments(List.of(new Argument(file.toString(), "*.txt")));
+
+    Result result = action.execute(new Result(), 0);
+
+    assertTrue(result.isResult(), "file entry must succeed even with a 
non-regex wildcard");
+    assertEquals(0, result.getNrErrors());
+    assertEquals(1, result.getResultFiles().size());
+    assertTrue(
+        result.getResultFiles().keySet().stream().anyMatch(k -> 
k.endsWith("a.txt")),
+        "the file itself must be added");
+  }
+
+  /** Same rule when *Copy previous results to args?* supplies path + wildcard 
from a result row. */
+  @Test
+  void previousResultFileRowIgnoresNonRegexWildcard() throws Exception {
+    Path file = testFolder.resolve("from-prev.txt");
+    Files.writeString(file, "x", StandardCharsets.UTF_8);
+
+    RowMetaAndData row = new RowMetaAndData();
+    row.addValue(new ValueMetaString("path"), file.toString());
+    row.addValue(new ValueMetaString("wildcard"), "*.txt");
+    Result previous = new Result();
+    previous.setRows(List.of(row));
+    action.setArgFromPrevious(true);
+
+    Result result = action.execute(previous, 0);
+
+    assertTrue(result.isResult(), "previous-result file row must ignore 
non-regex wildcard");
+    assertEquals(0, result.getNrErrors());
+    assertEquals(1, result.getResultFiles().size());
+    assertTrue(
+        result.getResultFiles().keySet().stream().anyMatch(k -> 
k.endsWith("from-prev.txt")));
+  }
+
+  /** Invalid regex on a previous-result folder row must still fail that 
entry. */
+  @Test
+  void previousResultFolderRowWithInvalidRegexFails() throws Exception {
+    Path folder = Files.createDirectories(testFolder.resolve("prev-folder"));
+    Files.writeString(folder.resolve("a.txt"), "a", StandardCharsets.UTF_8);
+
+    RowMetaAndData row = new RowMetaAndData();
+    row.addValue(new ValueMetaString("path"), folder.toString());
+    row.addValue(new ValueMetaString("wildcard"), "([");
+    Result previous = new Result();
+    previous.setRows(List.of(row));
+    action.setArgFromPrevious(true);
+
+    Result result = action.execute(previous, 0);
+
+    assertFalse(result.isResult(), "invalid regex on a folder row must fail");
+    assertTrue(result.getNrErrors() > 0);
+    assertEquals(0, result.getResultFiles().size());
+  }
+}
diff --git 
a/plugins/actions/deletefolders/src/main/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFolders.java
 
b/plugins/actions/deletefolders/src/main/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFolders.java
index 91545f1ffd..546c9cc7ba 100644
--- 
a/plugins/actions/deletefolders/src/main/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFolders.java
+++ 
b/plugins/actions/deletefolders/src/main/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFolders.java
@@ -151,64 +151,16 @@ public class ActionDeleteFolders extends ActionBase {
     }
 
     if (argFromPrevious && rows != null) {
-      for (int iteration = 0; iteration < rows.size() && 
!parentWorkflow.isStopped(); iteration++) {
-        if (successConditionBroken) {
-          logError(
-              BaseMessages.getString(
-                  PKG, "ActionDeleteFolders.Error.SuccessConditionBroken", "" 
+ nrErrors));
-          result.setNrErrors(nrErrors);
-          result.setNrLinesDeleted(nrSuccess);
-          return result;
-        }
-        RowMetaAndData resultRow = rows.get(iteration);
-        // Result rows are data. Leave ${...} unresolved, as the other file 
actions do.
-        String argsPrevious = resultRow.getString(0, null);
-        if (!StringUtils.isBlank(argsPrevious)) {
-          if (deleteFolder(argsPrevious)) {
-            updateSuccess();
-          } else {
-            updateErrors();
-          }
-        } else {
-          // empty filename !
-          logError(BaseMessages.getString(PKG, 
"ActionDeleteFolders.Error.EmptyLine"));
-        }
+      if (!processPreviousRows(rows, result)) {
+        return result;
       }
     } else if (fileItems != null) {
-      for (FileItem item : fileItems) {
-        if (parentWorkflow.isStopped()) break;
-        if (successConditionBroken) {
-          logError(
-              BaseMessages.getString(
-                  PKG, "ActionDeleteFolders.Error.SuccessConditionBroken", "" 
+ nrErrors));
-          result.setNrErrors(nrErrors);
-          result.setNrLinesDeleted(nrSuccess);
-          return result;
-        }
-        String realFileName = resolve(item.getFileName());
-        if (!StringUtils.isBlank(realFileName)) {
-          if (deleteFolder(realFileName)) {
-            updateSuccess();
-          } else {
-            updateErrors();
-          }
-        } else {
-          // empty filename !
-          logError(BaseMessages.getString(PKG, 
"ActionDeleteFolders.Error.EmptyLine"));
-        }
+      if (!processConfiguredFolders(result)) {
+        return result;
       }
     }
 
-    if (isDetailed()) {
-      logDetailed("=======================================");
-      logDetailed(
-          BaseMessages.getString(PKG, "ActionDeleteFolders.Log.Info.NrError", 
"" + nrErrors));
-      logDetailed(
-          BaseMessages.getString(
-              PKG, "ActionDeleteFolders.Log.Info.NrDeletedFolders", "" + 
nrSuccess));
-      logDetailed("=======================================");
-    }
-
+    logExecutionSummary();
     result.setNrErrors(nrErrors);
     result.setNrLinesDeleted(nrSuccess);
     if (getSuccessStatus()) {
@@ -218,10 +170,73 @@ public class ActionDeleteFolders extends ActionBase {
     return result;
   }
 
+  /**
+   * @return false when the success condition broke mid-list and the action 
should stop immediately
+   */
+  private boolean processPreviousRows(List<RowMetaAndData> rows, Result result)
+      throws HopException {
+    for (int iteration = 0; iteration < rows.size() && 
!parentWorkflow.isStopped(); iteration++) {
+      if (successConditionBroken) {
+        return stopAfterBrokenCondition(result);
+      }
+      // Result rows are data. Leave ${...} unresolved, as the other file 
actions do.
+      processFolderName(rows.get(iteration).getString(0, null));
+    }
+    return true;
+  }
+
+  /**
+   * @return false when the success condition broke mid-list and the action 
should stop immediately
+   */
+  private boolean processConfiguredFolders(Result result) {
+    for (FileItem item : fileItems) {
+      if (parentWorkflow.isStopped()) {
+        break;
+      }
+      if (successConditionBroken) {
+        return stopAfterBrokenCondition(result);
+      }
+      processFolderName(resolve(item.getFileName()));
+    }
+    return true;
+  }
+
+  private void processFolderName(String folderName) {
+    if (StringUtils.isBlank(folderName)) {
+      logError(BaseMessages.getString(PKG, 
"ActionDeleteFolders.Error.EmptyLine"));
+      return;
+    }
+    if (deleteFolder(folderName)) {
+      nrSuccess++;
+    } else {
+      updateErrors();
+    }
+  }
+
+  private boolean stopAfterBrokenCondition(Result result) {
+    logError(
+        BaseMessages.getString(
+            PKG, "ActionDeleteFolders.Error.SuccessConditionBroken", "" + 
nrErrors));
+    result.setNrErrors(nrErrors);
+    result.setNrLinesDeleted(nrSuccess);
+    return false;
+  }
+
+  private void logExecutionSummary() {
+    if (!isDetailed()) {
+      return;
+    }
+    logDetailed("=======================================");
+    logDetailed(BaseMessages.getString(PKG, 
"ActionDeleteFolders.Log.Info.NrError", "" + nrErrors));
+    logDetailed(
+        BaseMessages.getString(
+            PKG, "ActionDeleteFolders.Log.Info.NrDeletedFolders", "" + 
nrSuccess));
+    logDetailed("=======================================");
+  }
+
   private void updateErrors() {
     nrErrors++;
     if (checkIfSuccessConditionBroken()) {
-      // Success condition was broken
       successConditionBroken = true;
     }
   }
@@ -231,61 +246,49 @@ public class ActionDeleteFolders extends ActionBase {
         || (nrErrors >= nrLimitFolders && getSuccessCondition() == 
SuccessCondition.ERRORS_LESS);
   }
 
-  private void updateSuccess() {
-    nrSuccess++;
-  }
-
   private boolean getSuccessStatus() {
+    // ERRORS_LESS matches the UI label "less than" (strict <), same as Move 
files (#9175 / #9264).
     return (nrErrors == 0 && getSuccessCondition() == 
SuccessCondition.NO_ERRORS)
         || (nrSuccess >= nrLimitFolders
             && getSuccessCondition() == 
SuccessCondition.AT_LEAST_X_FOLDERS_DELETED)
-        || (nrErrors <= nrLimitFolders && getSuccessCondition() == 
SuccessCondition.ERRORS_LESS);
+        || (nrErrors < nrLimitFolders && getSuccessCondition() == 
SuccessCondition.ERRORS_LESS);
   }
 
   private boolean deleteFolder(String folderName) {
-    boolean rcode = false;
-
     try (FileObject folder = HopVfs.getFileObject(folderName, getVariables())) 
{
-
-      if (folder.exists()) {
-        // the file or folder exists
-        if (folder.getType() == FileType.FOLDER) {
-          // It's a folder
-          if (isDetailed()) {
-            logDetailed(
-                BaseMessages.getString(PKG, 
"ActionDeleteFolders.ProcessingFolder", folderName));
-          }
-          // Delete Files
-          int count = folder.delete(new TextFileSelector());
-
-          if (isDetailed()) {
-            logDetailed(
-                BaseMessages.getString(
-                    PKG, "ActionDeleteFolders.TotalDeleted", folderName, 
String.valueOf(count)));
-          }
-          rcode = true;
-        } else {
-          // Error...This file is not a folder!
-          logError(
-              BaseMessages.getString(
-                  PKG, "ActionDeleteFolders.Error.NotFolder", 
folder.toString()));
-        }
-      } else {
-        // File already deleted, no reason to try to delete it
+      if (!folder.exists()) {
         if (isBasic()) {
           logBasic(
               BaseMessages.getString(PKG, 
"ActionDeleteFolders.FolderAlreadyDeleted", folderName));
         }
-        rcode = true;
+        return true;
       }
+      if (folder.getType() != FileType.FOLDER) {
+        logError(
+            BaseMessages.getString(PKG, "ActionDeleteFolders.Error.NotFolder", 
folder.toString()));
+        return false;
+      }
+      return deleteExistingFolder(folder, folderName);
     } catch (Exception e) {
       logError(
           BaseMessages.getString(
               PKG, "ActionDeleteFolders.CouldNotDelete", folderName, 
e.getMessage()),
           e);
+      return false;
     }
+  }
 
-    return rcode;
+  private boolean deleteExistingFolder(FileObject folder, String folderName) 
throws Exception {
+    if (isDetailed()) {
+      logDetailed(BaseMessages.getString(PKG, 
"ActionDeleteFolders.ProcessingFolder", folderName));
+    }
+    int count = folder.delete(new TextFileSelector());
+    if (isDetailed()) {
+      logDetailed(
+          BaseMessages.getString(
+              PKG, "ActionDeleteFolders.TotalDeleted", folderName, 
String.valueOf(count)));
+    }
+    return true;
   }
 
   @Override
diff --git 
a/plugins/actions/deletefolders/src/test/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFoldersResultTest.java
 
b/plugins/actions/deletefolders/src/test/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFoldersResultTest.java
new file mode 100644
index 0000000000..c93d1d35e8
--- /dev/null
+++ 
b/plugins/actions/deletefolders/src/test/java/org/apache/hop/workflow/actions/deletefolders/ActionDeleteFoldersResultTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.hop.workflow.actions.deletefolders;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import org.apache.hop.core.Result;
+import org.apache.hop.core.logging.HopLogStore;
+import org.apache.hop.workflow.WorkflowMeta;
+import org.apache.hop.workflow.engine.IWorkflowEngine;
+import org.apache.hop.workflow.engines.local.LocalWorkflowEngine;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Result / nrErrors behaviour for Delete folders (issue #9264): 
success-condition boundary must not
+ * depend on whether the failing entry is last or earlier in the list.
+ */
+class ActionDeleteFoldersResultTest {
+
+  @TempDir Path testFolder;
+
+  private ActionDeleteFolders action;
+
+  @BeforeAll
+  static void setUpBeforeClass() {
+    HopLogStore.init();
+  }
+
+  @BeforeEach
+  void setUp() {
+    action = attachedAction();
+    action.setArgFromPrevious(false);
+    
action.setSuccessCondition(ActionDeleteFolders.SuccessCondition.ERRORS_LESS);
+  }
+
+  /** #9264: last entry fails with limit 1 — "less than" means strict &lt;, so 
false. */
+  @Test
+  void errorsLessThanIsFalseWhenLastEntryHitsTheLimit() throws Exception {
+    Path folder = Files.createDirectories(testFolder.resolve("keep-me"));
+    Path notAFolder = writeFile(testFolder.resolve("not-a-folder.txt"), "x");
+
+    action.setLimitFolders("1");
+    action.setFileItems(
+        List.of(new FileItem(folder.toString()), new 
FileItem(notAFolder.toString())));
+
+    Result result = action.execute(new Result(), 0);
+
+    assertFalse(result.isResult(), "1 error is not less than 1");
+    assertEquals(1, result.getNrErrors());
+    assertFalse(Files.exists(folder), "the real folder must still be deleted 
before the error");
+  }
+
+  /**
+   * #9264: first entry hits the limit — early exit and final status both 
false (same as last-entry
+   * case), and the remaining folder is not deleted.
+   */
+  @Test
+  void errorsLessThanIsFalseWhenFirstEntryHitsTheLimit() throws Exception {
+    Path folder = Files.createDirectories(testFolder.resolve("keep-me"));
+    Files.writeString(folder.resolve("marker.txt"), "x", 
StandardCharsets.UTF_8);
+    Path notAFolder = writeFile(testFolder.resolve("not-a-folder.txt"), "x");
+
+    action.setLimitFolders("1");
+    action.setFileItems(
+        List.of(new FileItem(notAFolder.toString()), new 
FileItem(folder.toString())));
+
+    Result result = action.execute(new Result(), 0);
+
+    assertFalse(result.isResult(), "1 error is not less than 1");
+    assertEquals(1, result.getNrErrors());
+    assertTrue(Files.exists(folder), "remaining folder must not be deleted 
after the limit");
+  }
+
+  /** With limit 2, a single non-folder entry still succeeds under "errors 
less than". */
+  @Test
+  void errorsLessThanAllowsFewerErrorsThanTheLimit() throws Exception {
+    Path folder = Files.createDirectories(testFolder.resolve("ok"));
+    Path notAFolder = writeFile(testFolder.resolve("not-a-folder.txt"), "x");
+
+    action.setLimitFolders("2");
+    action.setFileItems(
+        List.of(new FileItem(folder.toString()), new 
FileItem(notAFolder.toString())));
+
+    Result result = action.execute(new Result(), 0);
+
+    assertTrue(result.isResult());
+    assertEquals(1, result.getNrErrors());
+    assertFalse(Files.exists(folder));
+  }
+
+  private static Path writeFile(Path path, String content) throws Exception {
+    Files.createDirectories(path.getParent());
+    return Files.writeString(path, content, StandardCharsets.UTF_8);
+  }
+
+  private static ActionDeleteFolders attachedAction() {
+    ActionDeleteFolders deleteFolders = new ActionDeleteFolders("delete 
folders");
+    IWorkflowEngine<WorkflowMeta> workflow = new LocalWorkflowEngine(new 
WorkflowMeta());
+    workflow.setStopped(false);
+    deleteFolders.setParentWorkflow(workflow);
+    return deleteFolders;
+  }
+}

Reply via email to