Skip to content

Commit

Permalink
Change when max_diff_size is applied
Browse files Browse the repository at this point in the history
- This appears to satisfy the issues found in #60 as well as providing the
  additional fixes required to properly test ldiff.

Resolves #60
  • Loading branch information
halostatue committed Jun 24, 2020
1 parent cef2f82 commit 9aabe2f
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 14 deletions.
16 changes: 16 additions & 0 deletions 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
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions 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]
Expand Down
2 changes: 1 addition & 1 deletion lib/diff/lcs.rb
Expand Up @@ -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'
Expand Down
19 changes: 11 additions & 8 deletions lib/diff/lcs/hunk.rb
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions 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"
}
4 changes: 4 additions & 0 deletions spec/fixtures/new-chef
@@ -0,0 +1,4 @@
{
"name": "x",
"description": "lo"
}
4 changes: 4 additions & 0 deletions spec/fixtures/old-chef
@@ -0,0 +1,4 @@
{
"name": "x",
"description": "hi"
}
31 changes: 31 additions & 0 deletions 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
Expand Down Expand Up @@ -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
9 changes: 7 additions & 2 deletions spec/ldiff_spec.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9aabe2f

Please sign in to comment.