Skip to content

Commit

Permalink
(CAT-1743) Address .sync.yml not overriding cops
Browse files Browse the repository at this point in the history
Prior to this commit, it was noticed that Rubocop rules that were set to
false in .sync.yml would not be properly overriden if the cop already
existed as a default enabled cop.

This commit aims to correct this unintended functionality.
  • Loading branch information
LukasAud committed Mar 13, 2024
1 parent 4fb29e7 commit 2f2e8df
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion moduleroot/.rubocop.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ dynamic_path = File.absolute_path(File.join(base_path, "defaults-#{version}.yml"
default_path = File.absolute_path(File.join(base_path, "defaults-#{default_version}.yml"))
path = File.exist?(dynamic_path) ? dynamic_path : default_path

# The following block builds default_enabled_cops and default_pending_cops based on the existing values at defaults-<current_version>.yml, which usually lives at ∼/rubocop/
require 'yaml'
config_defaults = YAML.load(File.read(path))
default_enabled_cops = config_defaults[:default_enabled_cops] || []
default_pending_cops = config_defaults[:default_pending_cops] || []

# This line builds cop_configs based on the values existing at profile, which was previously configured to retrieve the values of a specific profile in config_defaults.yml
cop_configs = (profile['enabled_cops'] || {})

if cop_configs == 'all'
Expand All @@ -71,12 +73,19 @@ else
enabled_cops = []
end

disabled_cops = (enabled_cops).sort.each { |c| configs[c] if configs[c]['Enabled'] = false }

(enabled_cops & default_enabled_cops).sort.each { |c| configs[c] ||= cop_configs[c] if cop_configs[c] }
(enabled_cops - default_enabled_cops).sort.each { |c| configs[c] ||= cop_configs[c] || {}; configs[c]['Enabled'] = true }
(default_enabled_cops - enabled_cops).sort.each { |c| configs[c] ||= cop_configs[c] || {}; configs[c]['Enabled'] = false }

# force set pending cops to something to avoid the messages
(default_pending_cops & enabled_cops).sort.each { |c| configs[c] ||= cop_configs[c] if cop_configs[c]; configs[c]['Enabled'] = true }
(default_pending_cops & enabled_cops).sort.each { |c| configs[c] ||= cop_configs[c] if cop_configs[c]; configs[c]['Enabled'] = true }
(default_pending_cops - enabled_cops).sort.each { |c| configs[c] = { 'Enabled' => false } }

# The logic established previously will set any cop existing within config_defaults.yml to true by default, even if that cop has been overriden by .sync.yml to be 'Enabled = false'.
# The following line sets overriden cops to false if 'Enabled: false'.
(disabled_cops).sort.each { |c| configs[c] = { 'Enabled' => false } }

-%>
<%= YAML.dump(configs) -%>

0 comments on commit 2f2e8df

Please sign in to comment.