From 657bacd47268487e93e17e16897281820447fe64 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Sat, 2 Feb 2019 22:52:28 -0500 Subject: [PATCH] Resolve ldiff output issues Resolves #5 and #6 by adding system-output comparison calls to `bin/ldiff` compared against some pre-generated output. --- History.md | 5 +++ lib/diff/lcs/hunk.rb | 3 ++ lib/diff/lcs/ldiff.rb | 9 +++-- spec/fixtures/aX | 1 + spec/fixtures/bXaX | 1 + spec/fixtures/ldiff/output.diff | 4 ++ spec/fixtures/ldiff/output.diff-c | 7 ++++ spec/fixtures/ldiff/output.diff-e | 3 ++ spec/fixtures/ldiff/output.diff-f | 3 ++ spec/fixtures/ldiff/output.diff-u | 5 +++ spec/hunk_spec.rb | 14 +++---- spec/ldiff_spec.rb | 64 +++++++++++++------------------ 12 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 spec/fixtures/aX create mode 100644 spec/fixtures/bXaX create mode 100644 spec/fixtures/ldiff/output.diff create mode 100644 spec/fixtures/ldiff/output.diff-c create mode 100644 spec/fixtures/ldiff/output.diff-e create mode 100644 spec/fixtures/ldiff/output.diff-f create mode 100644 spec/fixtures/ldiff/output.diff-u diff --git a/History.md b/History.md index 11afd6d..ccfa9f5 100644 --- a/History.md +++ b/History.md @@ -6,6 +6,9 @@ implicitly treated arrays. Originally provided as pull request [#47][], but it introduced a number of test failures as documented in [#48][], and remediation of Diff::LCS itself was introduced in [#49][]. +* Resolved [#5][] with some tests comparing output from `system` calls to + `bin/ldiff` with some pre-generated output. Resolved [#6][] with these + tests. ## 1.3 / 2017-01-18 @@ -212,6 +215,8 @@ [#2]: https://github.com/halostatue/diff-lcs/issues/2 [#3]: https://github.com/halostatue/diff-lcs/issues/3 [#4]: https://github.com/halostatue/diff-lcs/issues/4 +[#5]: https://github.com/halostatue/diff-lcs/issues/5 +[#6]: https://github.com/halostatue/diff-lcs/issues/6 [#8]: https://github.com/halostatue/diff-lcs/pull/8 [#9]: https://github.com/halostatue/diff-lcs/pull/9 [#10]: https://github.com/halostatue/diff-lcs/pull/10 diff --git a/lib/diff/lcs/hunk.rb b/lib/diff/lcs/hunk.rb index 58a68db..d0164a5 100644 --- a/lib/diff/lcs/hunk.rb +++ b/lib/diff/lcs/hunk.rb @@ -20,6 +20,7 @@ def initialize(data_old, data_new, piece, flag_context, file_length_difference) before = after = file_length_difference after += @blocks[0].diff_size @file_length_difference = after # The caller must get this manually + @max_diff_size = @blocks.lazy.map { |e| e.diff_size }.max # Save the start & end of each array. If the array doesn't exist (e.g., # we're only adding items in this block), then figure out the line @@ -70,6 +71,8 @@ def flag_context=(context) #:nodoc: # rubocop:disable Lint/DuplicateMethods context end + add_end = @max_diff_size if add_end > @max_diff_size + @end_old += add_end @end_new += add_end end diff --git a/lib/diff/lcs/ldiff.rb b/lib/diff/lcs/ldiff.rb index d385f72..5899aec 100644 --- a/lib/diff/lcs/ldiff.rb +++ b/lib/diff/lcs/ldiff.rb @@ -134,9 +134,9 @@ def run(args, _input = $stdin, output = $stdout, error = $stderr) #:nodoc: end if (@format == :unified) or (@format == :context) - ft = File.stat(file_old).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z') + ft = File.stat(file_old).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.000000000 %z') output << "#{char_old} #{file_old}\t#{ft}\n" - ft = File.stat(file_new).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.%N %z') + ft = File.stat(file_new).mtime.localtime.strftime('%Y-%m-%d %H:%M:%S.000000000 %z') output << "#{char_new} #{file_new}\t#{ft}\n" end @@ -163,7 +163,10 @@ def run(args, _input = $stdin, output = $stdout, error = $stderr) #:nodoc: end end - output << oldhunk.diff(@format) << "\n" + last = oldhunk.diff(@format) + last << "\n" if last.respond_to?(:end_with?) && !last.end_with?("\n") + + output << last output.reverse_each { |e| real_output << e.diff(:ed_finish) } if @format == :ed diff --git a/spec/fixtures/aX b/spec/fixtures/aX new file mode 100644 index 0000000..5765d6a --- /dev/null +++ b/spec/fixtures/aX @@ -0,0 +1 @@ +aX diff --git a/spec/fixtures/bXaX b/spec/fixtures/bXaX new file mode 100644 index 0000000..a1c813d --- /dev/null +++ b/spec/fixtures/bXaX @@ -0,0 +1 @@ +bXaX diff --git a/spec/fixtures/ldiff/output.diff b/spec/fixtures/ldiff/output.diff new file mode 100644 index 0000000..fa1a347 --- /dev/null +++ b/spec/fixtures/ldiff/output.diff @@ -0,0 +1,4 @@ +1c1 +< aX +--- +> bXaX diff --git a/spec/fixtures/ldiff/output.diff-c b/spec/fixtures/ldiff/output.diff-c new file mode 100644 index 0000000..92ea5a2 --- /dev/null +++ b/spec/fixtures/ldiff/output.diff-c @@ -0,0 +1,7 @@ +*** spec/fixtures/aX 2019-02-01 22:29:34.000000000 -0500 +--- spec/fixtures/bXaX 2019-02-01 22:29:43.000000000 -0500 +*************** +*** 1 **** +! aX +--- 1 ---- +! bXaX diff --git a/spec/fixtures/ldiff/output.diff-e b/spec/fixtures/ldiff/output.diff-e new file mode 100644 index 0000000..13e0f7f --- /dev/null +++ b/spec/fixtures/ldiff/output.diff-e @@ -0,0 +1,3 @@ +1c +bXaX +. diff --git a/spec/fixtures/ldiff/output.diff-f b/spec/fixtures/ldiff/output.diff-f new file mode 100644 index 0000000..77710c7 --- /dev/null +++ b/spec/fixtures/ldiff/output.diff-f @@ -0,0 +1,3 @@ +c1 +bXaX +. diff --git a/spec/fixtures/ldiff/output.diff-u b/spec/fixtures/ldiff/output.diff-u new file mode 100644 index 0000000..c4ba30f --- /dev/null +++ b/spec/fixtures/ldiff/output.diff-u @@ -0,0 +1,5 @@ +--- spec/fixtures/aX 2019-02-01 22:29:34.000000000 -0500 ++++ spec/fixtures/bXaX 2019-02-01 22:29:43.000000000 -0500 +@@ -1 +1 @@ +-aX ++bXaX diff --git a/spec/hunk_spec.rb b/spec/hunk_spec.rb index 5a45072..277f739 100644 --- a/spec/hunk_spec.rb +++ b/spec/hunk_spec.rb @@ -13,7 +13,7 @@ it 'produces a unified diff from the two pieces' do expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp - @@ -1,2 +1,2 @@ + @@ -1 +1 @@ -Tu avec carté {count} itém has +Tu avec carte {count} item has EXPECTED @@ -24,10 +24,10 @@ it 'produces a context diff from the two pieces' do expected = <<-EXPECTED.gsub(/^\s+/, '').encode('UTF-16LE').chomp *************** - *** 1,2 **** - !Tu avec carté {count} itém has - --- 1,2 ---- - !Tu avec carte {count} item has + *** 1 **** + ! Tu avec carté {count} itém has + --- 1 ---- + ! Tu avec carte {count} item has EXPECTED expect(hunk.diff(:context)).to eq(expected) @@ -35,7 +35,7 @@ it 'produces an old diff from the two pieces' do expected = <<-EXPECTED.gsub(/^ +/, '').encode('UTF-16LE').chomp - 1,2c1,2 + 1c1 < Tu avec carté {count} itém has --- > Tu avec carte {count} item has @@ -47,7 +47,7 @@ it 'produces a reverse ed diff from the two pieces' do expected = <<-EXPECTED.gsub(/^ +/, '').encode('UTF-16LE').chomp - c1,2 + c1 Tu avec carte {count} item has . diff --git a/spec/ldiff_spec.rb b/spec/ldiff_spec.rb index 113c0dd..b91497b 100644 --- a/spec/ldiff_spec.rb +++ b/spec/ldiff_spec.rb @@ -2,50 +2,40 @@ require 'spec_helper' -describe 'Diff::LCS.diff' do - include Diff::LCS::SpecHelper::Matchers - - it 'correctly diffs seq1 to seq2' do - diff_s1_s2 = Diff::LCS.diff(seq1, seq2) - expect(change_diff(correct_forward_diff)).to eq(diff_s1_s2) - end - - it 'correctly diffs seq2 to seq1' do - diff_s2_s1 = Diff::LCS.diff(seq2, seq1) - expect(change_diff(correct_backward_diff)).to eq(diff_s2_s1) +RSpec.describe 'bin/ldiff' do + let(:output_diff) { IO.binread('spec/fixtures/ldiff/output.diff') } + let(:output_diff_c) { IO.binread('spec/fixtures/ldiff/output.diff-c') } + let(:output_diff_e) { IO.binread('spec/fixtures/ldiff/output.diff-e') } + let(:output_diff_f) { IO.binread('spec/fixtures/ldiff/output.diff-f') } + let(:output_diff_u) { IO.binread('spec/fixtures/ldiff/output.diff-u') } + + specify do + expect { + system 'ruby -Ilib bin/ldiff spec/fixtures/aX spec/fixtures/bXaX' + }.to output(output_diff).to_stdout_from_any_process end - it 'correctly diffs against an empty sequence' do - diff = Diff::LCS.diff(word_sequence, []) - correct_diff = [ - [ - ['-', 0, 'abcd'], - ['-', 1, 'efgh'], - ['-', 2, 'ijkl'], - ['-', 3, 'mnopqrstuvwxyz'] - ] - ] - - expect(change_diff(correct_diff)).to eq(diff) - - diff = Diff::LCS.diff([], word_sequence) - correct_diff.each do |hunk| - hunk.each do |change| change[0] = '+' end - end - expect(change_diff(correct_diff)).to eq(diff) + specify do + expect { + system 'ruby -Ilib bin/ldiff -c spec/fixtures/aX spec/fixtures/bXaX' + }.to output(output_diff_c).to_stdout_from_any_process end - it "correctly diffs 'xx' and 'xaxb'" do - left = 'xx' - right = 'xaxb' - expect(Diff::LCS.patch(left, Diff::LCS.diff(left, right))).to eq(right) + specify do + expect { + system 'ruby -Ilib bin/ldiff -e spec/fixtures/aX spec/fixtures/bXaX' + }.to output(output_diff_e).to_stdout_from_any_process end - it 'returns an empty diff with (hello, hello)' do - expect(Diff::LCS.diff(hello, hello)).to eq([]) + specify do + expect { + system 'ruby -Ilib bin/ldiff -f spec/fixtures/aX spec/fixtures/bXaX' + }.to output(output_diff_f).to_stdout_from_any_process end - it 'returns an empty diff with (hello_ary, hello_ary)' do - expect(Diff::LCS.diff(hello_ary, hello_ary)).to eq([]) + specify do + expect { + system 'ruby -Ilib bin/ldiff -u spec/fixtures/aX spec/fixtures/bXaX' + }.to output(output_diff_u).to_stdout_from_any_process end end