diff-lcs 1.3 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +518 -0
  3. data/CODE_OF_CONDUCT.md +128 -0
  4. data/CONTRIBUTING.md +71 -0
  5. data/CONTRIBUTORS.md +49 -0
  6. data/{License.md → LICENCE.md} +21 -20
  7. data/Manifest.txt +84 -6
  8. data/README.md +92 -0
  9. data/Rakefile +104 -46
  10. data/SECURITY.md +41 -0
  11. data/bin/htmldiff +9 -6
  12. data/bin/ldiff +4 -1
  13. data/docs/artistic.txt +1 -1
  14. data/lib/diff/lcs/array.rb +2 -2
  15. data/lib/diff/lcs/backports.rb +13 -0
  16. data/lib/diff/lcs/block.rb +5 -5
  17. data/lib/diff/lcs/callbacks.rb +22 -17
  18. data/lib/diff/lcs/change.rb +44 -51
  19. data/lib/diff/lcs/htmldiff.rb +25 -14
  20. data/lib/diff/lcs/hunk.rb +174 -71
  21. data/lib/diff/lcs/internals.rb +57 -56
  22. data/lib/diff/lcs/ldiff.rb +101 -79
  23. data/lib/diff/lcs/string.rb +1 -1
  24. data/lib/diff/lcs/version.rb +7 -0
  25. data/lib/diff/lcs.rb +229 -212
  26. data/lib/diff-lcs.rb +2 -2
  27. data/mise.toml +5 -0
  28. data/spec/change_spec.rb +58 -34
  29. data/spec/diff_spec.rb +13 -9
  30. data/spec/fixtures/123_x +2 -0
  31. data/spec/fixtures/456_x +2 -0
  32. data/spec/fixtures/aX +1 -0
  33. data/spec/fixtures/bXaX +1 -0
  34. data/spec/fixtures/empty +0 -0
  35. data/spec/fixtures/file1.bin +0 -0
  36. data/spec/fixtures/file2.bin +0 -0
  37. data/spec/fixtures/four_lines +4 -0
  38. data/spec/fixtures/four_lines_with_missing_new_line +4 -0
  39. data/spec/fixtures/ldiff/diff.missing_new_line1-e +1 -0
  40. data/spec/fixtures/ldiff/diff.missing_new_line1-f +1 -0
  41. data/spec/fixtures/ldiff/diff.missing_new_line2-e +1 -0
  42. data/spec/fixtures/ldiff/diff.missing_new_line2-f +1 -0
  43. data/spec/fixtures/ldiff/error.diff.chef-e +2 -0
  44. data/spec/fixtures/ldiff/error.diff.chef-f +2 -0
  45. data/spec/fixtures/ldiff/error.diff.missing_new_line1-e +1 -0
  46. data/spec/fixtures/ldiff/error.diff.missing_new_line1-f +1 -0
  47. data/spec/fixtures/ldiff/error.diff.missing_new_line2-e +1 -0
  48. data/spec/fixtures/ldiff/error.diff.missing_new_line2-f +1 -0
  49. data/spec/fixtures/ldiff/output.diff +4 -0
  50. data/spec/fixtures/ldiff/output.diff-c +7 -0
  51. data/spec/fixtures/ldiff/output.diff-e +3 -0
  52. data/spec/fixtures/ldiff/output.diff-f +3 -0
  53. data/spec/fixtures/ldiff/output.diff-u +5 -0
  54. data/spec/fixtures/ldiff/output.diff.bin1 +0 -0
  55. data/spec/fixtures/ldiff/output.diff.bin1-c +0 -0
  56. data/spec/fixtures/ldiff/output.diff.bin1-e +0 -0
  57. data/spec/fixtures/ldiff/output.diff.bin1-f +0 -0
  58. data/spec/fixtures/ldiff/output.diff.bin1-u +0 -0
  59. data/spec/fixtures/ldiff/output.diff.bin2 +1 -0
  60. data/spec/fixtures/ldiff/output.diff.bin2-c +1 -0
  61. data/spec/fixtures/ldiff/output.diff.bin2-e +1 -0
  62. data/spec/fixtures/ldiff/output.diff.bin2-f +1 -0
  63. data/spec/fixtures/ldiff/output.diff.bin2-u +1 -0
  64. data/spec/fixtures/ldiff/output.diff.chef +4 -0
  65. data/spec/fixtures/ldiff/output.diff.chef-c +15 -0
  66. data/spec/fixtures/ldiff/output.diff.chef-e +3 -0
  67. data/spec/fixtures/ldiff/output.diff.chef-f +3 -0
  68. data/spec/fixtures/ldiff/output.diff.chef-u +9 -0
  69. data/spec/fixtures/ldiff/output.diff.chef2 +7 -0
  70. data/spec/fixtures/ldiff/output.diff.chef2-c +20 -0
  71. data/spec/fixtures/ldiff/output.diff.chef2-d +7 -0
  72. data/spec/fixtures/ldiff/output.diff.chef2-e +7 -0
  73. data/spec/fixtures/ldiff/output.diff.chef2-f +7 -0
  74. data/spec/fixtures/ldiff/output.diff.chef2-u +16 -0
  75. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines +5 -0
  76. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-c +9 -0
  77. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-e +6 -0
  78. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-f +6 -0
  79. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-u +7 -0
  80. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty +5 -0
  81. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-c +9 -0
  82. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-e +1 -0
  83. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-f +1 -0
  84. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-u +7 -0
  85. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context +4 -0
  86. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-c +9 -0
  87. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-e +3 -0
  88. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-f +3 -0
  89. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-u +6 -0
  90. data/spec/fixtures/ldiff/output.diff.missing_new_line1 +5 -0
  91. data/spec/fixtures/ldiff/output.diff.missing_new_line1-c +14 -0
  92. data/spec/fixtures/ldiff/output.diff.missing_new_line1-e +0 -0
  93. data/spec/fixtures/ldiff/output.diff.missing_new_line1-f +0 -0
  94. data/spec/fixtures/ldiff/output.diff.missing_new_line1-u +9 -0
  95. data/spec/fixtures/ldiff/output.diff.missing_new_line2 +5 -0
  96. data/spec/fixtures/ldiff/output.diff.missing_new_line2-c +14 -0
  97. data/spec/fixtures/ldiff/output.diff.missing_new_line2-e +0 -0
  98. data/spec/fixtures/ldiff/output.diff.missing_new_line2-f +0 -0
  99. data/spec/fixtures/ldiff/output.diff.missing_new_line2-u +9 -0
  100. data/spec/fixtures/new-chef +4 -0
  101. data/spec/fixtures/new-chef2 +17 -0
  102. data/spec/fixtures/old-chef +4 -0
  103. data/spec/fixtures/old-chef2 +14 -0
  104. data/spec/hunk_spec.rb +49 -38
  105. data/spec/issues_spec.rb +132 -21
  106. data/spec/lcs_spec.rb +3 -3
  107. data/spec/ldiff_spec.rb +83 -30
  108. data/spec/patch_spec.rb +14 -20
  109. data/spec/sdiff_spec.rb +83 -81
  110. data/spec/spec_helper.rb +220 -165
  111. data/spec/traverse_balanced_spec.rb +138 -136
  112. data/spec/traverse_sequences_spec.rb +7 -9
  113. metadata +127 -77
  114. data/Code-of-Conduct.md +0 -74
  115. data/Contributing.md +0 -83
  116. data/History.md +0 -220
  117. data/README.rdoc +0 -84
  118. data/autotest/discover.rb +0 -1
