Skip to content

Commit 3f37b69

Browse files
Merge pull request #9052 from joshcooper/remove_env_monkeypatches
(PUP-11348) Use ENV directly on Windows
2 parents 16977d9 + 6fcaef2 commit 3f37b69

File tree

16 files changed

+123
-217
lines changed

16 files changed

+123
-217
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/exec/windows.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def checkexe(command)
4545
end
4646

4747
if resource[:path]
48-
Puppet::Util.withenv( {'PATH' => resource[:path].join(File::PATH_SEPARATOR)}, :windows) do
48+
Puppet::Util.withenv('PATH' => resource[:path].join(File::PATH_SEPARATOR)) do
4949
return if which(exe)
5050
end
5151
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-21
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,20 +182,8 @@ 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
197-
# Restore the global process environment. Can't just assign because this
198-
# is a magic variable, sadly, and doesn't do that™. It is sufficiently
199-
# faster to use the compare-then-set model to avoid excessive work that it
200-
# 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) }
204-
end
185+
# Restore the global process environment.
186+
ENV.replace($old_env)
205187

206188
# Clear all environments
207189
Puppet.lookup(:environments).clear_all

lib/puppet/util.rb

+22-73
Original file line numberDiff line numberDiff line change
@@ -37,106 +37,55 @@ def default_env
3737
def create_erb(content)
3838
ERB.new(content, trim_mode: '-')
3939
end
40-
4140
module_function :create_erb
4241

43-
# @param name [String] The name of the environment variable to retrieve
44-
# @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
45-
# @return [String] Value of the specified environment variable. nil if it does not exist
42+
# @deprecated Use ENV instead
4643
# @api private
4744
def get_env(name, mode = default_env)
48-
if mode == :windows
49-
Puppet::Util::Windows::Process.get_environment_strings.each do |key, value |
50-
if name.casecmp(key) == 0 then
51-
return value
52-
end
53-
end
54-
return nil
55-
else
56-
ENV[name]
57-
end
45+
ENV[name]
5846
end
5947
module_function :get_env
6048

61-
# @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
62-
# @return [Hash] A hashtable of all environment variables
49+
# @deprecated Use ENV instead
6350
# @api private
6451
def get_environment(mode = default_env)
65-
case mode
66-
when :posix
67-
ENV.to_hash
68-
when :windows
69-
Puppet::Util::Windows::Process.get_environment_strings
70-
else
71-
raise _("Unable to retrieve the environment for mode %{mode}") % { mode: mode }
72-
end
52+
ENV.to_hash
7353
end
7454
module_function :get_environment
7555

76-
# Removes all environment variables
77-
# @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
56+
# @deprecated Use ENV instead
7857
# @api private
7958
def clear_environment(mode = default_env)
80-
case mode
81-
when :posix
82-
ENV.clear
83-
when :windows
84-
Puppet::Util::Windows::Process.get_environment_strings.each do |key, _|
85-
Puppet::Util::Windows::Process.set_environment_variable(key, nil)
86-
end
87-
else
88-
raise _("Unable to clear the environment for mode %{mode}") % { mode: mode }
89-
end
59+
ENV.clear
9060
end
9161
module_function :clear_environment
9262

93-
# @param name [String] The name of the environment variable to set
94-
# @param value [String] The value to set the variable to. nil deletes the environment variable
95-
# @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
63+
# @deprecated Use ENV instead
9664
# @api private
9765
def set_env(name, value = nil, mode = default_env)
98-
case mode
99-
when :posix
100-
ENV[name] = value
101-
when :windows
102-
Puppet::Util::Windows::Process.set_environment_variable(name,value)
103-
else
104-
raise _("Unable to set the environment variable %{name} for mode %{mode}") % { name: name, mode: mode }
105-
end
66+
ENV[name] = value
10667
end
10768
module_function :set_env
10869

109-
# @param name [Hash] Environment variables to merge into the existing environment. nil values will remove the variable
110-
# @param mode [Symbol] Which operating system mode to use e.g. :posix or :windows. Use nil to autodetect
70+
# @deprecated Use ENV instead
11171
# @api private
11272
def merge_environment(env_hash, mode = default_env)
113-
case mode
114-
when :posix
115-
env_hash.each { |name, val| ENV[name.to_s] = val }
116-
when :windows
117-
env_hash.each do |name, val|
118-
Puppet::Util::Windows::Process.set_environment_variable(name.to_s, val)
119-
end
120-
else
121-
raise _("Unable to merge given values into the current environment for mode %{mode}") % { mode: mode }
122-
end
73+
ENV.merge!(hash.transform_keys(&:to_s))
12374
end
12475
module_function :merge_environment
12576

