diff-lcs 1.5.1 → 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 (81) hide show
  1. checksums.yaml +4 -4
  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} +15 -16
  7. data/Manifest.txt +61 -5
  8. data/README.md +92 -0
  9. data/Rakefile +48 -63
  10. data/SECURITY.md +41 -0
  11. data/docs/artistic.txt +1 -1
  12. data/lib/diff/lcs/backports.rb +4 -0
  13. data/lib/diff/lcs/change.rb +3 -3
  14. data/lib/diff/lcs/htmldiff.rb +5 -3
  15. data/lib/diff/lcs/hunk.rb +38 -22
  16. data/lib/diff/lcs/ldiff.rb +65 -49
  17. data/lib/diff/lcs/version.rb +7 -0
  18. data/lib/diff/lcs.rb +12 -11
  19. data/mise.toml +5 -0
  20. data/spec/fixtures/123_x +2 -0
  21. data/spec/fixtures/456_x +2 -0
  22. data/spec/fixtures/empty +0 -0
  23. data/spec/fixtures/file1.bin +0 -0
  24. data/spec/fixtures/file2.bin +0 -0
  25. data/spec/fixtures/four_lines +4 -0
  26. data/spec/fixtures/four_lines_with_missing_new_line +4 -0
  27. data/spec/fixtures/ldiff/diff.missing_new_line1-e +1 -0
  28. data/spec/fixtures/ldiff/diff.missing_new_line1-f +1 -0
  29. data/spec/fixtures/ldiff/diff.missing_new_line2-e +1 -0
  30. data/spec/fixtures/ldiff/diff.missing_new_line2-f +1 -0
  31. data/spec/fixtures/ldiff/error.diff.chef-e +2 -0
  32. data/spec/fixtures/ldiff/error.diff.chef-f +2 -0
  33. data/spec/fixtures/ldiff/error.diff.missing_new_line1-e +1 -0
  34. data/spec/fixtures/ldiff/error.diff.missing_new_line1-f +1 -0
  35. data/spec/fixtures/ldiff/error.diff.missing_new_line2-e +1 -0
  36. data/spec/fixtures/ldiff/error.diff.missing_new_line2-f +1 -0
  37. data/spec/fixtures/ldiff/output.diff.bin1 +0 -0
  38. data/spec/fixtures/ldiff/output.diff.bin1-c +0 -0
  39. data/spec/fixtures/ldiff/output.diff.bin1-e +0 -0
  40. data/spec/fixtures/ldiff/output.diff.bin1-f +0 -0
  41. data/spec/fixtures/ldiff/output.diff.bin1-u +0 -0
  42. data/spec/fixtures/ldiff/output.diff.bin2 +1 -0
  43. data/spec/fixtures/ldiff/output.diff.bin2-c +1 -0
  44. data/spec/fixtures/ldiff/output.diff.bin2-e +1 -0
  45. data/spec/fixtures/ldiff/output.diff.bin2-f +1 -0
  46. data/spec/fixtures/ldiff/output.diff.bin2-u +1 -0
  47. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines +5 -0
  48. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-c +9 -0
  49. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-e +6 -0
  50. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-f +6 -0
  51. data/spec/fixtures/ldiff/output.diff.empty.vs.four_lines-u +7 -0
  52. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty +5 -0
  53. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-c +9 -0
  54. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-e +1 -0
  55. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-f +1 -0
  56. data/spec/fixtures/ldiff/output.diff.four_lines.vs.empty-u +7 -0
  57. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context +4 -0
  58. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-c +9 -0
  59. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-e +3 -0
  60. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-f +3 -0
  61. data/spec/fixtures/ldiff/output.diff.issue95_trailing_context-u +6 -0
  62. data/spec/fixtures/ldiff/output.diff.missing_new_line1 +5 -0
  63. data/spec/fixtures/ldiff/output.diff.missing_new_line1-c +14 -0
  64. data/spec/fixtures/ldiff/output.diff.missing_new_line1-e +0 -0
  65. data/spec/fixtures/ldiff/output.diff.missing_new_line1-f +0 -0
  66. data/spec/fixtures/ldiff/output.diff.missing_new_line1-u +9 -0
  67. data/spec/fixtures/ldiff/output.diff.missing_new_line2 +5 -0
  68. data/spec/fixtures/ldiff/output.diff.missing_new_line2-c +14 -0
  69. data/spec/fixtures/ldiff/output.diff.missing_new_line2-e +0 -0
  70. data/spec/fixtures/ldiff/output.diff.missing_new_line2-f +0 -0
  71. data/spec/fixtures/ldiff/output.diff.missing_new_line2-u +9 -0
  72. data/spec/hunk_spec.rb +1 -1
  73. data/spec/issues_spec.rb +32 -32
  74. data/spec/ldiff_spec.rb +21 -10
  75. data/spec/patch_spec.rb +1 -1
  76. data/spec/spec_helper.rb +93 -93
  77. metadata +87 -65
  78. data/Code-of-Conduct.md +0 -74
  79. data/Contributing.md +0 -121
  80. data/History.md +0 -431
  81. data/README.rdoc +0 -84
