Skip to content

Commit

Permalink
Merge pull request #200 from manicmaniac/introduce-warning-as-error-o…
Browse files Browse the repository at this point in the history
…ption

Introduce option to treat all warnings as errors
  • Loading branch information
manicmaniac authored Feb 26, 2024
2 parents 1232044 + 8cf10fa commit 6716d2b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ guard :rspec, cmd: 'bundle exec rspec -t ~slow', run_all: { cmd: 'bundle exec rs
dsl.watch_spec_files_for(ruby.lib_files)
end

guard :rubocop do
guard :rubocop, all_on_start: false, cli: '-D' do
watch(/.+\.rb$/)
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
end
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ periphery.scan_all_files = true
periphery.scan
```

You can force `danger-periphery` treat all warnings as errors by changing `warning_as_error` flag.

```ruby
periphery.warning_as_error = true
periphery.scan
```

## Advanced Usage

### Skip building for faster analysis
Expand Down
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 self.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 6716d2b

Please sign in to comment.