Skip to content

Commit

Permalink
(PUP-11767) Changes for rubocop Style/AutoResourceCleanup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tvpartytonight committed Jan 26, 2024
1 parent 3192f4a commit d18bc75
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,7 @@ RSpec/FactoryBot/SyntaxMethods: # new in 2.7

RSpec/Rails/AvoidSetupHook: # new in 2.4
Enabled: true

Style/AutoResourceCleanup:
Enabled: true

6 changes: 3 additions & 3 deletions lib/puppet/util/at_fork/solaris.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/util/log/destinations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/util/selinux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/util/selinux_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
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

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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d18bc75

Please sign in to comment.