Skip to content

Commit

Permalink
(PUP-7130) Fix problem with File#expand_path with tilde and ENV['HOME']
Browse files Browse the repository at this point in the history
Setting `ENV['HOME'] = nil` was used to provoke an exception when using
a tilde character in `expand_path`. In 2.4.0, the expansion works OK
even after assigning nil.

NOTE! The exception can be provoked in an acceptance test if HOME set
to empty before the Ruby process is started:
```
$ export HOME=
$ irb
2.4.0 :001 > File.expand_path('~/tmp')
ArgumentError: non-absolute home
```
  • Loading branch information
thallgren committed Feb 23, 2017
1 parent 7619782 commit f4f5ccb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 12 additions & 5 deletions spec/unit/util/run_mode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
require 'spec_helper'

describe Puppet::Util::RunMode do

# Discriminator for tests that attempts to unset HOME since that, for reasons currently unknown,
# doesn't work in Ruby >= 2.4.0
def self.gte_ruby_2_4
@gte_ruby_2_4 ||= SemanticPuppet::Version.parse(RUBY_VERSION) >= SemanticPuppet::Version.parse('2.4.0')
end

before do
@run_mode = Puppet::Util::RunMode.new('fake')
end
Expand Down Expand Up @@ -29,7 +36,7 @@
end
end

it "fails when asking for the conf_dir as non-root and there is no $HOME" do
it "fails when asking for the conf_dir as non-root and there is no $HOME", :unless => gte_ruby_2_4 || Puppet.features.microsoft_windows? do
as_non_root do
without_home do
expect { @run_mode.conf_dir }.to raise_error ArgumentError, /couldn't find HOME/
Expand Down Expand Up @@ -57,7 +64,7 @@
end
end

it "fails when asking for the code_dir as non-root and there is no $HOME" do
it "fails when asking for the code_dir as non-root and there is no $HOME", :unless => gte_ruby_2_4 || Puppet.features.microsoft_windows? do
as_non_root do
without_home do
expect { @run_mode.code_dir }.to raise_error ArgumentError, /couldn't find HOME/
Expand All @@ -75,7 +82,7 @@
as_non_root { expect(@run_mode.var_dir).to eq(File.expand_path('~/.puppetlabs/opt/puppet/cache')) }
end

it "fails when asking for the var_dir as non-root and there is no $HOME" do
it "fails when asking for the var_dir as non-root and there is no $HOME", :unless => gte_ruby_2_4 || Puppet.features.microsoft_windows? do
as_non_root do
without_home do
expect { @run_mode.var_dir }.to raise_error ArgumentError, /couldn't find HOME/
Expand All @@ -96,7 +103,7 @@
as_non_root { expect(@run_mode.log_dir).to eq(File.expand_path('~/.puppetlabs/var/log')) }
end

it "fails when asking for the log_dir and there is no $HOME" do
it "fails when asking for the log_dir and there is no $HOME", :unless => gte_ruby_2_4 || Puppet.features.microsoft_windows? do
as_non_root do
without_home do
expect { @run_mode.log_dir }.to raise_error ArgumentError, /couldn't find HOME/
Expand All @@ -118,7 +125,7 @@
as_non_root { expect(@run_mode.run_dir).to eq(File.expand_path('~/.puppetlabs/var/run')) }
end

it "fails when asking for the run_dir and there is no $HOME" do
it "fails when asking for the run_dir and there is no $HOME", :unless => gte_ruby_2_4 || Puppet.features.microsoft_windows? do
as_non_root do
without_home do
expect { @run_mode.run_dir }.to raise_error ArgumentError, /couldn't find HOME/
Expand Down
8 changes: 7 additions & 1 deletion spec/unit/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
describe Puppet::Util do
include PuppetSpec::Files

# Discriminator for tests that attempts to unset HOME since that, for reasons currently unknown,
# doesn't work in Ruby >= 2.4.0
def self.gte_ruby_2_4
@gte_ruby_2_4 ||= SemanticPuppet::Version.parse(RUBY_VERSION) >= SemanticPuppet::Version.parse('2.4.0')
end

if Puppet.features.microsoft_windows?
def set_mode(mode, file)
Puppet::Util::Windows::Security.set_mode(mode, file)
Expand Down Expand Up @@ -474,7 +480,7 @@ def withenv_utf8(&block)
expect(Puppet::Util.which('doesnotexist')).to be_nil
end

it "should warn if the user's HOME is not set but their PATH contains a ~" do
it "should warn if the user's HOME is not set but their PATH contains a ~", :unless => gte_ruby_2_4 do
env_path = %w[~/bin /usr/bin /bin].join(File::PATH_SEPARATOR)

env = {:HOME => nil, :PATH => env_path}
Expand Down

0 comments on commit f4f5ccb

Please sign in to comment.