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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. Setting.commit_ref_keywords = 'refs , references, IssueID'
  155. Setting.commit_update_keywords = [
  156. {'keywords' => 'fixes , closes', 'status_id' => IssueStatus.where(:is_closed => true).first.id, 'done_ratio' => '90'}
  157. ]
  158. Setting.default_language = 'en'
  159. ActionMailer::Base.deliveries.clear
  160. # make sure issue 1 is not already closed
  161. fixed_issue = Issue.find(1)
  162. assert !fixed_issue.status.is_closed?
  163. old_status = fixed_issue.status
  164. with_settings :notified_events => %w(issue_added issue_updated) do
  165. Repository.scan_changesets_for_issue_ids
  166. end
  167. assert_equal [101, 102], Issue.find(3).changeset_ids
  168. # fixed issues
  169. fixed_issue.reload
  170. assert fixed_issue.status.is_closed?
  171. assert_equal 90, fixed_issue.done_ratio
  172. assert_equal [101], fixed_issue.changeset_ids
  173. # issue change
  174. journal = fixed_issue.journals.reorder('created_on desc').first
  175. assert_equal User.find_by_login('dlopper'), journal.user
  176. assert_equal 'Applied in changeset r2.', journal.notes
  177. # 2 email notifications
  178. assert_equal 2, ActionMailer::Base.deliveries.size
  179. mail = ActionMailer::Base.deliveries.first
  180. assert_not_nil mail
  181. assert mail.subject.starts_with?(
  182. "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
  183. assert_mail_body_match(
  184. "Status changed from #{old_status} to #{fixed_issue.status}", mail)
  185. # ignoring commits referencing an issue of another project
  186. assert_equal [], Issue.find(4).changesets
  187. end
  188. def test_for_changeset_comments_strip
  189. repository = Repository::Mercurial.create(
  190. :project => Project.find( 4 ),
  191. :url => '/foo/bar/baz' )
  192. comment = <<-COMMENT
  193. This is a loooooooooooooooooooooooooooong comment
  194. COMMENT
  195. changeset = Changeset.new(
  196. :comments => comment, :commit_date => Time.now,
  197. :revision => 0, :scmid => 'f39b7922fb3c',
  198. :committer => 'foo <foo@example.com>',
  199. :committed_on => Time.now, :repository => repository )
  200. assert( changeset.save )
  201. assert_not_equal( comment, changeset.comments )
  202. assert_equal( 'This is a loooooooooooooooooooooooooooong comment',
  203. changeset.comments )
  204. end
  205. def test_for_urls_strip_cvs
  206. repository = Repository::Cvs.create(
  207. :project => Project.find(4),
  208. :url => ' :pserver:login:password@host:/path/to/the/repository',
  209. :root_url => 'foo ',
  210. :log_encoding => 'UTF-8')
  211. assert repository.save
  212. repository.reload
  213. assert_equal ':pserver:login:password@host:/path/to/the/repository',
  214. repository.url
  215. assert_equal 'foo', repository.root_url
  216. end
  217. def test_for_urls_strip_subversion
  218. repository = Repository::Subversion.create(
  219. :project => Project.find(4),
  220. :url => ' file:///dummy ')
  221. assert repository.save
  222. repository.reload
  223. assert_equal 'file:///dummy', repository.url
  224. end
  225. def test_for_urls_strip_git
  226. repository = Repository::Git.create(
  227. :project => Project.find(4),
  228. :url => ' c:\dummy ')
  229. assert repository.save
  230. repository.reload
  231. assert_equal 'c:\dummy', repository.url
  232. end
  233. def test_manual_user_mapping
  234. assert_no_difference "Changeset.where('user_id <> 2').count" do
  235. c = Changeset.create!(
  236. :repository => @repository,
  237. :committer => 'foo',
  238. :committed_on => Time.now,
  239. :revision => 100,
  240. :comments => 'Committed by foo.'
  241. )
  242. assert_nil c.user
  243. @repository.committer_ids = {'foo' => '2'}
  244. assert_equal User.find(2), c.reload.user
  245. # committer is now mapped
  246. c = Changeset.create!(
  247. :repository => @repository,
  248. :committer => 'foo',
  249. :committed_on => Time.now,
  250. :revision => 101,
  251. :comments => 'Another commit by foo.'
  252. )
  253. assert_equal User.find(2), c.user
  254. end
  255. end
  256. def test_auto_user_mapping_by_username
  257. c = Changeset.create!(
  258. :repository => @repository,
  259. :committer => 'jsmith',
  260. :committed_on => Time.now,
  261. :revision => 100,
  262. :comments => 'Committed by john.'
  263. )
  264. assert_equal User.find(2), c.user
  265. end
  266. def test_auto_user_mapping_by_email
  267. c = Changeset.create!(
  268. :repository => @repository,
  269. :committer => 'john <jsmith@somenet.foo>',
  270. :committed_on => Time.now,
  271. :revision => 100,
  272. :comments => 'Committed by john.'
  273. )
  274. assert_equal User.find(2), c.user
  275. end
  276. def test_filesystem_avaialbe
  277. klass = Repository::Filesystem
  278. assert klass.scm_adapter_class
  279. assert_equal true, klass.scm_available
  280. end
  281. def test_merge_extra_info
  282. repo = Repository::Subversion.new(:project => Project.find(3))
  283. assert !repo.save
  284. repo.url = "svn://localhost"
  285. assert repo.save
  286. repo.reload
  287. project = Project.find(3)
  288. assert_equal repo, project.repository
  289. assert_nil repo.extra_info
  290. h1 = {"test_1" => {"test_11" => "test_value_11"}}
  291. repo.merge_extra_info(h1)
  292. assert_equal h1, repo.extra_info
  293. h2 = {"test_2" => {
  294. "test_21" => "test_value_21",
  295. "test_22" => "test_value_22",
  296. }}
  297. repo.merge_extra_info(h2)
  298. assert_equal (h = {"test_11" => "test_value_11"}),
  299. repo.extra_info["test_1"]
  300. assert_equal "test_value_21",
  301. repo.extra_info["test_2"]["test_21"]
  302. h3 = {"test_2" => {
  303. "test_23" => "test_value_23",
  304. "test_24" => "test_value_24",
  305. }}
  306. repo.merge_extra_info(h3)
  307. assert_equal (h = {"test_11" => "test_value_11"}),
  308. repo.extra_info["test_1"]
  309. assert_nil repo.extra_info["test_2"]["test_21"]
  310. assert_equal "test_value_23",
  311. repo.extra_info["test_2"]["test_23"]
  312. end
  313. def test_sort_should_not_raise_an_error_with_nil_identifiers
  314. r1 = Repository.new
  315. r2 = Repository.new
  316. assert_nothing_raised do
  317. [r1, r2].sort
  318. end
  319. end
  320. end