-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ module Standard | |
require "standard/formatter" | ||
|
||
require "standard/plugin" | ||
require "standard/plugin_support" |
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 @@ | ||
require "standard/plugin_support/merges_upstream_metadata" |
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,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
67
test/standard/plugin_support/merges_upstream_metadata_test.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,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 |