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-643) Disable non-exec validator spinners when noninteractive #385

Merged
merged 1 commit into from
Dec 12, 2017
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
12 changes: 2 additions & 10 deletions lib/pdk/cli/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,21 @@ def context=(new_context)
end

def register_spinner(spinner, opts = {})
return unless display_spinner?
return unless PDK::CLI::Util.interactive?
@success_message = opts.delete(:success)
@failure_message = opts.delete(:failure)

@spinner = spinner
end

def add_spinner(message, opts = {})
return unless display_spinner?
return unless PDK::CLI::Util.interactive?
@success_message = opts.delete(:success)
@failure_message = opts.delete(:failure)

@spinner = TTY::Spinner.new("[:spinner] #{message}", opts.merge(PDK::CLI::Util.spinner_opts_for_platform))
end

def display_spinner?
return false if PDK.logger.debug?
return (ENV['PDK_FRONTEND'] != 'NONINTERACTIVE') if ENV['PDK_FRONTEND']
return false unless $stderr.isatty

true
end

def execute!
# Start spinning if configured.
@spinner.auto_spin if @spinner
Expand Down
4 changes: 2 additions & 2 deletions lib/pdk/cli/exec_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ExecGroup
def initialize(message, opts = {})
@options = opts.merge(PDK::CLI::Util.spinner_opts_for_platform)

unless PDK.logger.debug?
if PDK::CLI::Util.interactive?
@multi_spinner = TTY::Spinner::Multi.new("[:spinner] #{message}", @options)
@multi_spinner.auto_spin
end
Expand All @@ -31,7 +31,7 @@ def register
end

def add_spinner(message, opts = {})
return if PDK.logger.debug?
return unless PDK::CLI::Util.interactive?
@multi_spinner.register("[:spinner] #{message}", @options.merge(opts).merge(PDK::CLI::Util.spinner_opts_for_platform))
end

Expand Down
9 changes: 9 additions & 0 deletions lib/pdk/cli/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def prompt_for_yes(question_text, opts = {})
response
end
module_function :prompt_for_yes

def interactive?
return false if PDK.logger.debug?
return !ENV['PDK_FRONTEND'].casecmp('noninteractive').zero? if ENV['PDK_FRONTEND']
return false unless $stderr.isatty

true
end
module_function :interactive?
end
end
end
2 changes: 1 addition & 1 deletion lib/pdk/validate/metadata/metadata_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.spinner_text(_targets = [])
end

def self.create_spinner(targets = [], options = {})
return if PDK.logger.debug?
return unless PDK::CLI::Util.interactive?
options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)

exec_group = options[:exec_group]
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/validate/metadata/task_metadata_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.spinner_text(_targets = [])
end

def self.create_spinner(targets = [], options = {})
return if PDK.logger.debug?
return unless PDK::CLI::Util.interactive?
options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)

exec_group = options[:exec_group]
Expand Down
38 changes: 38 additions & 0 deletions spec/unit/pdk/cli/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,42 @@
end
end
end

describe '.interactive?' do
subject { described_class.interactive? }

before(:each) do
allow(PDK.logger).to receive(:debug?).and_return(false)
allow($stderr).to receive(:isatty).and_return(true)
ENV.delete('PDK_FRONTEND')
end

context 'by default' do
it { is_expected.to be_truthy }
end

context 'when the logger is in debug mode' do
before(:each) do
allow(PDK.logger).to receive(:debug?).and_return(true)
end

it { is_expected.to be_falsey }
end

context 'when PDK_FRONTEND env var is set to noninteractive' do
before(:each) do
ENV['PDK_FRONTEND'] = 'noninteractive'
end

it { is_expected.to be_falsey }
end

context 'when STDERR is not a TTY' do
before(:each) do
allow($stderr).to receive(:isatty).and_return(false)
end

it { is_expected.to be_falsey }
end
end
end