Skip to content

Commit de0dde1

Browse files
caiofbpapbrisbin
authored andcommitted
Assert that expected attributes are present in XML
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.
1 parent ee2d4bc commit de0dde1

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

Diff for: lib/cc/engine/csslint.rb

+31-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
module CC
55
module Engine
6+
MissingAttributesError = Class.new(StandardError)
7+
68
class CSSlint
79
autoload :CheckDetails, "cc/engine/csslint/check_details"
810

@@ -18,32 +20,7 @@ def run
1820
path = file['name'].sub(/\A#{@directory}\//, '')
1921
file.children.each do |node|
2022
next unless node.name == "error"
21-
22-
lint = node.attributes
23-
check_name = lint["identifier"].value
24-
check_details = CheckDetails.fetch(check_name)
25-
26-
issue = {
27-
type: "issue",
28-
check_name: check_name,
29-
description: lint["message"].value,
30-
categories: check_details.categories,
31-
remediation_points: check_details.remediation_points,
32-
location: {
33-
path: path,
34-
positions: {
35-
begin: {
36-
line: lint["line"].value.to_i,
37-
column: lint["column"].value.to_i
38-
},
39-
end: {
40-
line: lint["line"].value.to_i,
41-
column: lint["column"].value.to_i
42-
}
43-
}
44-
}
45-
}
46-
23+
issue = create_issue(node, path)
4724
puts("#{issue.to_json}\0")
4825
end
4926
end
@@ -52,6 +29,34 @@ def run
5229

5330
private
5431

32+
def create_issue(node, path)
33+
check_name = node.attributes.fetch("identifier").value
34+
check_details = CheckDetails.fetch(check_name)
35+
36+
{
37+
type: "issue",
38+
check_name: check_name,
39+
description: node.attributes.fetch("message").value,
40+
categories: check_details.categories,
41+
remediation_points: check_details.remediation_points,
42+
location: {
43+
path: path,
44+
positions: {
45+
begin: {
46+
line: node.attributes.fetch("line").value.to_i,
47+
column: node.attributes.fetch("column").value.to_i
48+
},
49+
end: {
50+
line: node.attributes.fetch("line").value.to_i,
51+
column: node.attributes.fetch("column").value.to_i
52+
}
53+
}
54+
}
55+
}
56+
rescue KeyError => ex
57+
raise MissingAttributesError, "#{ex.message} on XML '#{node}' when analyzing file '#{path}'"
58+
end
59+
5560
def results
5661
@results ||= Nokogiri::XML(csslint_xml)
5762
end

Diff for: spec/cc/engine/csslint_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ module Engine
1616
expect{ lint.run }.to output(/Don't use IDs in selectors./).to_stdout
1717
end
1818

19+
it 'fails on malformed file' do
20+
create_source_file('foo.css', '�6�')
21+
expect{ lint.run }.to raise_error(MissingAttributesError)
22+
end
23+
1924
it "doesn't analyze *.scss files" do
2025
create_source_file('foo.scss', id_selector_content)
2126
expect{ lint.run }.to_not output.to_stdout

0 commit comments

Comments
 (0)