From d18bc75250c2f587ed7cfe02402cd5d0e6cc88a2 Mon Sep 17 00:00:00 2001 From: tvpartytonight Date: Fri, 26 Jan 2024 12:41:02 -0800 Subject: [PATCH] (PUP-11767) Changes for rubocop Style/AutoResourceCleanup Prefer usage of `File.new` to `File.open` to differentiate when the file object is expected to persist and stay open and to enable AutoResourceCleanup entirely. --- .rubocop.yml | 4 ++++ lib/puppet/util/at_fork/solaris.rb | 6 +++--- lib/puppet/util/log/destinations.rb | 2 +- lib/puppet/util/selinux.rb | 2 +- spec/unit/util/selinux_spec.rb | 8 ++++---- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 71540019f6c..88b9bd82956 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -199,3 +199,7 @@ RSpec/FactoryBot/SyntaxMethods: # new in 2.7 RSpec/Rails/AvoidSetupHook: # new in 2.4 Enabled: true + +Style/AutoResourceCleanup: + Enabled: true + diff --git a/lib/puppet/util/at_fork/solaris.rb b/lib/puppet/util/at_fork/solaris.rb index c7c24152b0e..5930695dc4f 100644 --- a/lib/puppet/util/at_fork/solaris.rb +++ b/lib/puppet/util/at_fork/solaris.rb @@ -64,7 +64,7 @@ def raise_if_error(&block) def activate_new_contract_template begin - tmpl = File.open(CTFS_PR_TEMPLATE, File::RDWR) + tmpl = File.new(CTFS_PR_TEMPLATE, File::RDWR) begin tmpl_fd = tmpl.fileno @@ -109,7 +109,7 @@ def deactivate_contract_template(parent) def get_latest_child_contract_id begin - stat = File.open(CTFS_PR_LATEST, File::RDONLY) + stat = File.new(CTFS_PR_LATEST, File::RDONLY) begin stathdl = Fiddle::Pointer.new(0) @@ -133,7 +133,7 @@ def abandon_latest_child_contract return if ctid.nil? begin - ctl = File.open(File.join(CTFS_PR_ROOT, ctid.to_s, %q(ctl)), File::WRONLY) + ctl = File.new(File.join(CTFS_PR_ROOT, ctid.to_s, %q(ctl)), File::WRONLY) begin raise_if_error { ct_ctl_abandon(ctl.fileno) } diff --git a/lib/puppet/util/log/destinations.rb b/lib/puppet/util/log/destinations.rb index 9ace2cd9f71..67dd4de93cf 100644 --- a/lib/puppet/util/log/destinations.rb +++ b/lib/puppet/util/log/destinations.rb @@ -91,7 +91,7 @@ def initialize(path) end end - file = File.open(path, File::WRONLY | File::CREAT | File::APPEND) + file = File.new(path, File::WRONLY | File::CREAT | File::APPEND) file.puts('[') if need_array_start @file = file diff --git a/lib/puppet/util/selinux.rb b/lib/puppet/util/selinux.rb index 75a0701f0ba..074be48e8bd 100644 --- a/lib/puppet/util/selinux.rb +++ b/lib/puppet/util/selinux.rb @@ -242,7 +242,7 @@ def read_mounts if File.method_defined? "read_nonblock" # If possible we use read_nonblock in a loop rather than read to work- # a linux kernel bug. See ticket #1963 for details. - mountfh = File.open("/proc/mounts") + mountfh = File.new("/proc/mounts") loop do mounts += mountfh.read_nonblock(1024) end diff --git a/spec/unit/util/selinux_spec.rb b/spec/unit/util/selinux_spec.rb index ab6547e1d5d..21c1fea4801 100644 --- a/spec/unit/util/selinux_spec.rb +++ b/spec/unit/util/selinux_spec.rb @@ -29,7 +29,7 @@ end it "should return nil if /proc/mounts does not exist" do - allow(File).to receive(:open).with("/proc/mounts").and_raise("No such file or directory - /proc/mounts") + allow(File).to receive(:new).with("/proc/mounts").and_raise("No such file or directory - /proc/mounts") expect(read_mounts).to eq(nil) end end @@ -37,8 +37,8 @@ describe "read_mounts" do before :each do fh = double('fh', :close => nil) - allow(File).to receive(:open).and_call_original() - allow(File).to receive(:open).with("/proc/mounts").and_return(fh) + allow(File).to receive(:new).and_call_original() + allow(File).to receive(:new).with("/proc/mounts").and_return(fh) times_fh_called = 0 expect(fh).to receive(:read_nonblock) do times_fh_called += 1 @@ -304,7 +304,7 @@ describe "set_selinux_context" do before :each do fh = double('fh', :close => nil) - allow(File).to receive(:open).with("/proc/mounts").and_return(fh) + allow(File).to receive(:new).with("/proc/mounts").and_return(fh) times_fh_called = 0 allow(fh).to receive(:read_nonblock) do times_fh_called += 1