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

Add --fail-on-warning option #1093

Merged
merged 1 commit into from
Nov 18, 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
10 changes: 10 additions & 0 deletions lib/yard/cli/yardoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ class Yardoc < YardoptsCommand
# @since 0.7.0
attr_accessor :has_markup

# @return [Boolean] whether yard exits with error status code if a warning occurs
attr_accessor :fail_on_warning

# Creates a new instance of the commandline utility
def initialize
super
Expand All @@ -218,6 +221,7 @@ def initialize
@list = false
@save_yardoc = true
@has_markup = false
@fail_on_warning = false

if defined?(::Encoding) && ::Encoding.respond_to?(:default_external=)
utf8 = ::Encoding.find('utf-8')
Expand Down Expand Up @@ -273,6 +277,8 @@ def run(*args)
end
end

abort if fail_on_warning && log.warned

true
ensure
log.show_progress = false
Expand Down Expand Up @@ -555,6 +561,10 @@ def general_options(opts)
opts.on('--exclude REGEXP', 'Ignores a file if it matches path match (regexp)') do |path|
excluded << path
end

opts.on('--fail-on-warning', 'Exit with error status code if a warning occurs') do
self.fail_on_warning = true
end
end

# Adds output options
Expand Down
8 changes: 8 additions & 0 deletions lib/yard/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def initialize(pipe, *args)
self.show_progress = false
self.level = WARN
self.formatter = method(:format_log)
self.warned = false
@progress_indicator = 0
@mutex = Mutex.new
@progress_msg = nil
Expand All @@ -60,6 +61,13 @@ def debug(*args)
super
end

# Remembers when a warning occurs and writes a warning message.
def warn(*args)
self.warned = true
super
end
attr_accessor :warned

# Captures the duration of a block of code for benchmark analysis. Also
# calls {#progress} on the message to display it to the user.
#
Expand Down
19 changes: 19 additions & 0 deletions spec/cli/yardoc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ def self.should_accept(*args, &block)
expect(Registry).not_to receive(:save)
@yardoc.run(arg)
end

should_accept('--fail-on-warning') do |arg|
expect(YARD).to receive(:parse)
@yardoc.run(arg)
end
end

describe "Output options" do
Expand Down Expand Up @@ -816,5 +821,19 @@ def tag_hidden(tag)
expect(Registry).not_to receive(:lock_for_writing)
@yardoc.run('--no-save')
end

context "with --fail-on-warning" do
it "exits with error status code if a warning occurs" do
allow(log).to receive(:warned).and_return(true)
expect { @yardoc.run("--fail-on-warning") }.to raise_error(SystemExit) do |error|
expect(error).not_to be_success
end
end

it "does not exit if a warning does not occur" do
allow(log).to receive(:warned).and_return(false)
expect { @yardoc.run("--fail-on-warning") }.not_to raise_error
end
end
end
end
9 changes: 9 additions & 0 deletions spec/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@
log.enter_level(Logger::INFO) { log.backtrace(exc, :warn) }
end
end

describe '#warn' do
before { log.warned = false }
after { log.warned = false }

it 'changes #warned from false to true' do
expect { log.warn('message') }.to change(log, :warned).from(false).to(true)
end
end
end