diff --git a/.rubocop.yml b/.rubocop.yml index 62ed74a22..5eae219a4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,6 @@ --- require: - - chefstyle + - cookstyle AllCops: TargetRubyVersion: 3.1 @@ -9,6 +9,15 @@ 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/**/*" + - "my-cookbook/**/*" + - "test123/**/*" Style/StringLiterals: Exclude: - "spec/unit/fixtures/**/*" diff --git a/Gemfile b/Gemfile index 8537e4ef5..e99a98887 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,6 @@ group :test do gem "rspec-expectations", "~> 3.8" gem "rspec-mocks", "~> 3.8" gem "cookstyle" - gem "chefstyle" gem "faraday_middleware" gem "chef-test-kitchen-enterprise", git: "https://github.com/chef/chef-test-kitchen-enterprise", branch: "main" gem "simplecov", require: false @@ -30,4 +29,4 @@ group :profile do gem "stackprof-webnav" gem "memory_profiler" end -end \ No newline at end of file +end diff --git a/Rakefile b/Rakefile index f25cfd7f5..52b238a19 100644 --- a/Rakefile +++ b/Rakefile @@ -19,39 +19,20 @@ require "bundler/gem_tasks" namespace :style do begin - require "rubocop/rake_task" - desc "Run Cookbook Ruby style checks" - RuboCop::RakeTask.new(:cookstyle) do |t| - t.requires = ["cookstyle"] - t.patterns = ["lib/chef-cli/skeletons/code_generator"] - t.options = ["--display-cop-names"] + task :cookstyle do + sh "find lib/chef-cli/skeletons/code_generator -name '*.rb' -print0 | xargs -0 cookstyle --display-cop-names" end rescue LoadError => e puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"] 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 + sh "cookstyle --chefstyle --display-cop-names" end rescue LoadError => e puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"] end end - diff --git a/chef-cli.gemspec b/chef-cli.gemspec index 7fecd2f47..5f40a1100 100644 --- a/chef-cli.gemspec +++ b/chef-cli.gemspec @@ -36,7 +36,6 @@ Gem::Specification.new do |gem| Dir.glob("*.gemspec") + Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) } gem.executables = %w{ chef-cli } - gem.test_files = gem.files.grep(%r{^spec/}) gem.require_paths = ["lib"] gem.add_dependency "mixlib-cli", ">= 1.7", "< 3.0" diff --git a/coverage/.last_run.json b/coverage/.last_run.json deleted file mode 100644 index 5bb3b2a19..000000000 --- a/coverage/.last_run.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "result": { - "line": 98.51 - } -} diff --git a/lib/chef-cli/builtin_commands.rb b/lib/chef-cli/builtin_commands.rb index 1b239792a..4b11929ce 100644 --- a/lib/chef-cli/builtin_commands.rb +++ b/lib/chef-cli/builtin_commands.rb @@ -19,13 +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" @@ -56,7 +56,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 d22088f36..c11bfdcf4 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 9e81e1dc6..52065d5b0 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 a8d48ec36..6704ff63f 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 da0a1b2fd..ad4a46b85 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 7d4d142f6..25f694a0f 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 b6d29b946..712eef635 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 0af1e642f..a8c6fed95 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 e47da13c5..6645f7c1e 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/helpers.rb b/lib/chef-cli/helpers.rb index 926a3eb0b..5b726e028 100644 --- a/lib/chef-cli/helpers.rb +++ b/lib/chef-cli/helpers.rb @@ -156,38 +156,38 @@ def git_windows_bin_dir # def habitat_env(show_warning: false) @habitat_env ||= - begin - if habitat_chef_dke? - bin_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME) - end - versioned_pkg_prefix = fetch_chef_cli_version_pkg if ENV["CHEF_CLI_VERSION"] + begin + if habitat_chef_dke? + bin_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME) + end + versioned_pkg_prefix = fetch_chef_cli_version_pkg if ENV["CHEF_CLI_VERSION"] + + if show_warning && ENV["CHEF_CLI_VERSION"] && !versioned_pkg_prefix + ChefCLI::UI.new.msg("Warning: Habitat package '#{ChefCLI::Dist::HAB_PKG_NAME}' with version '#{ENV["CHEF_CLI_VERSION"]}' not found.") + end + # Use the first available package for bin_pkg_prefix + bin_pkg_prefix ||= versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME) + raise "Error: Could not determine the Habitat package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless bin_pkg_prefix + + # Determine vendor_dir by prioritizing the versioned package first + vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME) + raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix + + vendor_dir = File.join(vendor_pkg_prefix, "vendor") + # Construct PATH + path = [ + File.join(bin_pkg_prefix, "bin"), + File.join(vendor_dir, "bin"), + ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH + ].flatten.uniq - if show_warning && ENV["CHEF_CLI_VERSION"] && !versioned_pkg_prefix - ChefCLI::UI.new.msg("Warning: Habitat package '#{ChefCLI::Dist::HAB_PKG_NAME}' with version '#{ENV["CHEF_CLI_VERSION"]}' not found.") + { + "PATH" => path.join(File::PATH_SEPARATOR), + "GEM_ROOT" => Gem.default_dir, # Default directory for gems + "GEM_HOME" => vendor_dir, # Set only if vendor_dir exists + "GEM_PATH" => vendor_dir, # Set only if vendor_dir exists + } end - # Use the first available package for bin_pkg_prefix - bin_pkg_prefix ||= versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME) - raise "Error: Could not determine the Habitat package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless bin_pkg_prefix - - # Determine vendor_dir by prioritizing the versioned package first - vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME) - raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix - - vendor_dir = File.join(vendor_pkg_prefix, "vendor") - # Construct PATH - path = [ - File.join(bin_pkg_prefix, "bin"), - File.join(vendor_dir, "bin"), - ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH - ].flatten.uniq - - { - "PATH" => path.join(File::PATH_SEPARATOR), - "GEM_ROOT" => Gem.default_dir, # Default directory for gems - "GEM_HOME" => vendor_dir, # Set only if vendor_dir exists - "GEM_PATH" => vendor_dir, # Set only if vendor_dir exists - } - end end # diff --git a/lib/chef-cli/policyfile/git_lock_fetcher.rb b/lib/chef-cli/policyfile/git_lock_fetcher.rb index c705cc08d..349f644b4 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/files/default/repo/cookbooks/example/recipes/default.rb b/lib/chef-cli/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb index 82f8fa310..e3f5f0e18 100644 --- a/lib/chef-cli/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb +++ b/lib/chef-cli/skeletons/code_generator/files/default/repo/cookbooks/example/recipes/default.rb @@ -1,7 +1,7 @@ # This is a Chef Infra Client recipe file. It can be used to specify resources # which will apply configuration to a server. -log "Welcome to Chef Infra Client, #{node["example"]["name"]}!" do +log "Welcome to Chef Infra Client, #{node['example']['name']}!" do level :info end diff --git a/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb b/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb index b375b352c..180c35712 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 367f2d300..7650c04bb 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 3ab78a9f6..07abb8e86 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 db36c684b..f05436d72 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. diff --git a/spec/unit/command/generator_commands/repo_spec.rb b/spec/unit/command/generator_commands/repo_spec.rb index 77fec47e3..86499b831 100644 --- a/spec/unit/command/generator_commands/repo_spec.rb +++ b/spec/unit/command/generator_commands/repo_spec.rb @@ -256,7 +256,7 @@ def generator_context let(:file) { "cookbooks/example/recipes/default.rb" } it "has the right contents" do - expect(file_contents).to match(/log "Welcome to Chef Infra Client, \#\{node\["example"\]\["name"\]\}!" do/) + expect(file_contents).to match(/log "Welcome to Chef Infra Client, \#\{node\['example'\]\['name'\]\}!" do/) end end end