summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2021-12-26 18:50:43 -0800
committerTim Smith <tsmith84@gmail.com>2021-12-26 18:50:43 -0800
commitf84a238a1d8d0060d5064eee0b74ed942088d4fd (patch)
treed26039e8502c84a0c2e2c947227bab5f02139c81
parent50a0001bae754c66feb69c84b64cb305a03a34d2 (diff)
downloadchef-f84a238a1d8d0060d5064eee0b74ed942088d4fd.tar.gz
Resolve Performance/RegexpMatch warningsPerformance_RegexpMatch
Do regex checks without creating objects for the results. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--Gemfile2
-rw-r--r--kitchen-tests/cookbooks/end_to_end/recipes/_habitat_service.rb8
-rw-r--r--knife/lib/chef/knife/core/gem_glob_loader.rb2
-rw-r--r--knife/lib/chef/knife/core/subcommand_loader.rb2
-rw-r--r--lib/chef/cookbook_version.rb2
-rw-r--r--lib/chef/provider/ifconfig.rb2
-rw-r--r--lib/chef/resource.rb2
-rw-r--r--lib/chef/resource/habitat/habitat_sup.rb2
-rw-r--r--lib/chef/resource/habitat/habitat_sup_windows.rb2
-rw-r--r--lib/chef/resource/inspec_waiver_file_entry.rb2
-rw-r--r--lib/chef/secret_fetcher/azure_key_vault.rb2
-rw-r--r--spec/support/ruby_installer.rb2
12 files changed, 15 insertions, 15 deletions
diff --git a/Gemfile b/Gemfile
index 4923e3e4a6..affd8adcc0 100644
--- a/Gemfile
+++ b/Gemfile
@@ -29,7 +29,7 @@ group(:omnibus_package, :pry) do
# some work is ongoing? https://github.com/deivid-rodriguez/pry-byebug/issues/343
gem "pry", "= 0.13.0"
# byebug does not install on freebsd on ruby 3.0
- gem "pry-byebug" unless RUBY_PLATFORM =~ /freebsd/i
+ gem "pry-byebug" unless RUBY_PLATFORM.match?(/freebsd/i)
gem "pry-stack_explorer"
end
diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/_habitat_service.rb b/kitchen-tests/cookbooks/end_to_end/recipes/_habitat_service.rb
index d928e01466..294cb982b0 100644
--- a/kitchen-tests/cookbooks/end_to_end/recipes/_habitat_service.rb
+++ b/kitchen-tests/cookbooks/end_to_end/recipes/_habitat_service.rb
@@ -39,7 +39,7 @@ ruby_block "wait-for-nginx-load" do
end
ruby_block "wait-for-nginx-up" do
block do
- raise "nginx not loaded" unless `hab svc status core/nginx`.match(/standalone\s+up\s+up/)
+ raise "nginx not loaded" unless `hab svc status core/nginx`.match?(/standalone\s+up\s+up/)
end
retries 8
retry_delay 10
@@ -78,7 +78,7 @@ end
ruby_block "wait-for-redis-started" do
block do
sleep 10
- raise "redis not started" unless `hab svc status core/redis`.match(/standalone\s+up\s+up/)
+ raise "redis not started" unless `hab svc status core/redis`.match?(/standalone\s+up\s+up/)
end
retries 8
retry_delay 10
@@ -96,7 +96,7 @@ end
ruby_block "wait-for-redis-stopped" do
block do
sleep 10
- raise "redis not stopped" unless `hab svc status core/redis`.match(/standalone\s+down\s+down/)
+ raise "redis not stopped" unless `hab svc status core/redis`.match?(/standalone\s+down\s+down/)
end
retries 8
retry_delay 10
@@ -251,7 +251,7 @@ ruby_block "wait-for-consul-load" do
end
ruby_block "wait-for-consul-startup" do
block do
- raise "consul not started" unless `hab svc status core/consul`.match(/standalone\s+up\s+up/)
+ raise "consul not started" unless `hab svc status core/consul`.match?(/standalone\s+up\s+up/)
end
retries 8
retry_delay 10
diff --git a/knife/lib/chef/knife/core/gem_glob_loader.rb b/knife/lib/chef/knife/core/gem_glob_loader.rb
index d365602cb4..afc1575f5e 100644
--- a/knife/lib/chef/knife/core/gem_glob_loader.rb
+++ b/knife/lib/chef/knife/core/gem_glob_loader.rb
@@ -62,7 +62,7 @@ class Chef
# Exclude knife/chef/version. It's not a knife command, and force-loading
# when we load all of these files will emit constant-already-defined warnings
- next if rel_path =~ version_file_match
+ next if rel_path&.match?(version_file_match)
subcommand_files[rel_path] = file
end
diff --git a/knife/lib/chef/knife/core/subcommand_loader.rb b/knife/lib/chef/knife/core/subcommand_loader.rb
index ca7bfcd008..0f84dbb176 100644
--- a/knife/lib/chef/knife/core/subcommand_loader.rb
+++ b/knife/lib/chef/knife/core/subcommand_loader.rb
@@ -151,7 +151,7 @@ class Chef
rel_path = knife_file[/#{KNIFE_ROOT}#{Regexp.escape(File::SEPARATOR)}(.*)\.rb/, 1]
# Exclude version.rb file for the gem. It's not a knife command, and force-loading it later
# because loaded via in subcommand files generates CLI warnings about its consts already having been defined
- next if knife_file =~ version_file_match
+ next if knife_file&.match?(version_file_match)
subcommand_files[rel_path] = knife_file
end
diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb
index 6e4f13c291..8a616e5e07 100644
--- a/lib/chef/cookbook_version.rb
+++ b/lib/chef/cookbook_version.rb
@@ -598,7 +598,7 @@ class Chef
filename = record[:name]
base_dup_name = File.join(File.dirname(filename), File.basename(filename, File.extname(filename)))
yml_files.each do |other|
- if other[:name] =~ /#{(File.extname(filename) == ".yml") ? "#{base_dup_name}.yaml" : "#{base_dup_name}.yml"}$/
+ if /#{(File.extname(filename) == ".yml") ? "#{base_dup_name}.yaml" : "#{base_dup_name}.yml"}$/.match?(other[:name])
raise Chef::Exceptions::AmbiguousYAMLFile.new("Cookbook #{name}@#{version} contains ambiguous files: #{filename} and #{other[:name]}. Please update the cookbook to remove the incorrect file.")
end
end
diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb
index 63aa126c2e..30fe4c2545 100644
--- a/lib/chef/provider/ifconfig.rb
+++ b/lib/chef/provider/ifconfig.rb
@@ -120,7 +120,7 @@ class Chef
@status = shell_out("ifconfig")
@status.stdout.each_line do |line|
addr_regex = /^((\w|-)+):?(\d*):?\ .+$/
- if line =~ addr_regex
+ if line&.match?(addr_regex)
if line.match(addr_regex).nil?
@int_name = "nil"
elsif line.match(addr_regex)[3] == ""
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 746efb5fd4..dadcb5c51a 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -1096,7 +1096,7 @@ class Chef
rescue NameError => e
# This can happen when attempting to load a provider in a platform-specific
# environment where we have not required the necessary files yet
- raise unless e.message =~ /uninitialized constant/
+ raise unless /uninitialized constant/.match?(e.message)
end
# Define a method to load up this resource's properties with the current
diff --git a/lib/chef/resource/habitat/habitat_sup.rb b/lib/chef/resource/habitat/habitat_sup.rb
index 2fdfcd270f..b1cbe6cbed 100644
--- a/lib/chef/resource/habitat/habitat_sup.rb
+++ b/lib/chef/resource/habitat/habitat_sup.rb
@@ -284,7 +284,7 @@ class Chef
if new_resource.peer
peer_list = []
new_resource.peer.each do |p|
- peer_list << if p !~ /.*:.*/
+ peer_list << if !/.*:.*/.match?(p)
p + ":9632"
else
p
diff --git a/lib/chef/resource/habitat/habitat_sup_windows.rb b/lib/chef/resource/habitat/habitat_sup_windows.rb
index c73e648f3c..1c574025d2 100644
--- a/lib/chef/resource/habitat/habitat_sup_windows.rb
+++ b/lib/chef/resource/habitat/habitat_sup_windows.rb
@@ -15,7 +15,7 @@
# limitations under the License.
#
-require "win32/service" if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+require "win32/service" if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
require_relative "habitat_sup"
class Chef
diff --git a/lib/chef/resource/inspec_waiver_file_entry.rb b/lib/chef/resource/inspec_waiver_file_entry.rb
index 8b8a0b9415..2f3c806266 100644
--- a/lib/chef/resource/inspec_waiver_file_entry.rb
+++ b/lib/chef/resource/inspec_waiver_file_entry.rb
@@ -135,7 +135,7 @@ class Chef
action_class do
def load_waiver_file_to_hash(file_name)
- if file_name =~ %r{(/|C:\\).*(.yaml|.yml)}i
+ if %r{(/|C:\\).*(.yaml|.yml)}i.match?(file_name)
if ::File.exist?(file_name)
hash = ::YAML.load_file(file_name)
if hash == false || hash.nil? || hash == ""
diff --git a/lib/chef/secret_fetcher/azure_key_vault.rb b/lib/chef/secret_fetcher/azure_key_vault.rb
index d9acc7c3d0..c4bdd2b3ce 100644
--- a/lib/chef/secret_fetcher/azure_key_vault.rb
+++ b/lib/chef/secret_fetcher/azure_key_vault.rb
@@ -123,7 +123,7 @@ class Chef
body["access_token"]
when Net::HTTPBadRequest
body = JSON.parse(response.body)
- raise Chef::Exceptions::Secret::Azure::IdentityNotFound if body["error_description"] =~ /identity not found/i
+ raise Chef::Exceptions::Secret::Azure::IdentityNotFound if /identity not found/i.match?(body["error_description"])
else
body = JSON.parse(response.body)
body["access_token"]
diff --git a/spec/support/ruby_installer.rb b/spec/support/ruby_installer.rb
index ae282d3b2d..d03525268f 100644
--- a/spec/support/ruby_installer.rb
+++ b/spec/support/ruby_installer.rb
@@ -48,4 +48,4 @@ rescue LoadError
$stderr.puts "Failed to load ruby_installer. Assuming Ruby Installer is not being used."
end
-add_libarchive_dll_directory if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+add_libarchive_dll_directory if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)