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

Adding change and add'l tests #277

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 10 additions & 3 deletions lib/octocatalog-diff/catalog-diff/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,16 @@ def hashdiff_nested_changes(hashdiff_add_remove, remaining1, remaining2)
def dig_out_key(hash_in, key_array)
return hash_in if key_array.empty?
return hash_in unless hash_in.is_a?(Hash)
return nil unless hash_in.key?(key_array[0])
next_key = key_array.shift
dig_out_key(hash_in[next_key], key_array)
key_without_index = key_array[0].sub(/\[\d+\]/, '')
return nil unless hash_in.key?(key_without_index)
full_key = key_array.shift
next_obj = hash_in[key_without_index]
# jump into array index if needed
full_key.scan(/\[(\d+)\]/).flatten.each do |index|
return nil unless next_obj.is_a?(Array) && next_obj[index.to_i]
next_obj = next_obj[index.to_i]
end
dig_out_key(next_obj, key_array)
end

# This is a helper for the constructor, verifying that the incoming catalog is an expected
Expand Down
232 changes: 232 additions & 0 deletions spec/octocatalog-diff/tests/catalog-diff/differ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,156 @@
end

describe '#hashdiff_nested_changes' do
it 'should return the value at the nested array index when the key includes multiple array indices' do
hashdiff_add_remove = [
"Class\fOpenssl::Package\fparameters\fcommon-array[0][1]"
]

empty_puppet_catalog_json = File.read(OctocatalogDiff::Spec.fixture_path('catalogs/catalog-empty.json'))
empty_puppet_catalog = OctocatalogDiff::Catalog.create(json: empty_puppet_catalog_json)
obj = OctocatalogDiff::CatalogDiff::Differ.new({}, empty_puppet_catalog, empty_puppet_catalog)

cat1 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'common-array' => [['value1', 'value2'], ['value3', 'value4']]
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

cat2 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'common-array' => [['value1', 'value3'], ['value3', 'value4']]
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

remaining1 = obj.send(:resources_as_hashes_with_serialized_keys, cat1)
remaining2 = obj.send(:resources_as_hashes_with_serialized_keys, cat2)

result = obj.send(:hashdiff_nested_changes, hashdiff_add_remove, remaining1, remaining2)

expect(result).to be_a_kind_of(Array)
expect(result.size).to eq(1)

fileref = { 'file' => '/var/tmp/foo', 'line' => 5 }
expect(result[0]).to eq(['!', "Class\fOpenssl::Package\fparameters\fcommon-array[0][1]", 'value2', 'value3', fileref, fileref])
end

it 'should handle multiple keys and array indices in the key' do
hashdiff_add_remove = [
"Class\fOpenssl::Package\fparameters\fobject\farray[0]\fcommon-array[1]\fvalue"
]

empty_puppet_catalog_json = File.read(OctocatalogDiff::Spec.fixture_path('catalogs/catalog-empty.json'))
empty_puppet_catalog = OctocatalogDiff::Catalog.create(json: empty_puppet_catalog_json)
obj = OctocatalogDiff::CatalogDiff::Differ.new({}, empty_puppet_catalog, empty_puppet_catalog)

cat1 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'object' => {
'array' => [
{
'common-array' => [
{ 'name' => 'test1', 'value' => 'abc' },
{ 'name' => 'test2', 'value' => 'def' }
]
}
]
}
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

cat2 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'object' => {
'array' => [
{
'common-array' => [
{ 'name' => 'test1', 'value' => 'abc' },
{ 'name' => 'test2', 'value' => 'ghi' }
]
}
]
}
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

remaining1 = obj.send(:resources_as_hashes_with_serialized_keys, cat1)
remaining2 = obj.send(:resources_as_hashes_with_serialized_keys, cat2)

result = obj.send(:hashdiff_nested_changes, hashdiff_add_remove, remaining1, remaining2)

