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

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