/subprojects/core/src/test/groovy/org/gradle/api/internal/file/BaseDirFileResolverSpec.groovy

https://github.com/andrewhj-mn/gradle · Groovy · 218 lines · 164 code · 39 blank · 15 comment · 30 complexity · 805abe80303840beb5450d1229cd4a15 MD5 · raw file

  1. /*
  2. * Copyright 2011 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.api.internal.file
  17. import org.gradle.internal.typeconversion.UnsupportedNotationException
  18. import org.gradle.test.fixtures.file.TestFile
  19. import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
  20. import org.gradle.util.Requires
  21. import org.gradle.util.TestPrecondition
  22. import org.junit.Rule
  23. import spock.lang.Specification
  24. import static org.gradle.util.TextUtil.toPlatformLineSeparators
  25. class BaseDirFileResolverSpec extends Specification {
  26. @Rule TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider()
  27. @Requires(TestPrecondition.SYMLINKS)
  28. def "normalizes absolute path which points to an absolute link"() {
  29. def target = createFile(new File(tmpDir.testDirectory, 'target.txt'))
  30. def file = new File(tmpDir.testDirectory, 'a/other.txt')
  31. createLink(file, target)
  32. assert file.exists() && file.file
  33. expect:
  34. normalize(file) == file
  35. }
  36. @Requires(TestPrecondition.SYMLINKS)
  37. def "normalizes absolute path which points to a relative link"() {
  38. def target = createFile(new File(tmpDir.testDirectory, 'target.txt'))
  39. def file = new File(tmpDir.testDirectory, 'a/other.txt')
  40. createLink(file, '../target.txt')
  41. assert file.exists() && file.file
  42. expect:
  43. normalize(file) == file
  44. }
  45. @Requires(TestPrecondition.CASE_INSENSITIVE_FS)
  46. def "normalizes absolute path which has mismatched case"() {
  47. def file = createFile(new File(tmpDir.testDirectory, 'dir/file.txt'))
  48. def path = new File(tmpDir.testDirectory, 'dir/FILE.txt')
  49. assert path.exists() && path.file
  50. expect:
  51. normalize(path) == file
  52. }
  53. @Requires([TestPrecondition.SYMLINKS, TestPrecondition.CASE_INSENSITIVE_FS])
  54. def "normalizes absolute path which points to a target using mismatched case"() {
  55. def target = createFile(new File(tmpDir.testDirectory, 'target.txt'))
  56. def file = new File(tmpDir.testDirectory, 'dir/file.txt')
  57. createLink(file, target)
  58. def path = new File(tmpDir.testDirectory, 'dir/FILE.txt')
  59. assert path.exists() && path.file
  60. expect:
  61. normalize(path) == file
  62. }
  63. @Requires(TestPrecondition.SYMLINKS)
  64. def "normalizes path which points to a link to something that does not exist"() {
  65. def file = new File(tmpDir.testDirectory, 'a/other.txt')
  66. createLink(file, 'unknown.txt')
  67. assert !file.exists() && !file.file
  68. expect:
  69. normalize(file) == file
  70. }
  71. @Requires(TestPrecondition.SYMLINKS)
  72. def "normalizes path when ancestor is an absolute link"() {
  73. def target = createFile(new File(tmpDir.testDirectory, 'target/file.txt'))
  74. def file = new File(tmpDir.testDirectory, 'a/b/file.txt')
  75. createLink(file.parentFile, target.parentFile)
  76. assert file.exists() && file.file
  77. expect:
  78. normalize(file) == file
  79. }
  80. @Requires(TestPrecondition.CASE_INSENSITIVE_FS)
  81. def "normalizes path when ancestor has mismatched case"() {
  82. def file = createFile(new File(tmpDir.testDirectory, "a/b/file.txt"))
  83. def path = new File(tmpDir.testDirectory, "A/b/file.txt")
  84. assert file.exists() && file.file
  85. expect:
  86. normalize(path) == file
  87. }
  88. @Requires(TestPrecondition.CASE_INSENSITIVE_FS)
  89. def "normalizes ancestor with mismatched case when target file does not exist"() {
  90. tmpDir.createDir("a")
  91. def file = new File(tmpDir.testDirectory, "a/b/file.txt")
  92. def path = new File(tmpDir.testDirectory, "A/b/file.txt")
  93. expect:
  94. normalize(path) == file
  95. }
  96. def "normalizes relative path"() {
  97. def ancestor = new File(tmpDir.testDirectory, "test")
  98. def baseDir = new File(ancestor, "base")
  99. def sibling = new File(ancestor, "sub")
  100. def child = createFile(new File(baseDir, "a/b/file.txt"))
  101. expect:
  102. normalize("a/b/file.txt", baseDir) == child
  103. normalize("./a/b/file.txt", baseDir) == child
  104. normalize(".//a/b//file.txt", baseDir) == child
  105. normalize("sub/../a/b/file.txt", baseDir) == child
  106. normalize("../sub", baseDir) == sibling
  107. normalize("..", baseDir) == ancestor
  108. normalize(".", baseDir) == baseDir
  109. }
  110. @Requires(TestPrecondition.SYMLINKS)
  111. def "normalizes relative path when base dir is a link"() {
  112. def target = createFile(new File(tmpDir.testDirectory, 'target/file.txt'))
  113. def baseDir = new File(tmpDir.testDirectory, 'base')
  114. createLink(baseDir, "target")
  115. def file = new File(baseDir, 'file.txt')
  116. assert file.exists() && file.file
  117. expect:
  118. normalize('file.txt', baseDir) == file
  119. }
  120. @Requires(TestPrecondition.WINDOWS)
  121. def "normalizes path which uses windows 8.3 name"() {
  122. def file = createFile(new File(tmpDir.testDirectory, 'dir/file-with-long-name.txt'))
  123. def path = new File(tmpDir.testDirectory, 'dir/FILE-W~1.TXT')
  124. assert path.exists() && path.file
  125. expect:
  126. normalize(path) == file
  127. }
  128. def "normalizes file system roots"() {
  129. expect:
  130. normalize(root) == root
  131. where:
  132. root << getFsRoots()
  133. }
  134. @Requires(TestPrecondition.WINDOWS)
  135. def "normalizes non-existent file system root"() {
  136. def file = new File("Q:\\")
  137. assert !file.exists()
  138. assert file.absolute
  139. expect:
  140. normalize(file) == file
  141. }
  142. def "normalizes relative path that refers to ancestor of file system root"() {
  143. File root = getFsRoots()[0]
  144. expect:
  145. normalize("../../..", root) == root
  146. }
  147. def "cannot resolve file using unsupported notation"() {
  148. when:
  149. resolver().resolve(12)
  150. then:
  151. UnsupportedNotationException e = thrown()
  152. e.message == toPlatformLineSeparators("""Cannot convert the provided notation to a File or URI: 12.
  153. The following types/formats are supported:
  154. - A String or CharSequence path, for example 'src/main/java' or '/usr/include'.
  155. - A String or CharSequence URI, for example 'file:/usr/include'.
  156. - A File instance.
  157. - A URI or URL instance.""")
  158. }
  159. def createLink(File link, File target) {
  160. createLink(link, target.absolutePath)
  161. }
  162. def createLink(File link, String target) {
  163. new TestFile(link).createLink(target)
  164. }
  165. def createFile(File file) {
  166. file.parentFile.mkdirs()
  167. file.text = 'content'
  168. file
  169. }
  170. def normalize(Object path, File baseDir = tmpDir.testDirectory) {
  171. resolver(baseDir).resolve(path)
  172. }
  173. private BaseDirFileResolver resolver(File baseDir = tmpDir.testDirectory) {
  174. new BaseDirFileResolver(TestFiles.fileSystem(), baseDir)
  175. }
  176. private File[] getFsRoots() {
  177. File.listRoots().findAll { !it.absolutePath.startsWith("A:") }
  178. }
  179. }