12677
# Run some code with a specific environment. Resets the environment back to
12778
# what it was at the end of the code.
128-
# Windows can store Unicode chars in the environment as keys or values, but
129-
# Ruby's ENV tries to roundtrip them through the local codepage, which can
130-
# cause encoding problems - underlying helpers use Windows APIs on Windows
131-
# see https://bugs.ruby-lang.org/issues/8822
79+
#
80+
# @param hash [Hash{String,Symbol => String}] Environment variables to override the current environment.
81+
# @param mode [Symbol] ignored
13282
def withenv(hash, mode = :posix)
133-
saved = get_environment(mode)
134-
merge_environment(hash, mode)
135-
yield
136-
ensure
137-
if saved
138-
clear_environment(mode)
139-
merge_environment(saved, mode)
83+
saved = ENV.to_hash
84+
begin
85+
ENV.merge!(hash.transform_keys(&:to_s))
86+
yield
87+
ensure
88+
ENV.replace(saved)
14089
end
14190
end
14291
module_function :withenv
@@ -258,16 +207,16 @@ def which(bin)
258207
if absolute_path?(bin)
259208
return bin if FileTest.file? bin and FileTest.executable? bin
260209
else
261-
exts = Puppet::Util.get_env('PATHEXT')
210+
exts = ENV['PATHEXT']
262211
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|
212+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
264213
begin
265214
dest = File.expand_path(File.join(dir, bin))
266215
rescue ArgumentError => e
267216
# if the user's PATH contains a literal tilde (~) character and HOME is not set, we may get
268217
# an ArgumentError here. Let's check to see if that is the case; if not, re-raise whatever error
269218
# was thrown.
270-
if e.to_s =~ /HOME/ and (Puppet::Util.get_env('HOME').nil? || Puppet::Util.get_env('HOME') == "")
219+
if e.to_s =~ /HOME/ and (ENV['HOME'].nil? || ENV['HOME'] == "")
271220
# if we get here they have a tilde in their PATH. We'll issue a single warning about this and then
272221
# ignore this path element and carry on with our lives.
273222
#TRANSLATORS PATH and HOME are environment variables and should not be translated

lib/puppet/util/execution.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def self.execute_windows(command, options, stdin, stdout, stderr)
379379
end.join(" ") if command.is_a?(Array)
380380

381381
options[:custom_environment] ||= {}
382-
Puppet::Util.withenv(options[:custom_environment], :windows) do
382+
Puppet::Util.withenv(options[:custom_environment]) do
383383
Puppet::Util::Windows::Process.execute(command, options, stdin, stdout, stderr)
384384
end
385385
end

spec/integration/defaults_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,18 @@
137137

138138
it "path should not add anything" do
139139
path = "c:\\windows\\system32#{File::PATH_SEPARATOR}c:\\windows"
140-
Puppet::Util.withenv( {"PATH" => path }, :windows ) do
140+
Puppet::Util.withenv("PATH" => path) do
141141
Puppet.settings[:path] = "none" # this causes it to ignore the setting
142142
expect(ENV["PATH"]).to eq(path)
143143
end
144144
end
145145

146146
it "path should support UTF8 characters" do
147147
path = "c:\\windows\\system32#{File::PATH_SEPARATOR}c:\\windows#{File::PATH_SEPARATOR}C:\\" + rune_utf8
148-
Puppet::Util.withenv( {"PATH" => path }, :windows) do
148+
Puppet::Util.withenv("PATH" => path) 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/integration/util_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
Puppet::FileSystem.touch(filepath)
9797

9898
path = [utf8, "c:\\windows\\system32", "c:\\windows"].join(File::PATH_SEPARATOR)
99-
Puppet::Util.withenv( { "PATH" => path } , :windows) do
99+
Puppet::Util.withenv("PATH" => path) do
100100
expect(Puppet::Util.which(filename)).to eq(filepath)
101101
end
102102
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

0 commit comments

Comments
 (0)