@@ -0,0 +1,14 @@
1
+ *** spec/fixtures/four_lines 2025-01-31 12:17:43.926013315 +0100
2
+ --- spec/fixtures/four_lines_with_missing_new_line 2025-01-31 12:17:43.926013315 +0100
3
+ ***************
4
+ *** 1,4 ****
5
+ one
6
+ two
7
+ three
8
+ ! four
9
+ --- 1,4 ----
10
+ one
11
+ two
12
+ three
13
+ ! four
14
+
@@ -0,0 +1,9 @@
1
+ --- spec/fixtures/four_lines 2025-01-31 12:17:43.926013315 +0100
2
+ +++ spec/fixtures/four_lines_with_missing_new_line 2025-01-31 12:17:43.926013315 +0100
3
+ @@ -1,4 +1,4 @@
4
+ one
5
+ two
6
+ three
7
+ -four
8
+ +four
9
+
@@ -0,0 +1,5 @@
1
+ 4c4
2
+ < four
3
+
4
+ ---
5
+ > four
@@ -0,0 +1,14 @@
1
+ *** spec/fixtures/four_lines_with_missing_new_line 2025-01-31 12:17:43.926013315 +0100
2
+ --- spec/fixtures/four_lines 2025-01-31 12:17:43.926013315 +0100
3
+ ***************
4
+ *** 1,4 ****
5
+ one
6
+ two
7
+ three
8
+ ! four
9
+
10
+ --- 1,4 ----
11
+ one
12
+ two
13
+ three
14
+ ! four
@@ -0,0 +1,9 @@
1
+ --- spec/fixtures/four_lines_with_missing_new_line 2025-01-31 12:17:43.926013315 +0100
2
+ +++ spec/fixtures/four_lines 2025-01-31 12:17:43.926013315 +0100
3
+ @@ -1,4 +1,4 @@
4
+ one
5
+ two
6
+ three
7
+ -four
8
+
9
+ +four
data/spec/hunk_spec.rb CHANGED
@@ -72,7 +72,7 @@ if String.method_defined?(:encoding)
72
72
 
73
73
  it "produces a unified diff" do
74
74
  expected = <<-EXPECTED.gsub(/^\s+/, "").encode("UTF-16LE").chomp
75
- @@ -1 +1,2 @@
75
+ @@ -0,0 +1 @@
76
76
  +Tu a un carte avec {count} items
77
77
  EXPECTED
78
78
 
data/spec/issues_spec.rb CHANGED
@@ -68,38 +68,6 @@ describe "Diff::LCS Issues" do
68
68
  end
69
69
  end
70
70
 
71
- describe "issue #60" do
72
- it "should produce unified output with correct context" do
73
- # standard:disable Layout/HeredocIndentation
74
- old_data = <<-DATA_OLD.strip.split("\n").map(&:chomp)
75
- {
76
- "name": "x",
77
- "description": "hi"
78
- }
79
- DATA_OLD
80
-
81
- new_data = <<-DATA_NEW.strip.split("\n").map(&:chomp)
82
- {
83
- "name": "x",
84
- "description": "lo"
85
- }
86
- DATA_NEW
87
-
88
- diff = ::Diff::LCS.diff(old_data, new_data)
89
- hunk = ::Diff::LCS::Hunk.new(old_data, new_data, diff.first, 3, 0)
90
-
91
- expect(hunk.diff(:unified)).to eq(<<-EXPECTED.chomp)
92
- @@ -1,5 +1,5 @@
93
- {
94
- "name": "x",
95
- - "description": "hi"
96
- + "description": "lo"
97
- }
98
- EXPECTED
99
- # standard:enable Layout/HeredocIndentation
100
- end
101
- end
102
-
103
71
  describe "issue #65" do
104
72
  def diff_lines(old_lines, new_lines)
105
73
  file_length_difference = 0
@@ -157,4 +125,36 @@ describe "Diff::LCS Issues" do
157
125
  # standard:enable Layout/HeredocIndentation
158
126
  end
159
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
160
160
  end
data/spec/ldiff_spec.rb CHANGED
@@ -7,9 +7,16 @@ RSpec.describe "bin/ldiff" do
7
7
 
8
8
  # standard:disable Style/HashSyntax
