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_cvs_test.rb 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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. require 'pp'
  20. class RepositoryCvsTest < ActiveSupport::TestCase
  21. fixtures :projects
  22. include Redmine::I18n
  23. REPOSITORY_PATH = repository_path('cvs')
  24. REPOSITORY_PATH.tr!('/', "\\") if Redmine::Platform.mswin?
  25. # CVS module
  26. MODULE_NAME = 'test'
  27. CHANGESETS_NUM = 7
  28. def setup
  29. User.current = nil
  30. @project = Project.find(3)
  31. @repository = Repository::Cvs.create(:project => @project,
  32. :root_url => REPOSITORY_PATH,
  33. :url => MODULE_NAME,
  34. :log_encoding => 'UTF-8')
  35. assert @repository
  36. end
  37. def test_blank_module_error_message
  38. set_language_if_valid 'en'
  39. repo = Repository::Cvs.new(
  40. :project => @project,
  41. :identifier => 'test',
  42. :log_encoding => 'UTF-8',
  43. :root_url => REPOSITORY_PATH
  44. )
  45. assert !repo.save
  46. assert_include "Module cannot be blank",
  47. repo.errors.full_messages
  48. end
  49. def test_blank_module_error_message_fr
  50. set_language_if_valid 'fr'
  51. repo = Repository::Cvs.new(
  52. :project => @project,
  53. :identifier => 'test',
  54. :log_encoding => 'UTF-8',
  55. :path_encoding => '',
  56. :url => '',
  57. :root_url => REPOSITORY_PATH
  58. )
  59. assert !repo.save
  60. assert_include 'Module doit être renseigné(e)', repo.errors.full_messages
  61. end
  62. def test_blank_cvsroot_error_message
  63. set_language_if_valid 'en'
  64. repo = Repository::Cvs.new(
  65. :project => @project,
  66. :identifier => 'test',
  67. :log_encoding => 'UTF-8',
  68. :url => MODULE_NAME
  69. )
  70. assert !repo.save
  71. assert_include "CVSROOT cannot be blank",
  72. repo.errors.full_messages
  73. end
  74. def test_blank_cvsroot_error_message_fr
  75. set_language_if_valid 'fr'
  76. repo = Repository::Cvs.new(
  77. :project => @project,
  78. :identifier => 'test',
  79. :log_encoding => 'UTF-8',
  80. :path_encoding => '',
  81. :url => MODULE_NAME,
  82. :root_url => ''
  83. )
  84. assert !repo.save
  85. assert_include 'CVSROOT doit être renseigné(e)', repo.errors.full_messages
  86. end
  87. def test_root_url_should_be_validated_against_regexp_set_in_configuration
  88. Redmine::Configuration.with 'scm_cvs_path_regexp' => '/cvspath/[a-z]+' do
  89. repo = Repository::Cvs.new(
  90. :project => @project,
  91. :identifier => 'test',
  92. :log_encoding => 'UTF-8',
  93. :path_encoding => '',
  94. :url => MODULE_NAME
  95. )
  96. repo.root_url = '/wrong_path'
  97. assert !repo.valid?
  98. assert repo.errors[:root_url].present?
  99. repo.root_url = '/cvspath/foo'
  100. assert repo.valid?
  101. end
  102. end
  103. if File.directory?(REPOSITORY_PATH)
  104. def test_fetch_changesets_from_scratch
  105. assert_equal 0, @repository.changesets.count
  106. @repository.fetch_changesets
  107. @project.reload
  108. assert_equal CHANGESETS_NUM, @repository.changesets.count
  109. assert_equal 16, @repository.filechanges.count
  110. assert_not_nil @repository.changesets.find_by_comments('Two files changed')
  111. r2 = @repository.changesets.find_by_revision('2')
  112. assert_equal 'v1-20071213-162510', r2.scmid
  113. end
  114. def test_fetch_changesets_incremental
  115. assert_equal 0, @repository.changesets.count
  116. @repository.fetch_changesets
  117. @project.reload
  118. assert_equal CHANGESETS_NUM, @repository.changesets.count
  119. # Remove changesets with revision > 3
  120. @repository.changesets.each {|c| c.destroy if c.revision.to_i > 3}
  121. @project.reload
  122. @repository.reload
  123. assert_equal 3, @repository.changesets.count
  124. assert_equal %w|3 2 1|, @repository.changesets.collect(&:revision)
  125. rev3_commit = @repository.changesets.reorder('committed_on DESC').first
  126. assert_equal '3', rev3_commit.revision
  127. # 2007-12-14 01:27:22 +0900
  128. rev3_committed_on = Time.gm(2007, 12, 13, 16, 27, 22)
  129. assert_equal 'HEAD-20071213-162722', rev3_commit.scmid
  130. assert_equal rev3_committed_on, rev3_commit.committed_on
  131. latest_rev = @repository.latest_changeset
  132. assert_equal rev3_committed_on, latest_rev.committed_on
  133. @repository.fetch_changesets
  134. @project.reload
  135. @repository.reload
  136. assert_equal CHANGESETS_NUM, @repository.changesets.count
  137. assert_equal %w|7 6 5 4 3 2 1|, @repository.changesets.collect(&:revision)
  138. rev5_commit = @repository.changesets.find_by_revision('5')
  139. assert_equal 'HEAD-20071213-163001', rev5_commit.scmid
  140. # 2007-12-14 01:30:01 +0900
  141. rev5_committed_on = Time.gm(2007, 12, 13, 16, 30, 1)
  142. assert_equal rev5_committed_on, rev5_commit.committed_on
  143. end
  144. def test_deleted_files_should_not_be_listed
  145. assert_equal 0, @repository.changesets.count
  146. @repository.fetch_changesets
  147. @project.reload
  148. assert_equal CHANGESETS_NUM, @repository.changesets.count
  149. entries = @repository.entries('sources')
  150. assert entries.detect {|e| e.name == 'watchers_controller.rb'}
  151. assert_nil entries.detect {|e| e.name == 'welcome_controller.rb'}
  152. end
  153. def test_entries_rev3
  154. assert_equal 0, @repository.changesets.count
  155. @repository.fetch_changesets
  156. @project.reload
  157. assert_equal CHANGESETS_NUM, @repository.changesets.count
  158. rev3_commit = @repository.changesets.find_by_revision('3')
  159. assert_equal "3", rev3_commit.revision
  160. assert_equal "LANG", rev3_commit.committer
  161. assert_equal 2, rev3_commit.filechanges.length
  162. filechanges = rev3_commit.filechanges.order(:path => :asc)
  163. assert_equal "1.2", filechanges[0].revision
  164. assert_equal "1.2", filechanges[1].revision
  165. assert_equal "/README", filechanges[0].path
  166. assert_equal "/sources/watchers_controller.rb", filechanges[1].path
  167. entries = @repository.entries('', '3')
  168. assert_kind_of Redmine::Scm::Adapters::Entries, entries
  169. assert_equal 3, entries.size
  170. assert_equal "README", entries[2].name
  171. assert_equal 'UTF-8', entries[2].path.encoding.to_s
  172. assert_equal Time.gm(2007, 12, 13, 16, 27, 22), entries[2].lastrev.time
  173. assert_equal '3', entries[2].lastrev.identifier
  174. assert_equal '3', entries[2].lastrev.revision
  175. assert_equal 'LANG', entries[2].lastrev.author
  176. end
  177. def test_entries_invalid_path
  178. assert_equal 0, @repository.changesets.count
  179. @repository.fetch_changesets
  180. @project.reload
  181. assert_equal CHANGESETS_NUM, @repository.changesets.count
  182. assert_nil @repository.entries('missing')
  183. assert_nil @repository.entries('missing', '3')
  184. end
  185. def test_entries_invalid_revision
  186. assert_equal 0, @repository.changesets.count
  187. @repository.fetch_changesets
  188. @project.reload
  189. assert_equal CHANGESETS_NUM, @repository.changesets.count
  190. assert_nil @repository.entries('', '123')
  191. end
  192. def test_cat
  193. assert_equal 0, @repository.changesets.count
  194. @repository.fetch_changesets
  195. @project.reload
  196. assert_equal CHANGESETS_NUM, @repository.changesets.count
  197. buf = @repository.cat('README')
  198. assert buf
  199. lines = buf.split("\n")
  200. assert_equal 3, lines.length
  201. buf = lines[1].gsub(/\r$/, "")
  202. assert_equal 'with one change', buf
  203. buf = @repository.cat('README', '1')
  204. assert buf
  205. lines = buf.split("\n")
  206. assert_equal 1, lines.length
  207. buf = lines[0].gsub(/\r$/, "")
  208. assert_equal 'CVS test repository', buf
  209. assert_nil @repository.cat('missing.rb')
  210. # sources/welcome_controller.rb is removed at revision 5.
  211. assert @repository.cat('sources/welcome_controller.rb', '4')
  212. assert @repository.cat('sources/welcome_controller.rb', '5').blank?
  213. # invalid revision
  214. assert @repository.cat('README', '123').blank?
  215. end
  216. def test_annotate
  217. assert_equal 0, @repository.changesets.count
  218. @repository.fetch_changesets
  219. @project.reload
  220. assert_equal CHANGESETS_NUM, @repository.changesets.count
  221. ann = @repository.annotate('README')
  222. assert ann
  223. assert_equal 3, ann.revisions.length
  224. assert_equal '1.2', ann.revisions[1].revision
  225. assert_equal 'LANG', ann.revisions[1].author
  226. assert_equal 'with one change', ann.lines[1]
  227. ann = @repository.annotate('README', '1')
  228. assert ann
  229. assert_equal 1, ann.revisions.length
  230. assert_equal '1.1', ann.revisions[0].revision
  231. assert_equal 'LANG', ann.revisions[0].author
  232. assert_equal 'CVS test repository', ann.lines[0]
  233. # invalid revision
  234. assert_nil @repository.annotate('README', '123')
  235. end
  236. else
  237. puts "CVS test repository NOT FOUND. Skipping unit tests !!!"
  238. def test_fake; assert true end
  239. end
  240. end