Skip to content

Commit

Permalink
Merge branch '98_to_master' into RAT-98_report_skipped_files
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw committed Feb 4, 2025
2 parents f7679b5 + ec474c3 commit 24637cb
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,4 @@ private void extractPaths(final MatcherSet.Builder matcherBuilder) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static Builder builder(final FileSystem fileSystem) {
}

/**
* Creates a builder from a File. The {@link #baseName} is set to the file name if it is a directory otherwise
* Creates a builder from a File. The {@link #baseName} is set to the file name if it is a directory otherwise
* it is set to the directory containing the file.
* @param file The file to set defaults from.
* @return the Builder.
Expand Down Expand Up @@ -188,15 +188,32 @@ public DocumentName resolve(final String child) {
if (StringUtils.isBlank(child)) {
return this;
}
String separator = fsInfo.dirSeparator();
String separator = getDirectorySeparator();
String pattern = separator.equals("/") ? child.replace('\\', '/') :
child.replace('/', '\\');

if (!pattern.startsWith(separator)) {
pattern = name + separator + pattern;
}

return new Builder(this).setName(pattern).build();
return new Builder(this).setName(normalize(pattern)).build();
}

private String normalize(final String pattern) {
List<String> parts = new ArrayList<>(Arrays.asList(tokenize(pattern)));
for (int i = 0; i < parts.size(); i++) {
String part = parts.get(i);
if (part.equals("..")) {
if (i == 0) {
throw new IllegalStateException("can not creat path before root");
}
parts.set(i - 1, null);
parts.set(i, null);
} else if (part.equals(".")) {
parts.set(i, null);
}
}
return parts.stream().filter(Objects::nonNull).collect(Collectors.joining(getDirectorySeparator()));
}

/**
Expand Down Expand Up @@ -295,6 +312,15 @@ public String localized(final String dirSeparator) {
return startsWithRootOrSeparator(result, modifiedRoot, dirSeparator) ? result : dirSeparator + result;
}

/**
* Tokenizes the string based on the directory separator of this DocumentName.
* @param source the source to tokenize
* @return the array of tokenized strings.
*/
public String[] tokenize(final String source) {
return source.split("\\Q" + fsInfo.dirSeparator() + "\\E");
}

/**
* Gets the last segment of the name. This is the part after the last directory separator.
* @return the last segment of the name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public final class DocumentNameMatcher {
public DocumentNameMatcher(final String name, final Predicate<DocumentName> predicate) {
this.name = name;
this.predicate = predicate;
this.isCollection = predicate instanceof CollectionPredicateImpl;
this.isCollection = predicate instanceof CollectionPredicate;
}

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ private void decompose(final int level, final DocumentNameMatcher matcher, final
* @return true if the documentName matchers this DocumentNameMatcher.
*/
public boolean matches(final DocumentName documentName) {
return predicate.test(documentName);
return predicate.test(documentName);
}

/**
Expand Down Expand Up @@ -210,8 +210,7 @@ private static String join(final Collection<DocumentNameMatcher> matchers) {
return String.join(", ", children);
}

private static Optional<DocumentNameMatcher> standardCollectionCheck(final Collection<DocumentNameMatcher> matchers,
final DocumentNameMatcher override) {
private static Optional<DocumentNameMatcher> standardCollectionCheck(final Collection<DocumentNameMatcher> matchers, final DocumentNameMatcher override) {
if (matchers.isEmpty()) {
throw new ConfigurationException("Empty matcher collection");
}
Expand Down Expand Up @@ -303,13 +302,16 @@ public static DocumentNameMatcher matcherSet(final DocumentNameMatcher includes,
}
List<DocumentNameMatcher> workingSet = Arrays.asList(includes, excludes);
return new DocumentNameMatcher(format("matcherSet(%s)", join(workingSet)),
new CollectionPredicateImpl(workingSet) {
new CollectionPredicateImpl(Arrays.asList(includes, excludes)) {
@Override
public boolean test(final DocumentName documentName) {
if (includes.matches(documentName)) {
return true;
}
return !excludes.matches(documentName);
if (excludes.matches(documentName)) {
return false;
}
return true;
}
});
}
Expand Down Expand Up @@ -400,9 +402,8 @@ public String toString() {
interface CollectionPredicate extends Predicate<DocumentName> {
Iterable<DocumentNameMatcher> getMatchers();
}

/**
* A marker class to indicate this predicate contains a collection of matchers.
* CollectionPredicate implementation.
*/
abstract static class CollectionPredicateImpl implements CollectionPredicate {
/** The collection for matchers that make up this predicate */
Expand Down Expand Up @@ -484,7 +485,7 @@ public static final class DecomposeData {
private final DocumentNameMatcher matcher;
/** The result of the check. */
private final boolean result;
/** The actual candidate. */
/** The candidate */
private final DocumentName candidate;

private DecomposeData(final int level, final DocumentNameMatcher matcher, final DocumentName candidate, final boolean result) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CVSIgnoreBuilderTest extends AbstractIgnoreBuilderTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
*/
package org.apache.rat.document;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -213,7 +210,6 @@ void localizeTest() {
.setBaseName("/a").build();
assertThat(documentName.localized()).isEqualTo("/b/c");
assertThat(documentName.localized("-")).isEqualTo("-b-c");

}

@ParameterizedTest(name = "{index} {0}")
Expand All @@ -226,7 +222,7 @@ void validBuilderTest(String testName, DocumentName.Builder builder, String root
assertThat(underTest.getBaseName()).as(testName).isEqualTo(root + dirSeparator + baseName);
}

private static Stream<Arguments> validBuilderData() throws IOException {
private static Stream<Arguments> validBuilderData() {
List<Arguments> lst = new ArrayList<>();
File f = Files.newTemporaryFile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@
*/
package org.apache.rat.document.guesser;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.apache.rat.document.DocumentName;
import org.apache.rat.document.FSInfoTest;
import org.apache.rat.testhelpers.TestingDocument;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.apache.rat.document.FSInfoTest.OSX;
import static org.apache.rat.document.FSInfoTest.UNIX;
import static org.apache.rat.document.FSInfoTest.WINDOWS;

public class NoteGuesserTest {

Expand All @@ -44,9 +45,9 @@ public void testMatches(DocumentName testingName, boolean expected) {
private static Stream<Arguments> nameData() throws IOException {
List<Arguments> lst = new ArrayList<>();

final DocumentName linuxBaseName = DocumentName.builder(FSInfoTest.UNIX).setName("/").setBaseName("/").build();
final DocumentName windowsBaseName = DocumentName.builder(FSInfoTest.WINDOWS).setName("\\").setBaseName("\\").build();
final DocumentName osxBaseName = DocumentName.builder(FSInfoTest.OSX).setName("/").setBaseName("/").build();
final DocumentName osxBaseName = DocumentName.builder(OSX).setName("/").setBaseName("/").build();
final DocumentName linuxBaseName = DocumentName.builder(UNIX).setName("/").setBaseName("/").build();
final DocumentName windowsBaseName = DocumentName.builder(WINDOWS).setName("\\").setBaseName("\\").build();

lst.add(Arguments.of(linuxBaseName.resolve("DEPENDENCIES"), true));
lst.add(Arguments.of(linuxBaseName.resolve("LICENSE"), true));
Expand Down
Loading

0 comments on commit 24637cb

Please sign in to comment.