Skip to content

Commit

Permalink
Add PluginSupport module
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Jul 7, 2023
1 parent 95eeb20 commit bac19c0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ module Standard
require "standard/formatter"

require "standard/plugin"
require "standard/plugin_support"
1 change: 1 addition & 0 deletions lib/standard/plugin_support.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "standard/plugin_support/merges_upstream_metadata"
23 changes: 23 additions & 0 deletions lib/standard/plugin_support/merges_upstream_metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Standard
module PluginSupport
class MergesUpstreamMetadata
def merge(plugin_yaml, upstream_yaml)
common_upstream_values = upstream_yaml.select { |key| plugin_yaml.key?(key) }

plugin_yaml.merge(common_upstream_values) { |key, plugin_value, upstream_value|
if plugin_value.is_a?(Hash) && upstream_value.is_a?(Hash)
plugin_value.merge(upstream_value) { |sub_key, plugin_sub_value, upstream_sub_value|
if plugin_value.key?(sub_key)
plugin_sub_value
else
upstream_sub_value
end
}
else
plugin_value
end
}
end
end
end
end
67 changes: 67 additions & 0 deletions test/standard/plugin_support/merges_upstream_metadata_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "test_helper"

module Standard
module PluginSupport
class MergesUpstreamMetadataTest < Minitest::Test
def setup
@subject = MergesUpstreamMetadata.new
end

def test_no_op
assert_equal({}, @subject.merge({}, {}))
assert_equal({}, @subject.merge({}, {:junk => {}, "Some/Rule" => {}}))
assert_equal({foo: "berry"}, @subject.merge({foo: "berry"}, {:junk => {}, "Some/Rule" => {}}))
end

def test_merges_undefined_sub_keys
assert_equal(
{"Some/Rule" => {"Enabled" => true, "Description" => "foo"}},
@subject.merge(
{"Some/Rule" => {"Enabled" => true}},
{"Some/Rule" => {"Description" => "foo"}}
)
)
end

def test_doesnt_merge_nil_values
assert_equal(
{"Some/Rule" => {"Description" => nil}},
@subject.merge(
{"Some/Rule" => {"Description" => nil}},
{"Some/Rule" => {"Description" => "foo"}}
)
)
end

def test_doesnt_override_defined_values
assert_equal(
{"Some/Rule" => {"Enabled" => false}},
@subject.merge(
{"Some/Rule" => {"Enabled" => false}},
{"Some/Rule" => {"Enabled" => true}}
)
)
end

def test_doesnt_munge_arrays
assert_equal(
{"Some/Rule" => {"Included" => ["lol"]}},
@subject.merge(
{"Some/Rule" => {"Included" => ["lol"]}},
{"Some/Rule" => {"Included" => ["kek"]}}
)
)
end

def test_doesnt_munge_hashes
assert_equal(
{"Some/Rule" => {"Conf" => {a: 1}}},
@subject.merge(
{"Some/Rule" => {"Conf" => {a: 1}}},
{"Some/Rule" => {"Conf" => {a: 2, b: 3}}}
)
)
end
end
end
end

0 comments on commit bac19c0

Please sign in to comment.