Skip to content

Commit

Permalink
Merge pull request #671 from rodjek/pdk-1337
Browse files Browse the repository at this point in the history
(PDK-1337) Warn and unset any of the legacy *_GEM_VERSION env vars
  • Loading branch information
rodjek authored Jun 3, 2019
2 parents b156261 + 2ac43e4 commit 2cb86ed
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/pdk/cli/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ def update_environment(additional_env)
@environment.merge!(additional_env)
end

def check_for_legacy_env_vars
if ENV['PUPPET_GEM_VERSION']
PDK.logger.warn_once _(
'PUPPET_GEM_VERSION is not supported by PDK. ' \
'Please use the --puppet-version option on your PDK command ' \
'or set the PDK_PUPPET_VERSION environment variable instead',
)
@process.environment['PUPPET_GEM_VERSION'] = nil
end

%w[FACTER HIERA].each do |gem|
if ENV["#{gem}_GEM_VERSION"]
PDK.logger.warn_once _("#{gem}_GEM_VERSION is not supported by PDK.")
@process.environment["#{gem}_GEM_VERSION"] = nil
end
end
end

def execute!
# Start spinning if configured.
@spinner.auto_spin if @spinner
Expand All @@ -130,6 +148,8 @@ def execute!
@process.environment[k] = v
end

check_for_legacy_env_vars

@process.environment['BUNDLE_IGNORE_CONFIG'] = '1'

if [:module, :pwd].include?(context)
Expand Down
8 changes: 8 additions & 0 deletions lib/pdk/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Logger < ::Logger

def initialize
super(STDERR)
@sent_messages = {}

# TODO: Decide on output format.
self.formatter = proc do |severity, _datetime, _progname, msg|
Expand All @@ -29,6 +30,13 @@ def initialize
self.level = ::Logger::INFO
end

def warn_once(*args)
hash = args.inspect.hash
return if (@sent_messages[::Logger::WARN] ||= {}).key?(hash)
@sent_messages[::Logger::WARN][hash] = true
warn(*args)
end

def enable_debug_output
self.level = ::Logger::DEBUG
end
Expand Down
17 changes: 17 additions & 0 deletions spec/acceptance/version_changer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
context 'in a new module' do
include_context 'in a new module', 'version_select'

%w[PUPPET FACTER HIERA].each do |gem|
context "when the legacy #{gem}_GEM_VERSION environment variable is used" do
if Gem.win_platform?
pre_cmd = "$env:#{gem}_GEM_VERSION='1.0.0';"
post_cmd = "; remove-item env:\\#{gem}_GEM_VERSION"
else
pre_cmd = "#{gem}_GEM_VERSION=1.0.0"
post_cmd = ''
end

describe command("#{pre_cmd} pdk validate#{post_cmd}") do
its(:exit_status) { is_expected.to eq(0) }
its(:stderr) { is_expected.to match(%r{#{gem}_GEM_VERSION is not supported by PDK}im) }
end
end
end

%w[5.5.0 4.10.10].each do |puppet_version|
describe command("pdk validate --puppet-version #{puppet_version}") do
its(:exit_status) { is_expected.to eq(0) }
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/pdk/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@

it { is_expected.to have_attributes(debug?: true) }
end

describe '#warn_once' do
it 'only sends each message once' do
expect(STDERR).to receive(:write).with("pdk (WARN): message 1\n").once

pdk_logger.warn_once('message 1')
pdk_logger.warn_once('message 1')
end
end
end

0 comments on commit 2cb86ed

Please sign in to comment.