"Fossies" - the Fresh Open Source Software Archive

Member "gradle-8.1.1/subprojects/core/src/test/groovy/org/gradle/internal/fingerprint/impl/AbsolutePathFileCollectionFingerprinterTest.groovy" (20 Apr 2023, 10107 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "AbsolutePathFileCollectionFingerprinterTest.groovy": 7.6.0_vs_8.0.0.

    1 /*
    2  * Copyright 2018 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 package org.gradle.internal.fingerprint.impl
   17 
   18 import org.gradle.api.file.FileCollection
   19 import org.gradle.api.internal.file.TestFiles
   20 import org.gradle.internal.execution.history.changes.AbsolutePathFingerprintCompareStrategy
   21 import org.gradle.internal.execution.history.changes.ChangeTypeInternal
   22 import org.gradle.internal.execution.history.changes.DefaultFileChange
   23 import org.gradle.internal.fingerprint.DirectorySensitivity
   24 import org.gradle.internal.fingerprint.FileCollectionFingerprint
   25 import org.gradle.internal.fingerprint.hashing.FileSystemLocationSnapshotHasher
   26 import org.gradle.test.fixtures.file.TestFile
   27 import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
   28 import org.junit.Rule
   29 import spock.lang.Specification
   30 
   31 class AbsolutePathFileCollectionFingerprinterTest extends Specification {
   32     def virtualFileSystem = TestFiles.virtualFileSystem()
   33     def fileSystemAccess = TestFiles.fileSystemAccess(virtualFileSystem)
   34     def fileCollectionSnapshotter = new DefaultFileCollectionSnapshotter(fileSystemAccess, TestFiles.fileSystem())
   35     def fingerprinter = new AbsolutePathFileCollectionFingerprinter(DirectorySensitivity.DEFAULT, fileCollectionSnapshotter, FileSystemLocationSnapshotHasher.DEFAULT)
   36     def listener = Mock(ChangeListener)
   37 
   38     @Rule
   39     public final TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider(getClass())
   40 
   41     def "retains order of files in the snapshot"() {
   42         given:
   43         TestFile file = tmpDir.createFile('file1')
   44         TestFile file2 = tmpDir.createFile('file2')
   45         TestFile file3 = tmpDir.createFile('file3')
   46 
   47         when:
   48         def fingerprint = fingerprinter.fingerprint(files(file, file2, file3))
   49 
   50         then:
   51         fingerprint.fingerprints.keySet().collect { new File(it) } == [file, file2, file3]
   52     }
   53 
   54     def "fingerprint includes existing root elements"() {
   55         given:
   56         TestFile file = tmpDir.createFile('file1')
   57         TestFile dir = tmpDir.createDir('dir')
   58         TestFile dir2 = dir.createDir('dir2')
   59         TestFile file2 = dir2.createFile('file2')
   60         TestFile noExist = tmpDir.file('file3')
   61 
   62         when:
   63         def fingerprint = fingerprinter.fingerprint(files(file, dir, noExist))
   64 
   65         then:
   66         fingerprint.fingerprints.keySet().collect { new File(it) } == [file, dir, dir2, file2]
   67     }
   68 
   69     def "retains order of elements in the snapshot"() {
   70         given:
   71         TestFile file = tmpDir.createFile('file1')
   72         TestFile file2 = tmpDir.createDir('file2')
   73         TestFile file3 = tmpDir.createDir('file3')
   74         TestFile file4 = tmpDir.createFile('file4')
   75 
   76         when:
   77         def fingerprint = fingerprinter.fingerprint(files(file, file2, file3, file4))
   78 
   79         then:
   80         fingerprint.fingerprints.keySet().collect { new File(it) } == [file, file2, file3, file4]
   81     }
   82 
   83     def generatesEventWhenFileAdded() {
   84         given:
   85         TestFile file1 = tmpDir.createFile('file1')
   86         TestFile file2 = tmpDir.createFile('file2')
   87 
   88         when:
   89         def fingerprint = fingerprinter.fingerprint(files(file1))
   90         virtualFileSystem.invalidateAll()
   91         changes(fingerprint, fingerprinter.fingerprint(files(file1, file2)), listener)
   92 
   93         then:
   94         1 * listener.added(file2.path)
   95         0 * _
   96     }
   97 
   98     def generatesEventWhenFileRemoved() {
   99         given:
  100         TestFile file1 = tmpDir.createFile('file1')
  101         TestFile file2 = tmpDir.createFile('file2')
  102 
  103         when:
  104         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(files(file1, file2))
  105         virtualFileSystem.invalidateAll()
  106         changes(fingerprint, fingerprinter.fingerprint(files(file1)), listener)
  107 
  108         then:
  109         1 * listener.removed(file2.path)
  110         0 * _
  111     }
  112 
  113     def doesNotGenerateEventForFileWhoseTypeAndMetaDataAndContentHaveNotChanged() {
  114         given:
  115         TestFile file = tmpDir.createFile('file')
  116         file.setLastModified(1234L)
  117 
  118         when:
  119         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(files(file))
  120         virtualFileSystem.invalidateAll()
  121         changes(fingerprint, fingerprinter.fingerprint(files(file)), listener)
  122         file.setLastModified(45600L)
  123         virtualFileSystem.invalidateAll()
  124         changes(fingerprint, fingerprinter.fingerprint(files(file)), listener)
  125 
  126         then:
  127         0 * listener._
  128     }
  129 
  130     def generatesEventWhenFileBecomesADirectory() {
  131         given:
  132         TestFile root = tmpDir.createDir('root')
  133         TestFile file = root.createFile('file')
  134         def fileCollection = files(root)
  135 
  136         when:
  137         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(fileCollection)
  138         file.delete()
  139         file.createDir()
  140         virtualFileSystem.invalidateAll()
  141         changes(fingerprint, fingerprinter.fingerprint(fileCollection), listener)
  142 
  143         then:
  144         1 * listener.changed(file.path)
  145         0 * _
  146     }
  147 
  148     def generatesEventWhenFileContentChanges() {
  149         TestFile file = tmpDir.createFile('file')
  150 
  151         when:
  152         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(files(file))
  153         file.write('new content')
  154         virtualFileSystem.invalidateAll()
  155         changes(fingerprint, fingerprinter.fingerprint(files(file)), listener)
  156 
  157         then:
  158         1 * listener.changed(file.path)
  159         0 * _
  160     }
  161 
  162     def doesNotGenerateEventForDirectoryThatHasNotChanged() {
  163         TestFile dir = tmpDir.createDir('dir')
  164 
  165         when:
  166         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(files(dir))
  167         virtualFileSystem.invalidateAll()
  168         changes(fingerprint, fingerprinter.fingerprint(files(dir)), listener)
  169 
  170         then:
  171         0 * _
  172     }
  173 
  174     def generatesEventForDirectoryThatBecomesAFile() {
  175         TestFile root = tmpDir.createDir('root')
  176         def fileCollection = files(root)
  177         TestFile dir = root.createDir('dir')
  178 
  179         when:
  180         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(fileCollection)
  181         dir.deleteDir()
  182         dir.createFile()
  183         virtualFileSystem.invalidateAll()
  184         changes(fingerprint, fingerprinter.fingerprint(fileCollection), listener)
  185 
  186         then:
  187         1 * listener.changed(dir.path)
  188         0 * listener._
  189     }
  190 
  191     def doesNotGenerateEventForMissingFileThatStillIsMissing() {
  192         TestFile file = tmpDir.file('unknown')
  193 
  194         when:
  195         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(files(file))
  196         virtualFileSystem.invalidateAll()
  197         changes(fingerprint, fingerprinter.fingerprint(files(file)), listener)
  198 
  199         then:
  200         0 * _
  201     }
  202 
  203     def generatesEventWhenMissingFileIsCreated() {
  204         TestFile root = tmpDir.createDir('root')
  205         def fileCollection = files(root)
  206         TestFile file = root.file('newfile')
  207 
  208         when:
  209         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(fileCollection)
  210         file.createFile()
  211         virtualFileSystem.invalidateAll()
  212         changes(fingerprint, fingerprinter.fingerprint(fileCollection), listener)
  213 
  214         then:
  215         1 * listener.added(file.path)
  216     }
  217 
  218     def generatesEventWhenFileIsDeleted() {
  219         TestFile root = tmpDir.createDir('root')
  220         def fileCollection = files(root)
  221         TestFile file = root.createFile('file')
  222 
  223         when:
  224         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(fileCollection)
  225         file.delete()
  226         virtualFileSystem.invalidateAll()
  227         changes(fingerprint, fingerprinter.fingerprint(fileCollection), listener)
  228 
  229         then:
  230         1 * listener.removed(file.path)
  231     }
  232 
  233     def ignoresDuplicatesInFileCollection() {
  234         TestFile file1 = tmpDir.createFile('file')
  235         TestFile file2 = tmpDir.createFile('file')
  236 
  237         when:
  238         FileCollectionFingerprint fingerprint = fingerprinter.fingerprint(files(file1, file2))
  239         virtualFileSystem.invalidateAll()
  240         changes(fingerprint, fingerprinter.fingerprint(files(file1)), listener)
  241 
  242         then:
  243         0 * _
  244     }
  245 
  246     def canCreateEmptySnapshot() {
  247         TestFile file = tmpDir.createFile('file')
  248 
  249         when:
  250         FileCollectionFingerprint fingerprint = FileCollectionFingerprint.EMPTY
  251         FileCollectionFingerprint newFingerprint = fingerprinter.fingerprint(files(file))
  252         virtualFileSystem.invalidateAll()
  253         changes(fingerprint, newFingerprint, listener)
  254 
  255         then:
  256         fingerprint.fingerprints.isEmpty()
  257         1 * listener.added(file.path)
  258         0 * listener._
  259     }
  260 
  261     private static void changes(FileCollectionFingerprint previous, FileCollectionFingerprint current, ChangeListener<String> listener) {
  262         AbsolutePathFingerprintCompareStrategy.INSTANCE.visitChangesSince(previous, current, "TYPE") { DefaultFileChange change ->
  263             switch (change.type) {
  264                 case ChangeTypeInternal.ADDED:
  265                     listener.added(change.path)
  266                     break
  267                 case ChangeTypeInternal.MODIFIED:
  268                     listener.changed(change.path)
  269                     break
  270                 case ChangeTypeInternal.REMOVED:
  271                     listener.removed(change.path)
  272                     break
  273             }
  274             return true
  275         }
  276     }
  277 
  278     private static FileCollection files(File... files) {
  279         TestFiles.fixed(files)
  280     }
  281 }
  282 
  283 interface ChangeListener<T> {
  284     void added(T element);
  285 
  286     void removed(T element);
  287 
  288     void changed(T element);
  289 }