Skip to content

Commit

Permalink
Merge pull request #808 from chef/dr/source-location
Browse files Browse the repository at this point in the history
api: report source location with field identifiers
  • Loading branch information
arlimus committed Jun 28, 2016
2 parents b288df1 + 5da73db commit 4c8abea
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/inspec/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ def check # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metri

# iterate over hash of groups
params[:controls].each { |id, control|
sfile, sline = control[:source_location]
sfile = control[:source_location][:ref]
sline = control[:source_location][:line]
error.call(sfile, sline, nil, id, 'Avoid controls with empty IDs') if id.nil? or id.empty?
next if id.start_with? '(generated '
warn.call(sfile, sline, nil, id, "Control #{id} has no title") if control[:title].to_s.empty?
Expand Down
7 changes: 4 additions & 3 deletions lib/inspec/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ def __get_block_source(&block)

# get the source location of the block
def __get_block_source_location(&block)
return [nil, nil] unless block_given?
block.source_location
return {} unless block_given?
r, l = block.source_location
{ ref: r, line: l }
rescue MethodSource::SourceNotFoundError
[nil, nil]
{}
end
end
end
4 changes: 2 additions & 2 deletions test/functional/inspec_exec_json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
actual = ex1.dup

src = actual.delete('source_location')
src[0].must_match %r{examples/profile/controls/example.rb$}
src[1].must_equal 8
src['ref'].must_match %r{examples/profile/controls/example.rb$}
src['line'].must_equal 8

result = actual.delete('results')[0]
result.wont_be :nil?
Expand Down
3 changes: 2 additions & 1 deletion test/functional/inspec_json_profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@

it 'has a source location' do
loc = File.join(example_profile, '/controls/example.rb')
control['source_location'].must_equal [loc, 8]
control['source_location']['ref'].must_equal loc
control['source_location']['line'].must_equal 8
end

it 'has a the source code' do
Expand Down

0 comments on commit 4c8abea

Please sign in to comment.