Skip to content

Commit

Permalink
Merge pull request #97 from chef/custom_cops
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 authored Aug 22, 2020
2 parents 560b01f + dcdace6 commit 9460ddb
Show file tree
Hide file tree
Showing 16 changed files with 496 additions and 20 deletions.
20 changes: 10 additions & 10 deletions .expeditor/verify.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ expeditor:

steps:

- label: run-chefstyle-ruby-2.4
- label: run-chefstyle-and-specs-ruby-2.4
command:
- .expeditor/run_linux_tests.sh rake style
- .expeditor/run_linux_tests.sh rake
expeditor:
executor:
docker:
image: ruby:2.4-buster

- label: run-chefstyle-ruby-2.5
- label: run-chefstyle-and-specs-ruby-2.5
command:
- .expeditor/run_linux_tests.sh rake style
- .expeditor/run_linux_tests.sh rake
expeditor:
executor:
docker:
image: ruby:2.5-buster

- label: run-chefstyle-ruby-2.6
- label: run-chefstyle-and-specs-ruby-2.6
command:
- .expeditor/run_linux_tests.sh rake style
- .expeditor/run_linux_tests.sh rake
expeditor:
executor:
docker:
image: ruby:2.6-buster

- label: run-chefstyle-ruby-2.7
- label: run-chefstyle-and-specs-ruby-2.7
command:
- .expeditor/run_linux_tests.sh rake style
- .expeditor/run_linux_tests.sh rake
expeditor:
executor:
docker:
image: ruby:2.7-buster

- label: run-chefstyle-windows
- label: run-chefstyle-and-specs-windows
command:
- bundle install --jobs=7 --retry=3 --without docs debug
- bundle exec rake style
- bundle exec rake
expeditor:
executor:
docker:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
/test.rb # used for quick testing of cops
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ source "https://rubygems.org"

# Specify your gem's dependencies in chefstyle.gemspec
gemspec

group :debug do
gem "pry"
gem "pry-byebug"
gem "pry-stack_explorer", "~> 0.4.0" # 0.5.0 drops support for Ruby < 2.6
end

group :development do
gem "rake", ">= 12.0"
gem "rspec", ">= 3.4"
end
40 changes: 37 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ task :vendor do
cp(src.join("default.yml"), dst.join("upstream.yml"))

require "rubocop"
require "yaml"
require "yaml" unless defined?(YAML)
cfg = RuboCop::Cop::Cop.all.inject({}) { |acc, cop| acc[cop.cop_name] = { "Enabled" => false }; acc }
File.open(dst.join("disable_all.yml"), "w") { |fh| fh.write cfg.to_yaml }

Expand All @@ -26,8 +26,42 @@ RuboCop::RakeTask.new(:style) do |task|
task.options << "--display-cop-names"
end

require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList["spec/cop/**/*.rb"]
end

desc "Run RSpec with code coverage"
task :coverage do
ENV["COVERAGE"] = "true"
Rake::Task["spec"].execute
end

desc "Ensure that all cops are defined in the chefstyle.yml config"
task :validate_config do
require "chefstyle"
require "yaml" unless defined?(YAML)
status = 0
config = YAML.load_file("config/chefstyle.yml")

puts "Checking that all cops are defined in config/chefstyle.yml:"

RuboCop::Cop::Chef.constants.each do |dep|
RuboCop::Cop::Chef.const_get(dep).constants.each do |cop|
unless config["#{dep}/#{cop}"]
puts "Error: #{dep}/#{cop} not found in config/chefstyle.yml"
status = 1
end
end
end

puts "All Cops found in the config. Good work." if status == 0

exit status
end

begin
require "yard"
require "yard" unless defined?(YARD)
YARD::Rake::YardocTask.new(:docs)
rescue LoadError
puts "yard is not available. bundle install first to make sure all dependencies are installed."
Expand All @@ -39,4 +73,4 @@ task :console do
ARGV.clear
IRB.start
end
task default: %i{build install}
task default: %i{style spec validate_config}
3 changes: 0 additions & 3 deletions chefstyle.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@ Gem::Specification.new do |spec|
spec.executables = %w{chefstyle}
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake", ">= 12.0"
spec.add_development_dependency "rspec"
spec.add_dependency("rubocop", Chefstyle::RUBOCOP_VERSION)
end
23 changes: 21 additions & 2 deletions config/chefstyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ Style/RescueModifier:
Style/AsciiComments:
Enabled: false