@@ -0,0 +1,17 @@
1
+ recipe[a::default]
2
+ recipe[c::default]
3
+ recipe[d::default]
4
+ recipe[e::default]
5
+ recipe[f::default]
6
+ recipe[g::default]
7
+ recipe[h::default]
8
+ recipe[i::default]
9
+ recipe[j::default]
10
+ recipe[k::default]
11
+ recipe[l::default]
12
+ recipe[m::default]
13
+ recipe[n::default]
14
+ recipe[o::new]
15
+ recipe[p::new]
16
+ recipe[q::new]
17
+ recipe[r::new]
@@ -0,0 +1,4 @@
1
+ {
2
+ "name": "x",
3
+ "description": "hi"
4
+ }
@@ -0,0 +1,14 @@
1
+ recipe[a::default]
2
+ recipe[b::default]
3
+ recipe[c::default]
4
+ recipe[d::default]
5
+ recipe[e::default]
6
+ recipe[f::default]
7
+ recipe[g::default]
8
+ recipe[h::default]
9
+ recipe[i::default]
10
+ recipe[j::default]
11
+ recipe[k::default]
12
+ recipe[l::default]
13
+ recipe[m::default]
14
+ recipe[n::default]
data/spec/hunk_spec.rb CHANGED
@@ -1,69 +1,80 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
4
 
5
5
  if String.method_defined?(:encoding)
6
- require 'diff/lcs/hunk'
6
+ require "diff/lcs/hunk"
7
7
 
8
8
  describe Diff::LCS::Hunk do
9
- let(:old_data) { ["Tu avec carté {count} itém has".encode('UTF-16LE')] }
10
- let(:new_data) { ["Tu avec carte {count} item has".encode('UTF-16LE')] }
11
- let(:pieces) { Diff::LCS.diff old_data, new_data }
12
- let(:hunk) { Diff::LCS::Hunk.new(old_data, new_data, pieces[0], 3, 0) }
13
-
14
- it 'produces a unified diff from the two pieces' do
15
- expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
16
- @@ -1,2 +1,2 @@
17
- -Tu avec carté {count} itém has
18
- +Tu avec carte {count} item has
19
- EOD
9
+ let(:old_data) { ["Tu a un carté avec {count} itéms".encode("UTF-16LE")] }
10
+ let(:new_data) { ["Tu a un carte avec {count} items".encode("UTF-16LE")] }
11
+ let(:pieces) { Diff::LCS.diff old_data, new_data }
12
+ let(:hunk) { Diff::LCS::Hunk.new(old_data, new_data, pieces[0], 3, 0) }
13
+
14
+ it "produces a unified diff from the two pieces" do
15
+ expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
16
+ @@ -1 +1 @@
17
+ -Tu a un carté avec {count} itéms
18
+ +Tu a un carte avec {count} items
19
+ EXPECTED
20
20
 
21
21
  expect(hunk.diff(:unified)).to eq(expected)
22
22
  end
23
23
 
24
- it 'produces a context diff from the two pieces' do
25
- expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
24
+ it "produces a unified diff from the two pieces (last entry)" do
25
+ expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
26
+ @@ -1 +1 @@
27
+ -Tu a un carté avec {count} itéms
28
+ +Tu a un carte avec {count} items
29
+ \
30
+ EXPECTED
31
+
32
+ expect(hunk.diff(:unified, true)).to eq(expected)
33
+ end
34
+
35
+ it "produces a context diff from the two pieces" do
36
+ expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
26
37
  ***************
27
- *** 1,2 ****
28
- !Tu avec carté {count} itém has
29
- --- 1,2 ----
30
- !Tu avec carte {count} item has
31
- EOD
38
+ *** 1 ****
39
+ ! Tu a un carté avec {count} itéms
40
+ --- 1 ----
41
+ ! Tu a un carte avec {count} items
42
+ EXPECTED
32
43
 
33
44
  expect(hunk.diff(:context)).to eq(expected)
34
45
  end
35
46
 
36
- it 'produces an old diff from the two pieces' do
37
- expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
38
- 1,2c1,2
39
- < Tu avec carté {count} itém has
47
+ it "produces an old diff from the two pieces" do
48
+ expected = <<-EXPECTED.gsub(/^ +/, "").encode("UTF-16LE").chomp
49
+ 1c1
50
+ < Tu a un carté avec {count} itéms
40
51
  ---
41
- > Tu avec carte {count} item has
52
+ > Tu a un carte avec {count} items
42
53
 
43
- EOD
54
+ EXPECTED
44
55
 
45
56
  expect(hunk.diff(:old)).to eq(expected)
46
57
  end
47
58
 
