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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 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_identifier_should_accept_letters_digits_dashes_and_underscores
  88. r = Repository::Subversion.new(
  89. :project_id => 3,
  90. :identifier => 'svn-123_45',
  91. :url => 'file:///svn'
  92. )
  93. assert r.save
  94. end
  95. def test_identifier_should_not_be_frozen_for_a_new_repository
  96. assert_equal false, Repository.new.identifier_frozen?
  97. end
  98. def test_identifier_should_not_be_frozen_for_a_saved_repository_with_blank_identifier
  99. Repository.update_all(["identifier = ''"], "id = 10")
  100. assert_equal false, Repository.find(10).identifier_frozen?
  101. end
  102. def test_identifier_should_be_frozen_for_a_saved_repository_with_valid_identifier
  103. Repository.update_all(["identifier = 'abc123'"], "id = 10")
  104. assert_equal true, Repository.find(10).identifier_frozen?
  105. end
  106. def test_identifier_should_not_accept_change_if_frozen
  107. r = Repository.new(:identifier => 'foo')
  108. r.stubs(:identifier_frozen?).returns(true)
  109. r.identifier = 'bar'
  110. assert_equal 'foo', r.identifier
  111. end
  112. def test_identifier_should_accept_change_if_not_frozen
  113. r = Repository.new(:identifier => 'foo')
  114. r.stubs(:identifier_frozen?).returns(false)
  115. r.identifier = 'bar'
  116. assert_equal 'bar', r.identifier
  117. end
  118. def test_destroy
  119. repository = Repository.find(10)
  120. changesets = repository.changesets.count
  121. changes = repository.filechanges.count
  122. assert_difference 'Changeset.count', -changesets do
  123. assert_difference 'Change.count', -changes do
  124. Repository.find(10).destroy
  125. end
  126. end
  127. end
  128. def test_destroy_should_delete_parents_associations
  129. changeset = Changeset.find(102)
  130. changeset.parents = Changeset.find_all_by_id([100, 101])
  131. assert_difference 'Changeset.connection.select_all("select * from changeset_parents").size', -2 do
  132. Repository.find(10).destroy
  133. end
  134. end
  135. def test_destroy_should_delete_issues_associations
  136. changeset = Changeset.find(102)
  137. changeset.issues = Issue.find_all_by_id([1, 2])
  138. assert_difference 'Changeset.connection.select_all("select * from changesets_issues").size', -2 do
  139. Repository.find(10).destroy
  140. end
  141. end
  142. def test_should_not_create_with_disabled_scm
  143. # disable Subversion
  144. with_settings :enabled_scm => ['Darcs', 'Git'] do
  145. repository = Repository::Subversion.new(
  146. :project => Project.find(3), :url => "svn://localhost")
  147. assert !repository.save
  148. assert_include I18n.translate('activerecord.errors.messages.invalid'),
  149. repository.errors[:type]
  150. end
  151. end
  152. def test_scan_changesets_for_issue_ids
  153. Setting.default_language = 'en'
  154. # choosing a status to apply to fix issues
  155. Setting.commit_fix_status_id = IssueStatus.find(
  156. :first,
  157. :conditions => ["is_closed = ?", true]).id
  158. Setting.commit_fix_done_ratio = "90"
  159. Setting.commit_ref_keywords = 'refs , references, IssueID'
  160. Setting.commit_fix_keywords = 'fixes , closes'
  161. Setting.default_language = 'en'
  162. ActionMailer::Base.deliveries.clear
  163. # make sure issue 1 is not already closed
  164. fixed_issue = Issue.find(1)
  165. assert !fixed_issue.status.is_closed?
  166. old_status = fixed_issue.status
  167. with_settings :notified_events => %w(issue_added issue_updated) do
  168. Repository.scan_changesets_for_issue_ids
  169. end
  170. assert_equal [101, 102], Issue.find(3).changeset_ids
  171. # fixed issues
  172. fixed_issue.reload
  173. assert fixed_issue.status.is_closed?
  174. assert_equal 90, fixed_issue.done_ratio
  175. assert_equal [101], fixed_issue.changeset_ids
  176. # issue change
  177. journal = fixed_issue.journals.reorder('created_on desc').first
  178. assert_equal User.find_by_login('dlopper'), journal.user
  179. assert_equal 'Applied in changeset r2.', journal.notes
  180. # 2 email notifications
  181. assert_equal 2, ActionMailer::Base.deliveries.size
  182. mail = ActionMailer::Base.deliveries.first
  183. assert_not_nil mail
  184. assert mail.subject.starts_with?(
  185. "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
  186. assert_mail_body_match(
  187. "Status changed from #{old_status} to #{fixed_issue.status}", mail)
  188. # ignoring commits referencing an issue of another project
  189. assert_equal [], Issue.find(4).changesets
  190. end
  191. def test_for_changeset_comments_strip
  192. repository = Repository::Mercurial.create(
  193. :project => Project.find( 4 ),
  194. :url => '/foo/bar/baz' )
  195. comment = <<-COMMENT
  196. This is a loooooooooooooooooooooooooooong comment
  197. COMMENT
  198. changeset = Changeset.new(
  199. :comments => comment, :commit_date => Time.now,
  200. :revision => 0, :scmid => 'f39b7922fb3c',
  201. :committer => 'foo <foo@example.com>',
  202. :committed_on => Time.now, :repository => repository )
  203. assert( changeset.save )
  204. assert_not_equal( comment, changeset.comments )
  205. assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
  206. changeset.comments )
  207. end
  208. def test_for_urls_strip_cvs
  209. repository = Repository::Cvs.create(
  210. :project => Project.find(4),
  211. :url => ' :pserver:login:password@host:/path/to/the/repository',
  212. :root_url => 'foo ',
  213. :log_encoding => 'UTF-8')
  214. assert repository.save
  215. repository.reload
  216. assert_equal ':pserver:login:password@host:/path/to/the/repository',
  217. repository.url
  218. assert_equal 'foo', repository.root_url
  219. end
  220. def test_for_urls_strip_subversion
  221. repository = Repository::Subversion.create(
  222. :project => Project.find(4),
  223. :url => ' file:///dummy ')
  224. assert repository.save
  225. repository.reload
  226. assert_equal 'file:///dummy', repository.url
  227. end
  228. def test_for_urls_strip_git
  229. repository = Repository::Git.create(
  230. :project => Project.find(4),
  231. :url => ' c:\dummy ')
  232. assert repository.save
  233. repository.reload
  234. assert_equal 'c:\dummy', repository.url
  235. end
  236. def test_manual_user_mapping
  237. assert_no_difference "Changeset.count(:conditions => 'user_id <> 2')" do
  238. c = Changeset.create!(
  239. :repository => @repository,
  240. :committer => 'foo',
  241. :committed_on => Time.now,
  242. :revision => 100,
  243. :comments => 'Committed by foo.'
  244. )
  245. assert_nil c.user
  246. @repository.committer_ids = {'foo' => '2'}
  247. assert_equal User.find(2), c.reload.user
  248. # committer is now mapped
  249. c = Changeset.create!(
  250. :repository => @repository,
  251. :committer => 'foo',
  252. :committed_on => Time.now,
  253. :revision => 101,
  254. :comments => 'Another commit by foo.'
  255. )
  256. assert_equal User.find(2), c.user
  257. end
  258. end
  259. def test_auto_user_mapping_by_username
  260. c = Changeset.create!(
  261. :repository => @repository,
  262. :committer => 'jsmith',
  263. :committed_on => Time.now,
  264. :revision => 100,
  265. :comments => 'Committed by john.'
  266. )
  267. assert_equal User.find(2), c.user
  268. end
  269. def test_auto_user_mapping_by_email
  270. c = Changeset.create!(
  271. :repository => @repository,
  272. :committer => 'john <jsmith@somenet.foo>',
  273. :committed_on => Time.now,
  274. :revision => 100,
  275. :comments => 'Committed by john.'
  276. )
  277. assert_equal User.find(2), c.user
  278. end
  279. def test_filesystem_avaialbe
  280. klass = Repository::Filesystem
  281. assert klass.scm_adapter_class
  282. assert_equal true, klass.scm_available
  283. end
  284. def test_merge_extra_info
  285. repo = Repository::Subversion.new(:project => Project.find(3))
  286. assert !repo.save
  287. repo.url = "svn://localhost"
  288. assert repo.save
  289. repo.reload
  290. project = Project.find(3)
  291. assert_equal repo, project.repository
  292. assert_nil repo.extra_info
  293. h1 = {"test_1" => {"test_11" => "test_value_11"}}
  294. repo.merge_extra_info(h1)
  295. assert_equal h1, repo.extra_info
  296. h2 = {"test_2" => {
  297. "test_21" => "test_value_21",
  298. "test_22" => "test_value_22",
  299. }}
  300. repo.merge_extra_info(h2)
  301. assert_equal (h = {"test_11" => "test_value_11"}),
  302. repo.extra_info["test_1"]
  303. assert_equal "test_value_21",
  304. repo.extra_info["test_2"]["test_21"]
  305. h3 = {"test_2" => {
  306. "test_23" => "test_value_23",
  307. "test_24" => "test_value_24",
  308. }}
  309. repo.merge_extra_info(h3)
  310. assert_equal (h = {"test_11" => "test_value_11"}),
  311. repo.extra_info["test_1"]
  312. assert_nil repo.extra_info["test_2"]["test_21"]
  313. assert_equal "test_value_23",
  314. repo.extra_info["test_2"]["test_23"]
  315. end
  316. def test_sort_should_not_raise_an_error_with_nil_identifiers
  317. r1 = Repository.new
  318. r2 = Repository.new
  319. assert_nothing_raised do
  320. [r1, r2].sort
  321. end
  322. end
  323. end