Skip to content

Commit 121ed91

Browse files
(PUP-11767) Enable Style/FetchEnvVar
1 parent 8988a6c commit 121ed91

File tree

18 files changed

+41
-39
lines changed

18 files changed

+41
-39
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,6 @@ Style/EvenOdd:
268268

269269
Style/ExpandPathArguments:
270270
Enabled: true
271+
272+
Style/FetchEnvVar:
273+
Enabled: true

ext/windows/service/daemon.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WindowsDaemon < Puppet::Util::Windows::Daemon
1818
@run_thread = nil
1919
@LOG_TO_FILE = false
2020
@loglevel = 0
21-
LOG_FILE = File.expand_path(File.join(ENV['ALLUSERSPROFILE'], 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log'))
21+
LOG_FILE = File.expand_path(File.join(ENV.fetch('ALLUSERSPROFILE', nil), 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log'))
2222
LEVELS = [:debug, :info, :notice, :warning, :err, :alert, :emerg, :crit]
2323
LEVELS.each do |level|
2424
define_method("log_#{level}") do |msg|
@@ -203,10 +203,10 @@ def load_env(base_dir)
203203
ENV['Path'] = [
204204
File.join(base_dir, 'puppet', 'bin'),
205205
File.join(base_dir, 'bin'),
206-
].join(';').tr('/', '\\') + ';' + ENV['Path']
206+
].join(';').tr('/', '\\') + ';' + ENV.fetch('Path', nil)
207207

208208
# ENV that uses forward slashes
209-
ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV['RUBYLIB']}"
209+
ENV['RUBYLIB'] = "#{File.join(base_dir, 'puppet', 'lib')};#{ENV.fetch('RUBYLIB', nil)}"
210210
rescue => e
211211
log_exception(e)
212212
end

lib/puppet/application/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def handle_editing(text)
188188
tmpfile.puts text
189189

190190
# edit the content
191-
system(ENV["EDITOR"] || 'vi', tmpfile.path)
191+
system(ENV.fetch("EDITOR", nil) || 'vi', tmpfile.path)
192192

193193
# ...and, now, pass that file to puppet to apply. Because
194194
# many editors rename or replace the original file we need to

lib/puppet/defaults.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def self.default_cadir
4949
def self.default_basemodulepath
5050
if Puppet::Util::Platform.windows?
5151
path = ['$codedir/modules']
52-
installdir = ENV["FACTER_env_windows_installdir"]
52+
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
5353
if installdir
5454
path << "#{installdir}/puppet/modules"
5555
end
@@ -61,7 +61,7 @@ def self.default_basemodulepath
6161

6262
def self.default_vendormoduledir
6363
if Puppet::Util::Platform.windows?
64-
installdir = ENV["FACTER_env_windows_installdir"]
64+
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
6565
if installdir
6666
"#{installdir}\\puppet\\vendor_modules"
6767
else
@@ -373,7 +373,7 @@ def self.initialize_default_settings!(settings)
373373
Puppet::Util::Platform.default_paths.each do |path|
374374
next if paths.include?(path)
375375

376-
ENV['PATH'] = ENV['PATH'] + File::PATH_SEPARATOR + path
376+
ENV['PATH'] = ENV.fetch('PATH', nil) + File::PATH_SEPARATOR + path
377377
end
378378
value
379379
end

lib/puppet/file_system/uniquefile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def try_convert_to_hash(h)
153153

154154
def tmpdir
155155
tmp = '.'
156-
for dir in [ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp']
156+
for dir in [ENV.fetch('TMPDIR', nil), ENV.fetch('TMP', nil), ENV.fetch('TEMP', nil), @@systmpdir, '/tmp']
157157
stat = File.stat(dir) if dir
158158
if stat && stat.directory? && stat.writable?
159159
tmp = dir

lib/puppet/http/proxy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def self.proxy(uri)
1818

1919
def self.http_proxy_env
2020
# Returns a URI object if proxy is set, or nil
21-
proxy_env = ENV["http_proxy"] || ENV["HTTP_PROXY"]
21+
proxy_env = ENV.fetch("http_proxy", nil) || ENV.fetch("HTTP_PROXY", nil)
2222
begin
2323
return URI.parse(proxy_env) if proxy_env
2424
rescue URI::InvalidURIError
@@ -124,7 +124,7 @@ def self.http_proxy_password
124124
end
125125

126126
def self.no_proxy
127-
no_proxy_env = ENV["no_proxy"] || ENV["NO_PROXY"]
127+
no_proxy_env = ENV.fetch("no_proxy", nil) || ENV.fetch("NO_PROXY", nil)
128128

129129
if no_proxy_env
130130
return no_proxy_env

lib/puppet/node/environment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def hash
554554
# not private so it can be called in tests
555555
def self.extralibs
556556
if ENV['PUPPETLIB']
557-
split_path(ENV['PUPPETLIB'])
557+
split_path(ENV.fetch('PUPPETLIB', nil))
558558
else
559559
[]
560560
end

lib/puppet/provider/package/gem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def self.execute_gem_command(command, command_options, custom_environment = {})
7777
validate_command(command)
7878
cmd = [command] << command_options
7979

80-
custom_environment = { 'HOME' => ENV['HOME'] }.merge(custom_environment)
80+
custom_environment = { 'HOME' => ENV.fetch('HOME', nil) }.merge(custom_environment)
8181

8282
if Puppet::Util::Platform.windows?
8383
custom_environment[:PATH] = windows_path_without_puppet_bin

lib/puppet/provider/package/pkgutil.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
confine 'os.family' => :solaris
1313

1414
has_command(:pkguti, pkgutil_bin) do
15-
environment :HOME => ENV['HOME']
15+
environment :HOME => ENV.fetch('HOME', nil)
1616
end
1717

1818
def self.healthcheck

lib/puppet/provider/package/puppet_gem.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
confine :true => Puppet.runtime[:facter].value(:aio_agent_version)
1010

1111
def self.windows_gemcmd
12-
puppet_dir = ENV['PUPPET_DIR']
12+
puppet_dir = ENV.fetch('PUPPET_DIR', nil)
1313
if puppet_dir
14-
File.join(ENV['PUPPET_DIR'].to_s, 'bin', 'gem.bat')
14+
File.join(puppet_dir.to_s, 'bin', 'gem.bat')
1515
else
1616
File.join(Gem.default_bindir, 'gem.bat')
1717
end

lib/puppet/provider/package/puppetserver_gem.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def self.provider_command
3333
# CommandDefiner in provider.rb will set failonfail, combine, and environment.
3434

3535
has_command(:puppetservercmd, '/opt/puppetlabs/bin/puppetserver') do
36-
environment(HOME: ENV['HOME'])
36+
environment(HOME: ENV.fetch('HOME', nil))
3737
end
3838

3939
def self.gemlist(options)

lib/puppet/util.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def create_erb(content)
4444
# @deprecated Use ENV instead
4545
# @api private
4646
def get_env(name, mode = default_env)
47-
ENV[name]
47+
ENV.fetch(name, nil)
4848
end
4949
module_function :get_env
5050

@@ -208,16 +208,16 @@ def which(bin)
208208
if absolute_path?(bin)
209209
return bin if FileTest.file? bin and FileTest.executable? bin
210210
else
211-
exts = ENV['PATHEXT']
211+
exts = ENV.fetch('PATHEXT', nil)
212212
exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
213-
ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
213+
ENV.fetch('PATH').split(File::PATH_SEPARATOR).each do |dir|
214214
begin
215215
dest = File.expand_path(File.join(dir, bin))
216216
rescue ArgumentError => e
217217
# if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
218218
# an ArgumentError here. Let's check to see if that is the case; if not, re-raise whatever error
219219
# was thrown.
220-
if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV['HOME'] == "")
220+
if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV.fetch('HOME', nil) == "")
221221
# if we get here they have a tilde in their PATH. We'll issue a single warning about this and then
222222
# ignore this path element and carry on with our lives.
223223
# TRANSLATORS PATH and HOME are environment variables and should not be translated

lib/puppet/util/run_mode.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def log_dir
117117
private
118118

119119
def windows_common_base(*extra)
120-
[ENV['ALLUSERSPROFILE'], "PuppetLabs"] + extra
120+
[ENV.fetch('ALLUSERSPROFILE', nil), "PuppetLabs"] + extra
121121
end
122122
end
123123
end

lib/puppet/util/windows/process.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def wait_process(handle)
5454
# and since it's read-only, we can't set it. But we can execute a
5555
# a shell that simply returns the desired exit status, which has the
5656
# desired effect.
57-
%x{#{ENV['COMSPEC']} /c exit #{exit_status}}
57+
%x{#{ENV.fetch('COMSPEC', nil)} /c exit #{exit_status}}
5858
end
5959

6060
exit_status

spec/unit/defaults_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' }
102102

103103
it 'includes user and system modules' do
104-
allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(installdir)
104+
allow(ENV).to receive(:fetch).with("FACTER_env_windows_installdir", anything).and_return(installdir)
105105

106106
expect(
107107
Puppet.default_basemodulepath
@@ -129,7 +129,7 @@
129129
let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' }
130130

131131
it 'includes the default vendormoduledir' do
132-
allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(installdir)
132+
allow(ENV).to receive(:fetch).with("FACTER_env_windows_installdir", anything).and_return(installdir)
133133

134134
expect(
135135
Puppet.default_vendormoduledir

spec/unit/provider/package/puppet_gem_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
describe '.windows_gemcmd' do
3636
context 'when PUPPET_DIR is not set' do
3737
before do
38-
allow(ENV).to receive(:[]).and_call_original
39-
allow(ENV).to receive(:[]).with('PUPPET_DIR').and_return(nil)
38+
# allow(ENV).to receive(:fetch, anything).and_call_original
39+
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return(nil)
4040
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
4141
end
4242

@@ -48,8 +48,7 @@
4848

4949
context 'when PUPPET_DIR is set' do
5050
before do
51-
allow(ENV).to receive(:[]).and_call_original
52-
allow(ENV).to receive(:[]).with('PUPPET_DIR').and_return('puppet_dir')
51+
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return('puppet_dir')
5352
end
5453

5554
it 'uses Gem.default_bindir' do

spec/unit/util_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,8 @@ def withenv_utf8(&block)
706706
end
707707

708708
it "should walk the search PATH returning the first executable" do
709-
allow(ENV).to receive(:[]).with('PATH').and_return(File.expand_path('/bin'))
710-
allow(ENV).to receive(:[]).with('PATHEXT').and_return(nil)
709+
allow(ENV).to receive(:fetch).with('PATH').and_return(File.expand_path('/bin'))
710+
allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(nil)
711711

712712
expect(Puppet::Util.which('foo')).to eq(path)
713713
end
@@ -723,8 +723,8 @@ def withenv_utf8(&block)
723723

724724
describe "when a file extension is specified" do
725725
it "should walk each directory in PATH ignoring PATHEXT" do
726-
allow(ENV).to receive(:[]).with('PATH').and_return(%w[/bar /bin].map{|dir| File.expand_path(dir)}.join(File::PATH_SEPARATOR))
727-
allow(ENV).to receive(:[]).with('PATHEXT').and_return('.FOOBAR')
726+
allow(ENV).to receive(:fetch).with('PATH').and_return(%w[/bar /bin].map{|dir| File.expand_path(dir)}.join(File::PATH_SEPARATOR))
727+
allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return('.FOOBAR')
728728

729729
expect(FileTest).to receive(:file?).with(File.join(File.expand_path('/bar'), 'foo.CMD')).and_return(false)
730730

@@ -735,8 +735,8 @@ def withenv_utf8(&block)
735735
describe "when a file extension is not specified" do
736736
it "should walk each extension in PATHEXT until an executable is found" do
737737
bar = File.expand_path('/bar')
738-
allow(ENV).to receive(:[]).with('PATH').and_return("#{bar}#{File::PATH_SEPARATOR}#{base}")
739-
allow(ENV).to receive(:[]).with('PATHEXT').and_return(".EXE#{File::PATH_SEPARATOR}.CMD")
738+
allow(ENV).to receive(:fetch).with('PATH').and_return("#{bar}#{File::PATH_SEPARATOR}#{base}")
739+
allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(".EXE#{File::PATH_SEPARATOR}.CMD")
740740

741741
expect(FileTest).to receive(:file?).ordered().with(File.join(bar, 'foo.EXE')).and_return(false)
742742
expect(FileTest).to receive(:file?).ordered().with(File.join(bar, 'foo.CMD')).and_return(false)
@@ -747,8 +747,8 @@ def withenv_utf8(&block)
747747
end
748748

749749
it "should walk the default extension path if the environment variable is not defined" do
750-
allow(ENV).to receive(:[]).with('PATH').and_return(base)
751-
allow(ENV).to receive(:[]).with('PATHEXT').and_return(nil)
750+
allow(ENV).to receive(:fetch).with('PATH').and_return(base)
751+
allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(nil)
752752

753753
%w[.COM .EXE .BAT].each do |ext|
754754
expect(FileTest).to receive(:file?).ordered().with(File.join(base, "foo#{ext}")).and_return(false)
@@ -759,8 +759,8 @@ def withenv_utf8(&block)
759759
end
760760

761761
it "should fall back if no extension matches" do
762-
allow(ENV).to receive(:[]).with('PATH').and_return(base)
763-
allow(ENV).to receive(:[]).with('PATHEXT').and_return(".EXE")
762+
allow(ENV).to receive(:fetch).with('PATH').and_return(base)
763+
allow(ENV).to receive(:fetch).with('PATHEXT', anything).and_return(".EXE")
764764

765765
allow(FileTest).to receive(:file?).with(File.join(base, 'foo.EXE')).and_return(false)
766766
allow(FileTest).to receive(:file?).with(File.join(base, 'foo')).and_return(true)

util/rspec_grouper

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Parallel
2020

2121
def initialize(group_size)
2222
config = ::RSpec::Core::Configuration.new
23-
options = ::RSpec::Core::ConfigurationOptions.new((ENV['TEST'] || ENV['TESTS'] || 'spec').split(';'))
23+
options = ::RSpec::Core::ConfigurationOptions.new((ENV.fetch('TEST', nil) || ENV.fetch('TESTS', nil) || 'spec').split(';'))
2424
options.configure config
2525

2626
# This will scan and load all spec examples

0 commit comments

Comments
 (0)