chefstyle 1.1.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,18 @@
1
+ # frozen_string_literal: true
1
2
  require_relative "chefstyle/version"
2
3
 
3
4
  # ensure the desired target version of RuboCop is gem activated
4
5
  gem "rubocop", "= #{Chefstyle::RUBOCOP_VERSION}"
5
6
  require "rubocop"
6
7
 
8
+ # @TODO remove this monkeypatch after we upgrade from 0.91.0
9
+ require_relative "rubocop/monkey_patches/rescue_ensure_alignment"
10
+
7
11
  module RuboCop
8
12
  class ConfigLoader
9
13
  RUBOCOP_HOME.gsub!(
10
14
  /^.*$/,
11
- File.realpath(File.join(File.dirname(__FILE__), ".."))
15
+ File.realpath(File.join(__dir__, ".."))
12
16
  )
13
17
 
14
18
  DEFAULT_FILE.gsub!(
@@ -21,9 +25,17 @@ end
21
25
  # Chefstyle patches the RuboCop tool to set a new default configuration that
22
26
  # is vendored in the Chefstyle codebase.
23
27
  module Chefstyle
24
- # @return [String] the absolute path to the main RuboCop configuration YAML
25
- # file
28
+ # @return [String] the absolute path to the main RuboCop configuration YAML file
26
29
  def self.config
27
30
  RuboCop::ConfigLoader::DEFAULT_FILE
28
31
  end
29
32
  end
33
+
34
+ require_relative "rubocop/chef"
35
+
36
+ # Chef custom cops
37
+ Dir.glob(__dir__ + "/rubocop/cop/chef/**/*.rb") do |file|
38
+ next if File.directory?(file)
39
+
40
+ require_relative file # not actually relative but require_relative is faster
41
+ end
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
1
2
  module Chefstyle
2
- VERSION = "1.1.2".freeze
3
- RUBOCOP_VERSION = "0.86.0".freeze
4
- end
3
+ VERSION = "1.4.0"
4
+ RUBOCOP_VERSION = "0.91.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ module RuboCop
3
+ # RuboCop Chef project namespace
4
+ module Chef
5
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
6
+ CONFIG_DEFAULT = PROJECT_ROOT.join("config", "chefstyle.yml").freeze
7
+ CONFIG = YAML.load(CONFIG_DEFAULT.read).freeze
8
+
9
+ private_constant(*constants(false))
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module RuboCop
20
+ module Cop
21
+ module Chef
22
+ module ChefRuby
23
+ # Rubygems does not need to be required in a Gemspec. It's already loaded out of the box in Ruby now.
24
+ class GemspecRequireRubygems < Base
25
+ extend RuboCop::Cop::AutoCorrector
26
+ include RangeHelp
27
+
28
+ MSG = "Rubygems does not need to be required in a Gemspec. It's already loaded out of the box in Ruby now."
29
+
30
+ def_node_matcher :require_rubygems?, <<-PATTERN
31
+ (send nil? :require (str "rubygems") )
32
+ PATTERN
33
+
34
+ def on_send(node)
35
+ require_rubygems?(node) do |r|
36
+ node = node.parent if node.parent && node.parent.conditional? # make sure we identify conditionals on the require
37
+ add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
38
+ corrector.remove(range_with_surrounding_space(range: node.loc.expression, side: :left))
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module RuboCop
20
+ module Cop
21
+ module Chef
22
+ module ChefRuby
23
+ # net/https is deprecated and just includes net/http and openssl. We should include those directly instead.
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # require 'net/https'
29
+ #
30
+ # # good
31
+ # require 'net/http'
32
+ # require 'openssl'
33
+ #
34
+ class RequireNetHttps < Base
35
+ extend RuboCop::Cop::AutoCorrector
36
+
37
+ MSG = "net/https is deprecated and just includes net/http and openssl. We should include those directly instead."
38
+
39
+ def_node_matcher :require_net_https?, <<-PATTERN
40
+ (send nil? :require (str "net/https"))
41
+ PATTERN
42
+
43
+ def on_send(node)
44
+ require_net_https?(node) do
45
+ add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
46
+ corrector.replace(node, %Q{require "net/http"\nrequire "openssl"})
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module RuboCop
20
+ module Cop
21
+ module Chef
22
+ module ChefRuby
23
+ # Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings.
24
+ #
25
+ # @example
26
+ #
27
+ # # bad
28
+ # shell_out!('hostnamectl status', { returns: [0, 1] })
29
+ # shell_out('hostnamectl status', { returns: [0, 1] })
30
+ #
31
+ # # good
32
+ # shell_out!('hostnamectl status', returns: [0, 1])
33
+ # shell_out('hostnamectl status', returns: [0, 1])
34
+ #
35
+ class Ruby27KeywordArgumentWarnings < Base
36
+ extend RuboCop::Cop::AutoCorrector
37
+
38
+ MSG = "Pass options to shell_out helpers without the brackets to avoid Ruby 2.7 deprecation warnings."
39
+
40
+ def_node_matcher :positional_shellout?, <<-PATTERN
41
+ (send nil? {:shell_out :shell_out!} ... $(hash ... ))
42
+ PATTERN
43
+
44
+ def on_send(node)
45
+ positional_shellout?(node) do |h|
46
+ next unless h.braces?
47
+
48
+ add_offense(h.loc.expression, message: MSG, severity: :refactor) do |corrector|
49
+ corrector.replace(h.loc.expression, h.loc.expression.source[1..-2])
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # Copyright:: Chef Software, Inc.
4
+ # Author:: Tim Smith (<tsmith@chef.io>)
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module RuboCop
20
+ module Cop
21
+ module Chef
22
+ module ChefRuby
23
+ # Rubygems is VERY slow to require gems even if they've already been loaded. To work around this
24
+ # wrap your require statement with an `if defined?()` check.
25
+ #
26
+ class UnlessDefinedRequire < Base
27
+ extend RuboCop::Cop::AutoCorrector
28
+
29
+ MSG = "Workaround rubygems slow requires by only running require if the class isn't already defined"
30
+
31
+ REQUIRE_TO_CLASS = {
32
+ "addressable/uri" => "Addressable::URI",
33
+ "appscript" => "Appscript",
34
+ "base64" => "Base64",
35
+ "benchmark" => "Benchmark",
36
+ "cgi" => "CGI",
37
+ "chef-utils" => "ChefUtils",
38
+ "chef-utils/dist" => "ChefUtils::Dist",
39
+ "csv" => "CSV",
40
+ "digest" => "Digest",
41
+ "digest/md5" => "Digest::MD5",
42
+ "digest/sha1" => "Digest::SHA1",
43
+ "digest/sha2" => "Digest::SHA2",
44
+ "droplet_kit" => "DropletKit",
45
+ "erb" => "Erb",
46
+ "erubis" => "Erubis",
47
+ "etc" => "Etc",
48
+ "excon" => "Excon",
49
+ "faraday" => "Faraday",
50
+ "ffi_yajl" => "FFI_Yajl",
51
+ "ffi" => "FFI",
52
+ "fileutils" => "FileUtils",
53
+ "find" => "Find.find",
54
+ "forwardable" => "Forwardable",
55
+ "ipaddr" => "IPAddr",
56
+ "json" => "JSON",
57
+ "mime/types" => "MIME::Types",
58
+ "mixlib/archive" => "Mixlib::Archive",
59
+ "mixlib/cli" => "Mixlib::CLI",
60
+ "mixlib/config" => "Mixlib::Config",
61
+ "mixlib/shellout" => "Mixlib::ShellOut",
62
+ "multi_json" => "MultiJson",
63
+ "net/http" => "Net::HTTP",
64
+ "net/ssh" => "Net::SSH",
65
+ "netaddr" => "NetAddr",
66
+ "nokogiri" => "Nokogiri",
67
+ "ohai" => "Ohai::System",
68
+ "open-uri" => "OpenURI",
69
+ "openssl" => "OpenSSL",
70
+ "optparse" => "OptionParser",
71
+ "ostruct" => "OpenStruct",
72
+ "pathname" => "Pathname",
73
+ "pp" => "PP",
74
+ "rack" => "Rack",
75
+ "rbconfig" => "RbConfig",
76
+ "retryable" => "Retryable",
77
+ "rexml/document" => "REXML::Document",
78
+ "rubygems" => "Gem",
79
+ "rubygems/package" => "Gem::Package",
80
+ "securerandom" => "SecureRandom",
81
+ "set" => "Set",
82
+ "shellwords" => "Shellwords",
83
+ "singleton" => "Singleton",
84
+ "socket" => "Socket",
85
+ "sslshake" => "SSLShake",
86
+ "stringio" => "StringIO",
87
+ "tempfile" => "Tempfile",
88
+ "thor" => "Thor",
89
+ "time" => "Time.zone_offset",
90
+ "timeout" => "Timeout",
91
+ "tmpdir" => "Dir.mktmpdir",
92
+ "tomlrb" => "Tomlrb",
93
+ "uri" => "URI",
94
+ "webrick" => "WEBrick",
95
+ "win32/registry" => "Win32::Registry",
96
+ "win32ole" => "WIN32OLE",
97
+ "winrm" => "WinRM::Connection",
98
+ "yaml" => "YAML",
99
+ "yard" => "YARD",
100
+ "zip" => "Zip",
101
+ "zlib" => "Zlib",
102
+ }.freeze
103
+
104
+ def_node_matcher :require?, <<-PATTERN
105
+ (send nil? :require (str $_) )
106
+ PATTERN
107
+
108
+ def on_send(node)
109
+ require?(node) do |r|
110
+ next if node.parent && node.parent.conditional? # catch both if and unless
111
+ next unless REQUIRE_TO_CLASS[r]
112
+
113
+ add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
114
+ corrector.replace(node.loc.expression, "#{node.source} unless defined?(#{REQUIRE_TO_CLASS[r]})")
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module RuboCop
3
+ module Cop
4
+ module Layout
5
+ class RescueEnsureAlignment < Base
6
+ # @TODO remove this monkeypatch after we upgrade from RuboCop 0.91.0
7
+ def begin_end_alignment_style
8
+ # FIXME: Workaround for pending status for `Layout/BeginEndAlignment` cop
9
+ # When RuboCop 1.0 is released, please replace it with the following condition.
10
+ #
11
+ # config.for_cop('Layout/BeginEndAlignment')['Enabled'] &&
12
+ # config.for_cop('Layout/BeginEndAlignment')['EnforcedStyleAlignWith']
13
+ if config.for_all_cops["NewCops"] == "enable" ||
14
+ config.for_cop("Layout/BeginEndAlignment")["Enabled"] &&
15
+ config.for_cop("Layout/BeginEndAlignment")["Enabled"] != "pending"
16
+ config.for_cop("Layout/BeginEndAlignment")["EnforcedStyleAlignWith"]
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,71 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chefstyle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-25 00:00:00.000000000 Z
11
+ date: 2020-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '12.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '12.0'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
13
  - !ruby/object:Gem::Dependency
56
14
  name: rubocop
57
15
  requirement: !ruby/object:Gem::Requirement
58
16
  requirements:
59
17
  - - '='
60
18
  - !ruby/object:Gem::Version
61
- version: 0.86.0
19
+ version: 0.91.0
62
20
  type: :runtime
63
21
  prerelease: false
64
22
  version_requirements: !ruby/object:Gem::Requirement
65
23
  requirements:
66
24
  - - '='
67
25
  - !ruby/object:Gem::Version
68
- version: 0.86.0
26
+ version: 0.91.0
69
27
  description:
70
28
  email:
71
29
  - oss@chef.io
@@ -83,6 +41,12 @@ files:
83
41
  - config/upstream.yml
84
42
  - lib/chefstyle.rb
85
43
  - lib/chefstyle/version.rb
44
+ - lib/rubocop/chef.rb
45
+ - lib/rubocop/cop/chef/ruby/gemspec_require_rubygems.rb
46
+ - lib/rubocop/cop/chef/ruby/require_net_https.rb
47
+ - lib/rubocop/cop/chef/ruby/ruby_27_keyword_argument_warnings.rb
48
+ - lib/rubocop/cop/chef/ruby/unless_defined_require.rb
49
+ - lib/rubocop/monkey_patches/rescue_ensure_alignment.rb
86
50
  homepage: https://github.com/chef/chefstyle
87
51
  licenses:
88
52
  - Apache-2.0