-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DefaultFileOperationsTest.groovy
executable file
·256 lines (211 loc) · 8.02 KB
/
DefaultFileOperationsTest.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2010 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.file
import org.gradle.api.InvalidUserDataException
import org.gradle.api.PathValidation
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.internal.file.archive.TarFileTree
import org.gradle.api.internal.file.archive.ZipFileTree
import org.gradle.api.internal.file.collections.DefaultDirectoryFileTreeFactory
import org.gradle.api.internal.file.collections.FileTreeAdapter
import org.gradle.api.internal.file.copy.DefaultCopySpec
import org.gradle.api.internal.file.temp.DefaultTemporaryFileProvider
import org.gradle.api.internal.file.temp.TemporaryFileProvider
import org.gradle.api.internal.resources.DefaultResourceHandler
import org.gradle.cache.internal.TestCaches
import org.gradle.internal.hash.FileHasher
import org.gradle.internal.reflect.Instantiator
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.gradle.util.TestUtil
import org.gradle.util.UsesNativeServices
import org.junit.Rule
import spock.lang.Specification
import spock.lang.TempDir
@UsesNativeServices
class DefaultFileOperationsTest extends Specification {
@Rule
public final TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider(getClass())
@TempDir
File cacheDir
private final FileResolver resolver = Mock() {
getPatternSetFactory() >> TestFiles.getPatternSetFactory()
}
private final TemporaryFileProvider temporaryFileProvider = new DefaultTemporaryFileProvider(() -> cacheDir);
private final Instantiator instantiator = TestUtil.instantiatorFactory().decorateLenient()
private final DefaultDirectoryFileTreeFactory directoryFileTreeFactory = Mock()
private final FileHasher fileHasher = Mock()
private final DefaultResourceHandler.Factory resourceHandlerFactory = Mock()
private final FileCollectionFactory fileCollectionFactory = Mock()
private DefaultFileOperations fileOperations = instance()
private DefaultFileOperations instance(FileResolver resolver = resolver) {
instantiator.newInstance(
DefaultFileOperations,
resolver,
instantiator,
directoryFileTreeFactory,
fileHasher,
resourceHandlerFactory,
fileCollectionFactory,
TestUtil.objectFactory(),
TestFiles.fileSystem(),
TestFiles.patternSetFactory,
TestFiles.deleter(),
TestFiles.documentationRegistry(),
TestFiles.taskDependencyFactory(),
TestUtil.providerFactory(),
TestCaches.decompressionCacheFactory(temporaryFileProvider.newTemporaryDirectory("cache")),
null
)
}
def resolvesFile() {
when:
TestFile file = expectPathResolved('path')
then:
fileOperations.file('path') == file
}
def resolvesFileWithValidation() {
TestFile file = tmpDir.file('path')
resolver.resolve('path', PathValidation.EXISTS) >> file
expect:
fileOperations.file('path', PathValidation.EXISTS) == file
}
def resolvesURI() {
when:
URI uri = expectPathResolvedToUri('path')
then:
fileOperations.uri('path') == uri
}
def usesFactoryToCreateConfigurableFileCollection() {
def fileCollection = Mock(ConfigurableFileCollection)
when:
def result = fileOperations.configurableFiles('a', 'b', 'c')
then:
result == fileCollection
1 * fileCollectionFactory.configurableFiles() >> fileCollection
1 * fileCollection.from('a', 'b', 'c') >> fileCollection
}
def usesFactoryToCreateImmutableFiles() {
def fileCollection = Mock(FileCollectionInternal)
when:
def result = fileOperations.immutableFiles('a', 'b', 'c')
then:
result == fileCollection
1 * fileCollectionFactory.resolving(['a', 'b', 'c'] as Object[]) >> fileCollection
}
def createsFileTree() {
def tree = Mock(ConfigurableFileTree)
when:
def fileTree = fileOperations.fileTree('base')
then:
fileTree.is(tree)
1 * fileCollectionFactory.fileTree() >> tree
1 * tree.from('base')
}
def createsFileTreeFromMap() {
def tree = Mock(ConfigurableFileTree)
when:
def fileTree = fileOperations.fileTree(dir: 'base')
then:
fileTree.is(tree)
1 * fileCollectionFactory.fileTree() >> tree
1 * tree.setDir('base')
}
def createsZipFileTree() {
expectPathResolved('path')
expectTempFileCreated()
when:
def zipTree = fileOperations.zipTree('path')
then:
zipTree instanceof FileTreeAdapter
zipTree.tree instanceof ZipFileTree
}
def createsTarFileTree() {
TestFile file = tmpDir.file('path')
resolver.resolve('path') >> file
when:
def tarTree = fileOperations.tarTree('path')
then:
tarTree instanceof FileTreeAdapter
tarTree.tree instanceof TarFileTree
}
def copiesFiles() {
def fileCollection = Mock(ConfigurableFileCollection)
def fileTree = Mock(FileTreeInternal)
fileCollectionFactory.configurableFiles() >> fileCollection
fileCollection.asFileTree >> fileTree
fileTree.matching(_) >> fileTree
resolver.resolve('dir') >> tmpDir.getTestDirectory()
when:
def result = fileOperations.copy { from 'file'; into 'dir' }
then:
!result.didWork
}
def deletes() {
def fileToBeDeleted = tmpDir.file("file")
def fileCollection = Stub(FileCollectionInternal)
fileCollectionFactory.resolving(["file"] as Object[]) >> fileCollection
fileCollection.iterator() >> [fileToBeDeleted].iterator()
fileToBeDeleted.touch()
expect:
fileOperations.delete('file') == true
fileToBeDeleted.isFile() == false
}
def makesDir() {
TestFile dirToBeCreated = tmpDir.file("parentDir", "dir")
resolver.resolve('parentDir/dir') >> dirToBeCreated
when:
File actualDir = fileOperations.mkdir('parentDir/dir')
then:
actualDir == dirToBeCreated
actualDir.isDirectory() == true
}
def makesDirThrowsExceptionIfPathPointsToFile() {
TestFile dirToBeCreated = tmpDir.file("parentDir", "dir")
dirToBeCreated.touch();
resolver.resolve('parentDir/dir') >> dirToBeCreated
when:
fileOperations.mkdir('parentDir/dir')
then:
thrown(InvalidUserDataException)
}
def createsCopySpec() {
when:
def spec = fileOperations.copySpec { include 'pattern' }
then:
spec instanceof DefaultCopySpec
spec.includes == ['pattern'] as Set
}
private TestFile expectPathResolved(String path) {
TestFile file = tmpDir.file(path)
resolver.resolve(path) >> file
return file
}
private URI expectPathResolvedToUri(String path) {
TestFile file = tmpDir.file(path)
resolver.resolveUri(path) >> file.toURI()
return file.toURI()
}
private TestFile expectTempFileCreated() {
TestFile file = tmpDir.file('expandedArchives')
temporaryFileProvider.newTemporaryFile('expandedArchives') >> file
return file
}
def resolver() {
return TestFiles.resolver(tmpDir.testDirectory)
}
}