Skip to content

Commit 70e1de5

Browse files
author
Kevin Paulisse
committed
Add JSON filter file and passing test
1 parent d21d2ae commit 70e1de5

File tree

2 files changed

+118
-0
lines changed
  • lib/octocatalog-diff/catalog-diff/filter
  • spec/octocatalog-diff/tests/catalog-diff/filter

2 files changed

+118
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../filter'
4+
5+
require 'json'
6+
7+
module OctocatalogDiff
8+
module CatalogDiff
9+
class Filter
10+
# Filter based on equivalence of JSON objects for file resources with named extensions.
11+
class JSON < OctocatalogDiff::CatalogDiff::Filter
12+
# Public: Actually do the comparison of JSON objects for appropriate resources.
13+
# Return true if the JSON objects are known to be equivalent. Return false if they
14+
# are not equivalent, or if equivalence cannot be determined.
15+
#
16+
# @param diff_in [OctocatalogDiff::API::V1::Diff] Difference
17+
# @param _options [Hash] Additional options (there are none for this filter)
18+
# @return [Boolean] true if this difference is a JSON file with identical objects, false otherwise
19+
def filtered?(diff, _options = {})
20+
# Skip additions or removals - focus only on changes
21+
return false unless diff.change?
22+
23+
# Make sure we are comparing file content for a file ending in .json extension
24+
return false unless diff.type == 'File' && diff.structure == %w(parameters content)
25+
return false unless diff.title =~ /\.json\z/i
26+
27+
# Attempt to convert the old value and new value into JSON objects. Assuming
28+
# that doesn't error out, the return value is whether or not they're equal.
29+
obj_old = ::JSON.parse(diff.old_value)
30+
obj_new = ::JSON.parse(diff.new_value)
31+
obj_old == obj_new
32+
rescue # Rescue everything - if something failed, we aren't sure what's going on, so we'll return false.
33+
false
34+
end
35+
end
36+
end
37+
end
38+
end
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../spec_helper'
4+
require OctocatalogDiff::Spec.require_path('/api/v1/diff')
5+
require OctocatalogDiff::Spec.require_path('/catalog-diff/filter/json')
6+
7+
describe OctocatalogDiff::CatalogDiff::Filter::JSON do
8+
let(:subject) { described_class.new }
9+
10+
describe '#filtered?' do
11+
let(:str1a) { '{"foo":"bar"}' }
12+
let(:str1b) { "{\n \"foo\": \"bar\"\n}\n" }
13+
let(:str2) { '{"foo":"baz"}' }
14+
15+
it 'should not filter out an added resource' do
16+
diff = ['+', "File\ffoobar.json", { 'parameters' => { 'content' => str1a } }]
17+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
18+
result = subject.filtered?(diff_obj)
19+
expect(result).to eq(false)
20+
end
21+
22+
it 'should not filter out a removed resource' do
23+
diff = ['-', "File\ffoobar.json", { 'parameters' => { 'content' => str1a } }]
24+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
25+
result = subject.filtered?(diff_obj)
26+
expect(result).to eq(false)
27+
end
28+
29+
it 'should not filter out a non-file resource' do
30+
diff = ['~', "Exec\ffoobar.json\fparameters\fcontent", str1a, str1b]
31+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
32+
result = subject.filtered?(diff_obj)
33+
expect(result).to eq(false)
34+
end
35+
36+
it 'should not filter out a file whose extension is not .json' do
37+
diff = ['~', "File\ffoobar.txt\fparameters\fcontent", str1a, str1b]
38+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
39+
result = subject.filtered?(diff_obj)
40+
expect(result).to eq(false)
41+
end
42+
43+
it 'should not filter out a change with no content change' do
44+
diff = ['~', "File\ffoobar.json\fparameters\fowner", 'root', 'nobody']
45+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
46+
result = subject.filtered?(diff_obj)
47+
expect(result).to eq(false)
48+
end
49+
50+
it 'should not filter out a change where JSON objects are dissimilar' do
51+
diff = ['~', "File\ffoobar.json\fparameters\fcontent", str1a, str2]
52+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
53+
result = subject.filtered?(diff_obj)
54+
expect(result).to eq(false)
55+
end
56+
57+
it 'should not filter out a change where JSON is invalid' do
58+
x_str = '---{ "blah": "foo" }'
59+
diff = ['~', "File\ffoobar.json\fparameters\fcontent", x_str, x_str]
60+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
61+
result = subject.filtered?(diff_obj)
62+
expect(result).to eq(false)
63+
end
64+
65+
it 'should not filter out a change where JSON is unparseable' do
66+
x_str = "--- !ruby/object:This::Does::Not::Exist\n foo: bar"
67+
diff = ['~', "File\ffoobar.json\fparameters\fcontent", x_str, x_str]
68+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
69+
result = subject.filtered?(diff_obj)
70+
expect(result).to eq(false)
71+
end
72+
73+
it 'should filter out a whitespace-only change to a .json file' do
74+
diff = ['~', "File\ffoobar.json\fparameters\fcontent", str1a, str1b]
75+
diff_obj = OctocatalogDiff::API::V1::Diff.new(diff)
76+
result = subject.filtered?(diff_obj)
77+
expect(result).to eq(true)
78+
end
79+
end
80+
end

0 commit comments

Comments
 (0)