forked from gradle/gradle
/
AbstractFileWatcherUpdaterTest.groovy
108 lines (93 loc) · 4.31 KB
/
AbstractFileWatcherUpdaterTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright 2020 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.internal.watch.registry.impl
import net.rubygrapefruit.platform.file.FileWatcher
import org.gradle.api.internal.cache.StringInterner
import org.gradle.api.internal.file.TestFiles
import org.gradle.internal.file.FileMetadata.AccessType
import org.gradle.internal.file.impl.DefaultFileMetadata
import org.gradle.internal.snapshot.CaseSensitivity
import org.gradle.internal.snapshot.CompleteDirectorySnapshot
import org.gradle.internal.snapshot.CompleteFileSystemLocationSnapshot
import org.gradle.internal.snapshot.RegularFileSnapshot
import org.gradle.internal.snapshot.SnapshotHierarchy
import org.gradle.internal.snapshot.impl.DirectorySnapshotter
import org.gradle.internal.vfs.VirtualFileSystem
import org.gradle.internal.vfs.impl.DefaultSnapshotHierarchy
import org.gradle.internal.watch.registry.FileWatcherUpdater
import org.gradle.internal.watch.vfs.impl.SnapshotCollectingDiffListener
import org.gradle.test.fixtures.file.CleanupTestDirectory
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.junit.Rule
import spock.lang.Specification
import java.nio.file.Files
import java.nio.file.attribute.BasicFileAttributes
import java.util.concurrent.atomic.AtomicBoolean
@CleanupTestDirectory
abstract class AbstractFileWatcherUpdaterTest extends Specification {
@Rule
TestNameTestDirectoryProvider temporaryFolder = new TestNameTestDirectoryProvider(getClass())
def watcher = Mock(FileWatcher)
def directorySnapshotter = new DirectorySnapshotter(TestFiles.fileHasher(), new StringInterner(), [])
FileWatcherUpdater updater
def virtualFileSystem = new VirtualFileSystem() {
private SnapshotHierarchy root = DefaultSnapshotHierarchy.empty(CaseSensitivity.CASE_SENSITIVE)
@Override
SnapshotHierarchy getRoot() {
return root
}
@Override
void update(VirtualFileSystem.UpdateFunction updateFunction) {
def diffListener = new SnapshotCollectingDiffListener({ path -> true})
root = updateFunction.update(root, diffListener)
diffListener.publishSnapshotDiff(updater, root)
}
}
def setup() {
updater = createUpdater(watcher)
}
abstract FileWatcherUpdater createUpdater(FileWatcher watcher)
TestFile file(Object... path) {
temporaryFolder.testDirectory.file(path)
}
CompleteDirectorySnapshot snapshotDirectory(File directory) {
directorySnapshotter.snapshot(directory.absolutePath, null, new AtomicBoolean(false)) as CompleteDirectorySnapshot
}
void addSnapshot(CompleteFileSystemLocationSnapshot snapshot) {
virtualFileSystem.update({ currentRoot, listener -> currentRoot.store(snapshot.absolutePath, snapshot, listener) })
}
void invalidate(String absolutePath) {
virtualFileSystem.update({ currentRoot, listener -> currentRoot.invalidate(absolutePath, listener) })
}
void invalidate(CompleteFileSystemLocationSnapshot snapshot) {
invalidate(snapshot.absolutePath)
}
static RegularFileSnapshot snapshotRegularFile(File regularFile) {
def attributes = Files.readAttributes(regularFile.toPath(), BasicFileAttributes)
new RegularFileSnapshot(
regularFile.absolutePath,
regularFile.name,
TestFiles.fileHasher().hash(regularFile),
DefaultFileMetadata.file(attributes.lastModifiedTime().toMillis(), attributes.size(), AccessType.DIRECT)
)
}
static boolean equalIgnoringOrder(Object actual, Collection<?> expected) {
List<?> actualSorted = (actual as List).toSorted()
List<?> expectedSorted = (expected as List).toSorted()
return actualSorted == expectedSorted
}
}