9
9
  fixtures = [
10
- {:name => "output.diff", :left => "aX", :right => "bXaX"},
11
- {:name => "output.diff.chef", :left => "old-chef", :right => "new-chef"},
12
- {:name => "output.diff.chef2", :left => "old-chef2", :right => "new-chef2"}
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}
13
20
  ].product([nil, "-e", "-f", "-c", "-u"]).map { |(fixture, flag)|
14
21
  fixture = fixture.dup
15
22
  fixture[:flag] = flag
@@ -24,11 +31,14 @@ RSpec.describe "bin/ldiff" do
24
31
  "spec/fixtures/#{fixture[:right]}",
25
32
  "#",
26
33
  "=>",
27
- "spec/fixtures/ldiff/#{fixture[:name]}#{fixture[:flag]}"
34
+ "spec/fixtures/ldiff/output.#{fixture[:name]}#{fixture[:flag]}"
28
35
  ].join(" ")
29
36
 
30
37
  it desc do
31
- expect(run_ldiff(fixture)).to eq(read_fixture(fixture))
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))
32
42
  end
33
43
  end
34
44
 
@@ -36,10 +46,13 @@ RSpec.describe "bin/ldiff" do
36
46
  test_ldiff(fixture)
37
47
  end
38
48
 
39
- def read_fixture(options)
49
+ def read_fixture(options, mode: "output", allow_missing: false)
40
50
  fixture = options.fetch(:name)
41
51
  flag = options.fetch(:flag)
42
- name = "spec/fixtures/ldiff/#{fixture}#{flag}"
52
+ name = "spec/fixtures/ldiff/#{mode}.#{fixture}#{flag}"
53
+
54
+ return "" if !::File.exist?(name) && allow_missing
55
+
43
56
  data = IO.__send__(IO.respond_to?(:binread) ? :binread : :read, name)
44
57
  clean_data(data, flag)
45
58
  end
@@ -82,8 +95,6 @@ RSpec.describe "bin/ldiff" do
82
95
  system("ruby -Ilib bin/ldiff #{flag} spec/fixtures/#{left} spec/fixtures/#{right}")
83
96
  end
84
97
 
85
- expect(stderr).to be_empty if RUBY_VERSION >= "1.9"
86
- expect(stdout).not_to be_empty
87
- clean_data(stdout, flag)
98
+ [clean_data(stdout, flag), stderr, $?.exitstatus]
88
99
  end
89
100
  end
data/spec/patch_spec.rb CHANGED
@@ -181,7 +181,7 @@ 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] # rubocop:disable Layout/SpaceInsideArrayPercentLiteral
184
+ @s1 = %w[a b c d e f g h i j k] # standard:disable Layout/SpaceInsideArrayPercentLiteral
185
185
  @s2 = %w[a b c d D e f g h i j k]
186
186
  end
187
187
 
data/spec/spec_helper.rb CHANGED
@@ -7,28 +7,21 @@ require "psych" if RUBY_VERSION >= "1.9"
7
7
 
8
8
  if ENV["COVERAGE"]
9
9
  require "simplecov"
10
+ require "simplecov-lcov"
10
11
 
11
- def require_do(resource)
12
- require resource
13
- yield if block_given?
14
- rescue LoadError
15
- nil
12
+ SimpleCov::Formatter::LcovFormatter.config do |config|
13
+ config.report_with_single_file = true
14
+ config.lcov_file_name = "lcov.info"
16
15
  end
17
16
 
18
- formatters = [SimpleCov::Formatter::HTMLFormatter]
19
-
20
- require_do("simplecov-rcov") {
21
- formatters << SimpleCov::Formatter::RcovFormatter
22
- }
23
- require_do("simplecov-vim/formatter") {
24
- formatters << SimpleCov::Formatter::VimFormatter
25
- }
26
- require_do("simplecov-sublime-ruby-coverage") {
27
- formatters << SimpleCov::Formatter::SublimeRubyCoverageFormatter
28
- }
29
-
30
- SimpleCov.start do
31
- formatter SimpleCov::Formatter::MultiFormatter.new(formatters)
17
+ SimpleCov.start "test_frameworks" do
18
+ enable_coverage :branch
19
+ primary_coverage :branch
20
+ formatter SimpleCov::Formatter::MultiFormatter.new([
21
+ SimpleCov::Formatter::HTMLFormatter,
22
+ SimpleCov::Formatter::LcovFormatter,
23
+ SimpleCov::Formatter::SimpleFormatter
24
+ ])
32
25
  end
33
26
  end
34
27
 
@@ -250,100 +243,107 @@ module Diff::LCS::SpecHelper
250
243
  new_result
251
244
  end
252
245
 
