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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. 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. :email_addresses,
  31. :members,
  32. :member_roles,
  33. :roles,
  34. :enumerations
  35. include Redmine::I18n
  36. def setup
  37. @repository = Project.find(1).repository
  38. end
  39. def test_blank_log_encoding_error_message
  40. set_language_if_valid 'en'
  41. repo = Repository::Bazaar.new(
  42. :project => Project.find(3),
  43. :url => "/test",
  44. :log_encoding => ''
  45. )
  46. assert !repo.save
  47. assert_include "Commit messages encoding cannot be blank",
  48. repo.errors.full_messages
  49. end
  50. def test_blank_log_encoding_error_message_fr
  51. set_language_if_valid 'fr'
  52. str = "Encodage des messages de commit doit \xc3\xaatre renseign\xc3\xa9(e)".force_encoding('UTF-8')
  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_2_repositories_with_same_identifier_in_different_projects_should_be_valid
  70. Repository::Subversion.create!(:project_id => 2, :identifier => 'foo', :url => 'file:///foo')
  71. r = Repository::Subversion.new(:project_id => 3, :identifier => 'foo', :url => 'file:///bar')
  72. assert r.save
  73. end
  74. def test_2_repositories_with_same_identifier_should_not_be_valid
  75. Repository::Subversion.create!(:project_id => 3, :identifier => 'foo', :url => 'file:///foo')
  76. r = Repository::Subversion.new(:project_id => 3, :identifier => 'foo', :url => 'file:///bar')
  77. assert !r.save
  78. end
  79. def test_2_repositories_with_blank_identifier_should_not_be_valid
  80. Repository::Subversion.create!(:project_id => 3, :identifier => '', :url => 'file:///foo')
  81. r = Repository::Subversion.new(:project_id => 3, :identifier => '', :url => 'file:///bar')
  82. assert !r.save
  83. end
  84. def test_2_repositories_with_blank_identifier_and_one_as_default_should_not_be_valid
  85. Repository::Subversion.create!(:project_id => 3, :identifier => '', :url => 'file:///foo', :is_default => true)
  86. r = Repository::Subversion.new(:project_id => 3, :identifier => '', :url => 'file:///bar')
  87. assert !r.save
  88. end
  89. def test_2_repositories_with_blank_and_nil_identifier_should_not_be_valid
  90. Repository::Subversion.create!(:project_id => 3, :identifier => nil, :url => 'file:///foo')
  91. r = Repository::Subversion.new(:project_id => 3, :identifier => '', :url => 'file:///bar')
  92. assert !r.save
  93. end
  94. def test_first_repository_should_be_set_as_default
  95. repository1 = Repository::Subversion.new(
  96. :project => Project.find(3),
  97. :identifier => 'svn1',
  98. :url => 'file:///svn1'
  99. )
  100. assert repository1.save
  101. assert repository1.is_default?
  102. repository2 = Repository::Subversion.new(
  103. :project => Project.find(3),
  104. :identifier => 'svn2',
  105. :url => 'file:///svn2'
  106. )
  107. assert repository2.save
  108. assert !repository2.is_default?
  109. assert_equal repository1, Project.find(3).repository
  110. assert_equal [repository1, repository2], Project.find(3).repositories.sort
  111. end
  112. def test_default_repository_should_be_one
  113. assert_equal 0, Project.find(3).repositories.count
  114. repository1 = Repository::Subversion.new(
  115. :project => Project.find(3),
  116. :identifier => 'svn1',
  117. :url => 'file:///svn1'
  118. )
  119. assert repository1.save
  120. assert repository1.is_default?
  121. repository2 = Repository::Subversion.new(
  122. :project => Project.find(3),
  123. :identifier => 'svn2',
  124. :url => 'file:///svn2',
  125. :is_default => true
  126. )
  127. assert repository2.save
  128. assert repository2.is_default?
  129. repository1.reload
  130. assert !repository1.is_default?
  131. assert_equal repository2, Project.find(3).repository
  132. assert_equal [repository2, repository1], Project.find(3).repositories.sort
  133. end
  134. def test_identifier_should_accept_letters_digits_dashes_and_underscores
  135. r = Repository::Subversion.new(
  136. :project_id => 3,
  137. :identifier => 'svn-123_45',
  138. :url => 'file:///svn'
  139. )
  140. assert r.save
  141. end
  142. def test_identifier_should_not_be_frozen_for_a_new_repository
  143. assert_equal false, Repository.new.identifier_frozen?
  144. end
  145. def test_identifier_should_not_be_frozen_for_a_saved_repository_with_blank_identifier
  146. Repository.where(:id => 10).update_all(["identifier = ''"])
  147. assert_equal false, Repository.find(10).identifier_frozen?
  148. end
  149. def test_identifier_should_be_frozen_for_a_saved_repository_with_valid_identifier
  150. Repository.where(:id => 10).update_all(["identifier = 'abc123'"])
  151. assert_equal true, Repository.find(10).identifier_frozen?
  152. end
  153. def test_identifier_should_not_accept_change_if_frozen
  154. r = Repository.new(:identifier => 'foo')
  155. r.stubs(:identifier_frozen?).returns(true)
  156. r.identifier = 'bar'
  157. assert_equal 'foo', r.identifier
  158. end
  159. def test_identifier_should_accept_change_if_not_frozen
  160. r = Repository.new(:identifier => 'foo')
  161. r.stubs(:identifier_frozen?).returns(false)
  162. r.identifier = 'bar'
  163. assert_equal 'bar', r.identifier
  164. end
  165. def test_destroy
  166. repository = Repository.find(10)
  167. changesets = repository.changesets.count
  168. changes = repository.filechanges.count
  169. assert_difference 'Changeset.count', -changesets do
  170. assert_difference 'Change.count', -changes do
  171. Repository.find(10).destroy
  172. end
  173. end
  174. end
  175. def test_destroy_should_delete_parents_associations
  176. changeset = Changeset.find(102)
  177. changeset.parents = Changeset.where(:id => [100, 101]).to_a
  178. assert_difference 'Changeset.connection.select_all("select * from changeset_parents").count', -2 do
  179. Repository.find(10).destroy
  180. end
  181. end
  182. def test_destroy_should_delete_issues_associations
  183. changeset = Changeset.find(102)
  184. changeset.issues = Issue.where(:id => [1, 2]).to_a
  185. assert_difference 'Changeset.connection.select_all("select * from changesets_issues").count', -2 do
  186. Repository.find(10).destroy
  187. end
  188. end
  189. def test_should_not_create_with_disabled_scm
  190. # disable Subversion
  191. with_settings :enabled_scm => ['Mercurial', 'Git'] do
  192. repository = Repository::Subversion.new(
  193. :project => Project.find(3), :url => "svn://localhost")
  194. assert !repository.save
  195. assert_include I18n.translate('activerecord.errors.messages.invalid'),
  196. repository.errors[:type]
  197. end
  198. end
  199. def test_scan_changesets_for_issue_ids
  200. Setting.default_language = 'en'
  201. Setting.commit_ref_keywords = 'refs , references, IssueID'
  202. Setting.commit_update_keywords = [
  203. {'keywords' => 'fixes , closes',
  204. 'status_id' => IssueStatus.where(:is_closed => true).first.id,
  205. 'done_ratio' => '90'}
  206. ]
  207. Setting.default_language = 'en'
  208. ActionMailer::Base.deliveries.clear
  209. # make sure issue 1 is not already closed
  210. fixed_issue = Issue.find(1)
  211. assert !fixed_issue.closed?
  212. old_status = fixed_issue.status
  213. with_settings :notified_events => %w(issue_added issue_updated) do
  214. Repository.scan_changesets_for_issue_ids
  215. end
  216. assert_equal [101, 102], Issue.find(3).changeset_ids
  217. # fixed issues
  218. fixed_issue.reload
  219. assert fixed_issue.closed?
  220. assert_equal 90, fixed_issue.done_ratio
  221. assert_equal [101], fixed_issue.changeset_ids
  222. # issue change
  223. journal = fixed_issue.journals.reorder('created_on desc').first
  224. assert_equal User.find_by_login('dlopper'), journal.user
  225. assert_equal 'Applied in changeset r2.', journal.notes
  226. # 2 email notifications
  227. assert_equal 2, ActionMailer::Base.deliveries.size
  228. mail = ActionMailer::Base.deliveries.first
  229. assert_not_nil mail
  230. assert mail.subject.starts_with?(
  231. "[#{fixed_issue.project.name} - #{fixed_issue.tracker.name} ##{fixed_issue.id}]")
  232. assert_mail_body_match(
  233. "Status changed from #{old_status} to #{fixed_issue.status}", mail)
  234. # ignoring commits referencing an issue of another project
  235. assert_equal [], Issue.find(4).changesets
  236. end
  237. def test_for_changeset_comments_strip
  238. repository = Repository::Mercurial.create(
  239. :project => Project.find(4),
  240. :url => '/foo/bar/baz' )
  241. long_whitespace = " "
  242. expected_comment = "This is a loooooooooooooooooooooooooooong comment"
  243. comment = "#{expected_comment}#{long_whitespace}\n"
  244. 3.times {comment << "#{long_whitespace}\n"}
  245. changeset = Changeset.new(
  246. :comments => comment, :commit_date => Time.now,
  247. :revision => 0, :scmid => 'f39b7922fb3c',
  248. :committer => 'foo <foo@example.com>',
  249. :committed_on => Time.now, :repository => repository)
  250. assert(changeset.save)
  251. assert_not_equal comment, changeset.comments
  252. assert_equal expected_comment, changeset.comments
  253. assert_equal expected_comment, changeset.short_comments
  254. assert_equal "", changeset.long_comments
  255. end
  256. def test_for_urls_strip_cvs
  257. repository = Repository::Cvs.create(
  258. :project => Project.find(4),
  259. :url => ' :pserver:login:password@host:/path/to/the/repository',
  260. :root_url => 'foo ',
  261. :log_encoding => 'UTF-8')
  262. assert repository.save
  263. repository.reload
  264. assert_equal ':pserver:login:password@host:/path/to/the/repository',
  265. repository.url
  266. assert_equal 'foo', repository.root_url
  267. end
  268. def test_for_urls_strip_subversion
  269. repository = Repository::Subversion.create(
  270. :project => Project.find(4),
  271. :url => ' file:///dummy ')
  272. assert repository.save
  273. repository.reload
  274. assert_equal 'file:///dummy', repository.url
  275. end
  276. def test_for_urls_strip_git
  277. repository = Repository::Git.create(
  278. :project => Project.find(4),
  279. :url => ' c:\dummy ')
  280. assert repository.save
  281. repository.reload
  282. assert_equal 'c:\dummy', repository.url
  283. end
  284. def test_manual_user_mapping
  285. assert_no_difference "Changeset.where('user_id <> 2').count" do
  286. c = Changeset.create!(
  287. :repository => @repository,
  288. :committer => 'foo',
  289. :committed_on => Time.now,
  290. :revision => 100,
  291. :comments => 'Committed by foo.'
  292. )
  293. assert_nil c.user
  294. @repository.committer_ids = {'foo' => '2'}
  295. assert_equal User.find(2), c.reload.user
  296. # committer is now mapped
  297. c = Changeset.create!(
  298. :repository => @repository,
  299. :committer => 'foo',
  300. :committed_on => Time.now,
  301. :revision => 101,
  302. :comments => 'Another commit by foo.'
  303. )
  304. assert_equal User.find(2), c.user
  305. end
  306. end
  307. def test_auto_user_mapping_by_username
  308. c = Changeset.create!(
  309. :repository => @repository,
  310. :committer => 'jsmith',
  311. :committed_on => Time.now,
  312. :revision => 100,
  313. :comments => 'Committed by john.'
  314. )
  315. assert_equal User.find(2), c.user
  316. end
  317. def test_auto_user_mapping_by_email
  318. c = Changeset.create!(
  319. :repository => @repository,
  320. :committer => 'john <jsmith@somenet.foo>',
  321. :committed_on => Time.now,
  322. :revision => 100,
  323. :comments => 'Committed by john.'
  324. )
  325. assert_equal User.find(2), c.user
  326. end
  327. def test_filesystem_avaialbe
  328. klass = Repository::Filesystem
  329. assert klass.scm_adapter_class
  330. assert_equal true, klass.scm_available
  331. end
  332. def test_extra_info_should_not_return_non_hash_value
  333. repo = Repository.new
  334. repo.extra_info = "foo"
  335. assert_nil repo.extra_info
  336. end
  337. def test_merge_extra_info
  338. repo = Repository::Subversion.new(:project => Project.find(3))
  339. assert !repo.save
  340. repo.url = "svn://localhost"
  341. assert repo.save
  342. repo.reload
  343. project = Project.find(3)
  344. assert_equal repo, project.repository
  345. assert_nil repo.extra_info
  346. h1 = {"test_1" => {"test_11" => "test_value_11"}}
  347. repo.merge_extra_info(h1)
  348. assert_equal h1, repo.extra_info
  349. h2 = {"test_2" => {
  350. "test_21" => "test_value_21",
  351. "test_22" => "test_value_22",
  352. }}
  353. repo.merge_extra_info(h2)
  354. assert_equal (h = {"test_11" => "test_value_11"}),
  355. repo.extra_info["test_1"]
  356. assert_equal "test_value_21",
  357. repo.extra_info["test_2"]["test_21"]
  358. h3 = {"test_2" => {
  359. "test_23" => "test_value_23",
  360. "test_24" => "test_value_24",
  361. }}
  362. repo.merge_extra_info(h3)
  363. assert_equal (h = {"test_11" => "test_value_11"}),
  364. repo.extra_info["test_1"]
  365. assert_nil repo.extra_info["test_2"]["test_21"]
  366. assert_equal "test_value_23",
  367. repo.extra_info["test_2"]["test_23"]
  368. end
  369. def test_sort_should_not_raise_an_error_with_nil_identifiers
  370. r1 = Repository.new
  371. r2 = Repository.new
  372. assert_nothing_raised do
  373. [r1, r2].sort
  374. end
  375. end
  376. def test_stats_by_author_reflect_changesets_and_changes
  377. repository = Repository.find(10)
  378. expected = {"Dave Lopper"=>{:commits_count=>10, :changes_count=>3}}
  379. assert_equal expected, repository.stats_by_author
  380. set = Changeset.create!(
  381. :repository => repository,
  382. :committer => 'dlopper',
  383. :committed_on => Time.now,
  384. :revision => 101,
  385. :comments => 'Another commit by foo.'
  386. )
  387. Change.create!(:changeset => set, :action => 'A', :path => '/path/to/file1')
  388. Change.create!(:changeset => set, :action => 'A', :path => '/path/to/file2')
  389. expected = {"Dave Lopper"=>{:commits_count=>11, :changes_count=>5}}
  390. assert_equal expected, repository.stats_by_author
  391. end
  392. def test_stats_by_author_honnor_committers
  393. # in fact it is really tested above, but let's have a dedicated test
  394. # to ensure things are dynamically linked to Users
  395. User.find_by_login("dlopper").update_attribute(:firstname, "Dave's")
  396. repository = Repository.find(10)
  397. expected = {"Dave's Lopper"=>{:commits_count=>10, :changes_count=>3}}
  398. assert_equal expected, repository.stats_by_author
  399. end
  400. def test_stats_by_author_doesnt_drop_unmapped_users
  401. repository = Repository.find(10)
  402. Changeset.create!(
  403. :repository => repository,
  404. :committer => 'unnamed <foo@bar.net>',
  405. :committed_on => Time.now,
  406. :revision => 101,
  407. :comments => 'Another commit by foo.'
  408. )
  409. assert repository.stats_by_author.has_key?("unnamed <foo@bar.net>")
  410. end
  411. def test_stats_by_author_merge_correctly
  412. # as we honnor users->committer map and it's not injective,
  413. # we must be sure merges happen correctly and stats are not
  414. # wiped out when two source counts map to the same user.
  415. #
  416. # Here we have Changeset's with committer="dlopper" and others
  417. # with committer="dlopper <dlopper@somefoo.net>"
  418. repository = Repository.find(10)
  419. expected = {"Dave Lopper"=>{:commits_count=>10, :changes_count=>3}}
  420. assert_equal expected, repository.stats_by_author
  421. set = Changeset.create!(
  422. :repository => repository,
  423. :committer => 'dlopper <dlopper@somefoo.net>',
  424. :committed_on => Time.now,
  425. :revision => 101,
  426. :comments => 'Another commit by foo.'
  427. )
  428. expected = {"Dave Lopper"=>{:commits_count=>11, :changes_count=>3}}
  429. assert_equal expected, repository.stats_by_author
  430. end
  431. def test_fetch_changesets
  432. # 2 repositories in fixtures
  433. Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true)
  434. Repository.fetch_changesets
  435. end
  436. def test_repository_class
  437. assert_equal Repository::Subversion, Repository.repository_class('Subversion')
  438. assert_equal Repository::Git, Repository.repository_class('Git')
  439. assert_nil Repository.factory('Serializer')
  440. assert_nil Repository.factory('Query')
  441. end
  442. def test_factory
  443. assert_instance_of Repository::Subversion, Repository.factory('Subversion')
  444. assert_instance_of Repository::Git, Repository.factory('Git')
  445. assert_nil Repository.factory('Serializer')
  446. assert_nil Repository.factory('Query')
  447. end
  448. end