Skip to content

Commit 19e72dc

Browse files
committed
(CONT-801) Autocorrect safe group 5
1 parent 773e26d commit 19e72dc

File tree

10 files changed

+37
-86
lines changed

10 files changed

+37
-86
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -223,49 +223,12 @@ Style/ClassAndModuleChildren:
223223
Exclude:
224224
- 'lib/puppet_x/stdlib/toml_dumper.rb'
225225

226-
# Offense count: 1
227-
# This cop supports safe autocorrection (--autocorrect).
228-
# Configuration parameters: AllowedMethods, AllowedPatterns.
229-
# AllowedMethods: ==, equal?, eql?
230-
Style/ClassEqualityComparison:
231-
Exclude:
232-
- 'lib/puppet/parser/functions/fqdn_rotate.rb'
233-
234226
# Offense count: 2
235227
# This cop supports unsafe autocorrection (--autocorrect-all).
236228
Style/CollectionCompact:
237229
Exclude:
238230
- 'lib/puppet/functions/to_json_pretty.rb'
239231

240-
# Offense count: 3
241-
# This cop supports safe autocorrection (--autocorrect).
242-
Style/EachForSimpleLoop:
243-
Exclude:
244-
- 'spec/functions/deprecation_spec.rb'
245-
246-
# Offense count: 2
247-
# This cop supports safe autocorrection (--autocorrect).
248-
Style/Encoding:
249-
Exclude:
250-
- 'spec/type_aliases/filemode_spec.rb'
251-
- 'spec/type_aliases/yes_no_spec.rb'
252-
253-
# Offense count: 1
254-
# This cop supports safe autocorrection (--autocorrect).
255-
# Configuration parameters: AllowedVars.
256-
Style/FetchEnvVar:
257-
Exclude:
258-
- 'lib/puppet/parser/functions/time.rb'
259-
260-
# Offense count: 32
261-
# This cop supports safe autocorrection (--autocorrect).
262-
Style/FileWrite:
263-
Exclude:
264-
- 'lib/puppet/provider/file_line/ruby.rb'
265-
- 'spec/unit/puppet/provider/file_line/ruby_spec.rb'
266-
- 'spec/unit/puppet/provider/file_line/ruby_spec_alter.rb'
267-
- 'spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb'
268-
269232
# Offense count: 5
270233
# This cop supports unsafe autocorrection (--autocorrect-all).
271234
# Configuration parameters: EnforcedStyle.

lib/puppet/parser/functions/fqdn_rotate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
if Puppet::Util.respond_to?(:deterministic_rand)
4444
offset = Puppet::Util.deterministic_rand(seed, elements).to_i
4545
else
46-
return offset = Random.new(seed).rand(elements) if defined?(Random) == 'constant' && Random.class == Class
46+
return offset = Random.new(seed).rand(elements) if defined?(Random) == 'constant' && Random.instance_of?(Class)
4747

4848
old_seed = srand(seed)
4949
offset = rand(elements)

lib/puppet/parser/functions/time.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Puppet::Parser::Functions
3535

3636
# There is probably a better way to handle Time Zone ...
3737
if time_zone && !time_zone.empty?
38-
original_zone = ENV['TZ']
38+
original_zone = ENV.fetch('TZ', nil)
3939

4040
local_time = time.clone
4141
local_time = local_time.utc

lib/puppet/provider/file_line/ruby.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,12 @@ def handle_destroy_with_match
164164
end
165165

166166
local_lines = lines
167-
File.open(resource[:path], 'w') do |fh|
168-
fh.write(local_lines.reject { |line| match_regex.match(line) }.join(''))
169-
end
167+
File.write(resource[:path], local_lines.reject { |line| match_regex.match(line) }.join(''))
170168
end
171169

172170
def handle_destroy_line
173171
local_lines = lines
174-
File.open(resource[:path], 'w') do |fh|
175-
fh.write(local_lines.reject { |line| line.chomp == resource[:line] }.join(''))
176-
end
172+
File.write(resource[:path], local_lines.reject { |line| line.chomp == resource[:line] }.join(''))
177173
end
178174

179175
def handle_append_line

spec/functions/deprecation_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@
3434
else
3535
expect(Puppet).to receive(:warning).with(include('heelo')).once
3636
end
37-
(0..1).each do |_i|
37+
2.times do |_i|
3838
expect(subject).to run.with_params('key', 'heelo')
3939
end
4040
end
4141

4242
it 'fails twice with message, with multiple calls. when strict= :error' do
4343
Puppet.settings[:strict] = :error
4444
expect(Puppet).not_to receive(:warning).with(include('heelo'))
45-
(0..1).each do |_i|
45+
2.times do |_i|
4646
expect(subject).to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo})
4747
end
4848
end
4949

