/subprojects/dependency-management/src/test/groovy/org/gradle/api/internal/artifacts/mvnsettings/DefaultLocalMavenRepositoryLocatorTest.groovy

https://github.com/andrewhj-mn/gradle · Groovy · 188 lines · 139 code · 32 blank · 17 comment · 15 complexity · 9fb1823923a3b12f64436f3ae31ca61c MD5 · raw file

  1. /*
  2. * Copyright 2010 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.artifacts.mvnsettings
  17. import org.gradle.test.fixtures.file.TestFile
  18. import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
  19. import org.junit.Rule
  20. import spock.lang.Specification
  21. import spock.lang.Unroll
  22. class DefaultLocalMavenRepositoryLocatorTest extends Specification {
  23. @Rule TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider()
  24. SimpleMavenFileLocations locations
  25. DefaultLocalMavenRepositoryLocator locator
  26. def system = Mock(DefaultLocalMavenRepositoryLocator.SystemPropertyAccess)
  27. File repo1 = tmpDir.file("repo1")
  28. File repo2 = tmpDir.file("repo2")
  29. File userHome1 = tmpDir.file("user_home_1")
  30. File userHome2 = tmpDir.file("user_home_2")
  31. def setup() {
  32. locations = new SimpleMavenFileLocations()
  33. locator = new DefaultLocalMavenRepositoryLocator(new DefaultMavenSettingsProvider(locations), system)
  34. }
  35. def "returns default location if no settings file exists"() {
  36. when:
  37. 1 * system.getProperty("user.home") >> userHome1.absolutePath
  38. then:
  39. locator.localMavenRepository == new File(userHome1, ".m2/repository")
  40. // Ensure that modified user.home is honoured (see http://forums.gradle.org/gradle/topics/override_location_of_the_local_maven_repo)
  41. when:
  42. 1 * system.getProperty("user.home") >> userHome2.absolutePath
  43. then:
  44. locator.localMavenRepository == new File(userHome2, ".m2/repository")
  45. }
  46. def "returns value of system property if it is specified"() {
  47. when:
  48. 1 * system.getProperty("maven.repo.local") >> repo1.absolutePath
  49. then:
  50. locator.localMavenRepository == repo1
  51. // Ensure that modified system property is honoured
  52. when:
  53. 1 * system.getProperty("maven.repo.local") >> repo2.absolutePath
  54. then:
  55. locator.localMavenRepository == repo2
  56. }
  57. def "throws exception on broken global settings file with decent error message"() {
  58. given:
  59. def settingsFile = locations.globalSettingsFile
  60. settingsFile << "broken content"
  61. when:
  62. locator.localMavenRepository
  63. then:
  64. def ex = thrown(CannotLocateLocalMavenRepositoryException);
  65. ex.message == "Unable to parse local Maven settings."
  66. ex.cause.message.contains(settingsFile.absolutePath)
  67. }
  68. def "throws exception on broken user settings file with decent error message"() {
  69. given:
  70. def settingsFile = locations.userSettingsFile
  71. settingsFile << "broken content"
  72. when:
  73. locator.localMavenRepository
  74. then:
  75. def ex = thrown(CannotLocateLocalMavenRepositoryException)
  76. ex.message == "Unable to parse local Maven settings."
  77. ex.cause.message.contains(settingsFile.absolutePath)
  78. }
  79. def "honors location specified in user settings file"() {
  80. writeSettingsFile(locations.userSettingsFile, repo1)
  81. expect:
  82. locator.localMavenRepository == repo1
  83. }
  84. def "ignores changes to maven settings file after initial load"() {
  85. when:
  86. writeSettingsFile(locations.userSettingsFile, repo1)
  87. then:
  88. locator.localMavenRepository == repo1
  89. when:
  90. writeSettingsFile(locations.userSettingsFile, repo2)
  91. then:
  92. locator.localMavenRepository == repo1
  93. }
  94. def "honors location specified in global settings file"() {
  95. writeSettingsFile(locations.globalSettingsFile, repo1)
  96. expect:
  97. locator.localMavenRepository == repo1
  98. }
  99. def "prefers location specified in user settings file over that in global settings file"() {
  100. writeSettingsFile(locations.userSettingsFile, repo1)
  101. writeSettingsFile(locations.globalSettingsFile, repo2)
  102. expect:
  103. locator.localMavenRepository == repo1
  104. }
  105. def "handles the case where (potential) location of global settings file cannot be determined"() {
  106. locations.globalSettingsFile = null
  107. when:
  108. system.getProperty("user.home") >> userHome1.absolutePath
  109. then:
  110. locator.localMavenRepository == new File(userHome1, ".m2/repository")
  111. when:
  112. writeSettingsFile(locations.userSettingsFile, repo1)
  113. then:
  114. locator.localMavenRepository == repo1
  115. }
  116. def "replaces placeholders for system properties and environment variables"() {
  117. when:
  118. writeSettingsFile(locations.userSettingsFile, tmpDir.file('${sys.prop}/${env.ENV_VAR}'))
  119. and:
  120. system.getProperty("sys.prop") >> "sys/prop/value"
  121. system.getEnv("ENV_VAR") >> "env/var/value"
  122. then:
  123. locator.localMavenRepository == tmpDir.file("sys/prop/value/env/var/value")
  124. }
  125. @Unroll
  126. def "unresolvable placeholder for #propType throws exception with decent error message"() {
  127. TestFile repoPath = tmpDir.file("\${$prop}")
  128. writeSettingsFile(locations.userSettingsFile, repoPath)
  129. when:
  130. locator.localMavenRepository
  131. then:
  132. def ex = thrown(CannotLocateLocalMavenRepositoryException);
  133. ex.message == "Cannot resolve placeholder '${prop}' in value '${repoPath.absolutePath}'"
  134. where:
  135. prop | propType
  136. 'sys.unknown.prop' | "system property"
  137. 'env.unknown.ENV_VAR' | "environment variable"
  138. }
  139. private void writeSettingsFile(File settings, File repo) {
  140. writeSettingsFile(settings, repo.absolutePath)
  141. }
  142. private void writeSettingsFile(File settings, String repo) {
  143. settings.text = """
  144. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  145. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  146. <localRepository>$repo</localRepository>
  147. </settings>"""
  148. }
  149. private class SimpleMavenFileLocations implements MavenFileLocations {
  150. File userMavenDir
  151. File globalMavenDir
  152. File userSettingsFile = tmpDir.file("userSettingsFile")
  153. File globalSettingsFile = tmpDir.file("globalSettingsFile")
  154. }
  155. }