From bac19c03f83cb46e4962df01372ce63c43f4aa69 Mon Sep 17 00:00:00 2001 From: Justin Searls Date: Fri, 7 Jul 2023 09:52:00 -0400 Subject: [PATCH] Add PluginSupport module --- lib/standard.rb | 1 + lib/standard/plugin_support.rb | 1 + .../merges_upstream_metadata.rb | 23 +++++++ .../merges_upstream_metadata_test.rb | 67 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 lib/standard/plugin_support.rb create mode 100644 lib/standard/plugin_support/merges_upstream_metadata.rb create mode 100644 test/standard/plugin_support/merges_upstream_metadata_test.rb diff --git a/lib/standard.rb b/lib/standard.rb index ac476341..8f4ac948 100644 --- a/lib/standard.rb +++ b/lib/standard.rb @@ -13,3 +13,4 @@ module Standard require "standard/formatter" require "standard/plugin" +require "standard/plugin_support" diff --git a/lib/standard/plugin_support.rb b/lib/standard/plugin_support.rb new file mode 100644 index 00000000..d0414ada --- /dev/null +++ b/lib/standard/plugin_support.rb @@ -0,0 +1 @@ +require "standard/plugin_support/merges_upstream_metadata" diff --git a/lib/standard/plugin_support/merges_upstream_metadata.rb b/lib/standard/plugin_support/merges_upstream_metadata.rb new file mode 100644 index 00000000..952387fd --- /dev/null +++ b/lib/standard/plugin_support/merges_upstream_metadata.rb @@ -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 diff --git a/test/standard/plugin_support/merges_upstream_metadata_test.rb b/test/standard/plugin_support/merges_upstream_metadata_test.rb new file mode 100644 index 00000000..a73809ea --- /dev/null +++ b/test/standard/plugin_support/merges_upstream_metadata_test.rb @@ -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