您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

repository_test.rb 18KB

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