Skip to content

Commit 5021c97

Browse files
committed
(PUP-11348) Call regular ENV methods
Don't call deprecated environment methods in Puppet::Util
1 parent 4c879fc commit 5021c97

File tree

12 files changed

+41
-54
lines changed

12 files changed

+41
-54
lines changed

lib/puppet/defaults.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,12 @@ def self.initialize_default_settings!(settings)
369369
be set in `[server]`, `[agent]`, or an environment config section.",
370370
:call_hook => :on_define_and_write,
371371
:hook => proc do |value|
372-
Puppet::Util.set_env('PATH', '') if Puppet::Util.get_env('PATH').nil?
373-
Puppet::Util.set_env('PATH', value) unless value == 'none'
374-
paths = Puppet::Util.get_env('PATH').split(File::PATH_SEPARATOR)
372+
ENV['PATH'] = '' if ENV['PATH'].nil?
373+
ENV['PATH'] = value unless value == 'none'
374+
paths = ENV['PATH'].split(File::PATH_SEPARATOR)
375375
Puppet::Util::Platform.default_paths.each do |path|
376-
Puppet::Util.set_env('PATH', Puppet::Util.get_env('PATH') + File::PATH_SEPARATOR + path) unless paths.include?(path)
376+
next if paths.include?(path)
377+
ENV['PATH'] = ENV['PATH'] + File::PATH_SEPARATOR + path
377378
end
378379
value
379380
end

lib/puppet/file_system/uniquefile.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def try_convert_to_hash(h)
150150

