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 10.0KB

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