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

Allow converting singleton file tree to directory trees #8387

Merged
merged 2 commits into from Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,7 +21,7 @@
import org.gradle.api.tasks.util.PatternFilterable;
import org.gradle.api.tasks.util.PatternSet;

public abstract class AbstractSingletonFileTree implements SingletonFileTree, PatternFilterableFileTree {
public abstract class AbstractSingletonFileTree implements SingletonFileTree, PatternFilterableFileTree, FileSystemMirroringFileTree {
private final PatternSet patterns;

protected AbstractSingletonFileTree(PatternSet patterns) {
Expand Down Expand Up @@ -52,4 +52,9 @@ protected PatternSet filterPatternSet(PatternFilterable patterns) {
patternSet.copyFrom(patterns);
return patternSet;
}

@Override
public DirectoryFileTree getMirror() {
return new FileBackedDirectoryFileTree(getFile()).filter(patterns);
}
}
Expand Up @@ -15,13 +15,18 @@
*/
package org.gradle.api.internal.file.collections

import org.gradle.api.file.FileVisitDetails
import org.gradle.api.file.FileVisitor
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.gradle.util.UsesNativeServices
import org.junit.Rule
import spock.lang.Specification
import org.gradle.api.file.FileVisitor
import org.gradle.api.file.FileVisitDetails

@UsesNativeServices
class DefaultSingletonFileTreeTest extends Specification {
@Rule
final TestNameTestDirectoryProvider temporaryFolder = new TestNameTestDirectoryProvider()

def hasUsefulDisplayName() {
File f = new File('test-file')
DefaultSingletonFileTree tree = new DefaultSingletonFileTree(f)
Expand All @@ -45,4 +50,31 @@ class DefaultSingletonFileTreeTest extends Specification {
}
0 * visitor._
}

def "can be converted to a directory tree"() {
Copy link
Member

Choose a reason for hiding this comment

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

Should we mention the @Issue that triggered this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Added.

File f = temporaryFolder.file('test-file')
Copy link
Member

Choose a reason for hiding this comment

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

Nit: let's not use single-letter variable names.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed.

f.createNewFile()
DefaultSingletonFileTree singletonFileTree = new DefaultSingletonFileTree(f)
def tree = new FileTreeAdapter(singletonFileTree)

when:
def fileTrees = tree.getAsFileTrees()
then:
fileTrees.size() == 1
def directoryTree = fileTrees[0]
directoryTree.dir == f.parentFile
new FileTreeAdapter(directoryTree).files == [f] as Set
}

def "convert filtered tree to empty file trees"() {
File f = temporaryFolder.file('test-file')
f.createNewFile()
DefaultSingletonFileTree singletonFileTree = new DefaultSingletonFileTree(f)
def tree = new FileTreeAdapter(singletonFileTree).filter { it.name == 'different' }

when:
def fileTrees = tree.getAsFileTrees()
then:
fileTrees.size() == 0
}
}