From 4253f279018994196a67e0dbb8c81c56de774a95 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Tue, 3 Nov 2020 13:47:53 -0800 Subject: [PATCH] Prevent printing token warning more than once Mark warning of double tokens set when initially print to prevent the warning from being shown multiple times during a single run. --- plugins/commands/cloud/client/client.rb | 13 ++++++++++++- test/unit/plugins/commands/cloud/client_test.rb | 8 +++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/plugins/commands/cloud/client/client.rb b/plugins/commands/cloud/client/client.rb index 1c75c7901..0e9452e1d 100644 --- a/plugins/commands/cloud/client/client.rb +++ b/plugins/commands/cloud/client/client.rb @@ -6,6 +6,13 @@ require Vagrant.source_root.join("plugins/commands/cloud/errors") module VagrantPlugins module CloudCommand class Client + # @private + # Reset the cached values for scrubber. This is not considered a public + # API and should only be used for testing. + def self.reset! + class_variables.each(&method(:remove_class_variable)) + end + ###################################################################### # Class that deals with managing users 'local' token for Vagrant Cloud ###################################################################### @@ -111,7 +118,9 @@ module VagrantPlugins # @return [String] def token if present?(ENV["VAGRANT_CLOUD_TOKEN"]) && token_path.exist? - @env.ui.warn <<-EOH.strip + # Only show warning if it has not been previously shown + if !defined?(@@double_token_warning) + @env.ui.warn <<-EOH.strip Vagrant detected both the VAGRANT_CLOUD_TOKEN environment variable and a Vagrant login token are present on this system. The VAGRANT_CLOUD_TOKEN environment variable takes precedence over the locally stored token. To remove this error, either unset @@ -120,6 +129,8 @@ the VAGRANT_CLOUD_TOKEN environment variable or remove the login token stored on ~/.vagrant.d/data/vagrant_login_token EOH + @@double_token_warning = true + end end if present?(ENV["VAGRANT_CLOUD_TOKEN"]) diff --git a/test/unit/plugins/commands/cloud/client_test.rb b/test/unit/plugins/commands/cloud/client_test.rb index 654c9e89c..843d4e93b 100644 --- a/test/unit/plugins/commands/cloud/client_test.rb +++ b/test/unit/plugins/commands/cloud/client_test.rb @@ -24,6 +24,7 @@ describe VagrantPlugins::CloudCommand::Client do end after do + described_class.reset! Vagrant::Util::CredentialScrubber.reset! end @@ -192,7 +193,7 @@ describe VagrantPlugins::CloudCommand::Client do let(:path_exists) { false } before do - expect(subject).to receive(:token).and_call_original + allow(subject).to receive(:token).and_call_original allow(subject).to receive(:token_path).and_return(token_path) allow(token_path).to receive(:exist?).and_return(path_exists) end @@ -215,6 +216,11 @@ describe VagrantPlugins::CloudCommand::Client do expect(env.ui).to receive(:warn) subject.token end + + it "should only print warning of two tokens once" do + expect(env.ui).to receive(:warn).with(/detected/).once + 3.times { subject.token } + end end end