Skip to content

Commit

Permalink
Add --fail-on-warning option
Browse files Browse the repository at this point in the history
  • Loading branch information
yuku committed May 17, 2017
1 parent f16cf4c commit 8438e9d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
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 an 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

exit 1 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 an 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

# Remember an 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

0 comments on commit 8438e9d

Please sign in to comment.