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_test.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 RepositoryTest < ActiveSupport::TestCase
  19. fixtures :projects,
  20. :trackers,
  21. :projects_trackers,
  22. :enabled_modules,
  23. :repositories,
  24. :issues,
  25. :issue_statuses,
  26. :issue_categories,
  27. :changesets,
  28. :changes,
  29. :users,
  30. :members,
  31. :member_roles,
  32. :roles,
  33. :enumerations
  34. include Redmine::I18n
  35. def setup
  36. @repository = Project.find(1).repository
  37. end
  38. def test_blank_log_encoding_error_message
  39. set_language_if_valid 'en'
  40. repo = Repository::Bazaar.new(
  41. :project => Project.find(3),
  42. :url => "/test",
  43. :log_encoding => ''
  44. )
  45. assert !repo.save
  46. assert_include "Commit messages encoding can't be blank",
  47. repo.errors.full_messages
  48. end
  49. def test_blank_log_encoding_error_message_fr
  50. set_language_if_valid 'fr'
  51. str = "Encodage des messages de commit doit \xc3\xaatre renseign\xc3\xa9(e)"
  52. str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
  53. repo = Repository::Bazaar.new(
  54. :project => Project.find(3),
  55. :url => "/test"
  56. )
  57. assert !repo.save
  58. assert_include str, repo.errors.full_messages
  59. end
  60. def test_create
  61. repository = Repository::Subversion.new(:project => Project.find(3))
  62. assert !repository.save
  63. repository.url = "svn://localhost"
  64. assert repository.save
  65. repository.reload
  66. project = Project.find(3)
  67. assert_equal repository, project.repository
  68. end
  69. def test_first_repository_should_be_set_as_default
  70. repository1 = Repository::Subversion.new(
  71. :project => Project.find(3),
  72. :identifier => 'svn1',
  73. :url => 'file:///svn1'
  74. )
  75. assert repository1.save
  76. assert repository1.is_default?
  77. repository2 = Repository::Subversion.new(
  78. :project => Project.find(3),
  79. :identifier => 'svn2',
  80. :url => 'file:///svn2'
  81. )
  82. assert repository2.save
  83. assert !repository2.is_default?
  84. assert_equal repository1, Project.find(3).repository
  85. assert_equal [repository1, repository2], Project.find(3).repositories.sort
  86. end
  87. def test_destroy
  88. changesets = Changeset.count(:all, :conditions => "repository_id = 10")
  89. changes = Change.count(:all, :conditions => "repository_id = 10",
  90. :joins => :changeset)
  91. assert_difference 'Changeset.count', -changesets do
  92. assert_difference 'Change.count', -changes do
  93. Repository.find(10).destroy
  94. end
  95. end
  96. end
  97. def test_destroy_should_delete_parents_associations
  98. changeset = Changeset.find(102)
  99. changeset.parents = Changeset.find_all_by_id([100, 101])
  100. assert_difference 'Changeset.connection.select_all("select * from changeset_parents").size', -2 do
  101. Repository.find(10).destroy
  102. end
  103. end
  104. def test_destroy_should_delete_issues_associations
  105. changeset = Changeset.find(102)
  106. changeset.issues = Issue.find_all_by_id([1, 2])
  107. assert_difference 'Changeset.connection.select_all("select * from changesets_issues").size', -2 do
  108. Repository.find(10).destroy
  109. end
  110. end
  111. def test_should_not_create_with_disabled_scm
  112. # disable Subversion
  113. with_settings :enabled_scm => ['Darcs', 'Git'] do
  114. repository = Repository::Subversion.new(
  115. :project => Project.find(3), :url => "svn://localhost")
  116. assert !repository.save
  117. assert_include I18n.translate('activerecord.errors.messages.invalid'),
  118. repository.errors[:type]
  119. end
  120. end
  121. def test_scan_changesets_for_issue_ids
  122. Setting.default_language = 'en'
  123. Setting.notified_events = ['issue_added','issue_updated']
  124. # choosing a status to apply to fix issues
  125. Setting.commit_fix_status_id = IssueStatus.find(
  126. :first,
  127. :conditions => ["is_closed = ?", true]).id
  128. Setting.commit_fix_done_ratio = "90"
  129. Setting.commit_ref_keywords = 'refs , references, IssueID'
  130. Setting.commit_fix_keywords = 'fixes , closes'
  131. Setting.default_language = 'en'
  132. ActionMailer::Base.deliveries.clear
  133. # make sure issue 1 is not already closed
  134. fixed_issue = Issue.find(1)
  135. assert !fixed_issue.status.is_closed?
  136. old_status = fixed_issue.status
  137. Repository.scan_changesets_for_issue_ids
  138. assert_equal [101, 102], Issue.find(3).changeset_ids
  139. # fixed issues
  140. fixed_issue.reload
  141. assert fixed_issue.status.is_closed?
  142. assert_equal 90, fixed_issue.done_ratio
  143. assert_equal [101], fixed_issue.changeset_ids
  144. # issue change
  145. journal = fixed_issue.journals.find(:first, :order => 'created_on desc')
  146. assert_equal User.find_by_login('dlopper'), journal.user
  147. assert_equal 'Applied in changeset r2.', journal.notes
  148. # 2 email notifications
  149. assert_equal 2, ActionMailer::Base.deliveries.size
  150. mail = ActionMailer::Base.deliveries.first
  151. assert_not_nil mail
  152. assert mail.subject.starts_with?(
  153. "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
  154. assert_mail_body_match(
  155. "Status changed from #{old_status} to #{fixed_issue.status}", mail)
  156. # ignoring commits referencing an issue of another project
  157. assert_equal [], Issue.find(4).changesets
  158. end
  159. def test_for_changeset_comments_strip
  160. repository = Repository::Mercurial.create(
  161. :project => Project.find( 4 ),
  162. :url => '/foo/bar/baz' )
  163. comment = <<-COMMENT
  164. This is a loooooooooooooooooooooooooooong comment
  165. COMMENT
  166. changeset = Changeset.new(
  167. :comments => comment, :commit_date => Time.now,
  168. :revision => 0, :scmid => 'f39b7922fb3c',
  169. :committer => 'foo <foo@example.com>',
  170. :committed_on => Time.now, :repository => repository )
  171. assert( changeset.save )
  172. assert_not_equal( comment, changeset.comments )
  173. assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
  174. changeset.comments )
  175. end
  176. def test_for_urls_strip_cvs
  177. repository = Repository::Cvs.create(
  178. :project => Project.find(4),
  179. :url => ' :pserver:login:password@host:/path/to/the/repository',
  180. :root_url => 'foo ',
  181. :log_encoding => 'UTF-8')
  182. assert repository.save
  183. repository.reload
  184. assert_equal ':pserver:login:password@host:/path/to/the/repository',
  185. repository.url
  186. assert_equal 'foo', repository.root_url
  187. end
  188. def test_for_urls_strip_subversion
  189. repository = Repository::Subversion.create(
  190. :project => Project.find(4),
  191. :url => ' file:///dummy ')
  192. assert repository.save
  193. repository.reload
  194. assert_equal 'file:///dummy', repository.url
  195. end
  196. def test_for_urls_strip_git
  197. repository = Repository::Git.create(
  198. :project => Project.find(4),
  199. :url => ' c:\dummy ')
  200. assert repository.save
  201. repository.reload
  202. assert_equal 'c:\dummy', repository.url
  203. end
  204. def test_manual_user_mapping
  205. assert_no_difference "Changeset.count(:conditions => 'user_id <> 2')" do
  206. c = Changeset.create!(
  207. :repository => @repository,
  208. :committer => 'foo',
  209. :committed_on => Time.now,
  210. :revision => 100,
  211. :comments => 'Committed by foo.'
  212. )
  213. assert_nil c.user
  214. @repository.committer_ids = {'foo' => '2'}
  215. assert_equal User.find(2), c.reload.user
  216. # committer is now mapped
  217. c = Changeset.create!(
  218. :repository => @repository,
  219. :committer => 'foo',
  220. :committed_on => Time.now,
  221. :revision => 101,
  222. :comments => 'Another commit by foo.'
  223. )
  224. assert_equal User.find(2), c.user
  225. end
  226. end
  227. def test_auto_user_mapping_by_username
  228. c = Changeset.create!(
  229. :repository => @repository,
  230. :committer => 'jsmith',
  231. :committed_on => Time.now,
  232. :revision => 100,
  233. :comments => 'Committed by john.'
  234. )
  235. assert_equal User.find(2), c.user
  236. end
  237. def test_auto_user_mapping_by_email
  238. c = Changeset.create!(
  239. :repository => @repository,
  240. :committer => 'john <jsmith@somenet.foo>',
  241. :committed_on => Time.now,
  242. :revision => 100,
  243. :comments => 'Committed by john.'
  244. )
  245. assert_equal User.find(2), c.user
  246. end
  247. def test_filesystem_avaialbe
  248. klass = Repository::Filesystem
  249. assert klass.scm_adapter_class
  250. assert_equal true, klass.scm_available
  251. end
  252. def test_merge_extra_info
  253. repo = Repository::Subversion.new(:project => Project.find(3))
  254. assert !repo.save
  255. repo.url = "svn://localhost"
  256. assert repo.save
  257. repo.reload
  258. project = Project.find(3)
  259. assert_equal repo, project.repository
  260. assert_nil repo.extra_info
  261. h1 = {"test_1" => {"test_11" => "test_value_11"}}
  262. repo.merge_extra_info(h1)
  263. assert_equal h1, repo.extra_info
  264. h2 = {"test_2" => {
  265. "test_21" => "test_value_21",
  266. "test_22" => "test_value_22",
  267. }}
  268. repo.merge_extra_info(h2)
  269. assert_equal (h = {"test_11" => "test_value_11"}),
  270. repo.extra_info["test_1"]
  271. assert_equal "test_value_21",
  272. repo.extra_info["test_2"]["test_21"]
  273. h3 = {"test_2" => {
  274. "test_23" => "test_value_23",
  275. "test_24" => "test_value_24",
  276. }}
  277. repo.merge_extra_info(h3)
  278. assert_equal (h = {"test_11" => "test_value_11"}),
  279. repo.extra_info["test_1"]
  280. assert_nil repo.extra_info["test_2"]["test_21"]
  281. assert_equal "test_value_23",
  282. repo.extra_info["test_2"]["test_23"]
  283. end
  284. def test_sort_should_not_raise_an_error_with_nil_identifiers
  285. r1 = Repository.new
  286. r2 = Repository.new
  287. assert_nothing_raised do
  288. [r1, r2].sort
  289. end
  290. end
  291. end