chefstyle 1.1.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
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
@@ -21,9 +22,17 @@ end
21
22
  # Chefstyle patches the RuboCop tool to set a new default configuration that
22
23
  # is vendored in the Chefstyle codebase.
23
24
  module Chefstyle
24
- # @return [String] the absolute path to the main RuboCop configuration YAML
25
- # file
25
+ # @return [String] the absolute path to the main RuboCop configuration YAML file
26
26
  def self.config
27
27
  RuboCop::ConfigLoader::DEFAULT_FILE
28
28
  end
29
29
  end
30
+
31
+ require_relative "rubocop/chef"
32
+
33
+ # Chef custom cops
34
+ Dir.glob(File.dirname(__FILE__) + "/rubocop/cop/chef/**/*.rb") do |file|
35
+ next if File.directory?(file)
36
+
37
+ require_relative file # not actually relative but require_relative is faster
38
+ end
@@ -1,4 +1,5 @@
1
+ # frozen_string_literal: true
1
2
  module Chefstyle
2
- VERSION = "1.1.1".freeze
3
- RUBOCOP_VERSION = "0.85.1".freeze
3
+ VERSION = "1.3.2"
4
+ RUBOCOP_VERSION = "0.90"
4
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,121 @@
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
+ "csv" => "CSV",
39
+ "digest" => "Digest",
40
+ "digest/md5" => "Digest::MD5",
41
+ "digest/sha1" => "Digest::SHA1",
42
+ "digest/sha2" => "Digest::SHA2",
43
+ "droplet_kit" => "DropletKit",
44
+ "erb" => "Erb",
45
+ "erubis" => "Erubis",
46
+ "etc" => "Etc",
47
+ "excon" => "Excon",
48
+ "faraday" => "Faraday",
49
+ "ffi_yajl" => "FFI_Yajl",
50
+ "ffi" => "FFI",
51
+ "fileutils" => "FileUtils",
52
+ "find" => "Find.find",
53
+ "forwardable" => "Forwardable",
54
+ "ipaddr" => "IPAddr",
55
+ "json" => "JSON",
56
+ "mime/types" => "MIME::Types",
57
+ "mixlib/archive" => "Mixlib::Archive",
58
+ "mixlib/cli" => "Mixlib::CLI",
59
+ "mixlib/config" => "Mixlib::Config",
60
+ "mixlib/shellout" => "Mixlib::ShellOut",
61
+ "multi_json" => "MultiJson",
62
+ "net/http" => "Net::HTTP",
63
+ "net/ssh" => "Net::SSH",
64
+ "netaddr" => "NetAddr",
65
+ "nokogiri" => "Nokogiri",
66
+ "ohai" => "Ohai::System",
67
+ "open-uri" => "OpenURI",
68
+ "openssl" => "OpenSSL",
69
+ "optparse" => "OptionParser",
70
+ "ostruct" => "OpenStruct",
71
+ "pathname" => "Pathname",
72
+ "pp" => "PP",
73
+ "rack" => "Rack",
74
+ "rbconfig" => "RbConfig",
75
+ "retryable" => "Retryable",
76
+ "rexml/document" => "REXML::Document",
77
+ "rubygems" => "Gem",
78
+ "rubygems/package" => "Gem::Package",
79
+ "securerandom" => "SecureRandom",
80
+ "set" => "Set",
81
+ "shellwords" => "Shellwords",
82
+ "singleton" => "Singleton",
83
+ "socket" => "Socket",
84
+ "sslshake" => "SSLShake",
85
+ "stringio" => "StringIO",
86
+ "tempfile" => "Tempfile",
87
+ "thor" => "Thor",
88
+ "time" => "Time.zone_offset",
89
+ "timeout" => "Timeout",
90
+ "tmpdir" => "Dir.mktmpdir",
91
+ "tomlrb" => "Tomlrb",
92
+ "uri" => "URI",
93
+ "webrick" => "WEBrick",
94
+ "win32/registry" => "Win32::Registry",
95
+ "win32ole" => "WIN32OLE",
96
+ "winrm" => "WinRM::Connection",
97
+ "yaml" => "YAML",
98
+ "yard" => "YARD",
99
+ "zip" => "Zip",
100
+ "zlib" => "Zlib",
101
+ }.freeze
102
+
103
+ def_node_matcher :require?, <<-PATTERN
104
+ (send nil? :require (str $_) )
105
+ PATTERN
106
+
107
+ def on_send(node)
108
+ require?(node) do |r|
109
+ next if node.parent && node.parent.conditional? # catch both if and unless
110
+ next unless REQUIRE_TO_CLASS[r]
111
+
112
+ add_offense(node.loc.expression, message: MSG, severity: :refactor) do |corrector|
113
+ corrector.replace(node.loc.expression, "#{node.source} unless defined?(#{REQUIRE_TO_CLASS[r]})")
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ 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.1
4
+ version: 1.3.2
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-08 00:00:00.000000000 Z
11
+ date: 2020-09-01 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.85.1
19
+ version: '0.90'
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.85.1
26
+ version: '0.90'
69
27
  description:
70
28
  email:
71
29
  - oss@chef.io
@@ -83,6 +41,11 @@ 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
86
49
  homepage: https://github.com/chef/chefstyle
87
50
  licenses:
88
51
  - Apache-2.0