253
- def simple_callback
254
- callbacks = Object.new
255
- class << callbacks
256
- attr_reader :matched_a
257
- attr_reader :matched_b
258
- attr_reader :discards_a
259
- attr_reader :discards_b
260
- attr_reader :done_a
261
- attr_reader :done_b
262
-
263
- def reset
264
- @matched_a = []
265
- @matched_b = []
266
- @discards_a = []
267
- @discards_b = []
268
- @done_a = []
269
- @done_b = []
270
- end
246
+ class SimpleCallback
247
+ def initialize
248
+ reset
249
+ end
271
250
 
272
- def match(event)
273
- @matched_a << event.old_element
274
- @matched_b << event.new_element
275
- end
251
+ attr_reader :matched_a
252
+ attr_reader :matched_b
253
+ attr_reader :discards_a
254
+ attr_reader :discards_b
255
+ attr_reader :done_a
256
+ attr_reader :done_b
257
+
258
+ def reset
259
+ @matched_a = []
260
+ @matched_b = []
261
+ @discards_a = []
262
+ @discards_b = []
263
+ @done_a = []
264
+ @done_b = []
265
+ self
266
+ end
276
267
 
277
- def discard_b(event)
278
- @discards_b << event.new_element
279
- end
268
+ def match(event)
269
+ @matched_a << event.old_element
270
+ @matched_b << event.new_element
271
+ end
280
272
 
281
- def discard_a(event)
282
- @discards_a << event.old_element
283
- end
273
+ def discard_b(event)
274
+ @discards_b << event.new_element
275
+ end
284
276
 
285
- def finished_a(event)
286
- @done_a << [
287
- event.old_element, event.old_position,
288
- event.new_element, event.new_position
289
- ]
290
- end
277
+ def discard_a(event)
278
+ @discards_a << event.old_element
279
+ end
291
280
 
292
- def finished_b(event)
293
- @done_b << [
294
- event.old_element, event.old_position,
295
- event.new_element, event.new_position
296
- ]
297
- end
281
+ def finished_a(event)
282
+ @done_a << [
283
+ event.old_element, event.old_position,
284
+ event.new_element, event.new_position
285
+ ]
286
+ end
287
+
288
+ def finished_b(event)
289
+ @done_b << [
290
+ event.old_element, event.old_position,
291
+ event.new_element, event.new_position
292
+ ]
298
293
  end
299
- callbacks.reset
300
- callbacks
294
+ end
295
+
296
+ def simple_callback
297
+ SimpleCallback.new
298
+ end
299
+
300
+ class SimpleCallbackNoFinishers < SimpleCallback
301
+ undef :finished_a
302
+ undef :finished_b
301
303
  end
302
304
 
303
305
  def simple_callback_no_finishers
304
- simple = simple_callback
305
- class << simple
306
- undef :finished_a
307
- undef :finished_b
308
- end
309
- simple
306
+ SimpleCallbackNoFinishers.new
310
307
  end
311
308
 
312
- def balanced_callback
313
- cb = Object.new
314
- class << cb
315
- attr_reader :result
309
+ class BalancedCallback
310
+ def initialize
311
+ reset
312
+ end
316
313
 
317
- def reset
318
- @result = []
319
- end
314
+ attr_reader :result
320
315
 
321
- def match(event)
322
- @result << ["=", event.old_position, event.new_position]
323
- end
316
+ def reset
317
+ @result = []
318
+ end
324
319
 
325
- def discard_a(event)
326
- @result << ["<", event.old_position, event.new_position]
327
- end
320
+ def match(event)
321
+ @result << ["=", event.old_position, event.new_position]
322
+ end
328
323
 
329
- def discard_b(event)
330
- @result << [">", event.old_position, event.new_position]
331
- end
324
+ def discard_a(event)
325
+ @result << ["<", event.old_position, event.new_position]
326
+ end
332
327
 
333
- def change(event)
334
- @result << ["!", event.old_position, event.new_position]
335
- end
328
+ def discard_b(event)
329
+ @result << [">", event.old_position, event.new_position]
336
330
  end
337
- cb.reset
338
- cb
331
+
332
+ def change(event)
333
+ @result << ["!", event.old_position, event.new_position]
334
+ end
335
+ end
336
+
337
+ def balanced_callback
338
+ BalancedCallback.new
339
+ end
340
+
341
+ class BalancedCallbackNoChange < BalancedCallback
342
+ undef :change
339
343
  end
340
344
 
341
345
  def balanced_callback_no_change
342
- balanced = balanced_callback
343
- class << balanced
344
- undef :change
345
- end
346
- balanced
346
+ BalancedCallbackNoChange.new
347
347
  end
348
348
 
349
349
  module Matchers