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.

repositories_mercurial_controller_test.rb 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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 RepositoriesMercurialControllerTest < Redmine::ControllerTest
  19. tests RepositoriesController
  20. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
  21. :repositories, :enabled_modules
  22. REPOSITORY_PATH = Rails.root.join('tmp/test/mercurial_repository').to_s
  23. CHAR_1_HEX = "\xc3\x9c"
  24. PRJ_ID = 3
  25. NUM_REV = 34
  26. ruby19_non_utf8_pass = Encoding.default_external.to_s != 'UTF-8'
  27. def setup
  28. User.current = nil
  29. @project = Project.find(PRJ_ID)
  30. @repository = Repository::Mercurial.create(
  31. :project => @project,
  32. :url => REPOSITORY_PATH,
  33. :path_encoding => 'ISO-8859-1'
  34. )
  35. assert @repository
  36. @diff_c_support = true
  37. @char_1 = CHAR_1_HEX.dup.force_encoding('UTF-8')
  38. @tag_char_1 = "tag-#{CHAR_1_HEX}-00".force_encoding('UTF-8')
  39. @branch_char_0 = "branch-#{CHAR_1_HEX}-00".force_encoding('UTF-8')
  40. @branch_char_1 = "branch-#{CHAR_1_HEX}-01".force_encoding('UTF-8')
  41. end
  42. if ruby19_non_utf8_pass
  43. puts "TODO: Mercurial functional test fails " +
  44. "when Encoding.default_external is not UTF-8. " +
  45. "Current value is '#{Encoding.default_external.to_s}'"
  46. def test_fake; assert true end
  47. elsif File.directory?(REPOSITORY_PATH)
  48. def test_get_new
  49. @request.session[:user_id] = 1
  50. @project.repository.destroy
  51. get :new, :project_id => 'subproject1', :repository_scm => 'Mercurial'
  52. assert_response :success
  53. assert_select 'select[name=?]', 'repository_scm' do
  54. assert_select 'option[value=?][selected=selected]', 'Mercurial'
  55. end
  56. end
  57. def test_show_root
  58. assert_equal 0, @repository.changesets.count
  59. @repository.fetch_changesets
  60. @project.reload
  61. assert_equal NUM_REV, @repository.changesets.count
  62. get :show, :id => PRJ_ID
  63. assert_response :success
  64. assert_select 'table.entries tbody' do
  65. assert_select 'tr', 4
  66. assert_select 'tr.dir td.filename a', :text => 'images'
  67. assert_select 'tr.dir td.filename a', :text => 'sources'
  68. assert_select 'tr.file td.filename a', :text => '.hgtags'
  69. assert_select 'tr.file td.filename a', :text => 'README'
  70. end
  71. assert_select 'table.changesets tbody' do
  72. assert_select 'tr'
  73. end
  74. end
  75. def test_show_directory
  76. assert_equal 0, @repository.changesets.count
  77. @repository.fetch_changesets
  78. @project.reload
  79. assert_equal NUM_REV, @repository.changesets.count
  80. get :show, :id => PRJ_ID, :path => repository_path_hash(['images'])[:param]
  81. assert_response :success
  82. assert_select 'table.entries tbody' do
  83. assert_select 'tr', 2
  84. assert_select 'tr.file td.filename a', :text => 'delete.png'
  85. assert_select 'tr.file td.filename a', :text => 'edit.png'
  86. end
  87. assert_select 'table.changesets tbody' do
  88. assert_select 'tr'
  89. end
  90. end
  91. def test_show_at_given_revision
  92. assert_equal 0, @repository.changesets.count
  93. @repository.fetch_changesets
  94. @project.reload
  95. assert_equal NUM_REV, @repository.changesets.count
  96. [0, '0', '0885933ad4f6'].each do |r1|
  97. get :show, :id => PRJ_ID, :path => repository_path_hash(['images'])[:param],
  98. :rev => r1
  99. assert_response :success
  100. assert_select 'table.entries tbody' do
  101. assert_select 'tr', 1
  102. assert_select 'tr.file td.filename a', :text => 'delete.png'
  103. end
  104. assert_select 'table.changesets tbody' do
  105. assert_select 'tr'
  106. end
  107. end
  108. end
  109. def test_show_directory_sql_escape_percent
  110. assert_equal 0, @repository.changesets.count
  111. @repository.fetch_changesets
  112. @project.reload
  113. assert_equal NUM_REV, @repository.changesets.count
  114. [13, '13', '3a330eb32958'].each do |r1|
  115. get :show, :id => PRJ_ID,
  116. :path => repository_path_hash(['sql_escape', 'percent%dir'])[:param],
  117. :rev => r1
  118. assert_response :success
  119. assert_select 'table.entries tbody' do
  120. assert_select 'tr', 2
  121. assert_select 'tr.file td.filename a', :text => 'percent%file1.txt'
  122. assert_select 'tr.file td.filename a', :text => 'percentfile1.txt'
  123. end
  124. assert_select 'table.changesets tbody' do
  125. assert_select 'tr td.id a', :text => /^13:/
  126. assert_select 'tr td.id a', :text => /^11:/
  127. assert_select 'tr td.id a', :text => /^10:/
  128. assert_select 'tr td.id a', :text => /^9:/
  129. end
  130. end
  131. end
  132. def test_show_directory_latin_1_path
  133. assert_equal 0, @repository.changesets.count
  134. @repository.fetch_changesets
  135. @project.reload
  136. assert_equal NUM_REV, @repository.changesets.count
  137. [21, '21', 'adf805632193'].each do |r1|
  138. get :show, :id => PRJ_ID,
  139. :path => repository_path_hash(['latin-1-dir'])[:param],
  140. :rev => r1
  141. assert_response :success
  142. assert_select 'table.entries tbody' do
  143. assert_select 'tr', 4
  144. assert_select 'tr.file td.filename a', :text => "make-latin-1-file.rb"
  145. assert_select 'tr.file td.filename a', :text => "test-#{@char_1}-1.txt"
  146. assert_select 'tr.file td.filename a', :text => "test-#{@char_1}-2.txt"
  147. assert_select 'tr.file td.filename a', :text => "test-#{@char_1}.txt"
  148. end
  149. assert_select 'table.changesets tbody' do
  150. assert_select 'tr td.id a', :text => /^21:/
  151. assert_select 'tr td.id a', :text => /^20:/
  152. assert_select 'tr td.id a', :text => /^19:/
  153. assert_select 'tr td.id a', :text => /^18:/
  154. assert_select 'tr td.id a', :text => /^17:/
  155. end
  156. end
  157. end
  158. def show_should_show_branch_selection_form
  159. @repository.fetch_changesets
  160. @project.reload
  161. get :show, :id => PRJ_ID
  162. assert_select 'form#revision_selector[action=?]', '/projects/subproject1/repository/show' do
  163. assert_select 'select[name=branch]' do
  164. assert_select 'option[value=?]', 'test-branch-01'
  165. end
  166. end
  167. end
  168. def test_show_branch
  169. assert_equal 0, @repository.changesets.count
  170. @repository.fetch_changesets
  171. @project.reload
  172. assert_equal NUM_REV, @repository.changesets.count
  173. [
  174. 'default',
  175. @branch_char_1,
  176. 'branch (1)[2]&,%.-3_4',
  177. @branch_char_0,
  178. 'test_branch.latin-1',
  179. 'test-branch-00',
  180. ].each do |bra|
  181. get :show, :id => PRJ_ID, :rev => bra
  182. assert_response :success
  183. assert_select 'table.entries tbody tr'
  184. assert_select 'table.changesets tbody tr'
  185. end
  186. end
  187. def test_show_tag
  188. assert_equal 0, @repository.changesets.count
  189. @repository.fetch_changesets
  190. @project.reload
  191. assert_equal NUM_REV, @repository.changesets.count
  192. [
  193. @tag_char_1,
  194. 'tag_test.00',
  195. 'tag-init-revision'
  196. ].each do |tag|
  197. get :show, :id => PRJ_ID, :rev => tag
  198. assert_response :success
  199. assert_select 'table.entries tbody tr'
  200. assert_select 'table.changesets tbody tr'
  201. end
  202. end
  203. def test_changes
  204. get :changes, :id => PRJ_ID,
  205. :path => repository_path_hash(['images', 'edit.png'])[:param]
  206. assert_response :success
  207. assert_select 'h2', :text => /edit.png/
  208. end
  209. def test_entry_show
  210. get :entry, :id => PRJ_ID,
  211. :path => repository_path_hash(['sources', 'watchers_controller.rb'])[:param]
  212. assert_response :success
  213. # Line 10
  214. assert_select 'tr#L10 td.line-code', :text => /WITHOUT ANY WARRANTY/
  215. end
  216. def test_entry_show_latin_1_path
  217. [21, '21', 'adf805632193'].each do |r1|
  218. get :entry, :id => PRJ_ID,
  219. :path => repository_path_hash(['latin-1-dir', "test-#{@char_1}-2.txt"])[:param],
  220. :rev => r1
  221. assert_response :success
  222. assert_select 'tr#L1 td.line-code', :text => /Mercurial is a distributed version control system/
  223. end
  224. end
  225. def test_entry_show_latin_1_contents
  226. with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
  227. [27, '27', '7bbf4c738e71'].each do |r1|
  228. get :entry, :id => PRJ_ID,
  229. :path => repository_path_hash(['latin-1-dir', "test-#{@char_1}.txt"])[:param],
  230. :rev => r1
  231. assert_response :success
  232. assert_select 'tr#L1 td.line-code', :text => /test-#{@char_1}.txt/
  233. end
  234. end
  235. end
  236. def test_entry_download
  237. get :entry, :id => PRJ_ID,
  238. :path => repository_path_hash(['sources', 'watchers_controller.rb'])[:param],
  239. :format => 'raw'
  240. assert_response :success
  241. # File content
  242. assert @response.body.include?('WITHOUT ANY WARRANTY')
  243. end
  244. def test_entry_binary_force_download
  245. # TODO: add a binary file which is not an image to the test repo
  246. end
  247. def test_directory_entry
  248. get :entry, :id => PRJ_ID,
  249. :path => repository_path_hash(['sources'])[:param]
  250. assert_response :success
  251. assert_select 'h2 a', :text => 'sources'
  252. assert_select 'table.entries tbody'
  253. end
  254. def test_diff
  255. assert_equal 0, @repository.changesets.count
  256. @repository.fetch_changesets
  257. @project.reload
  258. assert_equal NUM_REV, @repository.changesets.count
  259. [4, '4', 'def6d2f1254a'].each do |r1|
  260. # Full diff of changeset 4
  261. ['inline', 'sbs'].each do |dt|
  262. get :diff, :id => PRJ_ID, :rev => r1, :type => dt
  263. assert_response :success
  264. if @diff_c_support
  265. # Line 22 removed
  266. assert_select 'th.line-num:contains(22) ~ td.diff_out', :text => /def remove/
  267. assert_select 'h2', :text => /4:def6d2f1254a/
  268. end
  269. end
  270. end
  271. end
  272. def test_diff_two_revs
  273. assert_equal 0, @repository.changesets.count
  274. @repository.fetch_changesets
  275. @project.reload
  276. assert_equal NUM_REV, @repository.changesets.count
  277. [2, '400bb8672109', '400', 400].each do |r1|
  278. [4, 'def6d2f1254a'].each do |r2|
  279. ['inline', 'sbs'].each do |dt|
  280. get :diff,
  281. :id => PRJ_ID,
  282. :rev => r1,
  283. :rev_to => r2,
  284. :type => dt
  285. assert_response :success
  286. assert_select 'h2', :text => /4:def6d2f1254a 2:400bb8672109/
  287. end
  288. end
  289. end
  290. end
  291. def test_diff_latin_1_path
  292. with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
  293. [21, 'adf805632193'].each do |r1|
  294. ['inline', 'sbs'].each do |dt|
  295. get :diff, :id => PRJ_ID, :rev => r1, :type => dt
  296. assert_response :success
  297. assert_select 'table' do
  298. assert_select 'thead th.filename', :text => /latin-1-dir\/test-#{@char_1}-2.txt/
  299. assert_select 'tbody td.diff_in', :text => /It is written in Python/
  300. end
  301. end
  302. end
  303. end
  304. end
  305. def test_diff_should_show_modified_filenames
  306. get :diff, :id => PRJ_ID, :rev => '400bb8672109', :type => 'inline'
  307. assert_response :success
  308. assert_select 'th.filename', :text => 'sources/watchers_controller.rb'
  309. end
  310. def test_diff_should_show_deleted_filenames
  311. get :diff, :id => PRJ_ID, :rev => 'b3a615152df8', :type => 'inline'
  312. assert_response :success
  313. assert_select 'th.filename', :text => 'sources/welcome_controller.rb'
  314. end
  315. def test_annotate
  316. get :annotate, :id => PRJ_ID,
  317. :path => repository_path_hash(['sources', 'watchers_controller.rb'])[:param]
  318. assert_response :success
  319. # Line 22, revision 4:def6d2f1254a
  320. assert_select 'tr' do
  321. assert_select 'th.line-num', :text => '22'
  322. assert_select 'td.revision', :text => '4:def6d2f1254a'
  323. assert_select 'td.author', :text => 'jsmith'
  324. assert_select 'td', :text => /remove_watcher/
  325. end
  326. end
  327. def test_annotate_not_in_tip
  328. assert_equal 0, @repository.changesets.count
  329. @repository.fetch_changesets
  330. @project.reload
  331. assert_equal NUM_REV, @repository.changesets.count
  332. get :annotate, :id => PRJ_ID,
  333. :path => repository_path_hash(['sources', 'welcome_controller.rb'])[:param]
  334. assert_response 404
  335. assert_select_error /was not found/
  336. end
  337. def test_annotate_at_given_revision
  338. assert_equal 0, @repository.changesets.count
  339. @repository.fetch_changesets
  340. @project.reload
  341. assert_equal NUM_REV, @repository.changesets.count
  342. [2, '400bb8672109', '400', 400].each do |r1|
  343. get :annotate, :id => PRJ_ID, :rev => r1,
  344. :path => repository_path_hash(['sources', 'watchers_controller.rb'])[:param]
  345. assert_response :success
  346. assert_select 'h2', :text => /@ 2:400bb8672109/
  347. end
  348. end
  349. def test_annotate_latin_1_path
  350. [21, '21', 'adf805632193'].each do |r1|
  351. get :annotate, :id => PRJ_ID,
  352. :path => repository_path_hash(['latin-1-dir', "test-#{@char_1}-2.txt"])[:param],
  353. :rev => r1
  354. assert_response :success
  355. assert_select "th.line-num", :text => '1' do
  356. assert_select "+ td.revision" do
  357. assert_select "a", :text => '20:709858aafd1b'
  358. assert_select "+ td.author", :text => "jsmith" do
  359. assert_select "+ td",
  360. :text => "Mercurial is a distributed version control system."
  361. end
  362. end
  363. end
  364. end
  365. end
  366. def test_annotate_latin_1_contents
  367. with_settings :repositories_encodings => 'UTF-8,ISO-8859-1' do
  368. [27, '7bbf4c738e71'].each do |r1|
  369. get :annotate, :id => PRJ_ID,
  370. :path => repository_path_hash(['latin-1-dir', "test-#{@char_1}.txt"])[:param],
  371. :rev => r1
  372. assert_select 'tr#L1 td.line-code', :text => /test-#{@char_1}.txt/
  373. end
  374. end
  375. end
  376. def test_revision
  377. assert_equal 0, @repository.changesets.count
  378. @repository.fetch_changesets
  379. @project.reload
  380. assert_equal NUM_REV, @repository.changesets.count
  381. ['1', '9d5b5b', '9d5b5b004199'].each do |r|
  382. with_settings :default_language => "en" do
  383. get :revision, :id => PRJ_ID, :rev => r
  384. assert_response :success
  385. assert_select 'title',
  386. :text => 'Revision 1:9d5b5b004199 - Added 2 files and modified one. - eCookbook Subproject 1 - Redmine'
  387. end
  388. end
  389. end
  390. def test_empty_revision
  391. assert_equal 0, @repository.changesets.count
  392. @repository.fetch_changesets
  393. @project.reload
  394. assert_equal NUM_REV, @repository.changesets.count
  395. ['', ' ', nil].each do |r|
  396. get :revision, :id => PRJ_ID, :rev => r
  397. assert_response 404
  398. assert_select_error /was not found/
  399. end
  400. end
  401. def test_destroy_valid_repository
  402. @request.session[:user_id] = 1 # admin
  403. assert_equal 0, @repository.changesets.count
  404. @repository.fetch_changesets
  405. assert_equal NUM_REV, @repository.changesets.count
  406. assert_difference 'Repository.count', -1 do
  407. delete :destroy, :id => @repository.id
  408. end
  409. assert_response 302
  410. @project.reload
  411. assert_nil @project.repository
  412. end
  413. def test_destroy_invalid_repository
  414. @request.session[:user_id] = 1 # admin
  415. @project.repository.destroy
  416. @repository = Repository::Mercurial.create!(
  417. :project => Project.find(PRJ_ID),
  418. :url => "/invalid",
  419. :path_encoding => 'ISO-8859-1'
  420. )
  421. @repository.fetch_changesets
  422. assert_equal 0, @repository.changesets.count
  423. assert_difference 'Repository.count', -1 do
  424. delete :destroy, :id => @repository.id
  425. end
  426. assert_response 302
  427. @project.reload
  428. assert_nil @project.repository
  429. end
  430. else
  431. puts "Mercurial test repository NOT FOUND. Skipping functional tests !!!"
  432. def test_fake; assert true end
  433. end
  434. end