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

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