-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from kpaulisse/kpaulisse-equivalent-array-filter
Add EquivalentArrayNoDatatypes filter
- Loading branch information
Showing
7 changed files
with
370 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
lib/octocatalog-diff/catalog-diff/filter/equivalent_array_no_datatypes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../filter' | ||
|
||
module OctocatalogDiff | ||
module CatalogDiff | ||
class Filter | ||
# Filter out changes in parameters where the elements of an array are the | ||
# same values but different data types. For example, this would filter out | ||
# the following diffs: | ||
# Exec[some command] => | ||
# parameters => | ||
# returns => | ||
# - ["0", "1"] | ||
# + [0, 1] | ||
class EquivalentArrayNoDatatypes < OctocatalogDiff::CatalogDiff::Filter | ||
# Public: Implement the filter for arrays that have the same elements | ||
# but possibly different data types. | ||
# | ||
# @param diff [OctocatalogDiff::API::V1::Diff] Difference | ||
# @param _options [Hash] Additional options (there are none for this filter) | ||
# @return [Boolean] true if this should be filtered out, false otherwise | ||
def filtered?(diff, _options = {}) | ||
# Skip additions or removals - focus only on changes | ||
return false unless diff.change? | ||
old_value = diff.old_value | ||
new_value = diff.new_value | ||
|
||
# Skip unless both the old and new values are arrays. | ||
return false unless old_value.is_a?(Array) && new_value.is_a?(Array) | ||
|
||
# Avoid generating comparable values if the arrays are a different | ||
# size, because there's no possible way that they are equivalent. | ||
return false unless old_value.size == new_value.size | ||
|
||
# Generate and then compare the comparable arrays. | ||
old_value.map { |x| comparable_value(x) } == new_value.map { |x| comparable_value(x) } | ||
end | ||
|
||
# Private: Get a more easily comparable value for an array element. | ||
# Integers, floats, and strings that look like integers or floats become | ||
# floats, and symbols are converted to string representation. | ||
# | ||
# @param input [any] Value to convert | ||
# @return [any] "Comparable" value | ||
def comparable_value(input) | ||
# Any string that looks like a number is converted to a float. | ||
if input.is_a?(String) && input =~ /\A-?(([0-9]*\.[0-9]+)|([0-9]+))\z/ | ||
return input.to_f | ||
end | ||
|
||
# Any number is converted to a float | ||
return input.to_f if input.is_a?(Integer) || input.is_a?(Float) | ||
|
||
# Symbols are converted to ":xxx" strings. | ||
return ":#{input}" if input.is_a?(Symbol) | ||
|
||
# Everything else is unconverted. | ||
input | ||
end | ||
end | ||
end | ||
end | ||
end |
55 changes: 55 additions & 0 deletions
55
spec/octocatalog-diff/fixtures/catalogs/filter-equivalent-array-1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"document_type": "Catalog", | ||
"data": { | ||
"tags": [ | ||
"settings" | ||
], | ||
"name": "my.rspec.node", | ||
"version": "production", | ||
"environment": "production", | ||
"resources": [ | ||
{ | ||
"type": "Exec", | ||
"title": "run-my-command 1", | ||
"file": "/environments/production/modules/foo/manifests/init.pp", | ||
"line": 10, | ||
"exported": false, | ||
"parameters": { | ||
"path": "/usr/bin", | ||
"command": "id", | ||
"returns": "0" | ||
} | ||
}, | ||
{ | ||
"type": "Exec", | ||
"title": "run-my-command 2", | ||
"file": "/environments/production/modules/foo/manifests/init.pp", | ||
"line": 10, | ||
"exported": false, | ||
"parameters": { | ||
"path": "/usr/bin", | ||
"command": "id", | ||
"returns": ["0", "1"] | ||
} | ||
}, | ||
{ | ||
"type": "Exec", | ||
"title": "run-my-command 3", | ||
"file": "/environments/production/modules/foo/manifests/init.pp", | ||
"line": 10, | ||
"exported": false, | ||
"parameters": { | ||
"path": "/usr/bin", | ||
"command": "id", | ||
"returns": ["0", "1", "2"] | ||
} | ||
} | ||
], | ||
"classes": [ | ||
"settings" | ||
] | ||
}, | ||
"metadata": { | ||
"api_version": 1 | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
spec/octocatalog-diff/fixtures/catalogs/filter-equivalent-array-2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"document_type": "Catalog", | ||
"data": { | ||
"tags": [ | ||
"settings" | ||
], | ||
"name": "my.rspec.node", | ||
"version": "production", | ||
"environment": "production", | ||
"resources": [ | ||
{ | ||
"type": "Exec", | ||
"title": "run-my-command 1", | ||
"file": "/environments/production/modules/foo/manifests/init.pp", | ||
"line": 10, | ||
"exported": false, | ||
"parameters": { | ||
"path": "/usr/bin", | ||
"command": "id", | ||
"returns": 0 | ||
} | ||
}, | ||
{ | ||
"type": "Exec", | ||
"title": "run-my-command 2", | ||
"file": "/environments/production/modules/foo/manifests/init.pp", | ||
"line": 10, | ||
"exported": false, | ||
"parameters": { | ||
"path": "/usr/bin", | ||
"command": "id", | ||
"returns": [0, 1] | ||
} | ||
}, | ||
{ | ||
"type": "Exec", | ||
"title": "run-my-command 3", | ||
"file": "/environments/production/modules/foo/manifests/init.pp", | ||
"line": 10, | ||
"exported": false, | ||
"parameters": { | ||
"path": "/usr/bin", | ||
"command": "id", | ||
"returns": [0, 1, 2, 3] | ||
} | ||
} | ||
], | ||
"classes": [ | ||
"settings" | ||
] | ||
}, | ||
"metadata": { | ||
"api_version": 1 | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
spec/octocatalog-diff/integration/filter_equivalent_array_no_datatypes_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'integration_helper' | ||
|
||
describe 'equivalent array no datatypes filter integration' do | ||
context 'with default behavior' do | ||
before(:all) do | ||
@result = OctocatalogDiff::Integration.integration( | ||
spec_catalog_old: 'filter-equivalent-array-1.json', | ||
spec_catalog_new: 'filter-equivalent-array-2.json' | ||
) | ||
end | ||
|
||
it 'should succeed' do | ||
expect(@result[:exitcode]).not_to eq(-1), "Internal error: #{@result[:exception]}\n#{@result[:logs]}" | ||
expect(@result[:exitcode]).to eq(2), "Runtime error: #{@result[:logs]}" | ||
end | ||
|
||
it 'should not suppress equivalent-but-for-data-type arrays' do | ||
diffs = OctocatalogDiff::Spec.remove_file_and_line(@result[:diffs]) | ||
expect(diffs.size).to eq(3), diffs.inspect | ||
expect(diffs[0][1..3]).to eq(["Exec\frun-my-command 1\fparameters\freturns", '0'.inspect, 0]) | ||
expect(diffs[1][1..3]).to eq(["Exec\frun-my-command 2\fparameters\freturns", %w[0 1], [0, 1]]) | ||
expect(diffs[2][1..3]).to eq(["Exec\frun-my-command 3\fparameters\freturns", %w[0 1 2], [0, 1, 2, 3]]) | ||
end | ||
end | ||
|
||
context 'with equivalent array no datatypes filter engaged' do | ||
before(:all) do | ||
@result = OctocatalogDiff::Integration.integration( | ||
spec_catalog_old: 'filter-equivalent-array-1.json', | ||
spec_catalog_new: 'filter-equivalent-array-2.json', | ||
argv: ['--filters', 'EquivalentArrayNoDatatypes'] | ||
) | ||
end | ||
|
||
it 'should succeed' do | ||
expect(@result[:exitcode]).not_to eq(-1), "Internal error: #{@result[:exception]}\n#{@result[:logs]}" | ||
expect(@result[:exitcode]).to eq(2), "Runtime error: #{@result[:logs]}" | ||
end | ||
|
||
it 'should suppress equivalent-but-for-data-type arrays' do | ||
diffs = OctocatalogDiff::Spec.remove_file_and_line(@result[:diffs]) | ||
expect(diffs.size).to eq(2), diffs.inspect | ||
# '0' => 0 is not suppressed because it's not an array | ||
expect(diffs[0][1..3]).to eq(["Exec\frun-my-command 1\fparameters\freturns", '0'.inspect, 0]) | ||
# %w[0 1] => [0, 1] is suppressed | ||
# %w[0 1 2] => [0, 1, 2, 3] is not suppressed because it's not equivalent | ||
expect(diffs[1][1..3]).to eq(["Exec\frun-my-command 3\fparameters\freturns", %w[0 1 2], [0, 1, 2, 3]]) | ||
end | ||
end | ||
end |
Oops, something went wrong.