Skip to content

Commit

Permalink
Clear warnings about missing properties caused by using empty AllCops…
Browse files Browse the repository at this point in the history
… (and make it more Real™ in our tests)
  • Loading branch information
searls committed Jan 18, 2023
1 parent dd28b6d commit fc34141
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class MergesUserConfigExtensions
def call(options_config, standard_config)
return unless standard_config[:extend_config]&.any?

extended_config = load_and_merge_extended_rubocop_configs(standard_config)
extended_config = load_and_merge_extended_rubocop_configs(options_config, standard_config)
merge_standard_and_user_all_cops!(options_config, extended_config)
merge_extended_rules_into_standard!(options_config, extended_config)
end

private

def load_and_merge_extended_rubocop_configs(standard_config)
# Blank configuration object to merge extensions into
config = RuboCop::Config.new({"AllCops" => {}}, "")

def load_and_merge_extended_rubocop_configs(options_config, standard_config)
orig_default_config = RuboCop::ConfigLoader.instance_variable_get(:@default_configuration)

# Blank configuration object to merge extensions into, with all known
# AllCops keys set to avoid warnings about unknown properties
config = RuboCop::Config.new(options_config.to_h.slice("AllCops"), "")
RuboCop::ConfigLoader.instance_variable_set(:@default_configuration, config)

standard_config[:extend_config].each do |path|
# RuboCop plugins often modify the default configuration in-place
# (e.g., load custom base configurations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ def setup
end

def test_doesnt_change_config_when_no_extensions_defined
options_config = {
options_config = RuboCop::Config.new({
"AllCops" => {}
}
}, "")

@subject.call(options_config, {
extend_config: []
})

assert_equal({
"AllCops" => {}
}, options_config)
}, options_config.to_h)
end

def test_when_one_file_extends
options_config = {
options_config = RuboCop::Config.new({
"AllCops" => {
"TargetRubyVersion" => "2.6"
"TargetRubyVersion" => "2.6",
"StyleGuideCopsOnly" => false,
"DisabledByDefault" => false,
"StyleGuideBaseURL" => "https://standardrb.example.com"
}
}
}, "")

@subject.call(options_config, {
extend_config: ["test/fixture/extend_config/all_cops.yml"]
Expand All @@ -34,20 +37,25 @@ def test_when_one_file_extends
"AllCops" => {
# Ignored b/c DISALLOWED_ALLCOPS_KEYS
"TargetRubyVersion" => "2.6",
# Excluded b/c DISALLOWED_ALLCOPS_KEYS
# "StyleGuideCopsOnly" => false,
# Ignored b/c DISALLOWED_ALLCOPS_KEYS
"StyleGuideCopsOnly" => false,

# Allowed to overwrite
"DisabledByDefault" => true,
"StyleGuideBaseURL" => "https://all_cops.yml"
}
}, options_config)
}, options_config.to_h)
end

def test_when_two_files_extend
options_config = {
"AllCops" => {}
}
options_config = RuboCop::Config.new({
"AllCops" => {
"TargetRubyVersion" => nil,
"StyleGuideCopsOnly" => false,
"DisabledByDefault" => false,
"StyleGuideBaseURL" => "https://standardrb.example.com"
}
}, "")

@subject.call(options_config, {
extend_config: [
Expand All @@ -58,6 +66,8 @@ def test_when_two_files_extend

assert_equal({
"AllCops" => {
"TargetRubyVersion" => nil,
"StyleGuideCopsOnly" => false,
"DisabledByDefault" => true,
# Last-in wins
"StyleGuideBaseURL" => "https://betterlint.yml"
Expand All @@ -66,14 +76,19 @@ def test_when_two_files_extend
"Enabled" => true,
"unauthenticated_models" => ["SystemConfiguration"]
}
}, options_config)
}, options_config.to_h)
end

def test_when_three_files_extend_with_monkey_business
options_config = {
"AllCops" => {},
options_config = RuboCop::Config.new({
"AllCops" => {
"TargetRubyVersion" => nil,
"StyleGuideCopsOnly" => false,
"DisabledByDefault" => false,
"StyleGuideBaseURL" => "https://standardrb.example.com"
},
"Naming/VariableName" => {"Enabled" => true}
}
}, "")

@subject.call(options_config, {
extend_config: [
Expand All @@ -85,6 +100,8 @@ def test_when_three_files_extend_with_monkey_business

assert_equal({
"AllCops" => {
"TargetRubyVersion" => nil,
"StyleGuideCopsOnly" => false,
"DisabledByDefault" => true,
"StyleGuideBaseURL" => "https://betterlint.yml"
},
Expand All @@ -99,6 +116,6 @@ def test_when_three_files_extend_with_monkey_business
"Naming/VariableName" => {
"Enabled" => true
}
}, options_config)
}, options_config.to_h)
end
end

0 comments on commit fc34141

Please sign in to comment.