expect(result).to be_a_kind_of(Array)
expect(result.size).to eq(1)

fileref = { 'file' => '/var/tmp/foo', 'line' => 5 }
expect(result[0]).to eq(['!', "Class\fOpenssl::Package\fparameters\fobject\farray[0]\fcommon-array[1]\fvalue", 'def', 'ghi', fileref, fileref])
end

it 'should return nil when the array index is out of bounds' do
hashdiff_add_remove = [
"Class\fOpenssl::Package\fparameters\fcommon-array[0][10]"
]

empty_puppet_catalog_json = File.read(OctocatalogDiff::Spec.fixture_path('catalogs/catalog-empty.json'))
empty_puppet_catalog = OctocatalogDiff::Catalog.create(json: empty_puppet_catalog_json)
obj = OctocatalogDiff::CatalogDiff::Differ.new({}, empty_puppet_catalog, empty_puppet_catalog)

cat1 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'common-array' => [['value1', 'value2'], ['value3', 'value4']]
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

cat2 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'common-array' => [['value1', 'value2'], ['value3', 'value4']]
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

remaining1 = obj.send(:resources_as_hashes_with_serialized_keys, cat1)
remaining2 = obj.send(:resources_as_hashes_with_serialized_keys, cat2)

result = obj.send(:hashdiff_nested_changes, hashdiff_add_remove, remaining1, remaining2)

expect(result).to be_a_kind_of(Array)
expect(result.size).to eq(0)
end

it 'should return array with proper results' do
hashdiff_add_remove = [
"Class\fOpenssl::Package\fparameters\fcommon-array"
Expand Down Expand Up @@ -1481,6 +1631,88 @@
fileref = { 'file' => '/var/tmp/foo', 'line' => 5 }
expect(result[0]).to eq(['!', "Class\fOpenssl::Package\fparameters\fcommon-array", [1, 2, 3], [1, 5, 25], fileref, fileref])
end

it 'should return array with proper results for deeply nested arrays of hashes' do
hashdiff_add_remove = [
"Class\fOpenssl::Package\fparameters\fobject\farray[0]\fcommon-array"
]

empty_puppet_catalog_json = File.read(OctocatalogDiff::Spec.fixture_path('catalogs/catalog-empty.json'))
empty_puppet_catalog = OctocatalogDiff::Catalog.create(json: empty_puppet_catalog_json)
obj = OctocatalogDiff::CatalogDiff::Differ.new({}, empty_puppet_catalog, empty_puppet_catalog)

arr1 = [
{
'name' => 'test', 'value' => 'abc'
},
{
'name' => 'test2', 'value' => 'def'
}
]
cat1 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'object' => {
'array' => [
{
'common-array' => arr1
}
]
}
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

arr2 = [
{
'name' => 'test', 'value' => 'abc'
},
{
'name' => 'test2', 'value' => 'def'
},
{
'name' => 'test3', 'value' => 'ghj'
}
]
cat2 = [
{
'type' => 'Class',
'title' => 'Openssl::Package',
'parameters' => {
'object' => {
'array' => [
{
'common-array' => arr2
}
]
}
},
'file' => '/var/tmp/foo',
'line' => 5
}
]

remaining1 = obj.send(:resources_as_hashes_with_serialized_keys, cat1)
remaining2 = obj.send(:resources_as_hashes_with_serialized_keys, cat2)

result = obj.send(:hashdiff_nested_changes, hashdiff_add_remove, remaining1, remaining2)
expect(result).to be_a_kind_of(Array)
expect(result.size).to eq(1)

fileref = { 'file' => '/var/tmp/foo', 'line' => 5 }
expect(result[0]).to eq([
'!',
"Class\fOpenssl::Package\fparameters\fobject\farray[0]\fcommon-array",
arr1,
arr2,
fileref,
fileref
])
end
end

describe '#regexp_operator_match?' do
Expand Down
Loading