48
- it 'produces a reverse ed diff from the two pieces' do
49
- expected = (<<-EOD.gsub(/^ +/,'').encode('UTF-16LE').chomp)
50
- c1,2
51
- Tu avec carte {count} item has
59
+ it "produces a reverse ed diff from the two pieces" do
60
+ expected = <<-EXPECTED.gsub(/^ +/, "").encode("UTF-16LE").chomp
61
+ c1
62
+ Tu a un carte avec {count} items
52
63
  .
53
64
 
54
- EOD
65
+ EXPECTED
55
66
 
56
67
  expect(hunk.diff(:reverse_ed)).to eq(expected)
57
68
  end
58
69
 
59
- context 'with empty first data set' do
70
+ context "with empty first data set" do
60
71
  let(:old_data) { [] }
61
72
 
62
- it 'produces a unified diff' do
63
- expected = (<<-EOD.gsub(/^\s+/,'').encode('UTF-16LE').chomp)
64
- @@ -1 +1,2 @@
65
- +Tu avec carte {count} item has
66
- EOD
73
+ it "produces a unified diff" do
74
+ expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
75
+ @@ -0,0 +1 @@
76
+ +Tu a un carte avec {count} items
77
+ EXPECTED
67
78
 
68
79
  expect(hunk.diff(:unified)).to eq(expected)
69
80
  end
data/spec/issues_spec.rb CHANGED
@@ -1,49 +1,160 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
+ require "diff/lcs/hunk"
4
5
 
5
6
  describe "Diff::LCS Issues" do
6
7
  include Diff::LCS::SpecHelper::Matchers
7
8
 
8
- describe 'issue #1' do
9
- shared_examples 'handles simple diffs' do |s1, s2, forward_diff|
9
+ describe "issue #1" do
10
+ shared_examples "handles simple diffs" do |s1, s2, forward_diff|
10
11
  before do
11
12
  @diff_s1_s2 = Diff::LCS.diff(s1, s2)
12
13
  end
13
14
 
14
- it 'creates the correct diff' do
15
+ it "creates the correct diff" do
15
16
  expect(change_diff(forward_diff)).to eq(@diff_s1_s2)
16
17
  end
17
18
 
18
- it 'creates the correct patch s1->s2' do
19
+ it "creates the correct patch s1->s2" do
19
20
  expect(Diff::LCS.patch(s1, @diff_s1_s2)).to eq(s2)
20
21
  end
21
22
 
22
- it 'creates the correct patch s2->s1' do
23
+ it "creates the correct patch s2->s1" do
23
24
  expect(Diff::LCS.patch(s2, @diff_s1_s2)).to eq(s1)
24
25
  end
25
26
  end
26
27
 