5050
it 'displays nothing, despite multiple calls. strict= :off' do
5151
Puppet.settings[:strict] = :off
5252
expect(Puppet).not_to receive(:warning).with(include('heelo'))
53-
(0..1).each do |_i|
53+
2.times do |_i|
5454
expect(subject).to run.with_params('key', 'heelo')
5555
end
5656
end

spec/type_aliases/filemode_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding: utf-8
21
# frozen_string_literal: true
32

43
require 'spec_helper'

spec/type_aliases/yes_no_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# coding: utf-8
21
# frozen_string_literal: true
32

43
require 'spec_helper'

spec/unit/puppet/provider/file_line/ruby_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
end
2929

3030
before :each do
31-
File.open(tmpfile, 'w') do |fh|
32-
fh.write(content)
33-
end
31+
File.write(tmpfile, content)
3432
end
3533

3634
describe 'line parameter' do

spec/unit/puppet/provider/file_line/ruby_spec_alter.rb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
end
2929

3030
before :each do
31-
File.open(tmpfile, 'w') do |fh|
32-
fh.write(content)
33-
end
31+
File.write(tmpfile, content)
3432
end
3533

3634
describe '#create' do
@@ -54,7 +52,7 @@
5452
end
5553

5654
it 'appends the line if no matches are found' do
57-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") }
55+
File.write(tmpfile, "foo1\nfoo2")
5856
expect(provider.exists?).to be false
5957
provider.create
6058
expect(File.read(tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
@@ -84,23 +82,23 @@
8482

8583
describe 'using match' do
8684
it 'raises an error if more than one line matches, and should not have modified the file' do
87-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") }
85+
File.write(tmpfile, "foo1\nfoo=blah\nfoo2\nfoo=baz")
8886
expect { @provider.create }.to raise_error(Puppet::Error, %r{More than one line.*matches})
8987
expect(File.read(tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
9088
end
9189

9290
it 'replaces all lines that matches' do
9391
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo = bar', match: '^foo\s*=.*$', multiple: true)
9492
@provider = provider_class.new(@resource)
95-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") }
93+
File.write(tmpfile, "foo1\nfoo=blah\nfoo2\nfoo=baz")
9694
@provider.create
9795
expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
9896
end
9997

10098
it 'replaces all lines that match, even when some lines are correct' do
10199
@resource = Puppet::Type::File_line.new(name: 'neil', path: tmpfile, line: "\thard\tcore\t0\n", match: '^[ \t]hard[ \t]+core[ \t]+.*', multiple: true)
102100
@provider = provider_class.new(@resource)
103-
File.open(tmpfile, 'w') { |fh| fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") }
101+
File.write(tmpfile, "\thard\tcore\t90\n\thard\tcore\t0\n")
104102
@provider.create
105103
expect(File.read(tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0")
106104
end
@@ -114,19 +112,19 @@
114112
end
115113

116114
it 'replaces a line that matches' do
117-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2") }
115+
File.write(tmpfile, "foo1\nfoo=blah\nfoo2")
118116
@provider.create
119117
expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
120118
end
121119

122120
it 'adds a new line if no lines match' do
123-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") }
121+
File.write(tmpfile, "foo1\nfoo2")
124122
@provider.create
125123
expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n")
126124
end
127125

128126
it 'does nothing if the exact line already exists' do
129-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = bar\nfoo2") }
127+
File.write(tmpfile, "foo1\nfoo = bar\nfoo2")
130128
@provider.create
131129
expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
132130
end
@@ -136,7 +134,7 @@
136134
it 'replaces line' do
137135
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false)
138136
@provider = provider_class.new(@resource)
139-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") }
137+
File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz")
140138
expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
141139
end
142140
end
@@ -145,7 +143,7 @@
145143
it 'does not add line after no matches found' do
146144
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'inserted = line', match: '^foo3$', append_on_no_match: false)
147145
@provider = provider_class.new(@resource)
148-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") }
146+
File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz")
149147
expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
150148
end
151149
end
@@ -181,7 +179,7 @@
181179
end
182180

183181
before :each do
184-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nfoo = baz") }
182+
File.write(tmpfile, "foo1\nfoo2\nfoo = baz")
185183
end
186184

187185
describe 'inserts at match' do
@@ -216,7 +214,7 @@
216214

217215
context 'with one line matching the after expression' do
218216
before :each do
219-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") }
217+
File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo = baz")
220218
end
221219

222220
it 'inserts the specified line after the line matching the "after" expression' do
@@ -227,7 +225,7 @@
227225

228226
context 'with multiple lines matching the after expression' do
229227
before :each do
230-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") }
228+
File.write(tmpfile, "foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz")
231229
end
232230

233231
it 'errors out stating "One or no line must match the pattern"' do
@@ -248,7 +246,7 @@
248246
end
249247

250248
before :each do
251-
File.open(tmpfile, 'w') { |fh| fh.write(content) }
249+
File.write(tmpfile, content)
252250
end
253251

254252
it 'appends the specified line to the file' do
@@ -273,27 +271,27 @@
273271
end
274272

275273
it 'removes the line if it exists' do
276-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
274+
File.write(tmpfile, "foo1\nfoo\nfoo2")
277275
@provider.destroy
278276
expect(File.read(tmpfile)).to eql("foo1\nfoo2")
279277
end
280278

281279
it 'removes the line without touching the last new line' do
282-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\n") }
280+
File.write(tmpfile, "foo1\nfoo\nfoo2\n")
283281
@provider.destroy
284282
expect(File.read(tmpfile)).to eql("foo1\nfoo2\n")
285283
end
286284

287285
it 'removes any occurence of the line' do
288-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") }
286+
File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo")
289287
@provider.destroy
290288
expect(File.read(tmpfile)).to eql("foo1\nfoo2\n")
291289
end
292290

293291
it 'example in the docs' do
294292
@resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, line: 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128')
295293
@provider = provider_class.new(@resource)
296-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") }
294+
File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n")
297295
@provider.destroy
298296
expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
299297
end
@@ -313,57 +311,57 @@
313311
end
314312

315313
it 'finds a line to match' do
316-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
314+
File.write(tmpfile, "foo1\nfoo\nfoo2")
317315
expect(@provider.exists?).to be true
318316
end
319317

320318
it 'removes one line if it matches' do
321-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
319+
File.write(tmpfile, "foo1\nfoo\nfoo2")
322320
@provider.destroy
323321
expect(File.read(tmpfile)).to eql("foo1\nfoo2")
324322
end
325323

326324
it 'the line parameter is actually not used at all but is silently ignored if here' do
327325
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'supercalifragilisticexpialidocious', ensure: 'absent', match: 'o$', match_for_absence: true)
328326
@provider = provider_class.new(@resource)
329-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
327+
File.write(tmpfile, "foo1\nfoo\nfoo2")
330328
@provider.destroy
331329
expect(File.read(tmpfile)).to eql("foo1\nfoo2")
332330
end
333331

334332
it 'and may not be here and does not need to be here' do
335333
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, ensure: 'absent', match: 'o$', match_for_absence: true)
336334
@provider = provider_class.new(@resource)
337-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
335+
File.write(tmpfile, "foo1\nfoo\nfoo2")
338336
@provider.destroy
339337
expect(File.read(tmpfile)).to eql("foo1\nfoo2")
340338
end
341339

342340
it 'raises an error if more than one line matches' do
343-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") }
341+
File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo")
344342
expect { @provider.destroy }.to raise_error(Puppet::Error, %r{More than one line})
345343
end
346344

347345
it 'removes multiple lines if :multiple is true' do
348346
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$', multiple: true, match_for_absence: true)
349347
@provider = provider_class.new(@resource)
350-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") }
348+
File.write(tmpfile, "foo1\nfoo\nfoo2\nfoo\nfoo")
351349
@provider.destroy
352350
expect(File.read(tmpfile)).to eql("foo1\nfoo2\n")
353351
end
354352

355353
it 'ignores the match if match_for_absence is not specified' do
356354
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$')
357355
@provider = provider_class.new(@resource)
358-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
356+
File.write(tmpfile, "foo1\nfoo\nfoo2")
359357
@provider.destroy
360358
expect(File.read(tmpfile)).to eql("foo1\nfoo\n")
361359
end
362360

363361
it 'ignores the match if match_for_absence is false' do
364362
@resource = Puppet::Type::File_line.new(name: 'foo', path: tmpfile, line: 'foo2', ensure: 'absent', match: 'o$', match_for_absence: false)
365363
@provider = provider_class.new(@resource)
366-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") }
364+
File.write(tmpfile, "foo1\nfoo\nfoo2")
367365
@provider.destroy
368366
expect(File.read(tmpfile)).to eql("foo1\nfoo\n")
369367
end
@@ -374,15 +372,15 @@
374372
match: '^export\ HTTP_PROXY\=', match_for_absence: true
375373
)
376374
@provider = provider_class.new(@resource)
377-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") }
375+
File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n")
378376
@provider.destroy
379377
expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
380378
end
381379

382380
it 'example in the docs showing line is redundant' do
383381
@resource = Puppet::Type::File_line.new(name: 'bashrc_proxy', ensure: 'absent', path: tmpfile, match: '^export\ HTTP_PROXY\=', match_for_absence: true)
384382
@provider = provider_class.new(@resource)
385-
File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") }
383+
File.write(tmpfile, "foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n")
386384
@provider.destroy
387385
expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n")
388386
end

spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@
2828
end
2929

3030
before :each do
31-
File.open(tmpfile, 'w') do |fh|
32-
fh.write(content)
33-
end
31+
File.write(tmpfile, content)
3432
end
3533

3634
describe 'customer use cases - no lines' do

0 commit comments

Comments
 (0)