Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework directory scanner to ensure it can enforce exclusions and it uses path #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
455 changes: 196 additions & 259 deletions src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
* Significantly more efficient than using strings, since re-evaluation and re-tokenizing is avoided.
*
* @author Kristian Rosenvold
* @deprecated use {@code java.nio.filejava.nio.file.DirectoryStream.Filter<T>} and related classes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please retain this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is wrong, it shouldn't have been merged IMO

*/
@Deprecated
public class MatchPattern
{
private final String source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
* A list of patterns to be matched
*
* @author Kristian Rosenvold
* @deprecated use {@code java.nio.filejava.nio.file.DirectoryStream.Filter<T>} and related classes
*/
@Deprecated
public class MatchPatterns
{
private final MatchPattern[] patterns;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
*
* @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
*
* @deprecated use {@code java.nio.file.Files.walkFileTree()} and related classes
*/
@Deprecated
public interface ScanConductor
{
/**
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/apache/maven/shared/utils/io/ScannerAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.apache.maven.shared.utils.io;

/*
* 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.
*/

/**
* Enables a {@link ScanConductor} to access the {@link DirectoryScanner} configuration.
* It is set once the scanner is initialized.
*/
public interface ScannerAware
{
void setDirectoryScanner( DirectoryScanner scanner );
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
* <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
* @author Magesh Umasankar
* @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
*
* @deprecated use {@code java.nio.file.Files.walkFileTree()} and related classes
*/
@Deprecated
public final class SelectorUtils
{

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.apache.maven.shared.utils.io.conductor;

/*
* 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.
*/

import org.apache.maven.shared.utils.io.DirectoryScanner;
import org.apache.maven.shared.utils.io.MatchPatterns;
import org.apache.maven.shared.utils.io.ScanConductor;
import org.apache.maven.shared.utils.io.ScannerAware;

import java.io.File;

/**
* If an exclude is defined on a folder it does not visit of the children
* even if some include can match children.
*/
public class EnforceExcludesOverIncludes implements ScanConductor, ScannerAware
Copy link
Member

@kwin kwin Jun 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense if this class would directly implement https://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.Filter.html instead? That way one could get rid of the ScanConductor interface...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @kwin let's make it slim

{
private MatchPatterns excludes;

@Override
public ScanAction visitDirectory( final String name, final File directory )
{
if ( excludes.matches( name, true ) )
{
return ScanAction.NO_RECURSE;
}
return ScanAction.CONTINUE;
}

@Override
public ScanAction visitFile( final String name, final File file )
{
return ScanAction.CONTINUE;
}

@Override
public void setDirectoryScanner( final DirectoryScanner scanner )
{
excludes = scanner.getExcludesPatterns();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void followSymlinksFalse()
ds.scan();
List<String> included = Arrays.asList( ds.getIncludedFiles() );
assertAlwaysIncluded( included );
assertEquals( 9, included.size() );
assertEquals( included.toString(), 9, included.size() );
List<String> includedDirs = Arrays.asList( ds.getIncludedDirectories() );
assertTrue( includedDirs.contains( "" ) );
assertTrue( includedDirs.contains( "aRegularDir" ) );
Expand Down Expand Up @@ -243,14 +243,6 @@ public void testSimpleExcludes()
/* expExclDirs */ NONE );
}

public void testIsSymbolicLink()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this test removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more relevant since there we use directly Files API and it was testing an internal

throws IOException
{
File file = new File( "." );
DirectoryScanner ds = new DirectoryScanner();
ds.isSymbolicLink( file, "abc" );
}

/**
* Performs a scan and test for the given parameters if not null.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.apache.maven.shared.utils.io.conductor;

/*
* 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.
*/

import org.apache.maven.shared.utils.io.DirectoryScanner;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileWriter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

danger: this class depends on platform default encoding. Avoid.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is fine, it is just used to "touch" the file, not write content

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;

public class EnforceExcludesOverIncludesTest
{
@Rule
public final TemporaryFolder folder = new TemporaryFolder();

@Test
public void dontVisitChildren() throws IOException
{
createFakeFiles();

DirectoryScanner scanner = new DirectoryScanner();
scanner.setScanConductor(new EnforceExcludesOverIncludes());
scanner.setExcludes( "**/target/**" );
scanner.setIncludes( "**" ); // files in target will match include but our conductor will bypass them anyway
scanner.setBasedir( folder.getRoot() );
scanner.scan();
assertResultEquals( asList( "bar", "sub/other" ), scanner.getIncludedFiles() );
assertResultEquals( singletonList( "target" ), scanner.getExcludedDirectories() );
}

@Test // we don't set the conductor to ensure we have a "control" test to compare to the other
public void controlTest() throws IOException
{
createFakeFiles();

DirectoryScanner scanner = new DirectoryScanner();
scanner.setExcludes( "**/target/**" );
scanner.setIncludes( "**" ); // files in target will match include but our conductor will bypass them anyway
scanner.setBasedir( folder.getRoot() );
scanner.scan();
assertResultEquals( asList( "bar", "sub/other" ), scanner.getIncludedFiles() );
assertResultEquals( asList( "target", "target/nested" ), scanner.getExcludedDirectories() );
}

private void createFakeFiles() throws IOException
{
touch(new File(folder.getRoot(), "bar"));
touch(new File(folder.getRoot(), "sub/other"));
touch(new File(folder.getRoot(), "target/foo"));
touch(new File(folder.getRoot(), "target/nested/dummy"));
}

private void assertResultEquals( final List<String> expected, final String[] actual )
{
final List<String> actualList = new ArrayList<>( asList( actual ) );
Collections.sort( actualList );
assertEquals( expected, actualList );
}

private void touch( final File file ) throws IOException
{
file.getParentFile().mkdirs();
file.createNewFile();
}
}