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.

unified_diff_test.rb 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 Redmine::UnifiedDiffTest < ActiveSupport::TestCase
  20. def test_subversion_diff
  21. diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'))
  22. # number of files
  23. assert_equal 4, diff.size
  24. assert diff.detect {|file| file.file_name =~ %r{^config/settings.yml}}
  25. end
  26. def test_truncate_diff
  27. diff = Redmine::UnifiedDiff.new(read_diff_fixture('subversion.diff'), :max_lines => 20)
  28. assert_equal 2, diff.size
  29. end
  30. def test_inline_partials
  31. diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'))
  32. assert_equal 1, diff.size
  33. diff = diff.first
  34. assert_equal 43, diff.size
  35. assert_equal [51, -1], diff[0].offsets
  36. assert_equal [51, -1], diff[1].offsets
  37. assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line
  38. assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[1].html_line
  39. assert_nil diff[2].offsets
  40. assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[2].html_line
  41. assert_equal [0, -14], diff[3].offsets
  42. assert_equal [0, -14], diff[4].offsets
  43. assert_equal '<span>Ut sed</span> auctor justo', diff[3].html_line
  44. assert_equal '<span>xxx</span> auctor justo', diff[4].html_line
  45. assert_equal [13, -19], diff[6].offsets
  46. assert_equal [13, -19], diff[7].offsets
  47. assert_equal [24, -8], diff[9].offsets
  48. assert_equal [24, -8], diff[10].offsets
  49. assert_equal [37, -1], diff[12].offsets
  50. assert_equal [37, -1], diff[13].offsets
  51. assert_equal [0, -38], diff[15].offsets
  52. assert_equal [0, -38], diff[16].offsets
  53. end
  54. def test_side_by_side_partials
  55. diff = Redmine::UnifiedDiff.new(read_diff_fixture('partials.diff'), :type => 'sbs')
  56. assert_equal 1, diff.size
  57. diff = diff.first
  58. assert_equal 32, diff.size
  59. assert_equal [51, -1], diff[0].offsets
  60. assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>elit</span>', diff[0].html_line_left
  61. assert_equal 'Lorem ipsum dolor sit amet, consectetur adipiscing <span>xx</span>', diff[0].html_line_right
  62. assert_nil diff[1].offsets
  63. assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_left
  64. assert_equal 'Praesent et sagittis dui. Vivamus ac diam diam', diff[1].html_line_right
  65. assert_equal [0, -14], diff[2].offsets
  66. assert_equal '<span>Ut sed</span> auctor justo', diff[2].html_line_left
  67. assert_equal '<span>xxx</span> auctor justo', diff[2].html_line_right
  68. assert_equal [13, -19], diff[4].offsets
  69. assert_equal [24, -8], diff[6].offsets
  70. assert_equal [37, -1], diff[8].offsets
  71. assert_equal [0, -38], diff[10].offsets
  72. end
  73. def test_partials_with_html_entities
  74. raw = <<~DIFF
  75. --- test.orig.txt Wed Feb 15 16:10:39 2012
  76. +++ test.new.txt Wed Feb 15 16:11:25 2012
  77. @@ -1,5 +1,5 @@
  78. Semicolons were mysteriously appearing in code diffs in the repository
  79. -void DoSomething(std::auto_ptr<MyClass> myObj)
  80. +void DoSomething(const MyClass& myObj)
  81. DIFF
  82. diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
  83. assert_equal 1, diff.size
  84. assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line_left
  85. assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[2].html_line_right
  86. diff = Redmine::UnifiedDiff.new(raw, :type => 'inline')
  87. assert_equal 1, diff.size
  88. assert_equal 'void DoSomething(<span>std::auto_ptr&lt;MyClass&gt;</span> myObj)', diff.first[2].html_line
  89. assert_equal 'void DoSomething(<span>const MyClass&amp;</span> myObj)', diff.first[3].html_line
  90. end
  91. def test_line_starting_with_dashes
  92. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  93. --- old.txt Wed Nov 11 14:24:58 2009
  94. +++ new.txt Wed Nov 11 14:25:02 2009
  95. @@ -1,8 +1,4 @@
  96. -Lines that starts with dashes:
  97. -
  98. -------------------------
  99. --- file.c
  100. -------------------------
  101. +A line that starts with dashes:
  102. and removed.
  103. @@ -23,4 +19,4 @@
  104. -Another chunk of change
  105. +Another chunk of changes
  106. DIFF
  107. assert_equal 1, diff.size
  108. end
  109. def test_one_line_new_files
  110. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  111. diff -r 000000000000 -r ea98b14f75f0 README1
  112. --- /dev/null
  113. +++ b/README1
  114. @@ -0,0 +1,1 @@
  115. +test1
  116. diff -r 000000000000 -r ea98b14f75f0 README2
  117. --- /dev/null
  118. +++ b/README2
  119. @@ -0,0 +1,1 @@
  120. +test2
  121. diff -r 000000000000 -r ea98b14f75f0 README3
  122. --- /dev/null
  123. +++ b/README3
  124. @@ -0,0 +1,3 @@
  125. +test4
  126. +test5
  127. +test6
  128. diff -r 000000000000 -r ea98b14f75f0 README4
  129. --- /dev/null
  130. +++ b/README4
  131. @@ -0,0 +1,3 @@
  132. +test4
  133. +test5
  134. +test6
  135. DIFF
  136. assert_equal 4, diff.size
  137. assert_equal "README1", diff[0].file_name
  138. end
  139. def test_both_git_diff
  140. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  141. # HG changeset patch
  142. # User test
  143. # Date 1348014182 -32400
  144. # Node ID d1c871b8ef113df7f1c56d41e6e3bfbaff976e1f
  145. # Parent 180b6605936cdc7909c5f08b59746ec1a7c99b3e
  146. modify test1.txt
  147. diff -r 180b6605936c -r d1c871b8ef11 test1.txt
  148. --- a/test1.txt
  149. +++ b/test1.txt
  150. @@ -1,1 +1,1 @@
  151. -test1
  152. +modify test1
  153. DIFF
  154. assert_equal 1, diff.size
  155. assert_equal "test1.txt", diff[0].file_name
  156. end
  157. def test_previous_file_name_with_git
  158. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  159. From 585da9683fb5ed7bf7cb438492e3347cdf3d83df Mon Sep 17 00:00:00 2001
  160. From: Gregor Schmidt <schmidt@nach-vorne.eu>
  161. Date: Mon, 5 Mar 2018 14:12:13 +0100
  162. Subject: [PATCH] changes including a rename, rename+modify and addition
  163. ---
  164. one.markdown => one.md | 0
  165. three.md | 2 ++
  166. two.markdown => two.md | 1 +
  167. 3 files changed, 3 insertions(+)
  168. rename one.markdown => one.md (100%)
  169. create mode 100644 three.md
  170. rename two.markdown => two.md (50%)
  171. diff --git a/one.markdown b/one.md
  172. similarity index 100%
  173. rename from one.markdown
  174. rename to one.md
  175. diff --git a/three.md b/three.md
  176. new file mode 100644
  177. index 0000000..288012f
  178. --- /dev/null
  179. +++ b/three.md
  180. @@ -0,0 +1,2 @@
  181. +three
  182. +=====
  183. diff --git a/two.markdown b/two.md
  184. similarity index 50%
  185. rename from two.markdown
  186. rename to two.md
  187. index f719efd..6a268ed 100644
  188. --- a/two.markdown
  189. +++ b/two.md
  190. @@ -1 +1,2 @@
  191. two
  192. +===
  193. --
  194. 2.14.1
  195. DIFF
  196. assert_equal 2, diff.size
  197. assert_equal "three.md", diff[0].file_name
  198. assert_nil diff[0].previous_file_name
  199. assert_equal "two.md", diff[1].file_name
  200. assert_equal "two.markdown", diff[1].previous_file_name
  201. end
  202. def test_include_a_b_slash
  203. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  204. --- test1.txt
  205. +++ b/test02.txt
  206. @@ -1 +0,0 @@
  207. -modify test1
  208. DIFF
  209. assert_equal 1, diff.size
  210. assert_equal "b/test02.txt", diff[0].file_name
  211. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  212. --- a/test1.txt
  213. +++ a/test02.txt
  214. @@ -1 +0,0 @@
  215. -modify test1
  216. DIFF
  217. assert_equal 1, diff.size
  218. assert_equal "a/test02.txt", diff[0].file_name
  219. diff = Redmine::UnifiedDiff.new(<<~DIFF)
  220. --- a/test1.txt
  221. +++ test02.txt
  222. @@ -1 +0,0 @@
  223. -modify test1
  224. DIFF
  225. assert_equal 1, diff.size
  226. assert_equal "test02.txt", diff[0].file_name
  227. end
  228. def test_utf8_ja
  229. with_settings :repositories_encodings => '' do
  230. diff = Redmine::UnifiedDiff.new(read_diff_fixture('issue-12641-ja.diff'), :type => 'inline')
  231. assert_equal 1, diff.size
  232. assert_equal 12, diff.first.size
  233. assert_equal ' text_tip_issue_end_day: この日に終了する<span>タスク</span>', diff.first[4].html_line_left
  234. end
  235. end
  236. def test_utf8_ru
  237. with_settings :repositories_encodings => '' do
  238. diff = Redmine::UnifiedDiff.new(read_diff_fixture('issue-12641-ru.diff'), :type => 'inline')
  239. assert_equal 1, diff.size
  240. assert_equal 8, diff.first.size
  241. assert_equal ' other: &quot;около %{count} час<span>а</span>&quot;', diff.first[3].html_line_left
  242. end
  243. end
  244. def test_offset_range_ascii_1
  245. raw = <<~DIFF
  246. --- a.txt 2013-04-05 14:19:39.000000000 +0900
  247. +++ b.txt 2013-04-05 14:19:51.000000000 +0900
  248. @@ -1,3 +1,3 @@
  249. aaaa
  250. -abc
  251. +abcd
  252. bbbb
  253. DIFF
  254. diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
  255. assert_equal 1, diff.size
  256. assert_equal 3, diff.first.size
  257. assert_equal "abc<span></span>", diff.first[1].html_line_left
  258. assert_equal "abc<span>d</span>", diff.first[1].html_line_right
  259. end
  260. def test_offset_range_ascii_2
  261. raw = <<~DIFF
  262. --- a.txt 2013-04-05 14:19:39.000000000 +0900
  263. +++ b.txt 2013-04-05 14:19:51.000000000 +0900
  264. @@ -1,3 +1,3 @@
  265. aaaa
  266. -abc
  267. +zabc
  268. bbbb
  269. DIFF
  270. diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
  271. assert_equal 1, diff.size
  272. assert_equal 3, diff.first.size
  273. assert_equal "<span></span>abc", diff.first[1].html_line_left
  274. assert_equal "<span>z</span>abc", diff.first[1].html_line_right
  275. end
  276. def test_offset_range_japanese_1
  277. with_settings :repositories_encodings => '' do
  278. diff =
  279. Redmine::UnifiedDiff.new(
  280. read_diff_fixture('issue-13644-1.diff'), :type => 'sbs'
  281. )
  282. assert_equal 1, diff.size
  283. assert_equal 3, diff.first.size
  284. assert_equal '日本<span></span>', diff.first[1].html_line_left
  285. assert_equal '日本<span>語</span>', diff.first[1].html_line_right
  286. end
  287. end
  288. def test_offset_range_japanese_2
  289. with_settings :repositories_encodings => '' do
  290. diff =
  291. Redmine::UnifiedDiff.new(
  292. read_diff_fixture('issue-13644-2.diff'), :type => 'sbs'
  293. )
  294. assert_equal 1, diff.size
  295. assert_equal 3, diff.first.size
  296. assert_equal '<span></span>日本', diff.first[1].html_line_left
  297. assert_equal '<span>にっぽん</span>日本', diff.first[1].html_line_right
  298. end
  299. end
  300. def test_offset_range_japanese_3
  301. # UTF-8 The 1st byte differs.
  302. with_settings :repositories_encodings => '' do
  303. diff =
  304. Redmine::UnifiedDiff.new(
  305. read_diff_fixture('issue-13644-3.diff'), :type => 'sbs'
  306. )
  307. assert_equal 1, diff.size
  308. assert_equal 3, diff.first.size
  309. assert_equal '日本<span>記</span>', diff.first[1].html_line_left
  310. assert_equal '日本<span>娘</span>', diff.first[1].html_line_right
  311. end
  312. end
  313. def test_offset_range_japanese_4
  314. # UTF-8 The 2nd byte differs.
  315. with_settings :repositories_encodings => '' do
  316. diff =
  317. Redmine::UnifiedDiff.new(
  318. read_diff_fixture('issue-13644-4.diff'), :type => 'sbs'
  319. )
  320. assert_equal 1, diff.size
  321. assert_equal 3, diff.first.size
  322. assert_equal '日本<span>記</span>', diff.first[1].html_line_left
  323. assert_equal '日本<span>誘</span>', diff.first[1].html_line_right
  324. end
  325. end
  326. def test_offset_range_japanese_5
  327. # UTF-8 The 2nd byte differs.
  328. with_settings :repositories_encodings => '' do
  329. diff =
  330. Redmine::UnifiedDiff.new(
  331. read_diff_fixture('issue-13644-5.diff'), :type => 'sbs'
  332. )
  333. assert_equal 1, diff.size
  334. assert_equal 3, diff.first.size
  335. assert_equal '日本<span>記</span>ok', diff.first[1].html_line_left
  336. assert_equal '日本<span>誘</span>ok', diff.first[1].html_line_right
  337. end
  338. end
  339. def test_keep_similar_git_footer_line
  340. raw = <<~DIFF
  341. diff --git a/test1.txt b/test1.txt
  342. --- a/test1.txt
  343. +++ b/test1.txt
  344. @@ -1,11 +1,6 @@
  345. $ git init --bare git_utf8_repository
  346. $ hg init git_utf8_repository_hg
  347. $ cd git_utf8_repository_hg
  348. --
  349. -Next line has white space after '-'
  350. --
  351. ---
  352. --
  353. $ touch test.txt
  354. $ hg add test.txt
  355. $ hg commit -m `echo -e "U+1F603\U1F603"` -u `echo -e "U+1F603\U1F603"`
  356. diff --git a/test2.txt b/test2.txt
  357. --- a/test2.txt
  358. +++ b/test2.txt
  359. @@ -5,9 +5,4 @@
  360. $ hg add test.txt
  361. $ hg commit -m `echo -e "U+1F603\U1F603"` -u `echo -e "U+1F603\U1F603"`
  362. $ hg bookmark master
  363. --
  364. -Next line has white space after '-'
  365. --
  366. ---
  367. --
  368. $ hg push ../git_utf8_repository
  369. DIFF
  370. lines = raw.split("\n")
  371. assert_equal '-- ', lines[9]
  372. assert_equal '-- ', lines[24]
  373. diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
  374. assert_equal 2, diff.size
  375. assert_equal 11, diff[0].size
  376. assert_equal 9, diff[1].size
  377. end
  378. def test_git_footer_line
  379. raw = <<~DIFF
  380. From 1ed13eda266a3e0a5a8624e79ae28874ebcdeb5c Mon Sep 17 00:00:00 2001
  381. From: test <none@none>
  382. Date: Thu, 30 Apr 2020 11:40:20 +0900
  383. Subject: [PATCH] add 'rpm -q git' and its result
  384. ---
  385. test.txt | 2 ++
  386. 1 file changed, 2 insertions(+)
  387. diff --git a/test.txt b/test.txt
  388. index 0a406b9..c39ee31 100644
  389. --- a/test.txt
  390. +++ b/test.txt
  391. @@ -6,3 +6,5 @@ $ hg add test.txt
  392. $ hg commit -m `echo -e "U+1F603\U1F603"` -u `echo -e "U+1F603\U1F603"`
  393. $ hg bookmark master
  394. $ hg push ../git_utf8_repository
  395. +$ rpm -q git
  396. +git-1.8.3.1-21.el7_7.x86_64
  397. --
  398. 1.8.3.1
  399. DIFF
  400. lines = raw.split("\n")
  401. lines << ""
  402. assert_equal '', lines[-1]
  403. body_lines = lines[0..-4]
  404. footer_lines = lines[-3..-1]
  405. assert_equal '+git-1.8.3.1-21.el7_7.x86_64', body_lines[-1]
  406. assert_equal '-- ', footer_lines[0]
  407. assert_equal '', footer_lines[-1]
  408. diff = Redmine::UnifiedDiff.new(body_lines.join("\n") + "\n", :type => 'sbs')
  409. diff_size = diff.size
  410. diff_0_size = diff[0].size
  411. assert_equal 1, diff_size
  412. assert_equal 5, diff_0_size
  413. diff = Redmine::UnifiedDiff.new("test\n", :type => 'sbs')
  414. assert_equal 0, diff.size
  415. diff = Redmine::UnifiedDiff.new("test\ntest\n", :type => 'sbs')
  416. assert_equal 0, diff.size
  417. diff = Redmine::UnifiedDiff.new(raw, :type => 'sbs')
  418. assert_equal diff_size, diff.size
  419. assert_equal diff_0_size, diff[0].size
  420. diff = Redmine::UnifiedDiff.new(raw + "\n\n\n\n", :type => 'sbs')
  421. assert_equal diff_size, diff.size
  422. assert_equal diff_0_size, diff[0].size
  423. end
  424. private
  425. def read_diff_fixture(filename)
  426. File.new(File.join(File.dirname(__FILE__), '/../../../fixtures/diffs', filename)).read
  427. end
  428. end