Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

unified_diff_test.rb 15KB

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