<commit_before>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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.gradle.api.internal.changedetection.state

import org.gradle.api.internal.file.FileResource
import org.gradle.api.internal.file.MaybeCompressedFileResource
import org.gradle.api.internal.file.archive.TarFileTree
import org.gradle.api.internal.file.collections.FileTreeAdapter
import org.gradle.api.internal.file.collections.SimpleFileCollection
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.gradle.util.UsesNativeServices
import org.junit.Rule
import spock.lang.Specification
import spock.lang.Subject

import static org.gradle.api.internal.file.TestFiles.directoryFileTreeFactory
import static org.gradle.api.internal.file.TestFiles.fileSystem

@UsesNativeServices
class BackingFileExtractorTest extends Specification {
    @Rule
    public final TestNameTestDirectoryProvider testDir = new TestNameTestDirectoryProvider();

    @Subject
    BackingFileExtractor backingFileExtractor = new BackingFileExtractor()

    def "should extract files and directories for simple cases"() {
        given:
        def files = (1..10).collect { file(it as String).absoluteFile }
        def fileCollection = new SimpleFileCollection(files)

        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(fileCollection)

        then:
        extracted.size() == files.size()
        extracted*.file as Set == files as Set

    }

    def "tar filetree doesn't get extracted"() {
        given:
        def tarFile = testDir.file("test.tar")
        testDir.createDir("tarRoot").with {
            file("a/file1.txt") << "content"
            file("b/file2.txt") << "some content"
            tarTo(tarFile)
        }
        def expandDir = testDir.createDir("tarExpand");
        def tarFileTree = new TarFileTree(tarFile, new MaybeCompressedFileResource(new FileResource(tarFile)), expandDir, fileSystem(), fileSystem(), directoryFileTreeFactory());
        def fileCollection = new FileTreeAdapter(tarFileTree)

        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(fileCollection)

        then:
        extracted.size() == 1
        extracted[0].file.absolutePath == tarFile.absolutePath
        !expandDir.listFiles()
    }

    TestFile file(Object... path) {
        testDir.file(path)
    }
}<commit_msg>Add tests that configuration and lazy file collections get skipped<commit_after>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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.gradle.api.internal.changedetection.state

import org.gradle.api.artifacts.Configuration
import org.gradle.api.internal.file.FileCollectionInternal
import org.gradle.api.internal.file.FileResource
import org.gradle.api.internal.file.MaybeCompressedFileResource
import org.gradle.api.internal.file.UnionFileCollection
import org.gradle.api.internal.file.archive.TarFileTree
import org.gradle.api.internal.file.collections.FileTreeAdapter
import org.gradle.api.internal.file.collections.LazilyInitializedFileCollection
import org.gradle.api.internal.file.collections.SimpleFileCollection
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.gradle.util.UsesNativeServices
import org.junit.Rule
import spock.lang.Specification
import spock.lang.Subject

import static org.gradle.api.internal.file.TestFiles.directoryFileTreeFactory
import static org.gradle.api.internal.file.TestFiles.fileSystem

@UsesNativeServices
class BackingFileExtractorTest extends Specification {
    @Rule
    public final TestNameTestDirectoryProvider testDir = new TestNameTestDirectoryProvider();

    @Subject
    BackingFileExtractor backingFileExtractor = new BackingFileExtractor()

    def "should extract files and directories for simple cases"() {
        given:
        def files = (1..10).collect { file(it as String).absoluteFile }
        def fileCollection = new SimpleFileCollection(files)

        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(fileCollection)

        then:
        extracted.size() == files.size()
        extracted*.file as Set == files as Set

    }

    def "tar filetree doesn't get extracted"() {
        given:
        def tarFile = testDir.file("test.tar")
        testDir.createDir("tarRoot").with {
            file("a/file1.txt") << "content"
            file("b/file2.txt") << "some content"
            tarTo(tarFile)
        }
        def expandDir = testDir.createDir("tarExpand");
        def tarFileTree = new TarFileTree(tarFile, new MaybeCompressedFileResource(new FileResource(tarFile)), expandDir, fileSystem(), fileSystem(), directoryFileTreeFactory());
        def fileCollection = new FileTreeAdapter(tarFileTree)

        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(fileCollection)

        then:
        extracted.size() == 1
        extracted[0].file.absolutePath == tarFile.absolutePath
        !expandDir.listFiles()
    }

    def "Configuration doesn't get extracted"() {
        given:
        def configurationFileCollection = Mock(ConfigurationFileCollection)
        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(configurationFileCollection)

        then:
        extracted.isEmpty()
        0 * _._
    }

    private interface ConfigurationFileCollection extends FileCollectionInternal, Configuration {

    }

    def "LazilyInitializedFileCollection doesn't get extracted"() {
        given:
        def lazyFileCollection = Mock(LazilyInitializedFileCollection)
        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(lazyFileCollection)

        then:
        extracted.isEmpty()
        0 * _._
    }

    def "Configuration in a nested CompositeFileCollection doesn't get extracted"() {
        given:
        def configurationFileCollection = Mock(ConfigurationFileCollection)
        def compositeFileCollection = new UnionFileCollection([configurationFileCollection])
        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(compositeFileCollection)

        then:
        extracted.isEmpty()
        0 * _._
    }

    def "Configuration in a deeply nested CompositeFileCollection doesn't get extracted"() {
        given:
        def configurationFileCollection = Mock(ConfigurationFileCollection)
        def compositeFileCollection = new UnionFileCollection([new UnionFileCollection([new UnionFileCollection([configurationFileCollection])])])
        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(compositeFileCollection)

        then:
        extracted.isEmpty()
        0 * _._
    }

    def "Configuration in a deeply nested CompositeFileCollection doesn't get extracted while simple file collections do"() {
        given:
        def configurationFileCollection = Mock(ConfigurationFileCollection)
        def files = (1..3).collect { file(it as String).absoluteFile }
        def compositeFileCollection = new UnionFileCollection([new SimpleFileCollection([files[0]]), new UnionFileCollection([new SimpleFileCollection([files[1]]), new UnionFileCollection([configurationFileCollection, new SimpleFileCollection([files[2]])])])])
        when:
        List<BackingFileExtractor.FileEntry> extracted = backingFileExtractor.extractFilesOrDirectories(compositeFileCollection)

        then:
        extracted.size() == files.size()
        extracted*.file as Set == files as Set
    }

    TestFile file(Object... path) {
        testDir.file(path)
    }
}
{'fraction_non_alphanumeric': 0.08443213296398892, 'fraction_numerical': 0.004321329639889197, 'mean_word_length': 3.730607966457023, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4633317', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2291, 'n_words': 586}