Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-1337) Warn and unset any of the legacy *_GEM_VERSION env vars #671

Merged
merged 2 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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