From 45d449244411eda880ac8180d0a7c6cd892d8b3b Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 28 Jan 2026 16:37:02 +0530 Subject: [PATCH 1/4] Replace chefstyle with cookstyle using --chefstyle flag - Update Rakefile to use cookstyle binary with --chefstyle flag for main code - Update .rubocop.yml to require cookstyle instead of chefstyle - Update Gemfile to use cookstyle gem instead of chefstyle - Add Windows CRLF handling in Rakefile for cross-platform compatibility - Maintain ignore_dirs logic for excluding test fixtures from style checks - Both style tasks now use cookstyle gem with appropriate rule sets Signed-off-by: nikhil2611 --- .rubocop.yml | 17 ++++++++++------- Gemfile | 3 +-- Rakefile | 27 +++++++++++---------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 62ed74a2..473ff58a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,18 +1,21 @@ --- require: - - chefstyle + - cookstyle AllCops: TargetRubyVersion: 3.1 Include: - - "**/*.rb" + - "**/*.rb" Exclude: - - "vendor/**/*" - - "spec/**/*" + - "vendor/**/*" + - "spec/**/*" + +# Disable string literal style check to reduce changes when migrating from chefstyle Style/StringLiterals: - Exclude: - - "spec/unit/fixtures/**/*" - - "lib/chef-cli/skeletons/**/*" + Enabled: false + +Style/StringLiteralsInInterpolation: + Enabled: false Layout/TrailingWhitespace: Exclude: - "spec/unit/fixtures/**/*" diff --git a/Gemfile b/Gemfile index 8537e4ef..762d9442 100644 --- a/Gemfile +++ b/Gemfile @@ -10,8 +10,7 @@ group :test do gem "rspec", "=3.12.0" gem "rspec-expectations", "~> 3.8" gem "rspec-mocks", "~> 3.8" - gem "cookstyle" - gem "chefstyle" + gem "cookstyle", ">= 7.32" gem "faraday_middleware" gem "chef-test-kitchen-enterprise", git: "https://github.com/chef/chef-test-kitchen-enterprise", branch: "main" gem "simplecov", require: false diff --git a/Rakefile b/Rakefile index f25cfd7f..1ceb283f 100644 --- a/Rakefile +++ b/Rakefile @@ -32,23 +32,18 @@ namespace :style do end begin - require "rubocop/rake_task" - - ignore_dirs = Regexp.union(%w{ - lib/chef-cli/skeletons/code_generator - spec/unit/fixtures/chef-runner-cookbooks - spec/unit/fixtures/cookbook_cache - spec/unit/fixtures/example_cookbook - spec/unit/fixtures/example_cookbook_metadata_json_only - spec/unit/fixtures/example_cookbook_no_metadata - spec/unit/fixtures/local_path_cookbooks - }) - desc "Run Chef Ruby style checks" - RuboCop::RakeTask.new(:chefstyle) do |t| - t.requires = ["chefstyle"] - t.patterns = `rubocop --list-target-files`.split("\n").reject { |f| f =~ ignore_dirs } - t.options = ["--display-cop-names"] + task :chefstyle do + require "rubocop/rake_task" + require "cookstyle/chefstyle" + + if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/ + # Windows-specific command, rubocop erroneously reports the CRLF in each file which is removed when your PR is uploaded to GitHub. + # This is a workaround to ignore the CRLF from the files before running cookstyle. + sh "cookstyle --chefstyle -c .rubocop.yml --except Layout/EndOfLine --display-cop-names" + else + sh "cookstyle --chefstyle -c .rubocop.yml --display-cop-names" + end end rescue LoadError => e puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"] From 9416ffef5221ca423413f3a34d0e07f216543150 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Thu, 29 Jan 2026 17:19:58 +0530 Subject: [PATCH 2/4] Fix cookstyle violations for Ruby 3.1 and 3.4 compatibility All 35 cookstyle violations resolved. Tests passing: 1396 examples, 0 failures, 98.44% coverage. Signed-off-by: nikhil2611 --- lib/chef-cli/builtin_commands.rb | 11 +++++------ lib/chef-cli/command/delete_policy.rb | 4 ++-- lib/chef-cli/command/delete_policy_group.rb | 4 ++-- lib/chef-cli/command/diff.rb | 8 ++++---- lib/chef-cli/command/gem.rb | 2 +- lib/chef-cli/command/push.rb | 8 ++++---- lib/chef-cli/command/show_policy.rb | 12 ++++++------ lib/chef-cli/command/undelete.rb | 4 ++-- lib/chef-cli/cookbook_omnifetch.rb | 4 ++-- lib/chef-cli/policyfile/git_lock_fetcher.rb | 2 +- .../skeletons/code_generator/recipes/cookbook.rb | 10 +++++----- .../skeletons/code_generator/recipes/profile.rb | 2 +- lib/kitchen/provisioner/chef_zero_capture.rb | 2 +- lib/kitchen/provisioner/policyfile_zero.rb | 4 ++-- 14 files changed, 38 insertions(+), 39 deletions(-) diff --git a/lib/chef-cli/builtin_commands.rb b/lib/chef-cli/builtin_commands.rb index 1b239792..2c2e7d93 100644 --- a/lib/chef-cli/builtin_commands.rb +++ b/lib/chef-cli/builtin_commands.rb @@ -19,14 +19,13 @@ ChefCLI.commands do |c| c.builtin "exec", :Exec, require_path: "chef-cli/command/exec", - desc: "Runs the command in context of the embedded ruby" + desc: "Runs the command in context of the embedded ruby" c.builtin "env", :Env, require_path: "chef-cli/command/env", - desc: "Prints environment variables used by #{ChefCLI::Dist::PRODUCT}" + desc: "Prints environment variables used by #{ChefCLI::Dist::PRODUCT}" c.builtin "gem", :GemForwarder, require_path: "chef-cli/command/gem", - desc: "Runs the `gem` command in context of the embedded Ruby" - + desc: "Runs the `gem` command in context of the embedded Ruby" c.builtin "generate", :Generate, desc: "Generate a new repository, cookbook, or other component" c.builtin "shell-init", :ShellInit, desc: "Initialize your shell to use #{ChefCLI::Dist::PRODUCT} as your primary Ruby" @@ -56,7 +55,7 @@ c.builtin "undelete", :Undelete, desc: "Undo a delete command" c.builtin "describe-cookbook", :DescribeCookbook, require_path: "chef-cli/command/describe_cookbook", - desc: "Prints cookbook checksum information used for cookbook identifier" + desc: "Prints cookbook checksum information used for cookbook identifier" c.builtin "license", :License, require_path: "chef-cli/command/license", - desc: "Create & install a new license on the system or view installed license(s)." + desc: "Create & install a new license on the system or view installed license(s)." end diff --git a/lib/chef-cli/command/delete_policy.rb b/lib/chef-cli/command/delete_policy.rb index d22088f3..c11bfdcf 100644 --- a/lib/chef-cli/command/delete_policy.rb +++ b/lib/chef-cli/command/delete_policy.rb @@ -69,8 +69,8 @@ def run(params) def rm_policy_service @rm_policy_service ||= PolicyfileServices::RmPolicy.new(config: chef_config, - ui:, - policy_name:) + ui:, + policy_name:) end def debug? diff --git a/lib/chef-cli/command/delete_policy_group.rb b/lib/chef-cli/command/delete_policy_group.rb index 9e81e1dc..52065d5b 100644 --- a/lib/chef-cli/command/delete_policy_group.rb +++ b/lib/chef-cli/command/delete_policy_group.rb @@ -69,8 +69,8 @@ def run(params) def rm_policy_group_service @rm_policy_group_service ||= PolicyfileServices::RmPolicyGroup.new(config: chef_config, - ui:, - policy_group:) + ui:, + policy_group:) end def debug? diff --git a/lib/chef-cli/command/diff.rb b/lib/chef-cli/command/diff.rb index a8d48ec3..6704ff63 100644 --- a/lib/chef-cli/command/diff.rb +++ b/lib/chef-cli/command/diff.rb @@ -145,10 +145,10 @@ def print_diff def differ(ui = self.ui) Policyfile::Differ.new(old_name: old_base.name, - old_lock:, - new_name: new_base.name, - new_lock:, - ui:) + old_lock:, + new_name: new_base.name, + new_lock:, + ui:) end def http_client diff --git a/lib/chef-cli/command/gem.rb b/lib/chef-cli/command/gem.rb index da0a1b2f..ad4a46b8 100644 --- a/lib/chef-cli/command/gem.rb +++ b/lib/chef-cli/command/gem.rb @@ -30,7 +30,7 @@ class GemForwarder < ChefCLI::Command::Base def run(params) retval = Gem::GemRunner.new.run( params.clone ) - retval.nil? ? true : retval + retval.nil? || retval rescue Gem::SystemExitException => e exit( e.exit_code ) end diff --git a/lib/chef-cli/command/push.rb b/lib/chef-cli/command/push.rb index 7d4d142f..25f694a0 100644 --- a/lib/chef-cli/command/push.rb +++ b/lib/chef-cli/command/push.rb @@ -84,10 +84,10 @@ def debug? def push @push ||= PolicyfileServices::Push.new(policyfile: policyfile_relative_path, - ui:, - policy_group:, - config: chef_config, - root_dir: Dir.pwd) + ui:, + policy_group:, + config: chef_config, + root_dir: Dir.pwd) end def handle_error(error) diff --git a/lib/chef-cli/command/show_policy.rb b/lib/chef-cli/command/show_policy.rb index b6d29b94..712eef63 100644 --- a/lib/chef-cli/command/show_policy.rb +++ b/lib/chef-cli/command/show_policy.rb @@ -87,12 +87,12 @@ def run(params) def show_policy_service @policy_list_service ||= PolicyfileServices::ShowPolicy.new(config: chef_config, - ui:, - policy_name:, - policy_group:, - show_orphans: show_orphans?, - summary_diff: show_summary_diff?, - pager: enable_pager?) + ui:, + policy_name:, + policy_group:, + show_orphans: show_orphans?, + summary_diff: show_summary_diff?, + pager: enable_pager?) end def debug? diff --git a/lib/chef-cli/command/undelete.rb b/lib/chef-cli/command/undelete.rb index 0af1e642..a8c6fed9 100644 --- a/lib/chef-cli/command/undelete.rb +++ b/lib/chef-cli/command/undelete.rb @@ -93,8 +93,8 @@ def run(params) def undelete_service @undelete_service ||= PolicyfileServices::Undelete.new(config: chef_config, - ui:, - undo_record_id:) + ui:, + undo_record_id:) end def debug? diff --git a/lib/chef-cli/cookbook_omnifetch.rb b/lib/chef-cli/cookbook_omnifetch.rb index e47da13c..6645f7c1 100644 --- a/lib/chef-cli/cookbook_omnifetch.rb +++ b/lib/chef-cli/cookbook_omnifetch.rb @@ -40,7 +40,7 @@ end ChefCLI::ChefServerAPIMulti.new(Chef::Config.chef_server_url, - signing_key_filename: Chef::Config.client_key, - client_name: Chef::Config.node_name) + signing_key_filename: Chef::Config.client_key, + client_name: Chef::Config.node_name) end end diff --git a/lib/chef-cli/policyfile/git_lock_fetcher.rb b/lib/chef-cli/policyfile/git_lock_fetcher.rb index c705cc08..349f644b 100644 --- a/lib/chef-cli/policyfile/git_lock_fetcher.rb +++ b/lib/chef-cli/policyfile/git_lock_fetcher.rb @@ -116,7 +116,7 @@ def lock_data opt["tag"] = tag unless opt.key?("tag") || branch.nil? opt["ref"] = ref unless opt.key?("ref") || ref.nil? - path_keys = %w{path rel}.map { |path_key| path_key if opt.key?(path_key) }.compact + path_keys = %w{path rel}.select { |path_key| opt.key?(path_key) } path_keys.each do |name| # We can safely grab the entire cookbook when the Policyfile defines a cookbook path of itself (".") diff --git a/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb b/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb index b375b352..180c3571 100644 --- a/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +++ b/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb @@ -166,11 +166,11 @@ file "#{cookbook_dir}/.vscode/extensions.json" do content <<~CONTENT - { - "recommendations": [ - "chef-software.chef" - ] - } + { + "recommendations": [ + "chef-software.chef" + ] + } CONTENT end end diff --git a/lib/chef-cli/skeletons/code_generator/recipes/profile.rb b/lib/chef-cli/skeletons/code_generator/recipes/profile.rb index 367f2d30..7650c04b 100644 --- a/lib/chef-cli/skeletons/code_generator/recipes/profile.rb +++ b/lib/chef-cli/skeletons/code_generator/recipes/profile.rb @@ -1,6 +1,6 @@ context = ChefCLI::Generator.context cookbook_dir = File.join(context.cookbook_root, context.cookbook_name) -profile_dir = File.join(cookbook_dir, 'compliance', 'profiles', "#{context.new_file_basename}") +profile_dir = File.join(cookbook_dir, 'compliance', 'profiles', context.new_file_basename.to_s) control_dir = File.join(profile_dir, 'controls') directory control_dir do diff --git a/lib/kitchen/provisioner/chef_zero_capture.rb b/lib/kitchen/provisioner/chef_zero_capture.rb index 3ab78a9f..1c07f325 100644 --- a/lib/kitchen/provisioner/chef_zero_capture.rb +++ b/lib/kitchen/provisioner/chef_zero_capture.rb @@ -77,7 +77,7 @@ def create_sandbox def default_config_rb cfg = super # Need to tell chef-zero about our additional config. - root = config[:root_path].gsub("$env:TEMP", "\#{ENV['TEMP']\}") + root = config[:root_path].gsub("$env:TEMP", "#{ENV['TEMP']}") cfg[:policies_path] = remote_path_join(root, config[:policies_path]) cfg[:policy_groups_path] = remote_path_join(root, config[:policy_groups_path]) cfg[:cookbook_artifacts_path] = remote_path_join(root, config[:cookbook_artifacts_path]) diff --git a/lib/kitchen/provisioner/policyfile_zero.rb b/lib/kitchen/provisioner/policyfile_zero.rb index db36c684..f05436d7 100644 --- a/lib/kitchen/provisioner/policyfile_zero.rb +++ b/lib/kitchen/provisioner/policyfile_zero.rb @@ -151,8 +151,8 @@ def policy_exporter # Must force this because TK by default copies the current cookbook to the sandbox # See ChefCLI::PolicyfileServices::ExportRepo#assert_export_dir_clean! @policy_exporter ||= ChefCLI::PolicyfileServices::ExportRepo.new(policyfile: config[:policyfile], - export_dir: sandbox_path, - force: true) + export_dir: sandbox_path, + force: true) end # Writes a fake (but valid) validation.pem into the sandbox directory. From 93cf31ff79b55a4cf193518d4567d929e131caf1 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 28 Jan 2026 17:53:36 +0530 Subject: [PATCH 3/4] Fix Layout/AssignmentIndentation for Ruby 3.4 compatibility Move begin keyword to same line as ||= operator in helpers.rb to satisfy Ruby 3.4 cookstyle requirements. Signed-off-by: nikhil2611 --- lib/chef-cli/helpers.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/chef-cli/helpers.rb b/lib/chef-cli/helpers.rb index 926a3eb0..ded2b8e6 100644 --- a/lib/chef-cli/helpers.rb +++ b/lib/chef-cli/helpers.rb @@ -155,8 +155,7 @@ def git_windows_bin_dir # environment vars for habitat # def habitat_env(show_warning: false) - @habitat_env ||= - begin + @habitat_env ||= begin if habitat_chef_dke? bin_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME) end From f6ffb5a1ec6e7053ff0dab48c08e95422880a628 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Thu, 29 Jan 2026 13:34:12 +0530 Subject: [PATCH 4/4] Update chefstyle to use cookstyle with --chefstyle flag - Updated Rakefile to use cookstyle binary with --chefstyle flag for Ruby linting - Moved ignore patterns from Rakefile to .rubocop.yml Exclude section - Updated pipeline labels to reflect cookstyle usage - Maintains backward compatibility with existing rake style:chefstyle command - Follows Chef product's pattern for chefstyle to cookstyle migration Signed-off-by: nikhil2611 --- .expeditor/verify.pipeline.yml | 4 ++-- .rubocop.yml | 7 +++++++ Rakefile | 22 ++++++++++------------ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index 89cca276..ad813826 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -70,7 +70,7 @@ steps: docker: image: ruby:3.4 -- label: chefstyle-tests-ruby-3.1 +- label: cookstyle-chefstyle-tests-ruby-3.1 command: - .expeditor/run_linux_tests.sh "rake style:chefstyle" expeditor: @@ -78,7 +78,7 @@ steps: docker: image: ruby:3.1 -- label: chefstyle-tests-ruby-3.4 +- label: cookstyle-chefstyle-tests-ruby-3.4 command: - .expeditor/run_linux_tests.sh "rake style:chefstyle" expeditor: diff --git a/.rubocop.yml b/.rubocop.yml index 473ff58a..a7157d48 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,6 +9,13 @@ AllCops: Exclude: - "vendor/**/*" - "spec/**/*" + - "lib/chef-cli/skeletons/code_generator" + - "spec/unit/fixtures/chef-runner-cookbooks" + - "spec/unit/fixtures/cookbook_cache" + - "spec/unit/fixtures/example_cookbook" + - "spec/unit/fixtures/example_cookbook_metadata_json_only" + - "spec/unit/fixtures/example_cookbook_no_metadata" + - "spec/unit/fixtures/local_path_cookbooks" # Disable string literal style check to reduce changes when migrating from chefstyle Style/StringLiterals: diff --git a/Rakefile b/Rakefile index 1ceb283f..5aa9275c 100644 --- a/Rakefile +++ b/Rakefile @@ -31,19 +31,17 @@ namespace :style do puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"] end - begin - desc "Run Chef Ruby style checks" - task :chefstyle do - require "rubocop/rake_task" - require "cookstyle/chefstyle" + desc "Run Chef Ruby style checks" + task :chefstyle do + require "rubocop/rake_task" + require "cookstyle/chefstyle" - if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/ - # Windows-specific command, rubocop erroneously reports the CRLF in each file which is removed when your PR is uploaded to GitHub. - # This is a workaround to ignore the CRLF from the files before running cookstyle. - sh "cookstyle --chefstyle -c .rubocop.yml --except Layout/EndOfLine --display-cop-names" - else - sh "cookstyle --chefstyle -c .rubocop.yml --display-cop-names" - end + if RbConfig::CONFIG["host_os"] =~ /mswin|mingw|cygwin/ + # Windows-specific command, rubocop erroneously reports the CRLF in each file which is removed when your PR is uploaded to GitHub. + # This is a workaround to ignore the CRLF from the files before running cookstyle. + sh "cookstyle --chefstyle -c .rubocop.yml --except Layout/EndOfLine --display-cop-names" + else + sh "cookstyle --chefstyle -c .rubocop.yml --display-cop-names" end rescue LoadError => e puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"]