# Parens around ternaries often make them more readable and reduces cognitive load over operator precidence
# Parens around ternaries often make them more readable and reduces cognitive load over operator precedence
Style/TernaryParentheses:
Enabled: false

Expand Down Expand Up @@ -631,4 +631,23 @@ Style/CommentedKeyword:

# make sure we catch this Ruby 3.0 breaking change now
Lint/DeprecatedOpenSSLConstant:
Enabled: true
Enabled: true

ChefRuby/Ruby27KeywordArgumentWarnings:
Description: Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.
Enabled: true
VersionAdded: '1.3.0'

ChefRuby/UnlessDefinedRequire:
Description: Workaround RubyGems' slow requires by only running require if the constant isn't already defined
Enabled: true
VersionAdded: '1.3.0'
Include:
- 'lib/**/*'

ChefRuby/GemspecRequireRubygems:
Description: Rubygems does not need to be required in a Gemspec
Enabled: true
VersionAdded: '1.3.0'
Include:
- '**/*.gemspec'
12 changes: 10 additions & 2 deletions lib/chefstyle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ class ConfigLoader
# Chefstyle patches the RuboCop tool to set a new default configuration that
# is vendored in the Chefstyle codebase.
module Chefstyle
# @return [String] the absolute path to the main RuboCop configuration YAML
# file
# @return [String] the absolute path to the main RuboCop configuration YAML file
def self.config
RuboCop::ConfigLoader::DEFAULT_FILE
end
end

require_relative "rubocop/chef"

# Chef custom cops
Dir.glob(File.dirname(__FILE__) + "/rubocop/cop/chef/**/*.rb") do |file|
next if File.directory?(file)

require_relative file # not actually relative but require_relative is faster
end
11 changes: 11 additions & 0 deletions lib/rubocop/chef.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true
module RuboCop
# RuboCop Chef project namespace
module Chef
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
CONFIG_DEFAULT = PROJECT_ROOT.join("config", "chefstyle.yml").freeze
CONFIG = YAML.load(CONFIG_DEFAULT.read).freeze

private_constant(*constants(false))
end
end
46 changes: 46 additions & 0 deletions lib/rubocop/cop/chef/ruby/gemspec_require_rubygems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true
#
# Copyright:: Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module RuboCop
module Cop
module Chef
module ChefRuby
# Rubygems does not need to be required in a Gemspec. It's already loaded out of the box in Ruby now.
class GemspecRequireRubygems < Base
extend RuboCop::Cop::AutoCorrector
include RangeHelp

MSG = "Rubygems does not need to be required in a Gemspec. It's already loaded out of the box in Ruby now."

def_node_matcher :require_rubygems?, <<-PATTERN
(send nil? :require (str "rubygems") )
PATTERN

def on_send(node)
require_rubygems?(node) do |r|
node = node.parent if node.parent && node.parent.conditional? # make sure we identify conditionals on the require
add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
end
end
end
end
end
end
end
end
57 changes: 57 additions & 0 deletions lib/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true
#
# Copyright:: Chef Software, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module RuboCop
module Cop
module Chef
module ChefRuby
# Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.
#
# @example
#
# # bad
# shell_out!('hostnamectl status', { returns: [0, 1] })
# shell_out('hostnamectl status', { returns: [0, 1] })
#
# # good
# shell_out!('hostnamectl status', returns: [0, 1])
# shell_out('hostnamectl status', returns: [0, 1])
#
class Ruby27KeywordArgumentWarnings < Base
extend RuboCop::Cop::AutoCorrector

MSG = "Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings."

def_node_matcher :positional_shellout?, <<-PATTERN
(send nil? {:shell_out :shell_out!} ... $(hash ... ))
PATTERN

def on_send(node)
positional_shellout?(node) do |h|
next unless h.braces?

add_offense(h.loc.expression, message: MSG, severity: :refactor) do |corrector|
corrector.replace(h.loc.expression, h.loc.expression.source[1..-2])
end
end
end
end
end
end
end
end
Loading

0 comments on commit 9460ddb

Please sign in to comment.