PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/lib/redmine/scm/adapters/mercurial_adapter_test.rb

https://bitbucket.org/eimajenthat/redmine
Ruby | 434 lines | 376 code | 38 blank | 20 comment | 7 complexity | cc8d57c2c986ec008be2a11435a69e1e MD5 | raw file
Possible License(s): GPL-2.0
  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. begin
  19. require 'mocha'
  20. class MercurialAdapterTest < ActiveSupport::TestCase
  21. HELPERS_DIR = Redmine::Scm::Adapters::MercurialAdapter::HELPERS_DIR
  22. TEMPLATE_NAME = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_NAME
  23. TEMPLATE_EXTENSION = Redmine::Scm::Adapters::MercurialAdapter::TEMPLATE_EXTENSION
  24. REPOSITORY_PATH = Rails.root.join('tmp/test/mercurial_repository').to_s
  25. CHAR_1_HEX = "\xc3\x9c"
  26. if File.directory?(REPOSITORY_PATH)
  27. def setup
  28. adapter_class = Redmine::Scm::Adapters::MercurialAdapter
  29. assert adapter_class
  30. assert adapter_class.client_command
  31. assert_equal true, adapter_class.client_available
  32. assert_equal true, adapter_class.client_version_above?([0, 9, 5])
  33. @adapter = Redmine::Scm::Adapters::MercurialAdapter.new(
  34. REPOSITORY_PATH,
  35. nil,
  36. nil,
  37. nil,
  38. 'ISO-8859-1')
  39. @diff_c_support = true
  40. @char_1 = CHAR_1_HEX.dup
  41. @tag_char_1 = "tag-#{CHAR_1_HEX}-00"
  42. @branch_char_0 = "branch-#{CHAR_1_HEX}-00"
  43. @branch_char_1 = "branch-#{CHAR_1_HEX}-01"
  44. if @tag_char_1.respond_to?(:force_encoding)
  45. @char_1.force_encoding('UTF-8')
  46. @tag_char_1.force_encoding('UTF-8')
  47. @branch_char_0.force_encoding('UTF-8')
  48. @branch_char_1.force_encoding('UTF-8')
  49. end
  50. end
  51. def test_hgversion
  52. to_test = { "Mercurial Distributed SCM (version 0.9.5)\n" => [0,9,5],
  53. "Mercurial Distributed SCM (1.0)\n" => [1,0],
  54. "Mercurial Distributed SCM (1e4ddc9ac9f7+20080325)\n" => nil,
  55. "Mercurial Distributed SCM (1.0.1+20080525)\n" => [1,0,1],
  56. "Mercurial Distributed SCM (1916e629a29d)\n" => nil,
  57. "Mercurial SCM Distribuito (versione 0.9.5)\n" => [0,9,5],
  58. "(1.6)\n(1.7)\n(1.8)" => [1,6],
  59. "(1.7.1)\r\n(1.8.1)\r\n(1.9.1)" => [1,7,1]}
  60. to_test.each do |s, v|
  61. test_hgversion_for(s, v)
  62. end
  63. end
  64. def test_template_path
  65. to_test = {
  66. [1,2] => "1.0",
  67. [] => "1.0",
  68. [1,2,1] => "1.0",
  69. [1,7] => "1.0",
  70. [1,7,1] => "1.0",
  71. [2,0] => "1.0",
  72. }
  73. to_test.each do |v, template|
  74. test_template_path_for(v, template)
  75. end
  76. end
  77. def test_info
  78. [REPOSITORY_PATH, REPOSITORY_PATH + "/",
  79. REPOSITORY_PATH + "//"].each do |repo|
  80. adp = Redmine::Scm::Adapters::MercurialAdapter.new(repo)
  81. repo_path = adp.info.root_url.gsub(/\\/, "/")
  82. assert_equal REPOSITORY_PATH, repo_path
  83. assert_equal '31', adp.info.lastrev.revision
  84. assert_equal '31eeee7395c8',adp.info.lastrev.scmid
  85. end
  86. end
  87. def test_revisions
  88. revisions = @adapter.revisions(nil, 2, 4)
  89. assert_equal 3, revisions.size
  90. assert_equal '2', revisions[0].revision
  91. assert_equal '400bb8672109', revisions[0].scmid
  92. assert_equal '4', revisions[2].revision
  93. assert_equal 'def6d2f1254a', revisions[2].scmid
  94. revisions = @adapter.revisions(nil, 2, 4, {:limit => 2})
  95. assert_equal 2, revisions.size
  96. assert_equal '2', revisions[0].revision
  97. assert_equal '400bb8672109', revisions[0].scmid
  98. end
  99. def test_parents
  100. revs1 = @adapter.revisions(nil, 0, 0)
  101. assert_equal 1, revs1.size
  102. assert_equal [], revs1[0].parents
  103. revs2 = @adapter.revisions(nil, 1, 1)
  104. assert_equal 1, revs2.size
  105. assert_equal 1, revs2[0].parents.size
  106. assert_equal "0885933ad4f6", revs2[0].parents[0]
  107. revs3 = @adapter.revisions(nil, 30, 30)
  108. assert_equal 1, revs3.size
  109. assert_equal 2, revs3[0].parents.size
  110. assert_equal "a94b0528f24f", revs3[0].parents[0]
  111. assert_equal "3a330eb32958", revs3[0].parents[1]
  112. end
  113. def test_diff
  114. if @adapter.class.client_version_above?([1, 2])
  115. assert_nil @adapter.diff(nil, '100000')
  116. end
  117. assert_nil @adapter.diff(nil, '100000', '200000')
  118. [2, '400bb8672109', '400', 400].each do |r1|
  119. diff1 = @adapter.diff(nil, r1)
  120. if @diff_c_support
  121. assert_equal 28, diff1.size
  122. buf = diff1[24].gsub(/\r\n|\r|\n/, "")
  123. assert_equal "+ return true unless klass.respond_to?('watched_by')", buf
  124. else
  125. assert_equal 0, diff1.size
  126. end
  127. [4, 'def6d2f1254a'].each do |r2|
  128. diff2 = @adapter.diff(nil, r1, r2)
  129. assert_equal 49, diff2.size
  130. buf = diff2[41].gsub(/\r\n|\r|\n/, "")
  131. assert_equal "+class WelcomeController < ApplicationController", buf
  132. diff3 = @adapter.diff('sources/watchers_controller.rb', r1, r2)
  133. assert_equal 20, diff3.size
  134. buf = diff3[12].gsub(/\r\n|\r|\n/, "")
  135. assert_equal "+ @watched.remove_watcher(user)", buf
  136. diff4 = @adapter.diff(nil, r2, r1)
  137. assert_equal 49, diff4.size
  138. buf = diff4[41].gsub(/\r\n|\r|\n/, "")
  139. assert_equal "-class WelcomeController < ApplicationController", buf
  140. diff5 = @adapter.diff('sources/watchers_controller.rb', r2, r1)
  141. assert_equal 20, diff5.size
  142. buf = diff5[9].gsub(/\r\n|\r|\n/, "")
  143. assert_equal "- @watched.remove_watcher(user)", buf
  144. end
  145. end
  146. end
  147. def test_diff_made_by_revision
  148. if @diff_c_support
  149. [24, '24', '4cddb4e45f52'].each do |r1|
  150. diff1 = @adapter.diff(nil, r1)
  151. assert_equal 5, diff1.size
  152. buf = diff1[4].gsub(/\r\n|\r|\n/, "")
  153. assert_equal '+0885933ad4f68d77c2649cd11f8311276e7ef7ce tag-init-revision', buf
  154. end
  155. end
  156. end
  157. def test_cat
  158. [2, '400bb8672109', '400', 400].each do |r|
  159. buf = @adapter.cat('sources/welcome_controller.rb', r)
  160. assert buf
  161. lines = buf.split("\r\n")
  162. assert_equal 25, lines.length
  163. assert_equal 'class WelcomeController < ApplicationController', lines[17]
  164. end
  165. assert_nil @adapter.cat('sources/welcome_controller.rb')
  166. end
  167. def test_annotate
  168. assert_equal [], @adapter.annotate("sources/welcome_controller.rb").lines
  169. [2, '400bb8672109', '400', 400].each do |r|
  170. ann = @adapter.annotate('sources/welcome_controller.rb', r)
  171. assert ann
  172. assert_equal '1', ann.revisions[17].revision
  173. assert_equal '9d5b5b004199', ann.revisions[17].identifier
  174. assert_equal 'jsmith', ann.revisions[0].author
  175. assert_equal 25, ann.lines.length
  176. assert_equal 'class WelcomeController < ApplicationController', ann.lines[17]
  177. end
  178. end
  179. def test_entries
  180. assert_nil @adapter.entries(nil, '100000')
  181. assert_equal 1, @adapter.entries("sources", 3).size
  182. assert_equal 1, @adapter.entries("sources", 'b3a615152df8').size
  183. [2, '400bb8672109', '400', 400].each do |r|
  184. entries1 = @adapter.entries(nil, r)
  185. assert entries1
  186. assert_equal 3, entries1.size
  187. assert_equal 'sources', entries1[1].name
  188. assert_equal 'sources', entries1[1].path
  189. assert_equal 'dir', entries1[1].kind
  190. readme = entries1[2]
  191. assert_equal 'README', readme.name
  192. assert_equal 'README', readme.path
  193. assert_equal 'file', readme.kind
  194. assert_equal 27, readme.size
  195. assert_equal '1', readme.lastrev.revision
  196. assert_equal '9d5b5b004199', readme.lastrev.identifier
  197. # 2007-12-14 10:24:01 +0100
  198. assert_equal Time.gm(2007, 12, 14, 9, 24, 1), readme.lastrev.time
  199. entries2 = @adapter.entries('sources', r)
  200. assert entries2
  201. assert_equal 2, entries2.size
  202. assert_equal 'watchers_controller.rb', entries2[0].name
  203. assert_equal 'sources/watchers_controller.rb', entries2[0].path
  204. assert_equal 'file', entries2[0].kind
  205. assert_equal 'welcome_controller.rb', entries2[1].name
  206. assert_equal 'sources/welcome_controller.rb', entries2[1].path
  207. assert_equal 'file', entries2[1].kind
  208. end
  209. end
  210. def test_entries_tag
  211. entries1 = @adapter.entries(nil, 'tag_test.00')
  212. assert entries1
  213. assert_equal 3, entries1.size
  214. assert_equal 'sources', entries1[1].name
  215. assert_equal 'sources', entries1[1].path
  216. assert_equal 'dir', entries1[1].kind
  217. readme = entries1[2]
  218. assert_equal 'README', readme.name
  219. assert_equal 'README', readme.path
  220. assert_equal 'file', readme.kind
  221. assert_equal 21, readme.size
  222. assert_equal '0', readme.lastrev.revision
  223. assert_equal '0885933ad4f6', readme.lastrev.identifier
  224. # 2007-12-14 10:22:52 +0100
  225. assert_equal Time.gm(2007, 12, 14, 9, 22, 52), readme.lastrev.time
  226. end
  227. def test_entries_branch
  228. entries1 = @adapter.entries(nil, 'test-branch-00')
  229. assert entries1
  230. assert_equal 5, entries1.size
  231. assert_equal 'sql_escape', entries1[2].name
  232. assert_equal 'sql_escape', entries1[2].path
  233. assert_equal 'dir', entries1[2].kind
  234. readme = entries1[4]
  235. assert_equal 'README', readme.name
  236. assert_equal 'README', readme.path
  237. assert_equal 'file', readme.kind
  238. assert_equal 365, readme.size
  239. assert_equal '8', readme.lastrev.revision
  240. assert_equal 'c51f5bb613cd', readme.lastrev.identifier
  241. # 2001-02-01 00:00:00 -0900
  242. assert_equal Time.gm(2001, 2, 1, 9, 0, 0), readme.lastrev.time
  243. end
  244. def test_locate_on_outdated_repository
  245. assert_equal 1, @adapter.entries("images", 0).size
  246. assert_equal 2, @adapter.entries("images").size
  247. assert_equal 2, @adapter.entries("images", 2).size
  248. end
  249. def test_access_by_nodeid
  250. path = 'sources/welcome_controller.rb'
  251. assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400bb8672109')
  252. end
  253. def test_access_by_fuzzy_nodeid
  254. path = 'sources/welcome_controller.rb'
  255. # falls back to nodeid
  256. assert_equal @adapter.cat(path, 2), @adapter.cat(path, '400')
  257. end
  258. def test_tags
  259. assert_equal [@tag_char_1, 'tag_test.00', 'tag-init-revision'], @adapter.tags
  260. end
  261. def test_tagmap
  262. tm = {
  263. @tag_char_1 => 'adf805632193',
  264. 'tag_test.00' => '6987191f453a',
  265. 'tag-init-revision' => '0885933ad4f6',
  266. }
  267. assert_equal tm, @adapter.tagmap
  268. end
  269. def test_branches
  270. brs = []
  271. @adapter.branches.each do |b|
  272. brs << b
  273. end
  274. assert_equal 7, brs.length
  275. assert_equal 'default', brs[0].to_s
  276. assert_equal '31', brs[0].revision
  277. assert_equal '31eeee7395c8', brs[0].scmid
  278. assert_equal 'test-branch-01', brs[1].to_s
  279. assert_equal '30', brs[1].revision
  280. assert_equal 'ad4dc4f80284', brs[1].scmid
  281. assert_equal @branch_char_1, brs[2].to_s
  282. assert_equal '27', brs[2].revision
  283. assert_equal '7bbf4c738e71', brs[2].scmid
  284. assert_equal 'branch (1)[2]&,%.-3_4', brs[3].to_s
  285. assert_equal '25', brs[3].revision
  286. assert_equal 'afc61e85bde7', brs[3].scmid
  287. assert_equal @branch_char_0, brs[4].to_s
  288. assert_equal '23', brs[4].revision
  289. assert_equal 'c8d3e4887474', brs[4].scmid
  290. assert_equal 'test_branch.latin-1', brs[5].to_s
  291. assert_equal '22', brs[5].revision
  292. assert_equal 'c2ffe7da686a', brs[5].scmid
  293. assert_equal 'test-branch-00', brs[6].to_s
  294. assert_equal '13', brs[6].revision
  295. assert_equal '3a330eb32958', brs[6].scmid
  296. end
  297. def test_branchmap
  298. bm = {
  299. 'default' => '31eeee7395c8',
  300. 'test_branch.latin-1' => 'c2ffe7da686a',
  301. 'branch (1)[2]&,%.-3_4' => 'afc61e85bde7',
  302. 'test-branch-00' => '3a330eb32958',
  303. "test-branch-01" => 'ad4dc4f80284',
  304. @branch_char_0 => 'c8d3e4887474',
  305. @branch_char_1 => '7bbf4c738e71',
  306. }
  307. assert_equal bm, @adapter.branchmap
  308. end
  309. def test_path_space
  310. p = 'README (1)[2]&,%.-3_4'
  311. [15, '933ca60293d7'].each do |r1|
  312. assert @adapter.diff(p, r1)
  313. assert @adapter.cat(p, r1)
  314. assert_equal 1, @adapter.annotate(p, r1).lines.length
  315. [25, 'afc61e85bde7'].each do |r2|
  316. assert @adapter.diff(p, r1, r2)
  317. end
  318. end
  319. end
  320. def test_tag_non_ascii
  321. p = "latin-1-dir/test-#{@char_1}-1.txt"
  322. assert @adapter.cat(p, @tag_char_1)
  323. assert_equal 1, @adapter.annotate(p, @tag_char_1).lines.length
  324. end
  325. def test_branch_non_ascii
  326. p = "latin-1-dir/test-#{@char_1}-subdir/test-#{@char_1}-1.txt"
  327. assert @adapter.cat(p, @branch_char_1)
  328. assert_equal 1, @adapter.annotate(p, @branch_char_1).lines.length
  329. end
  330. def test_nodes_in_branch
  331. [
  332. 'default',
  333. @branch_char_1,
  334. 'branch (1)[2]&,%.-3_4',
  335. @branch_char_0,
  336. 'test_branch.latin-1',
  337. 'test-branch-00',
  338. ].each do |bra|
  339. nib0 = @adapter.nodes_in_branch(bra)
  340. assert nib0
  341. nib1 = @adapter.nodes_in_branch(bra, :limit => 1)
  342. assert_equal 1, nib1.size
  343. case bra
  344. when 'branch (1)[2]&,%.-3_4'
  345. if @adapter.class.client_version_above?([1, 6])
  346. assert_equal 3, nib0.size
  347. assert_equal nib0[0], 'afc61e85bde7'
  348. nib2 = @adapter.nodes_in_branch(bra, :limit => 2)
  349. assert_equal 2, nib2.size
  350. assert_equal nib2[1], '933ca60293d7'
  351. end
  352. when @branch_char_1
  353. if @adapter.class.client_version_above?([1, 6])
  354. assert_equal 2, nib0.size
  355. assert_equal nib0[1], '08ff3227303e'
  356. nib2 = @adapter.nodes_in_branch(bra, :limit => 1)
  357. assert_equal 1, nib2.size
  358. assert_equal nib2[0], '7bbf4c738e71'
  359. end
  360. end
  361. end
  362. end
  363. def test_path_encoding_default_utf8
  364. adpt1 = Redmine::Scm::Adapters::MercurialAdapter.new(
  365. REPOSITORY_PATH
  366. )
  367. assert_equal "UTF-8", adpt1.path_encoding
  368. adpt2 = Redmine::Scm::Adapters::MercurialAdapter.new(
  369. REPOSITORY_PATH,
  370. nil,
  371. nil,
  372. nil,
  373. ""
  374. )
  375. assert_equal "UTF-8", adpt2.path_encoding
  376. end
  377. private
  378. def test_hgversion_for(hgversion, version)
  379. @adapter.class.expects(:hgversion_from_command_line).returns(hgversion)
  380. assert_equal version, @adapter.class.hgversion
  381. end
  382. def test_template_path_for(version, template)
  383. assert_equal "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{template}.#{TEMPLATE_EXTENSION}",
  384. @adapter.class.template_path_for(version)
  385. assert File.exist?(@adapter.class.template_path_for(version))
  386. end
  387. else
  388. puts "Mercurial test repository NOT FOUND. Skipping unit tests !!!"
  389. def test_fake; assert true end
  390. end
  391. end
  392. rescue LoadError
  393. class MercurialMochaFake < ActiveSupport::TestCase
  394. def test_fake; assert(false, "Requires mocha to run those tests") end
  395. end
  396. end