27
- describe 'string' do
28
- it_has_behavior 'handles simple diffs', 'aX', 'bXaX', [
29
- [ [ '+', 0, 'b' ],
30
- [ '+', 1, 'X' ] ],
28
+ describe "string" do
29
+ it_has_behavior "handles simple diffs", "aX", "bXaX", [
30
+ [
31
+ ["+", 0, "b"],
32
+ ["+", 1, "X"]
33
+ ]
31
34
  ]
32
- it_has_behavior 'handles simple diffs', 'bXaX', 'aX', [
33
- [ [ '-', 0, 'b' ],
34
- [ '-', 1, 'X' ] ],
35
+ it_has_behavior "handles simple diffs", "bXaX", "aX", [
36
+ [
37
+ ["-", 0, "b"],
38
+ ["-", 1, "X"]
39
+ ]
35
40
  ]
36
41
  end
37
42
 
38
- describe 'array' do
39
- it_has_behavior 'handles simple diffs', %w(a X), %w(b X a X), [
40
- [ [ '+', 0, 'b' ],
41
- [ '+', 1, 'X' ] ],
43
+ describe "array" do
44
+ it_has_behavior "handles simple diffs", %w[a X], %w[b X a X], [
45
+ [
46
+ ["+", 0, "b"],
47
+ ["+", 1, "X"]
48
+ ]
42
49
  ]
43
- it_has_behavior 'handles simple diffs', %w(b X a X), %w(a X), [
44
- [ [ '-', 0, 'b' ],
45
- [ '-', 1, 'X' ] ],
50
+ it_has_behavior "handles simple diffs", %w[b X a X], %w[a X], [
51
+ [
52
+ ["-", 0, "b"],
53
+ ["-", 1, "X"]
54
+ ]
46
55
  ]
47
56
  end
48
57
  end
58
+
59
+ describe "issue #57" do
60
+ it "should fail with a correct error" do
61
+ # standard:disable Style/HashSyntax
62
+ expect {
63
+ actual = {:category => "app.rack.request"}
64
+ expected = {:category => "rack.middleware", :title => "Anonymous Middleware"}
65
+ expect(actual).to eq(expected)
66
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
67
+ # standard:enable Style/HashSyntax
68
+ end
69
+ end
70
+
71
+ describe "issue #65" do
72
+ def diff_lines(old_lines, new_lines)
73
+ file_length_difference = 0
74
+ previous_hunk = nil
75
+ output = []
76
+
77
+ Diff::LCS.diff(old_lines, new_lines).each do |piece|
78
+ hunk = Diff::LCS::Hunk.new(old_lines, new_lines, piece, 3, file_length_difference)
79
+ file_length_difference = hunk.file_length_difference
80
+ maybe_contiguous_hunks = previous_hunk.nil? || hunk.merge(previous_hunk)
81
+
82
+ output << "#{previous_hunk.diff(:unified)}\n" unless maybe_contiguous_hunks
83
+
84
+ previous_hunk = hunk
85
+ end
86
+ output << "#{previous_hunk.diff(:unified, true)}\n" unless previous_hunk.nil?
87
+ output.join
88
+ end
89
+
90
+ it "should not misplace the new chunk" do
91
+ old_data = [
92
+ "recipe[a::default]", "recipe[b::default]", "recipe[c::default]",
93
+ "recipe[d::default]", "recipe[e::default]", "recipe[f::default]",
94
+ "recipe[g::default]", "recipe[h::default]", "recipe[i::default]",
95
+ "recipe[j::default]", "recipe[k::default]", "recipe[l::default]",
96
+ "recipe[m::default]", "recipe[n::default]"
97
+ ]
98
+
99
+ new_data = [
100
+ "recipe[a::default]", "recipe[c::default]", "recipe[d::default]",
101
+ "recipe[e::default]", "recipe[f::default]", "recipe[g::default]",
102
+ "recipe[h::default]", "recipe[i::default]", "recipe[j::default]",
103
+ "recipe[k::default]", "recipe[l::default]", "recipe[m::default]",
104
+ "recipe[n::default]", "recipe[o::new]", "recipe[p::new]",
105
+ "recipe[q::new]", "recipe[r::new]"
106
+ ]
107
+
108
+ # standard:disable Layout/HeredocIndentation
109
+ expect(diff_lines(old_data, new_data)).to eq(<<-EODIFF)
110
+ @@ -1,5 +1,4 @@
111
+ recipe[a::default]
112
+ -recipe[b::default]
113
+ recipe[c::default]
114
+ recipe[d::default]
115
+ recipe[e::default]
116
+ @@ -12,3 +11,7 @@
117
+ recipe[l::default]
118
+ recipe[m::default]
119
+ recipe[n::default]
120
+ +recipe[o::new]
121
+ +recipe[p::new]
122
+ +recipe[q::new]
123
+ +recipe[r::new]
124
+ EODIFF
125
+ # standard:enable Layout/HeredocIndentation
126
+ end
127
+ end
128
+
129
+ describe "issue #107 (replaces issue #60)" do
130
+ it "should produce unified output with correct context" do
131
+ # standard:disable Layout/HeredocIndentation
132
+ old_data = <<-DATA_OLD.strip.split("\n").map(&:chomp)
133
+ {
134
+ "name": "x",
135
+ "description": "hi"
136
+ }
137
+ DATA_OLD
138
+
139
+ new_data = <<-DATA_NEW.strip.split("\n").map(&:chomp)
140
+ {
141
+ "name": "x",
142
+ "description": "lo"
143
+ }
144
+ DATA_NEW
145
+
146
+ diff = ::Diff::LCS.diff(old_data, new_data)
147
+ hunk = ::Diff::LCS::Hunk.new(old_data, new_data, diff.first, 3, 0)
148
+
149
+ expect(hunk.diff(:unified)).to eq(<<-EXPECTED.chomp)
150
+ @@ -1,4 +1,4 @@
151
+ {
152
+ "name": "x",
153
+ - "description": "hi"
154
+ + "description": "lo"
155
+ }
156
+ EXPECTED
157
+ # standard:enable Layout/HeredocIndentation
158
+ end
159
+ end
49
160
  end
data/spec/lcs_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
4
 
5
5
  describe Diff::LCS::Internals, ".lcs" do
6
6
  include Diff::LCS::SpecHelper::Matchers
@@ -47,7 +47,7 @@ describe Diff::LCS, ".LCS" do
47
47
  end
48
48
 
49
49
  it "returns %W(h e l l o) with (hello, hello)" do
50
- expect(Diff::LCS.LCS(hello, hello)).to eq(hello.split(//))
50
+ expect(Diff::LCS.LCS(hello, hello)).to eq(hello.chars)
51
51
  end
52
52
 
53
53
  it "returns hello_ary with (hello_ary, hello_ary)" do
data/spec/ldiff_spec.rb CHANGED
@@ -1,47 +1,100 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
4
 
5
- describe "Diff::LCS.diff" do
6
- include Diff::LCS::SpecHelper::Matchers
5
+ RSpec.describe "bin/ldiff" do
6
+ include CaptureSubprocessIO
7
7
 
8
- it 'correctly diffs seq1 to seq2' do
9
- diff_s1_s2 = Diff::LCS.diff(seq1, seq2)
10
- expect(change_diff(correct_forward_diff)).to eq(diff_s1_s2)
8
+ # standard:disable Style/HashSyntax
9
+ fixtures = [
10
+ {:name => "diff", :left => "aX", :right => "bXaX", :diff => 1},
11
+ {:name => "diff.missing_new_line1", :left => "four_lines", :right => "four_lines_with_missing_new_line", :diff => 1},
12
+ {:name => "diff.missing_new_line2", :left => "four_lines_with_missing_new_line", :right => "four_lines", :diff => 1},
13
+ {:name => "diff.issue95_trailing_context", :left => "123_x", :right => "456_x", :diff => 1},
14
+ {:name => "diff.four_lines.vs.empty", :left => "four_lines", :right => "empty", :diff => 1},
15
+ {:name => "diff.empty.vs.four_lines", :left => "empty", :right => "four_lines", :diff => 1},
16
+ {:name => "diff.bin1", :left => "file1.bin", :right => "file1.bin", :diff => 0},
17
+ {:name => "diff.bin2", :left => "file1.bin", :right => "file2.bin", :diff => 1},
18
+ {:name => "diff.chef", :left => "old-chef", :right => "new-chef", :diff => 1},
19
+ {:name => "diff.chef2", :left => "old-chef2", :right => "new-chef2", :diff => 1}
20
+ ].product([nil, "-e", "-f", "-c", "-u"]).map { |(fixture, flag)|
21
+ fixture = fixture.dup
22
+ fixture[:flag] = flag
23
+ fixture
24
+ }
25
+ # standard:enable Style/HashSyntax
26
+
27
+ def self.test_ldiff(fixture)
28
+ desc = [
29
+ fixture[:flag],
30
+ "spec/fixtures/#{fixture[:left]}",
31
+ "spec/fixtures/#{fixture[:right]}",
32
+ "#",
33
+ "=>",
34
+ "spec/fixtures/ldiff/output.#{fixture[:name]}#{fixture[:flag]}"
35
+ ].join(" ")
36
+
37
+ it desc do
38
+ stdout, stderr, status = run_ldiff(fixture)
39
+ expect(status).to eq(fixture[:diff])
40
+ expect(stderr).to eq(read_fixture(fixture, mode: "error", allow_missing: true))
41
+ expect(stdout).to eq(read_fixture(fixture, mode: "output", allow_missing: false))
42
+ end
11
43
  end
12
44
 
13
- it 'correctly diffs seq2 to seq1' do
14
- diff_s2_s1 = Diff::LCS.diff(seq2, seq1)
15
- expect(change_diff(correct_backward_diff)).to eq(diff_s2_s1)
45
+ fixtures.each do |fixture|
46
+ test_ldiff(fixture)
16
47
  end
17
48
 
18
- it 'correctly diffs against an empty sequence' do
19
- diff = Diff::LCS.diff(word_sequence, [])
20
- correct_diff = [
21
- [ [ '-', 0, 'abcd' ],
22
- [ '-', 1, 'efgh' ],
23
- [ '-', 2, 'ijkl' ],
24
- [ '-', 3, 'mnopqrstuvwxyz' ] ]
25
- ]
49
+ def read_fixture(options, mode: "output", allow_missing: false)
50
+ fixture = options.fetch(:name)
51
+ flag = options.fetch(:flag)
52
+ name = "spec/fixtures/ldiff/#{mode}.#{fixture}#{flag}"
26
53
 
27
- expect(change_diff(correct_diff)).to eq(diff)
54
+ return "" if !::File.exist?(name) && allow_missing
28
55
 
29
- diff = Diff::LCS.diff([], word_sequence)
30
- correct_diff.each { |hunk| hunk.each { |change| change[0] = '+' } }
31
- expect(change_diff(correct_diff)).to eq(diff)
56
+ data = IO.__send__(IO.respond_to?(:binread) ? :binread : :read, name)
57
+ clean_data(data, flag)
32
58
  end
33
59
 
34
- it "correctly diffs 'xx' and 'xaxb'" do
35
- left = 'xx'
36
- right = 'xaxb'
37
- expect(Diff::LCS.patch(left, Diff::LCS.diff(left, right))).to eq(right)
60
+ def clean_data(data, flag)
61
+ data =
62
+ case flag
63
+ when "-c", "-u"
64
+ clean_output_timestamp(data)
65
+ else
66
+ data
67
+ end
68
+ data.gsub(/\r\n?/, "\n")
38
69
  end
39
70
 
40
- it "returns an empty diff with (hello, hello)" do
41
- expect(Diff::LCS.diff(hello, hello)).to eq([])
71
+ def clean_output_timestamp(data)
72
+ data.gsub(
73
+ %r{
74
+ ^
75
+ [-+*]{3}
76
+ \s*
77
+ spec/fixtures/(\S+)
78
+ \s*
79
+ \d{4}-\d\d-\d\d
80
+ \s*
81
+ \d\d:\d\d:\d\d(?:\.\d+)
82
+ \s*
83
+ (?:[-+]\d{4}|Z)
84
+ }x,
85
+ '*** spec/fixtures/\1 0000-00-00 :00 =>:00 =>00.000000000 -0000'
86
+ )
42
87
  end
43
88
 
44
- it "returns an empty diff with (hello_ary, hello_ary)" do
45
- expect(Diff::LCS.diff(hello_ary, hello_ary)).to eq([])
89
+ def run_ldiff(options)
90
+ flag = options.fetch(:flag)
91
+ left = options.fetch(:left)
92
+ right = options.fetch(:right)
93
+
94
+ stdout, stderr = capture_subprocess_io do
95
+ system("ruby -Ilib bin/ldiff #{flag} spec/fixtures/#{left} spec/fixtures/#{right}")
96
+ end
97
+
98
+ [clean_data(stdout, flag), stderr, $?.exitstatus]
46
99
  end
47
100
  end
data/spec/patch_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'spec_helper'
3
+ require "spec_helper"
4
4
 
5
5
  describe "Diff::LCS.patch" do
6
6
  include Diff::LCS::SpecHelper::Matchers
@@ -29,12 +29,12 @@ describe "Diff::LCS.patch" do
29
29
  describe "an empty patchset returns the source" do
30
30
  it "works on a string (hello)" do
31
31
  diff = Diff::LCS.diff(hello, hello)
32
- expect(Diff::LCS::patch(hello, diff)).to eq(hello)
32
+ expect(Diff::LCS.patch(hello, diff)).to eq(hello)
33
33
  end
34
34
 
35
35
  it "works on an array %W(h e l l o)" do
36
36
  diff = Diff::LCS.diff(hello_ary, hello_ary)
37
- expect(Diff::LCS::patch(hello_ary, diff)).to eq(hello_ary)
37
+ expect(Diff::LCS.patch(hello_ary, diff)).to eq(hello_ary)
38
38
  end
39
39
  end
40
40
 
@@ -104,11 +104,11 @@ describe "Diff::LCS.patch" do
104
104
  describe "using a Diff::LCS.sdiff patchset" do
105
105
  describe "an empty patchset returns the source" do
106
106
  it "works on a string (hello)" do
107
- expect(Diff::LCS::patch(hello, Diff::LCS.sdiff(hello, hello))).to eq(hello)
107
+ expect(Diff::LCS.patch(hello, Diff::LCS.sdiff(hello, hello))).to eq(hello)
108
108
  end
109
109
 
110
110
  it "works on an array %W(h e l l o)" do
111
- expect(Diff::LCS::patch(hello_ary, Diff::LCS.sdiff(hello_ary, hello_ary))).to eq(hello_ary)
111
+ expect(Diff::LCS.patch(hello_ary, Diff::LCS.sdiff(hello_ary, hello_ary))).to eq(hello_ary)
112
112
  end
113
113
  end
114
114
 
@@ -181,8 +181,8 @@ describe "Diff::LCS.patch" do
181
181
  # above.
182
182
  describe "fix bug 891: patchsets do not contain the last equal part" do
183
183
  before :each do
184
- @s1 = %w(a b c d e f g h i j k)
185
- @s2 = %w(a b c d D e f g h i j k)
184
+ @s1 = %w[a b c d e f g h i j k] # standard:disable Layout/SpaceInsideArrayPercentLiteral
185
+ @s2 = %w[a b c d D e f g h i j k]
186
186
  end
187
187
 
188
188
  describe "using Diff::LCS.diff with default diff callbacks" do
@@ -225,10 +225,8 @@ describe "Diff::LCS.patch" do
225
225
 
226
226
  describe "using Diff::LCS.diff with context diff callbacks" do
227
227
  before :each do
228
- @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2,
229
- Diff::LCS::ContextDiffCallbacks)
230
- @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1,
231
- Diff::LCS::ContextDiffCallbacks)
228
+ @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2, Diff::LCS::ContextDiffCallbacks)
229
+ @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1, Diff::LCS::ContextDiffCallbacks)
232
230
  end
233
231
 
234
232
  it "autodiscovers s1 to s2 patches" do
@@ -265,10 +263,8 @@ describe "Diff::LCS.patch" do
265
263
 
266
264
  describe "using Diff::LCS.diff with sdiff callbacks" do
267
265
  before(:each) do
268
- @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2,
269
- Diff::LCS::SDiffCallbacks)
270
- @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1,
271
- Diff::LCS::SDiffCallbacks)
266
+ @patch_set_s1_s2 = Diff::LCS.diff(@s1, @s2, Diff::LCS::SDiffCallbacks)
267
+ @patch_set_s2_s1 = Diff::LCS.diff(@s2, @s1, Diff::LCS::SDiffCallbacks)
272
268
  end
273
269
 
274
270
  it "autodiscovers s1 to s2 patches" do
@@ -343,10 +339,8 @@ describe "Diff::LCS.patch" do
343
339
 
344
340
  describe "using Diff::LCS.sdiff with context diff callbacks" do
345
341
  before(:each) do
346
- @patch_set_s1_s2 = Diff::LCS.sdiff(@s1, @s2,
347
- Diff::LCS::ContextDiffCallbacks)
348
- @patch_set_s2_s1 = Diff::LCS.sdiff(@s2, @s1,
349
- Diff::LCS::ContextDiffCallbacks)
342
+ @patch_set_s1_s2 = Diff::LCS.sdiff(@s1, @s2, Diff::LCS::ContextDiffCallbacks)
343
+ @patch_set_s2_s1 = Diff::LCS.sdiff(@s2, @s1, Diff::LCS::ContextDiffCallbacks)
350
344
  end
351
345
 
352
346
  it "autodiscovers s1 to s2 patches" do