151151
def tmpdir
152152
tmp = '.'
153-
for dir in [ Puppet::Util.get_env('TMPDIR'), Puppet::Util.get_env('TMP'), Puppet::Util.get_env('TEMP'), @@systmpdir, '/tmp']
153+
for dir in [ ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp']
154154
stat = File.stat(dir) if dir
155155
if stat && stat.directory? && stat.writable?
156156
tmp = dir

lib/puppet/node/environment.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,8 @@ def hash
549549

550550
# not private so it can be called in tests
551551
def self.extralibs()
552-
if Puppet::Util.get_env('PUPPETLIB')
553-
split_path(Puppet::Util.get_env('PUPPETLIB'))
552+
if ENV['PUPPETLIB']
553+
split_path(ENV['PUPPETLIB'])
554554
else
555555
[]
556556
end

lib/puppet/provider/package/gem.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ def self.provider_command
5555
# that contains the content of PATH but without puppet/bin dir.
5656
# This is used to pass a custom PATH and execute commands in a controlled environment
5757
def self.windows_path_without_puppet_bin
58-
@path ||= Puppet::Util.get_env('PATH').split(File::PATH_SEPARATOR)
59-
.reject { |dir| dir =~ /puppet\\bin$/ }
60-
.join(File::PATH_SEPARATOR)
58+
@path ||= ENV['PATH'].split(File::PATH_SEPARATOR)
59+
.reject { |dir| dir =~ /puppet\\bin$/ }
60+
.join(File::PATH_SEPARATOR)
6161
end
6262

6363
private_class_method :windows_path_without_puppet_bin
@@ -73,7 +73,7 @@ def self.execute_gem_command(command, command_options, custom_environment = {})
7373
validate_command(command)
7474
cmd = [command] << command_options
7575

76-
custom_environment = {'HOME'=>Puppet::Util.get_env('HOME')}.merge(custom_environment)
76+
custom_environment = {'HOME'=>ENV['HOME']}.merge(custom_environment)
7777

7878
if Puppet::Util::Platform.windows?
7979
custom_environment[:PATH] = windows_path_without_puppet_bin

lib/puppet/provider/package/puppet_gem.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
confine :true => Puppet.runtime[:facter].value(:aio_agent_version)
88

99
def self.windows_gemcmd
10-
puppet_dir = Puppet::Util.get_env('PUPPET_DIR')
10+
puppet_dir = ENV['PUPPET_DIR']
1111
if puppet_dir
12-
File.join(Puppet::Util.get_env('PUPPET_DIR').to_s, 'bin', 'gem.bat')
12+
File.join(ENV['PUPPET_DIR'].to_s, 'bin', 'gem.bat')
1313
else
1414
File.join(Gem.default_bindir, 'gem.bat')
1515
end

lib/puppet/test/test_helper.rb

+3-16
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ def self.initialize()
6969
# @return nil
7070
def self.before_all_tests()
7171
# The process environment is a shared, persistent resource.
72-
# Can't use Puppet.features.microsoft_windows? as it may be mocked out in a test. This can cause test recurring test failures
73-
if (!!File::ALT_SEPARATOR)
74-
mode = :windows
75-
else
76-
mode = :posix
77-
end
78-
$old_env = Puppet::Util.get_environment(mode)
72+
$old_env = ENV.to_hash
7973
end
8074

8175
# Call this method once, at the end of a test run, when no more tests
@@ -188,19 +182,12 @@ def self.after_each_test()
188182
end
189183
$saved_indirection_state = nil
190184

191-
# Can't use Puppet.features.microsoft_windows? as it may be mocked out in a test. This can cause test recurring test failures
192-
if (!!File::ALT_SEPARATOR)
193-
mode = :windows
194-
else
195-
mode = :posix
196-
end
197185
# Restore the global process environment. Can't just assign because this
198186
# is a magic variable, sadly, and doesn't do that™. It is sufficiently
199187
# faster to use the compare-then-set model to avoid excessive work that it
200188
# justifies the complexity. --daniel 2012-03-15
201-
unless Puppet::Util.get_environment(mode) == $old_env
202-
Puppet::Util.clear_environment(mode)
203-
$old_env.each {|k, v| Puppet::Util.set_env(k, v, mode) }
189+
if ENV.to_hash != $old_env
190+
ENV.replace($old_env)
204191
end
205192

206193
# Clear all environments

lib/puppet/util.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def default_env
3737
def create_erb(content)
3838
ERB.new(content, trim_mode: '-')
3939
end
40-
4140
module_function :create_erb
4241

4342
# @param name [String] The name of the environment variable to retrieve
@@ -258,16 +257,16 @@ def which(bin)
258257
if absolute_path?(bin)
259258
return bin if FileTest.file? bin and FileTest.executable? bin
260259
else
261-
exts = Puppet::Util.get_env('PATHEXT')
260+
exts = ENV['PATHEXT']
262261
exts = exts ? exts.split(File::PATH_SEPARATOR) : %w[.COM .EXE .BAT .CMD]
263-
Puppet::Util.get_env('PATH').split(File::PATH_SEPARATOR).each do |dir|
262+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
264263
begin
265264
dest = File.expand_path(File.join(dir, bin))
266265
rescue ArgumentError => e
267266
# if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
268267
# an ArgumentError here. Let's check to see if that is the case; if not, re-raise whatever error
269268
# was thrown.
270-
if e.to_s =~ /HOME/ and (Puppet::Util.get_env('HOME').nil? || Puppet::Util.get_env('HOME') == "")
269+
if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV['HOME'] == "")
271270
# if we get here they have a tilde in their PATH. We'll issue a single warning about this and then
272271
# ignore this path element and carry on with our lives.
273272
#TRANSLATORS PATH and HOME are environment variables and should not be translated

spec/integration/defaults_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
Puppet::Util.withenv( {"PATH" => path }, :windows) do
149149
Puppet.settings[:path] = "none" # this causes it to ignore the setting
150150

151-
expect(Puppet::Util.get_env('Path')).to eq(path)
151+
expect(ENV['Path']).to eq(path)
152152
end
153153
end
154154
end

spec/unit/provider/package/gem_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151

5252
before do
5353
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
54-
allow(Puppet::Util).to receive(:get_env)
55-
allow(Puppet::Util).to receive(:get_env).with('PATH').and_return(path)
54+
allow(ENV).to receive(:[]).and_call_original
55+
allow(ENV).to receive(:[]).with('PATH').and_return(path)
5656
allow(described_class).to receive(:validate_command).with(provider_gem_cmd)
5757
stub_const('::File::PATH_SEPARATOR', ';')
5858
end

spec/unit/provider/package/puppet_gem_spec.rb

+4-4
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(Puppet::Util).to receive(:get_env).and_call_original
39-
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').and_return(nil)
38+
allow(ENV).to receive(:[]).and_call_original
39+
allow(ENV).to receive(:[]).with('PUPPET_DIR').and_return(nil)
4040
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
4141
end
4242

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

4949
context 'when PUPPET_DIR is set' do
5050
before do
51-
allow(Puppet::Util).to receive(:get_env).and_call_original
52-
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').and_return('puppet_dir')
51+
allow(ENV).to receive(:[]).and_call_original
52+
allow(ENV).to receive(:[]).with('PUPPET_DIR').and_return('puppet_dir')
5353
end
5454

