PageRenderTime 23ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/repository_mercurial_test.rb

https://gitlab.com/ba0f3/redmine
Ruby | 295 lines | 220 code | 50 blank | 25 comment | 5 complexity | 212ac1aefec327f795e1e5e039cf3e4b MD5 | raw file
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2011 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class RepositoryMercurialTest < ActiveSupport::TestCase
  19. fixtures :projects
  20. # No '..' in the repository path
  21. REPOSITORY_PATH = RAILS_ROOT.gsub(%r{config\/\.\.}, '') + '/tmp/test/mercurial_repository'
  22. CHAR_1_HEX = "\xc3\x9c"
  23. if File.directory?(REPOSITORY_PATH)
  24. def setup
  25. klass = Repository::Mercurial
  26. assert_equal "Mercurial", klass.scm_name
  27. assert klass.scm_adapter_class
  28. assert_not_equal "", klass.scm_command
  29. assert_equal true, klass.scm_available
  30. @project = Project.find(3)
  31. @repository = Repository::Mercurial.create(
  32. :project => @project,
  33. :url => REPOSITORY_PATH,
  34. :path_encoding => 'ISO-8859-1'
  35. )
  36. assert @repository
  37. @char_1 = CHAR_1_HEX.dup
  38. @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
  39. @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
  40. @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
  41. if @char_1.respond_to?(:force_encoding)
  42. @char_1.force_encoding('UTF-8')
  43. @tag_char_1.force_encoding('UTF-8')
  44. @branch_char_0.force_encoding('UTF-8')
  45. @branch_char_1.force_encoding('UTF-8')
  46. end
  47. end
  48. def test_fetch_changesets_from_scratch
  49. @repository.fetch_changesets
  50. @repository.reload
  51. assert_equal 29, @repository.changesets.count
  52. assert_equal 37, @repository.changes.count
  53. assert_equal "Initial import.\nThe repository contains 3 files.",
  54. @repository.changesets.find_by_revision('0').comments
  55. end
  56. def test_fetch_changesets_incremental
  57. @repository.fetch_changesets
  58. # Remove changesets with revision > 2
  59. @repository.changesets.find(:all).each {|c| c.destroy if c.revision.to_i > 2}
  60. @repository.reload
  61. assert_equal 3, @repository.changesets.count
  62. @repository.fetch_changesets
  63. assert_equal 29, @repository.changesets.count
  64. end
  65. def test_isodatesec
  66. # Template keyword 'isodatesec' supported in Mercurial 1.0 and higher
  67. if @repository.scm.class.client_version_above?([1, 0])
  68. @repository.fetch_changesets
  69. @repository.reload
  70. rev0_committed_on = Time.gm(2007, 12, 14, 9, 22, 52)
  71. assert_equal @repository.changesets.find_by_revision('0').committed_on, rev0_committed_on
  72. end
  73. end
  74. def test_changeset_order_by_revision
  75. @repository.fetch_changesets
  76. @repository.reload
  77. c0 = @repository.latest_changeset
  78. c1 = @repository.changesets.find_by_revision('0')
  79. # sorted by revision (id), not by date
  80. assert c0.revision.to_i > c1.revision.to_i
  81. assert c0.committed_on < c1.committed_on
  82. end
  83. def test_latest_changesets
  84. @repository.fetch_changesets
  85. @repository.reload
  86. # with_limit
  87. changesets = @repository.latest_changesets('', nil, 2)
  88. assert_equal %w|28 27|, changesets.collect(&:revision)
  89. # with_filepath
  90. changesets = @repository.latest_changesets(
  91. '/sql_escape/percent%dir/percent%file1.txt', nil)
  92. assert_equal %w|11 10 9|, changesets.collect(&:revision)
  93. changesets = @repository.latest_changesets(
  94. '/sql_escape/underscore_dir/understrike_file.txt', nil)
  95. assert_equal %w|12 9|, changesets.collect(&:revision)
  96. changesets = @repository.latest_changesets('README', nil)
  97. assert_equal %w|28 17 8 6 1 0|, changesets.collect(&:revision)
  98. changesets = @repository.latest_changesets('README','8')
  99. assert_equal %w|8 6 1 0|, changesets.collect(&:revision)
  100. changesets = @repository.latest_changesets('README','8', 2)
  101. assert_equal %w|8 6|, changesets.collect(&:revision)
  102. # with_dirpath
  103. changesets = @repository.latest_changesets('images', nil)
  104. assert_equal %w|1 0|, changesets.collect(&:revision)
  105. path = 'sql_escape/percent%dir'
  106. changesets = @repository.latest_changesets(path, nil)
  107. assert_equal %w|13 11 10 9|, changesets.collect(&:revision)
  108. changesets = @repository.latest_changesets(path, '11')
  109. assert_equal %w|11 10 9|, changesets.collect(&:revision)
  110. changesets = @repository.latest_changesets(path, '11', 2)
  111. assert_equal %w|11 10|, changesets.collect(&:revision)
  112. path = 'sql_escape/underscore_dir'
  113. changesets = @repository.latest_changesets(path, nil)
  114. assert_equal %w|13 12 9|, changesets.collect(&:revision)
  115. changesets = @repository.latest_changesets(path, '12')
  116. assert_equal %w|12 9|, changesets.collect(&:revision)
  117. changesets = @repository.latest_changesets(path, '12', 1)
  118. assert_equal %w|12|, changesets.collect(&:revision)
  119. # tag
  120. changesets = @repository.latest_changesets('', 'tag_test.00')
  121. assert_equal %w|5 4 3 2 1 0|, changesets.collect(&:revision)
  122. changesets = @repository.latest_changesets('', 'tag_test.00', 2)
  123. assert_equal %w|5 4|, changesets.collect(&:revision)
  124. changesets = @repository.latest_changesets('sources', 'tag_test.00')
  125. assert_equal %w|4 3 2 1 0|, changesets.collect(&:revision)
  126. changesets = @repository.latest_changesets('sources', 'tag_test.00', 2)
  127. assert_equal %w|4 3|, changesets.collect(&:revision)
  128. # named branch
  129. if @repository.scm.class.client_version_above?([1, 6])
  130. changesets = @repository.latest_changesets('', @branch_char_1)
  131. assert_equal %w|27 26|, changesets.collect(&:revision)
  132. end
  133. changesets = @repository.latest_changesets("latin-1-dir/test-#{@char_1}-subdir", @branch_char_1)
  134. assert_equal %w|27|, changesets.collect(&:revision)
  135. end
  136. def test_copied_files
  137. @repository.fetch_changesets
  138. @repository.reload
  139. cs1 = @repository.changesets.find_by_revision('13')
  140. assert_not_nil cs1
  141. c1 = cs1.changes.sort_by(&:path)
  142. assert_equal 2, c1.size
  143. assert_equal 'A', c1[0].action
  144. assert_equal '/sql_escape/percent%dir/percentfile1.txt', c1[0].path
  145. assert_equal '/sql_escape/percent%dir/percent%file1.txt', c1[0].from_path
  146. assert_equal '3a330eb32958', c1[0].from_revision
  147. assert_equal 'A', c1[1].action
  148. assert_equal '/sql_escape/underscore_dir/understrike-file.txt', c1[1].path
  149. assert_equal '/sql_escape/underscore_dir/understrike_file.txt', c1[1].from_path
  150. cs2 = @repository.changesets.find_by_revision('15')
  151. c2 = cs2.changes
  152. assert_equal 1, c2.size
  153. assert_equal 'A', c2[0].action
  154. assert_equal '/README (1)[2]&,%.-3_4', c2[0].path
  155. assert_equal '/README', c2[0].from_path
  156. assert_equal '933ca60293d7', c2[0].from_revision
  157. cs3 = @repository.changesets.find_by_revision('19')
  158. c3 = cs3.changes
  159. assert_equal 1, c3.size
  160. assert_equal 'A', c3[0].action
  161. assert_equal "/latin-1-dir/test-#{@char_1}-1.txt", c3[0].path
  162. assert_equal "/latin-1-dir/test-#{@char_1}.txt", c3[0].from_path
  163. assert_equal '5d9891a1b425', c3[0].from_revision
  164. end
  165. def test_find_changeset_by_name
  166. @repository.fetch_changesets
  167. @repository.reload
  168. %w|2 400bb8672109 400|.each do |r|
  169. assert_equal '2', @repository.find_changeset_by_name(r).revision
  170. end
  171. end
  172. def test_find_changeset_by_invalid_name
  173. @repository.fetch_changesets
  174. @repository.reload
  175. assert_nil @repository.find_changeset_by_name('100000')
  176. end
  177. def test_identifier
  178. @repository.fetch_changesets
  179. @repository.reload
  180. c = @repository.changesets.find_by_revision('2')
  181. assert_equal c.scmid, c.identifier
  182. end
  183. def test_format_identifier
  184. @repository.fetch_changesets
  185. @repository.reload
  186. c = @repository.changesets.find_by_revision('2')
  187. assert_equal '2:400bb8672109', c.format_identifier
  188. end
  189. def test_find_changeset_by_empty_name
  190. @repository.fetch_changesets
  191. @repository.reload
  192. ['', ' ', nil].each do |r|
  193. assert_nil @repository.find_changeset_by_name(r)
  194. end
  195. end
  196. def test_activities
  197. c = Changeset.new(:repository => @repository,
  198. :committed_on => Time.now,
  199. :revision => '123',
  200. :scmid => 'abc400bb8672',
  201. :comments => 'test')
  202. assert c.event_title.include?('123:abc400bb8672:')
  203. assert_equal 'abc400bb8672', c.event_url[:rev]
  204. end
  205. def test_previous
  206. @repository.fetch_changesets
  207. @repository.reload
  208. %w|28 3ae45e2d177d 3ae45|.each do |r1|
  209. changeset = @repository.find_changeset_by_name(r1)
  210. %w|27 7bbf4c738e71 7bbf|.each do |r2|
  211. assert_equal @repository.find_changeset_by_name(r2), changeset.previous
  212. end
  213. end
  214. end
  215. def test_previous_nil
  216. @repository.fetch_changesets
  217. @repository.reload
  218. %w|0 0885933ad4f6 0885|.each do |r1|
  219. changeset = @repository.find_changeset_by_name(r1)
  220. assert_nil changeset.previous
  221. end
  222. end
  223. def test_next
  224. @repository.fetch_changesets
  225. @repository.reload
  226. %w|27 7bbf4c738e71 7bbf|.each do |r2|
  227. changeset = @repository.find_changeset_by_name(r2)
  228. %w|28 3ae45e2d177d 3ae45|.each do |r1|
  229. assert_equal @repository.find_changeset_by_name(r1), changeset.next
  230. end
  231. end
  232. end
  233. def test_next_nil
  234. @repository.fetch_changesets
  235. @repository.reload
  236. %w|28 3ae45e2d177d 3ae45|.each do |r1|
  237. changeset = @repository.find_changeset_by_name(r1)
  238. assert_nil changeset.next
  239. end
  240. end
  241. else
  242. puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
  243. def test_fake; assert true end
  244. end
  245. end