diff options
author | Austin Ziegler <austin@zieglers.ca> | 2019-02-02 22:52:28 -0500 |
---|---|---|
committer | Austin Ziegler <austin@zieglers.ca> | 2019-02-04 10:33:18 -0500 |
commit | 15169228be42559f98fa6729d5f6bb32edad44e3 (patch) | |
tree | 0fe1626255811fae6b3c85ce406f0dc02c4987a2 /spec | |
parent | 3a89de07745fea52f611e6955f61c11ffd68c754 (diff) | |
download | diff-lcs-15169228be42559f98fa6729d5f6bb32edad44e3.tar.gz |
Resolve multiple issues for 1.4
- Resolve ldiff output issues: Resolves #5 and #6 by adding system-output
comparison calls to `bin/ldiff` compared against some pre-generated output.
There is some timestamp manipulation involved with the output comparison,
as the timestamps are unstable because of the way that git clone works.
- Resolved a problem with bin/ldiff --context output.
- Resolved a Numeric/Integer OptParse issue: later versions of Ruby had
problems working with an `OptParse` option specification of `Numeric`; this
has been changed to `Integer`.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/fixtures/aX | 1 | ||||
-rw-r--r-- | spec/fixtures/bXaX | 1 | ||||
-rw-r--r-- | spec/fixtures/ldiff/output.diff | 4 | ||||
-rw-r--r-- | spec/fixtures/ldiff/output.diff-c | 7 | ||||
-rw-r--r-- | spec/fixtures/ldiff/output.diff-e | 3 | ||||
-rw-r--r-- | spec/fixtures/ldiff/output.diff-f | 3 | ||||
-rw-r--r-- | spec/fixtures/ldiff/output.diff-u | 5 | ||||
-rw-r--r-- | spec/hunk_spec.rb | 14 | ||||
-rw-r--r-- | spec/ldiff_spec.rb | 85 | ||||
-rw-r--r-- | spec/spec_helper.rb | 32 |
10 files changed, 116 insertions, 39 deletions
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 @@ if String.method_defined?(:encoding) 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 @@ if String.method_defined?(:encoding) 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 @@ if String.method_defined?(:encoding) 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 @@ if String.method_defined?(:encoding) 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..6dfea29 100644 --- a/spec/ldiff_spec.rb +++ b/spec/ldiff_spec.rb @@ -2,50 +2,71 @@ require 'spec_helper' -describe 'Diff::LCS.diff' do - include Diff::LCS::SpecHelper::Matchers +RSpec.describe 'bin/ldiff' do + include CaptureSubprocessIO - 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) + let(:output_diff) { read_fixture } + let(:output_diff_c) { read_fixture('-c') } + let(:output_diff_e) { read_fixture('-e') } + let(:output_diff_f) { read_fixture('-f') } + let(:output_diff_u) { read_fixture('-u') } + + specify do + expect(run_ldiff).to eq(output_diff) 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) + specify do + expect(run_ldiff('-c')).to eq(output_diff_c) 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'] - ] - ] + specify do + expect(run_ldiff('-e')).to eq(output_diff_e) + end - expect(change_diff(correct_diff)).to eq(diff) + specify do + expect(run_ldiff('-f')).to eq(output_diff_f) + end - 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(run_ldiff('-u')).to eq(output_diff_u) 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) + def read_fixture(flag = nil) + clean_data(IO.binread("spec/fixtures/ldiff/output.diff#{flag}"), flag) end - it 'returns an empty diff with (hello, hello)' do - expect(Diff::LCS.diff(hello, hello)).to eq([]) + def clean_data(data, flag) + case flag + when '-c', '-u' + clean_output_timestamp(data) + else + data + end + end + + def clean_output_timestamp(data) + data.gsub( + %r{ + [-*+]{3} + \s + spec/fixtures/(\w+) + \s + \d{4}-\d\d-\d\d + \s + \d\d:\d\d:\d\d(?:\.\d+) + \s + (?:[-+]\d{4}|Z) + }x, + '*** spec/fixtures/\1 0000-00-00 00:00:00.000000000 -0000' + ) end - it 'returns an empty diff with (hello_ary, hello_ary)' do - expect(Diff::LCS.diff(hello_ary, hello_ary)).to eq([]) + def run_ldiff(flag = nil, left: 'aX', right: 'bXaX') + stdout, stderr = capture_subprocess_io do + system("ruby -Ilib bin/ldiff #{flag} spec/fixtures/#{left} spec/fixtures/#{right}") + end + expect(stderr).to be_empty + expect(stdout).not_to be_empty + clean_data(stdout, flag) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4899c49..e566d0e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -40,6 +40,38 @@ parent = path.parent $:.unshift parent.join('lib') +module CaptureSubprocessIO + def _synchronize + yield + end + + def capture_subprocess_io + _synchronize do + begin + require 'tempfile' + + captured_stdout, captured_stderr = Tempfile.new('out'), Tempfile.new('err') + + orig_stdout, orig_stderr = $stdout.dup, $stderr.dup + $stdout.reopen captured_stdout + $stderr.reopen captured_stderr + + yield + + $stdout.rewind + $stderr.rewind + + return captured_stdout.read, captured_stderr.read + ensure + captured_stdout.unlink + captured_stderr.unlink + $stdout.reopen orig_stdout + $stderr.reopen orig_stderr + end + end + end +end + require 'diff-lcs' module Diff::LCS::SpecHelper |