Skip to content

Commit

Permalink
Assert that expected attributes are present in XML
Browse files Browse the repository at this point in the history
Raise a meaningful MissingAttributesError if not. The exception also
shows the raw XML node so the reason for the missing attribute can be
determined and addressed.
  • Loading branch information
caiofbpa authored and pbrisbin committed Feb 9, 2016
1 parent ee2d4bc commit de0dde1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
57 changes: 31 additions & 26 deletions lib/cc/engine/csslint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

module CC
module Engine
MissingAttributesError = Class.new(StandardError)

class CSSlint
autoload :CheckDetails, "cc/engine/csslint/check_details"

Expand All @@ -18,32 +20,7 @@ def run
path = file['name'].sub(/\A#{@directory}\//, '')
file.children.each do |node|
next unless node.name == "error"

lint = node.attributes
check_name = lint["identifier"].value
check_details = CheckDetails.fetch(check_name)

issue = {
type: "issue",
check_name: check_name,
description: lint["message"].value,
categories: check_details.categories,
remediation_points: check_details.remediation_points,
location: {
path: path,
positions: {
begin: {
line: lint["line"].value.to_i,
column: lint["column"].value.to_i
},
end: {
line: lint["line"].value.to_i,
column: lint["column"].value.to_i
}
}
}
}

issue = create_issue(node, path)
puts("#{issue.to_json}\0")
end
end
Expand All @@ -52,6 +29,34 @@ def run

private

def create_issue(node, path)
check_name = node.attributes.fetch("identifier").value
check_details = CheckDetails.fetch(check_name)

{
type: "issue",
check_name: check_name,
description: node.attributes.fetch("message").value,
categories: check_details.categories,
remediation_points: check_details.remediation_points,
location: {
path: path,
positions: {
begin: {
line: node.attributes.fetch("line").value.to_i,
column: node.attributes.fetch("column").value.to_i
},
end: {
line: node.attributes.fetch("line").value.to_i,
column: node.attributes.fetch("column").value.to_i
}
}
}
}
rescue KeyError => ex
raise MissingAttributesError, "#{ex.message} on XML '#{node}' when analyzing file '#{path}'"
end

def results
@results ||= Nokogiri::XML(csslint_xml)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/cc/engine/csslint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module Engine
expect{ lint.run }.to output(/Don't use IDs in selectors./).to_stdout
end

it 'fails on malformed file' do
create_source_file('foo.css', '�6�')
expect{ lint.run }.to raise_error(MissingAttributesError)
end

it "doesn't analyze *.scss files" do
create_source_file('foo.scss', id_selector_content)
expect{ lint.run }.to_not output.to_stdout
Expand Down

0 comments on commit de0dde1

Please sign in to comment.