Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change when max_diff_size is applied #61

Merged
merged 3 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--- spec/fixtures/old-chef 2019-02-01 21:57:15.000000000 -0400
+++ spec/fixtures/new-chef 2019-02-01 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "x",
"description": "lo"
}
4 changes: 4 additions & 0 deletions spec/fixtures/old-chef
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "x",
"description": "hi"
}
31 changes: 31 additions & 0 deletions spec/issues_spec.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 14 additions & 8 deletions spec/ldiff_spec.rb
Original file line number Diff line number Diff line change
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 All @@ -49,14 +54,15 @@ def clean_data(data, flag)
def clean_output_timestamp(data)
data.gsub(
%r{
[-*+]{3}
\s
spec/fixtures/(\w+)
\s
^
[-+*]{3}
\s*
spec/fixtures/(\S+)
\s*
\d{4}-\d\d-\d\d
\s
\s*
\d\d:\d\d:\d\d(?:\.\d+)
\s
\s*
(?:[-+]\d{4}|Z)
}x,
'*** spec/fixtures/\1 0000-00-00 00:00:00.000000000 -0000'
Expand Down