diff --git a/History.md b/History.md index 81a02d2..576409e 100644 --- a/History.md +++ b/History.md @@ -1,5 +1,16 @@ # History +## 1.4.2 / 2020-06-23 + +- Camille Drapier fixed a small issue with RuboCop configuration. [#59][] +- Applied another fix (and unit test) to fix an issue for the Chef team. + [#60][], [#61][] + +## 1.4.1 / 2020-06-23 + +- Fix an issue where diff sizes could be negative, and they should be. [#57][], + [#58][] + ## 1.4 / 2020-06-23 - Ruby versions lower than 2.4 are soft-deprecated and will not be run as @@ -245,3 +256,8 @@ [#49]: https://github.com/halostatue/diff-lcs/pull/49 [#52]: https://github.com/halostatue/diff-lcs/pull/52 [#53]: https://github.com/halostatue/diff-lcs/issues/53 +[#57]: https://github.com/halostatue/diff-lcs/issues/57 +[#58]: https://github.com/halostatue/diff-lcs/pull/58 +[#59]: https://github.com/halostatue/diff-lcs/pull/59 +[#60]: https://github.com/halostatue/diff-lcs/issues/60 +[#61]: https://github.com/halostatue/diff-lcs/pull/61 diff --git a/diff-lcs.gemspec b/diff-lcs.gemspec index 1b9c386..f3a59c6 100644 --- a/diff-lcs.gemspec +++ b/diff-lcs.gemspec @@ -1,15 +1,15 @@ # -*- encoding: utf-8 -*- -# stub: diff-lcs 1.4.1 ruby lib +# stub: diff-lcs 1.4.2 ruby lib Gem::Specification.new do |s| s.name = "diff-lcs".freeze - s.version = "1.4.1" + s.version = "1.4.2" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "bug_tracker_uri" => "https://github.com/halostatue/diff-lcs/issues", "homepage_uri" => "https://github.com/halostatue/diff-lcs", "source_code_uri" => "https://github.com/halostatue/diff-lcs" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] s.authors = ["Austin Ziegler".freeze] - s.date = "2020-06-23" + s.date = "2020-06-24" s.description = "Diff::LCS computes the difference between two Enumerable sequences using the\nMcIlroy-Hunt longest common subsequence (LCS) algorithm. It includes utilities\nto create a simple HTML diff output format and a standard diff-like tool.\n\nThis is release 1.4, providing a simple extension that allows for\nDiff::LCS::Change objects to be treated implicitly as arrays. Ruby versions\nbelow 2.5 are soft-deprecated.\n\nThis means that older versions are no longer part of the CI test suite. If any\nchanges have been introduced that break those versions, bug reports and patches\nwill be accepted, but it will be up to the reporter to verify any fixes prior\nto release. A future release will completely break compatibility.".freeze s.email = ["halostatue@gmail.com".freeze] s.executables = ["htmldiff".freeze, "ldiff".freeze] diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb index b6d0a86..1fce946 100644 --- a/lib/diff/lcs.rb +++ b/lib/diff/lcs.rb @@ -49,7 +49,7 @@ module Diff; end unless defined? Diff # rubocop:disable Style/Documentation # a x b y c z p d q # a b c a x b y c z module Diff::LCS - VERSION = '1.4.1' + VERSION = '1.4.2' end require 'diff/lcs/callbacks' diff --git a/lib/diff/lcs/hunk.rb b/lib/diff/lcs/hunk.rb index 4ec5123..c6b3b25 100644 --- a/lib/diff/lcs/hunk.rb +++ b/lib/diff/lcs/hunk.rb @@ -2,12 +2,12 @@ require 'diff/lcs/block' -# A Hunk is a group of Blocks which overlap because of the context -# surrounding each block. (So if we're not using context, every hunk will -# contain one block.) Used in the diff program (bin/diff). +# A Hunk is a group of Blocks which overlap because of the context surrounding +# each block. (So if we're not using context, every hunk will contain one +# block.) Used in the diff program (bin/ldiff). class Diff::LCS::Hunk - # Create a hunk using references to both the old and new data, as well as - # the piece of data. + # Create a hunk using references to both the old and new data, as well as the + # piece of data. def initialize(data_old, data_new, piece, flag_context, file_length_difference) # At first, a hunk will have just one Block in it @blocks = [Diff::LCS::Block.new(piece)] @@ -61,17 +61,20 @@ def flag_context=(context) #:nodoc: # rubocop:disable Lint/DuplicateMethods return if context.nil? or context.zero? add_start = context > @start_old ? @start_old : context + @start_old -= add_start @start_new -= add_start + old_size = @data_old.size + add_end = - if (@end_old + context) > @data_old.size - @data_old.size - @end_old + if (@end_old + context) > old_size + old_size - @end_old else context end - add_end = @max_diff_size if add_end > @max_diff_size + add_end = @max_diff_size if add_end >= old_size @end_old += add_end @end_new += add_end diff --git a/spec/fixtures/ldiff/output.diff.chef-u b/spec/fixtures/ldiff/output.diff.chef-u new file mode 100644 index 0000000..9939ecf --- /dev/null +++ b/spec/fixtures/ldiff/output.diff.chef-u @@ -0,0 +1,8 @@ +--- spec/fixtures/old-chef 2020-06-23 21:57:15.000000000 -0400 ++++ spec/fixtures/new-chef 2020-06-23 21:57:29.000000000 -0400 +@@ -1,5 +1,5 @@ + { + "name": "x", +- "description": "hi" ++ "description": "lo" + } diff --git a/spec/fixtures/new-chef b/spec/fixtures/new-chef new file mode 100644 index 0000000..d7babfe --- /dev/null +++ b/spec/fixtures/new-chef @@ -0,0 +1,4 @@ +{ + "name": "x", + "description": "lo" +} \ No newline at end of file diff --git a/spec/fixtures/old-chef b/spec/fixtures/old-chef new file mode 100644 index 0000000..5f9e38b --- /dev/null +++ b/spec/fixtures/old-chef @@ -0,0 +1,4 @@ +{ + "name": "x", + "description": "hi" +} \ No newline at end of file diff --git a/spec/issues_spec.rb b/spec/issues_spec.rb index c4542bb..c9a1e53 100644 --- a/spec/issues_spec.rb +++ b/spec/issues_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'spec_helper' +require 'diff/lcs/hunk' describe 'Diff::LCS Issues' do include Diff::LCS::SpecHelper::Matchers @@ -64,4 +65,34 @@ }.to raise_error(RSpec::Expectations::ExpectationNotMetError) end end + + describe "issue #60" do + it 'should produce unified output with correct context' do + old_data = <<-DATA_OLD.strip.split("\n").map(&:chomp) +{ + "name": "x", + "description": "hi" +} + DATA_OLD + + new_data = <<-DATA_NEW.strip.split("\n").map(&:chomp) +{ + "name": "x", + "description": "lo" +} + DATA_NEW + + diff = ::Diff::LCS.diff(old_data, new_data) + hunk = ::Diff::LCS::Hunk.new(old_data, new_data, diff.first, 3, 0) + + expect(hunk.diff(:unified)).to eq(<<-EXPECTED.chomp) +@@ -1,5 +1,5 @@ + { + "name": "x", +- "description": "hi" ++ "description": "lo" + } + EXPECTED + end + end end diff --git a/spec/ldiff_spec.rb b/spec/ldiff_spec.rb index bd3c1e9..eee6d86 100644 --- a/spec/ldiff_spec.rb +++ b/spec/ldiff_spec.rb @@ -10,6 +10,11 @@ let(:output_diff_e) { read_fixture('-e') } let(:output_diff_f) { read_fixture('-f') } let(:output_diff_u) { read_fixture('-u') } + let(:output_diff_chef) { read_fixture('-u', base: 'output.diff.chef')} + + specify do + expect(run_ldiff('-u', left: 'old-chef', right: 'new-chef')).to eq(output_diff_chef) + end specify do expect(run_ldiff).to eq(output_diff) @@ -31,8 +36,8 @@ expect(run_ldiff('-u')).to eq(output_diff_u) end - def read_fixture(flag = nil) - clean_data(IO.binread("spec/fixtures/ldiff/output.diff#{flag}"), flag) + def read_fixture(flag = nil, base: 'output.diff') + clean_data(IO.binread("spec/fixtures/ldiff/#{base}#{flag}"), flag) end def clean_data(data, flag)