forked from sds/haml-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using the `-r` or `--reporter` argument, now it's possible to specify a reporter other than the default. The `--show-reporters` argument returns back the list of available reporters. The Reporter class was modified to statically keep a list of its descendants. See: http://stackoverflow.com/a/7774881 For the detailed specification, see: http://www.rubydoc.info/gems/rubocop/0.11.0#JSON_Formatter `haml-lint` doesn't return the column where the offense is, so it's omitted from the location hash. Change-Id: Ib116914d7f00502503ae4e65bd0bf81cc33c99ee Reviewed-on: http://gerrit.causes.com/46518 Reviewed-by: Shane da Silva <[email protected]> Tested-by: Shane da Silva <[email protected]>
- Loading branch information
Showing
9 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module HamlLint | ||
# Outputs JSON | ||
class Reporter::JsonReporter < Reporter | ||
def report_lints | ||
grouped = lints.group_by(&:filename) | ||
report = { | ||
:metadata => { | ||
:hamllint_version => VERSION, | ||
:ruby_engine => RUBY_ENGINE, | ||
:ruby_patchlevel => RUBY_PATCHLEVEL.to_s, | ||
:ruby_platform => RUBY_PLATFORM | ||
}, | ||
:files => grouped.map { |l| map_file(l) }, | ||
:summary => { | ||
:offense_count => lints.length, | ||
:target_file_count => grouped.length, | ||
:inspected_file_count => files.length | ||
} | ||
} | ||
|
||
log.log report.to_json | ||
end | ||
|
||
private | ||
|
||
def map_file(file) | ||
{:path => file.first, :offenses => file.last.map { |o| map_offense(o) }} | ||
end | ||
|
||
def map_offense(offense) | ||
{:severity => offense.severity, :message => offense.message, :location => {:line => offense.line}} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'spec_helper' | ||
|
||
describe HamlLint::Reporter::JsonReporter do | ||
subject { HamlLint::Reporter::JsonReporter.new(lints) } | ||
|
||
describe '#report_lints' do | ||
let(:io) { StringIO.new } | ||
let(:output) { JSON.parse(io.string) } | ||
let(:logger) { HamlLint::Logger.new(io) } | ||
let(:report) { HamlLint::Report.new(lints, []) } | ||
let(:reporter) { described_class.new(logger, report) } | ||
|
||
subject { reporter.report_lints } | ||
|
||
shared_examples_for "output format specification" do | ||
it 'output should be consistent with the specification' do | ||
subject | ||
output.should be_a_kind_of(Hash) | ||
output['metadata'].should be_a_kind_of(Hash) | ||
output['metadata'].length.should eq 4 | ||
output['metadata']['hamllint_version'].should_not be_empty | ||
output['metadata']['ruby_engine'].should eq RUBY_ENGINE | ||
output['metadata']['ruby_patchlevel'].should eq RUBY_PATCHLEVEL.to_s | ||
output['metadata']['ruby_platform'].should eq RUBY_PLATFORM.to_s | ||
output['files'].should be_a_kind_of(Array) | ||
output['summary'].should be_a_kind_of(Hash) | ||
output['summary'].length.should eq 3 | ||
output['summary']['offense_count'].should be_a_kind_of(Integer) | ||
output['summary']['target_file_count'].should be_a_kind_of(Integer) | ||
output['summary']['inspected_file_count'].should be_a_kind_of(Integer) | ||
end | ||
end | ||
|
||
context 'when there are no lints' do | ||
let(:lints) { [] } | ||
let(:files) { [] } | ||
|
||
it 'list of files is empty' do | ||
subject | ||
output['files'].should be_empty | ||
end | ||
|
||
it 'number of target files is zero' do | ||
subject | ||
output['summary']['target_file_count'].should == 0 | ||
end | ||
|
||
it_behaves_like "output format specification" | ||
end | ||
|
||
context 'when there are lints' do | ||
let(:filenames) { ['some-filename.haml', 'other-filename.haml'] } | ||
let(:lines) { [502, 724] } | ||
let(:descriptions) { ['Description of lint 1', 'Description of lint 2'] } | ||
let(:severities) { [:warning, :error] } | ||
|
||
let(:lints) do | ||
filenames.each_with_index.map do |filename, index| | ||
HamlLint::Lint.new(nil, filename, lines[index], descriptions[index], severities[index]) | ||
end | ||
end | ||
|
||
it 'list of files contains files with offenses' do | ||
subject | ||
output['files'].map { |f| f['path'] }.sort.should eq filenames.sort | ||
end | ||
|
||
it_behaves_like "output format specification" | ||
end | ||
end | ||
end |