You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repository_subversion_test.rb 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class RepositorySubversionTest < ActiveSupport::TestCase
  20. fixtures :projects, :repositories, :enabled_modules, :users, :roles
  21. include Redmine::I18n
  22. NUM_REV = 14
  23. def setup
  24. User.current = nil
  25. @project = Project.find(3)
  26. @repository = Repository::Subversion.create(:project => @project,
  27. :url => self.class.subversion_repository_url)
  28. assert @repository
  29. end
  30. def test_invalid_url
  31. set_language_if_valid 'en'
  32. ['invalid', 'http://', 'svn://', 'svn+ssh://', 'file://'].each do |url|
  33. repo =
  34. Repository::Subversion.new(
  35. :project => @project,
  36. :identifier => 'test',
  37. :url => url
  38. )
  39. assert !repo.save
  40. assert_equal ["is invalid"], repo.errors[:url]
  41. end
  42. end
  43. def test_valid_url
  44. ['http://valid', 'svn://valid', 'svn+ssh://valid', 'file://valid'].each do |url|
  45. repo =
  46. Repository::Subversion.new(
  47. :project => @project,
  48. :identifier => 'test',
  49. :url => url
  50. )
  51. assert repo.save
  52. assert_equal [], repo.errors[:url]
  53. assert repo.destroy
  54. end
  55. end
  56. def test_url_should_be_validated_against_regexp_set_in_configuration
  57. Redmine::Configuration.with 'scm_subversion_path_regexp' => 'file:///svnpath/[a-z]+' do
  58. repo = Repository::Subversion.new(:project => @project, :identifier => 'test')
  59. repo.url = 'http://foo'
  60. assert !repo.valid?
  61. assert repo.errors[:url].present?
  62. repo.url = 'file:///svnpath/foo/bar'
  63. assert !repo.valid?
  64. assert repo.errors[:url].present?
  65. repo.url = 'file:///svnpath/foo'
  66. assert repo.valid?
  67. end
  68. end
  69. def test_url_should_be_validated_against_regexp_set_in_configuration_with_project_identifier
  70. Redmine::Configuration.with 'scm_subversion_path_regexp' => 'file:///svnpath/%project%(\.[a-z]+)?' do
  71. repo = Repository::Subversion.new(:project => @project, :identifier => 'test')
  72. repo.url = 'file:///svnpath/invalid'
  73. assert !repo.valid?
  74. assert repo.errors[:url].present?
  75. repo.url = 'file:///svnpath/subproject1'
  76. assert repo.valid?
  77. repo.url = 'file:///svnpath/subproject1.foo'
  78. assert repo.valid?
  79. end
  80. end
  81. if repository_configured?('subversion')
  82. def test_fetch_changesets_from_scratch
  83. assert_equal 0, @repository.changesets.count
  84. @repository.fetch_changesets
  85. @project.reload
  86. assert_equal NUM_REV, @repository.changesets.count
  87. assert_equal 24, @repository.filechanges.count
  88. assert_equal 'Initial import.', @repository.changesets.find_by_revision('1').comments
  89. end
  90. def test_fetch_changesets_incremental
  91. assert_equal 0, @repository.changesets.count
  92. @repository.fetch_changesets
  93. @project.reload
  94. assert_equal NUM_REV, @repository.changesets.count
  95. # Remove changesets with revision > 5
  96. @repository.changesets.each {|c| c.destroy if c.revision.to_i > 5}
  97. @project.reload
  98. @repository.reload
  99. assert_equal 5, @repository.changesets.count
  100. @repository.fetch_changesets
  101. @project.reload
  102. assert_equal NUM_REV, @repository.changesets.count
  103. end
  104. def test_entries
  105. entries = @repository.entries
  106. assert_kind_of Redmine::Scm::Adapters::Entries, entries
  107. end
  108. def test_entries_for_invalid_path_should_return_nil
  109. entries = @repository.entries('invalid_path')
  110. assert_nil entries
  111. end
  112. def test_latest_changesets
  113. assert_equal 0, @repository.changesets.count
  114. @repository.fetch_changesets
  115. @project.reload
  116. assert_equal NUM_REV, @repository.changesets.count
  117. # with limit
  118. changesets = @repository.latest_changesets('', nil, 2)
  119. assert_equal 2, changesets.size
  120. assert_equal @repository.latest_changesets('', nil).slice(0, 2), changesets
  121. # with path
  122. changesets = @repository.latest_changesets('subversion_test/folder', nil)
  123. assert_equal ["13", "12", "10", "9", "7", "6", "5", "2"], changesets.collect(&:revision)
  124. # with path and revision
  125. changesets = @repository.latest_changesets('subversion_test/folder', 8)
  126. assert_equal ["7", "6", "5", "2"], changesets.collect(&:revision)
  127. end
  128. def test_directory_listing_with_square_brackets_in_path
  129. assert_equal 0, @repository.changesets.count
  130. @repository.fetch_changesets
  131. @project.reload
  132. assert_equal NUM_REV, @repository.changesets.count
  133. entries = @repository.entries('subversion_test/[folder_with_brackets]')
  134. assert_not_nil entries, 'Expect to find entries in folder_with_brackets'
  135. assert_equal 1, entries.size, 'Expect one entry in folder_with_brackets'
  136. assert_equal 'README.txt', entries.first.name
  137. end
  138. def test_directory_listing_with_square_brackets_in_base
  139. @project = Project.find(3)
  140. @repository =
  141. Repository::Subversion.create(
  142. :project => @project,
  143. :url => "file:///#{self.class.repository_path('subversion')}/subversion_test/[folder_with_brackets]"
  144. )
  145. assert_equal 0, @repository.changesets.count
  146. @repository.fetch_changesets
  147. @project.reload
  148. assert_equal 1, @repository.changesets.count, 'Expected to see 1 revision'
  149. assert_equal 2, @repository.filechanges.count, 'Expected to see 2 changes, dir add and file add'
  150. entries = @repository.entries('')
  151. assert_not_nil entries, 'Expect to find entries'
  152. assert_equal 1, entries.size, 'Expect a single entry'
  153. assert_equal 'README.txt', entries.first.name
  154. end
  155. def test_identifier
  156. assert_equal 0, @repository.changesets.count
  157. @repository.fetch_changesets
  158. @project.reload
  159. assert_equal NUM_REV, @repository.changesets.count
  160. c = @repository.changesets.find_by_revision('1')
  161. assert_equal c.revision, c.identifier
  162. end
  163. def test_find_changeset_by_empty_name
  164. assert_equal 0, @repository.changesets.count
  165. @repository.fetch_changesets
  166. @project.reload
  167. assert_equal NUM_REV, @repository.changesets.count
  168. ['', ' ', nil].each do |r|
  169. assert_nil @repository.find_changeset_by_name(r)
  170. end
  171. end
  172. def test_identifier_nine_digit
  173. c = Changeset.new(:repository => @repository, :committed_on => Time.now,
  174. :revision => '123456789', :comments => 'test')
  175. assert_equal c.identifier, c.revision
  176. end
  177. def test_format_identifier
  178. assert_equal 0, @repository.changesets.count
  179. @repository.fetch_changesets
  180. @project.reload
  181. assert_equal NUM_REV, @repository.changesets.count
  182. c = @repository.changesets.find_by_revision('1')
  183. assert_equal c.format_identifier, c.revision
  184. end
  185. def test_format_identifier_nine_digit
  186. c = Changeset.new(:repository => @repository, :committed_on => Time.now,
  187. :revision => '123456789', :comments => 'test')
  188. assert_equal c.format_identifier, c.revision
  189. end
  190. def test_activities
  191. c = Changeset.new(:repository => @repository, :committed_on => Time.now,
  192. :revision => '1', :comments => 'test')
  193. assert c.event_title.include?('1:')
  194. assert_equal '1', c.event_url[:rev]
  195. end
  196. def test_activities_nine_digit
  197. c = Changeset.new(:repository => @repository, :committed_on => Time.now,
  198. :revision => '123456789', :comments => 'test')
  199. assert c.event_title.include?('123456789:')
  200. assert_equal '123456789', c.event_url[:rev]
  201. end
  202. def test_log_encoding_ignore_setting
  203. with_settings :commit_logs_encoding => 'windows-1252' do
  204. s2 = "Â\u0080"
  205. c = Changeset.new(:repository => @repository,
  206. :comments => s2,
  207. :revision => '123',
  208. :committed_on => Time.now)
  209. assert c.save
  210. assert_equal s2, c.comments
  211. end
  212. end
  213. def test_previous
  214. assert_equal 0, @repository.changesets.count
  215. @repository.fetch_changesets
  216. @project.reload
  217. assert_equal NUM_REV, @repository.changesets.count
  218. changeset = @repository.find_changeset_by_name('3')
  219. assert_equal @repository.find_changeset_by_name('2'), changeset.previous
  220. end
  221. def test_previous_nil
  222. assert_equal 0, @repository.changesets.count
  223. @repository.fetch_changesets
  224. @project.reload
  225. assert_equal NUM_REV, @repository.changesets.count
  226. changeset = @repository.find_changeset_by_name('1')
  227. assert_nil changeset.previous
  228. end
  229. def test_next
  230. assert_equal 0, @repository.changesets.count
  231. @repository.fetch_changesets
  232. @project.reload
  233. assert_equal NUM_REV, @repository.changesets.count
  234. changeset = @repository.find_changeset_by_name('2')
  235. assert_equal @repository.find_changeset_by_name('3'), changeset.next
  236. end
  237. def test_next_nil
  238. assert_equal 0, @repository.changesets.count
  239. @repository.fetch_changesets
  240. @project.reload
  241. assert_equal NUM_REV, @repository.changesets.count
  242. changeset = @repository.find_changeset_by_name(NUM_REV.to_s)
  243. assert_nil changeset.next
  244. end
  245. else
  246. puts "Subversion test repository NOT FOUND. Skipping unit tests !!!"
  247. def test_fake; assert true end
  248. end
  249. end