5555
it 'uses Gem.default_bindir' do

spec/unit/test/test_helper_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
context "#after_each_test" do
55
it "restores the original environment" do
66
varname = 'test_helper_spec-test_variable'
7-
Puppet::Util.set_env(varname, "\u16A0")
7+
ENV[varname] = "\u16A0"
88

9-
expect(Puppet::Util.get_env(varname)).to eq("\u16A0")
9+
expect(ENV[varname]).to eq("\u16A0")
1010

1111
# Prematurely trigger the after_each_test method
1212
Puppet::Test::TestHelper.after_each_test
1313

14-
expect(Puppet::Util::get_env(varname)).to be_nil
14+
expect(ENV[varname]).to be_nil
1515
end
1616
end
17-
end
17+
end

spec/unit/util_spec.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ def withenv_utf8(&block)
676676
end
677677

678678
it "should walk the search PATH returning the first executable" do
679-
allow(Puppet::Util).to receive(:get_env).with('PATH').and_return(File.expand_path('/bin'))
680-
allow(Puppet::Util).to receive(:get_env).with('PATHEXT').and_return(nil)
679+
allow(ENV).to receive(:[]).with('PATH').and_return(File.expand_path('/bin'))
680+
allow(ENV).to receive(:[]).with('PATHEXT').and_return(nil)
681681

682682
expect(Puppet::Util.which('foo')).to eq(path)
683683
end
@@ -693,8 +693,8 @@ def withenv_utf8(&block)
693693

694694
describe "when a file extension is specified" do
695695
it "should walk each directory in PATH ignoring PATHEXT" do
696-
allow(Puppet::Util).to receive(:get_env).with('PATH').and_return(%w[/bar /bin].map{|dir| File.expand_path(dir)}.join(File::PATH_SEPARATOR))
697-
allow(Puppet::Util).to receive(:get_env).with('PATHEXT').and_return('.FOOBAR')
696+
allow(ENV).to receive(:[]).with('PATH').and_return(%w[/bar /bin].map{|dir| File.expand_path(dir)}.join(File::PATH_SEPARATOR))
697+
allow(ENV).to receive(:[]).with('PATHEXT').and_return('.FOOBAR')
698698

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

@@ -705,8 +705,8 @@ def withenv_utf8(&block)
705705
describe "when a file extension is not specified" do
706706
it "should walk each extension in PATHEXT until an executable is found" do
707707
bar = File.expand_path('/bar')
708-
allow(Puppet::Util).to receive(:get_env).with('PATH').and_return("#{bar}#{File::PATH_SEPARATOR}#{base}")
709-
allow(Puppet::Util).to receive(:get_env).with('PATHEXT').and_return(".EXE#{File::PATH_SEPARATOR}.CMD")
708+
allow(ENV).to receive(:[]).with('PATH').and_return("#{bar}#{File::PATH_SEPARATOR}#{base}")
709+
allow(ENV).to receive(:[]).with('PATHEXT').and_return(".EXE#{File::PATH_SEPARATOR}.CMD")
710710

711711
expect(FileTest).to receive(:file?).ordered().with(File.join(bar, 'foo.EXE')).and_return(false)
712712
expect(FileTest).to receive(:file?).ordered().with(File.join(bar, 'foo.CMD')).and_return(false)
@@ -717,8 +717,8 @@ def withenv_utf8(&block)
717717
end
718718

719719
it "should walk the default extension path if the environment variable is not defined" do
720-
allow(Puppet::Util).to receive(:get_env).with('PATH').and_return(base)
721-
allow(Puppet::Util).to receive(:get_env).with('PATHEXT').and_return(nil)
720+
allow(ENV).to receive(:[]).with('PATH').and_return(base)
721+
allow(ENV).to receive(:[]).with('PATHEXT').and_return(nil)
722722

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

731731
it "should fall back if no extension matches" do
732-
allow(Puppet::Util).to receive(:get_env).with('PATH').and_return(base)
733-
allow(Puppet::Util).to receive(:get_env).with('PATHEXT').and_return(".EXE")
732+
allow(ENV).to receive(:[]).with('PATH').and_return(base)
733+
allow(ENV).to receive(:[]).with('PATHEXT').and_return(".EXE")
734734

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

0 commit comments

Comments
 (0)