diff --git a/lib/pdk/cli/exec.rb b/lib/pdk/cli/exec.rb index dac74edc9..dcc3d0d1d 100644 --- a/lib/pdk/cli/exec.rb +++ b/lib/pdk/cli/exec.rb @@ -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 @@ -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) diff --git a/lib/pdk/logger.rb b/lib/pdk/logger.rb index 1f5784cec..32c5911b3 100644 --- a/lib/pdk/logger.rb +++ b/lib/pdk/logger.rb @@ -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| @@ -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 diff --git a/spec/acceptance/version_changer_spec.rb b/spec/acceptance/version_changer_spec.rb index f02420a24..9e1a3973c 100644 --- a/spec/acceptance/version_changer_spec.rb +++ b/spec/acceptance/version_changer_spec.rb @@ -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) } diff --git a/spec/unit/pdk/logger_spec.rb b/spec/unit/pdk/logger_spec.rb index 665d43db9..4eba7ca7c 100644 --- a/spec/unit/pdk/logger_spec.rb +++ b/spec/unit/pdk/logger_spec.rb @@ -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