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

claude pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/creadur-rat.git


The following commit(s) were added to refs/heads/master by this push:
     new a08a54fe RAT-2: Added --input-exclude-size option (#398)
a08a54fe is described below

commit a08a54fe386fcd5f66e8d3fec3590ac9c03c08a9
Author: Claude Warren <[email protected]>
AuthorDate: Wed Nov 20 08:12:33 2024 +0000

    RAT-2: Added --input-exclude-size option (#398)
    
    * Added --input-exclude-size option
    
    * RAT-2: Adapt text of option
    
    * RAT-2: Add changelog entry for skipping by size
    
    ---------
    
    Co-authored-by: P. Ottlinger <[email protected]>
---
 .../java/org/apache/rat/ReportConfiguration.java   | 11 +++++++++-
 .../main/java/org/apache/rat/commandline/Arg.java  | 19 ++++++++++++++++
 .../rat/document/DocumentNameMatcherSupplier.java  | 15 +++++++++++++
 .../apache/rat/test/AbstractOptionsProvider.java   | 25 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 +++
 5 files changed, 72 insertions(+), 1 deletion(-)

diff --git 
a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java 
b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java
index fb71c524..f5e67097 100644
--- a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java
+++ b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java
@@ -296,6 +296,15 @@ public class ReportConfiguration {
         
exclusionProcessor.addExcludedFilter(DocumentNameMatcherSupplier.from(fileFilter));
     }
 
+    /**
+     * Excludes files that match a FileFilter.
+     * @param name the name of the DocumentNameMatcher.
+     * @param matcher the DocumentNameMatcher to match.
+     */
+    public void addExcludedMatcher(final String name, final 
DocumentNameMatcher matcher) {
+        
exclusionProcessor.addExcludedFilter(DocumentNameMatcherSupplier.from(name, 
matcher));
+    }
+
     /**
      * Excludes files that match the pattern.
      *
@@ -615,7 +624,7 @@ public class ReportConfiguration {
 
     /**
      * Adds a license family category (id) to the list of approved licenses
-     * @param licenseId the license Id to add.
+     * @param licenseId the license id to add.
      */
     public void addApprovedLicenseId(final String licenseId) {
         licenseSetFactory.addLicenseId(licenseId);
diff --git a/apache-rat-core/src/main/java/org/apache/rat/commandline/Arg.java 
b/apache-rat-core/src/main/java/org/apache/rat/commandline/Arg.java
index d0591900..e982ce6b 100644
--- a/apache-rat-core/src/main/java/org/apache/rat/commandline/Arg.java
+++ b/apache-rat-core/src/main/java/org/apache/rat/commandline/Arg.java
@@ -46,6 +46,7 @@ import org.apache.rat.ReportConfiguration;
 import org.apache.rat.config.AddLicenseHeaders;
 import org.apache.rat.config.exclusion.ExclusionUtils;
 import org.apache.rat.config.exclusion.StandardCollection;
+import org.apache.rat.document.DocumentNameMatcher;
 import org.apache.rat.license.LicenseSetFactory;
 import org.apache.rat.report.claim.ClaimStatistic.Counter;
 import org.apache.rat.utils.DefaultLog;
@@ -262,6 +263,15 @@ public enum Arg {
                     .build())
     ),
 
+    /**
+     * Excludes files if they are smaller than the given threshold.
+     */
+    EXCLUDE_SIZE(new OptionGroup()
+            
.addOption(Option.builder().longOpt("input-exclude-size").argName("Integer")
+                    .hasArg().type(Integer.class)
+                    .desc("Excludes files with sizes less than the given 
argument.")
+                    .build())
+    ),
     /**
      * Excludes files by expression.
      */
@@ -689,6 +699,15 @@ public enum Arg {
                     }
                 }
             }
+            if (EXCLUDE_SIZE.isSelected()) {
+                final int maxSize = 
EXCLUDE_SIZE.getParsedOptionValue(context.getCommandLine());
+                DocumentNameMatcher matcher =
+                    documentName -> {
+                        File f = new File(documentName.getName());
+                        return f.isFile() && f.length() < maxSize;
+                };
+                
context.getConfiguration().addExcludedMatcher(String.format("File size < %s 
bytes", maxSize), matcher);
+            }
             if (INCLUDE.isSelected()) {
                 String[] includes = 
context.getCommandLine().getOptionValues(INCLUDE.getSelected());
                 if (includes != null) {
diff --git 
a/apache-rat-core/src/main/java/org/apache/rat/document/DocumentNameMatcherSupplier.java
 
b/apache-rat-core/src/main/java/org/apache/rat/document/DocumentNameMatcherSupplier.java
index cc48ac26..662aae97 100644
--- 
a/apache-rat-core/src/main/java/org/apache/rat/document/DocumentNameMatcherSupplier.java
+++ 
b/apache-rat-core/src/main/java/org/apache/rat/document/DocumentNameMatcherSupplier.java
@@ -27,9 +27,24 @@ import java.io.FileFilter;
 public interface DocumentNameMatcherSupplier {
     DocumentNameMatcher get(DocumentName dir);
 
+    /**
+     * Creates a DocumentNameMatcherSupplier from a file filter.
+     * @param fileFilter the file filter to process.
+     * @return a DocumentNameMatcherSupplier.
+     */
     static DocumentNameMatcherSupplier from(final FileFilter fileFilter) {
         DocumentNameMatcher nameMatcher = DocumentNameMatcher.from(fileFilter);
         return docName -> new 
TraceableDocumentNameMatcher(fileFilter::toString, nameMatcher);
+    }
 
+    /**
+     * Create a DocumentNameMatcherSupplier from a DocumentNameMatcher and a 
name.
+     * @param name the name for the matcher.
+     * @param nameMatcher the matcher.
+     * @return A DocumentNameMatcherDupplier.
+     */
+    static DocumentNameMatcherSupplier from(final String name, final 
DocumentNameMatcher nameMatcher) {
+        TraceableDocumentNameMatcher tmatcher = 
TraceableDocumentNameMatcher.make(() -> name, nameMatcher);
+        return docName -> tmatcher;
     }
 }
diff --git 
a/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java
 
b/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java
index 772d3944..00cae7b1 100644
--- 
a/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java
+++ 
b/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java
@@ -118,6 +118,7 @@ public abstract class AbstractOptionsProvider {
         testMap.put("input-exclude-file", this::inputExcludeFileTest);
         testMap.put("input-exclude-parsed-scm", 
this::inputExcludeParsedScmTest);
         testMap.put("input-exclude-std", this::inputExcludeStdTest);
+        testMap.put("input-exclude-size", this::inputExcludeSizeTest);
         testMap.put("input-include", this::inputIncludeTest);
         testMap.put("input-include-file", this::inputIncludeFileTest);
         testMap.put("input-include-std", this::inputIncludeStdTest);
@@ -293,6 +294,30 @@ public abstract class AbstractOptionsProvider {
         }
     }
 
+    private void inputExcludeSizeTest() {
+        Option option = Arg.EXCLUDE_SIZE.option();
+        String[] args = { "5" };
+        writeFile("Hi.txt", Arrays.asList("Hi"));
+        writeFile("Hello.txt", Arrays.asList("Hello"));
+        writeFile("HelloWorld.txt", Arrays.asList("HelloWorld"));
+
+        String[] notExcluded = { "Hello.txt", "HelloWorld.txt"};
+        String[] excluded = { "Hi.txt" };
+
+        try {
+            ReportConfiguration config = 
generateConfig(ImmutablePair.of(option, args));
+            DocumentNameMatcher matcher = config.getNameMatcher(baseName());
+            for (String fname : excluded) {
+                assertFalse(matcher.matches(mkDocName(fname)), () -> 
option.getKey() + " " + fname);
+            }
+            for (String fname : notExcluded) {
+                assertTrue(matcher.matches(mkDocName(fname)), () -> 
option.getKey() + " " + fname);
+            }
+        } catch (IOException e) {
+            fail(e.getMessage());
+        }
+    }
+
     // include tests
     private void execIncludeTest(Option option, String[] args) {
         Option excludeOption = Arg.EXCLUDE.option();
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ef72932a..986def59 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -72,6 +72,9 @@ The <action> type attribute can be one of:
     </release>
     -->
     <release version="0.17-SNAPSHOT" date="xxxx-yy-zz" description="Current 
SNAPSHOT - release to be done">
+      <action issue="RAT-2" type="add" dev="claudenw">
+        Added --input-exclude-size as an option to skip the scanning of very 
small files.
+      </action>
       <action issue="RAT-41" type="fix" dev="claudenw">
         Added core integration tests and verified results without generating 
output via ClaimStatistics.
       </action>

Reply via email to