Skip to content

Commit

Permalink
Merge pull request #569 from puppetlabs/CAT-1743-Address-cop-overriding
Browse files Browse the repository at this point in the history
(CAT-1743) Address .sync.yml not overriding cops
  • Loading branch information
gavindidrichsen authored Mar 14, 2024
2 parents 4fb29e7 + d88c8cb commit 009f3bd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .ci/fixtures/new_provider_sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Gemfile:
- gem: 'puppet-resource_api'
spec/spec_helper.rb:
mock_with: ':rspec'
.rubocop.yml:
default_configs:
Performance/CaseWhenSplat:
Enabled: false
2 changes: 2 additions & 0 deletions .ci/test_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pdk new module new_module --template-url="file://$TEMPLATE_PR_DIR" --template-re
pushd new_module
grep template < metadata.json
cp "$TEMPLATE_PR_DIR/.ci/fixtures/new_provider_sync.yml" ./.sync.yml
grep -A 1 "Performance/CaseWhenSplat" ./.rubocop.yml | grep -q "true" # Ensure that the template is applied
pdk update --force
grep -A 1 "Performance/CaseWhenSplat" ./.rubocop.yml | grep -q "false" # Ensure that the update command changes the template
pdk new class new_module
pdk new defined_type test_type
pdk new fact test_fact || true # not available in pdk 1.18 yet
Expand Down
12 changes: 11 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,20 @@ else
enabled_cops = []
end

# Create a filtered list of enabled cops that should be disabled.
disabled_cops = enabled_cops.select { |c| configs[c] && configs[c]['Enabled'] == false }

# The following block builds the final configuration based on the values of enabled_cops, default_enabled_cops, default_pending_cops, and cop_configs
(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 } }

# Set overriden cops to false if 'Enabled: false'. This can be the case when users override default behaviour via .sync.yml
(disabled_cops).sort.each { |c| configs[c] = { 'Enabled' => false } }

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

0 comments on commit 009f3bd

Please sign in to comment.