Skip to content

Commit

Permalink
Add flag to treat all warnings as errors
Browse files Browse the repository at this point in the history
  • Loading branch information
manicmaniac committed Feb 26, 2024
1 parent 2da5738 commit 833b6a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/danger/danger_periphery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class DangerPeriphery < Plugin
# By default +false+ is set.
attr_accessor :scan_all_files

# A flag to treat warnings as errors.
# @return [Boolean] +true+ if this plugin reports all violations as errors.
# Otherwise +false+, it reports violations as warnings.
# By default +false+ is set.
attr_accessor :warning_as_error

# For internal use only.
#
# @return [Symbol]
Expand All @@ -42,6 +48,7 @@ class DangerPeriphery < Plugin
def initialize(dangerfile)
super(dangerfile)
@format = :checkstyle
@warning_as_error = false
end

# Scans Swift files.
Expand Down Expand Up @@ -75,7 +82,7 @@ def scan(options = {})

next if block_given? && !yield(entry)

warn(entry.message, file: entry.path, line: entry.line)
report(entry.message, file: entry.path, line: entry.line)
end
end

Expand All @@ -94,6 +101,12 @@ def install(version: :latest, path: 'periphery', force: false)

private

def report(*args, **kwargs)
return fail(*args, **kwargs) if warning_as_error

warn(*args, **kwargs)
end

def files_in_diff
# Taken from https://github.com/ashfurrow/danger-ruby-swiftlint/blob/5184909aab00f12954088684bbf2ce5627e08ed6/lib/danger_plugin.rb#L214-L216
renamed_files_hash = git.renamed_files.to_h { |rename| [rename[:before], rename[:after]] }
Expand Down
33 changes: 33 additions & 0 deletions spec/danger/danger_periphery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,39 @@
end
end

describe '#warning_as_error' do
subject(:errors) { dangerfile.status_report[:errors] }

before do
periphery.warning_as_error = warning_as_error
periphery.scan
end

context 'with false' do
let(:warning_as_error) { false }

it 'reports violations as warnings' do
expect(warnings).not_to be_empty
end

it 'does not report any errors' do
expect(errors).to be_empty
end
end

context 'with true' do
let(:warning_as_error) { true }

it 'reports violations as errors' do
expect(errors).not_to be_empty
end

it 'does not warn anything' do
expect(warnings).to be_empty
end
end
end

describe '#format' do
subject(:parser) { periphery.send(:parser) }

Expand Down

0 comments on commit 833b6a0

Please sign in to comment.