-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are many things we need to do project wide that are a lot easier to accomplish with rubocop cops. This copies of the scaffolding to do that from Cookstyle and add the shellout cop from cookstyle as well as a new cop. Signed-off-by: Tim Smith <tsmith@chef.io>
- Loading branch information
Showing
8 changed files
with
217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
/test.rb # used for quick testing of cops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
58 changes: 58 additions & 0 deletions
58
lib/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
# | ||
# Copyright:: 2020, 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 < Cop | ||
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| | ||
add_offense(h, location: :expression, message: MSG, severity: :refactor) if h.braces? | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
# @todo when we drop ruby 2.4 support we can convert this to just delete_prefix delete_suffix | ||
corrector.replace(node.loc.expression, node.loc.expression.source.gsub(/^{/, "").gsub(/}$/, "")) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# 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 is VERY slow to require gems even if they've already been loaded. To work around this | ||
# wrap your require statement with an `if defined?()` check. | ||
# | ||
class UnlessDefinedRequire < Base | ||
extend RuboCop::Cop::AutoCorrector | ||
|
||
MSG = "Workaround rubygems slow requires by only running require if the class isn't already defined" | ||
|
||
REQUIRE_TO_CLASS = { | ||
"addressable/uri" => "Addressable::URI", | ||
"appscript" => "Appscript", | ||
"base64" => "Base64", | ||
"benchmark" => "Benchmark", | ||
"cgi" => "CGI", | ||
"chef-utils" => "ChefUtils", | ||
"csv" => "CSV", | ||
"digest" => "Digest", | ||
"digest/md5" => "Digest::MD5", | ||
"digest/sha1" => "Digest::SHA1", | ||
"digest/sha2" => "Digest::SHA2", | ||
"droplet_kit" => "DropletKit", | ||
"erb" => "Erb", | ||
"erubis" => "Erubis", | ||
"etc" => "Etc", | ||
"excon" => "Excon", | ||
"ffi_yajl" => "FFI_Yajl", | ||
"ffi" => "FFI", | ||
"fileutils" => "FileUtils", | ||
"find" => "Find.find", | ||
"forwardable" => "Forwardable", | ||
"ipaddr" => "IPAddr", | ||
"json" => "JSON", | ||
"mime/types" => "MIME::Types", | ||
"mixlib/archive" => "Mixlib::Archive", | ||
"mixlib/cli" => "Mixlib::CLI", | ||
"mixlib/config" => "Mixlib::Config", | ||
"mixlib/shellout" => "Mixlib::ShellOut", | ||
"multi_json" => "MultiJson", | ||
"net/http" => "Net::HTTP", | ||
"net/ssh" => "Net::SSH", | ||
"netaddr" => "NetAddr", | ||
"ohai" => "Ohai::System", | ||
"open-uri" => "OpenURI", | ||
"openssl" => "OpenSSL", | ||
"optparse" => "OptionParser", | ||
"ostruct" => "OpenStruct", | ||
"pathname" => "Pathname", | ||
"pp" => "PP", | ||
"rack" => "Rack", | ||
"rbconfig" => "RbConfig", | ||
"retryable" => "Retryable", | ||
"rexml/document" => "REXML::Document", | ||
"rubygems" => "Gem", | ||
"rubygems/package" => "Gem::Package", | ||
"securerandom" => "SecureRandom", | ||
"set" => "Set", | ||
"shellwords" => "Shellwords", | ||
"singleton" => "Singleton", | ||
"socket" => "Socket", | ||
"sslshake" => "SSLShake", | ||
"stringio" => "StringIO", | ||
"tempfile" => "Tempfile", | ||
"thor" => "Thor", | ||
"time" => "Time", | ||
"timeout" => "Timeout", | ||
"tmpdir" => "Dir.mktmpdir", | ||
"tomlrb" => "Tomlrb", | ||
"uri" => "URI", | ||
"webrick" => "WEBrick", | ||
"win32/registry" => "Win32::Registry", | ||
"win32ole" => "WIN32OLE", | ||
"winrm" => "WinRM::Connection", | ||
"yaml" => "YAML", | ||
"yard" => "YARD", | ||
"zip" => "Zip", | ||
"zlib" => "Zlib", | ||
}.freeze | ||
|
||
def_node_matcher :require?, <<-PATTERN | ||
(send nil? :require (str $_) ) | ||
PATTERN | ||
|
||
def on_send(node) | ||
require?(node) do |r| | ||
next if node.parent && node.parent.conditional? # catch both if and unless | ||
next unless REQUIRE_TO_CLASS[r] | ||
|
||
add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector| | ||
corrector.replace(node.loc.expression, "#{node.source} unless defined?(#{REQUIRE_TO_CLASS[r]})") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |