Skip to content

Commit d18bc75

Browse files
(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.
1 parent 3192f4a commit d18bc75

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,7 @@ RSpec/FactoryBot/SyntaxMethods: # new in 2.7
199199

200200
RSpec/Rails/AvoidSetupHook: # new in 2.4
201201
Enabled: true
202+
203+
Style/AutoResourceCleanup:
204+
Enabled: true
205+

lib/puppet/util/at_fork/solaris.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def raise_if_error(&block)
6464

6565
def activate_new_contract_template
6666
begin
67-
tmpl = File.open(CTFS_PR_TEMPLATE, File::RDWR)
67+
tmpl = File.new(CTFS_PR_TEMPLATE, File::RDWR)
6868

6969
begin
7070
tmpl_fd = tmpl.fileno
@@ -109,7 +109,7 @@ def deactivate_contract_template(parent)
109109

110110
def get_latest_child_contract_id
111111
begin
112-
stat = File.open(CTFS_PR_LATEST, File::RDONLY)
112+
stat = File.new(CTFS_PR_LATEST, File::RDONLY)
113113

114114
begin
115115
stathdl = Fiddle::Pointer.new(0)
@@ -133,7 +133,7 @@ def abandon_latest_child_contract
133133
return if ctid.nil?
134134

135135
begin
136-
ctl = File.open(File.join(CTFS_PR_ROOT, ctid.to_s, %q(ctl)), File::WRONLY)
136+
ctl = File.new(File.join(CTFS_PR_ROOT, ctid.to_s, %q(ctl)), File::WRONLY)
137137

138138
begin
139139
raise_if_error { ct_ctl_abandon(ctl.fileno) }

lib/puppet/util/log/destinations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def initialize(path)
9191
end
9292
end
9393

94-
file = File.open(path, File::WRONLY | File::CREAT | File::APPEND)
94+
file = File.new(path, File::WRONLY | File::CREAT | File::APPEND)
9595
file.puts('[') if need_array_start
9696

9797
@file = file

lib/puppet/util/selinux.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def read_mounts
242242
if File.method_defined? "read_nonblock"
243243
# If possible we use read_nonblock in a loop rather than read to work-
244244
# a linux kernel bug. See ticket #1963 for details.
245-
mountfh = File.open("/proc/mounts")
245+
mountfh = File.new("/proc/mounts")
246246
loop do
247247
mounts += mountfh.read_nonblock(1024)
248248
end

spec/unit/util/selinux_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
end
3030

3131
it "should return nil if /proc/mounts does not exist" do
32-
allow(File).to receive(:open).with("/proc/mounts").and_raise("No such file or directory - /proc/mounts")
32+
allow(File).to receive(:new).with("/proc/mounts").and_raise("No such file or directory - /proc/mounts")
3333
expect(read_mounts).to eq(nil)
3434
end
3535
end
3636

3737
describe "read_mounts" do
3838
before :each do
3939
fh = double('fh', :close => nil)
40-
allow(File).to receive(:open).and_call_original()
41-
allow(File).to receive(:open).with("/proc/mounts").and_return(fh)
40+
allow(File).to receive(:new).and_call_original()
41+
allow(File).to receive(:new).with("/proc/mounts").and_return(fh)
4242
times_fh_called = 0
4343
expect(fh).to receive(:read_nonblock) do
4444
times_fh_called += 1
@@ -304,7 +304,7 @@
304304
describe "set_selinux_context" do
305305
before :each do
306306
fh = double('fh', :close => nil)
307-
allow(File).to receive(:open).with("/proc/mounts").and_return(fh)
307+
allow(File).to receive(:new).with("/proc/mounts").and_return(fh)
308308
times_fh_called = 0
309309
allow(fh).to receive(:read_nonblock) do
310310
times_fh_called += 1

0 commit comments

Comments
 (0)