Include ref when writing out inspec control objects (#2259)

* support ref for inspec control objects

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>

* lint

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2017-10-24 14:50:23 -07:00 committed by Adam Leff
parent b2597cecfa
commit 39d743b12e
2 changed files with 13 additions and 2 deletions

View file

@ -2,10 +2,11 @@
module Inspec module Inspec
class Control class Control
attr_accessor :id, :title, :desc, :impact, :tests, :tags attr_accessor :id, :title, :desc, :impact, :tests, :tags, :refs
def initialize def initialize
@tests = [] @tests = []
@tags = [] @tags = []
@refs = []
end end
def add_test(t) def add_test(t)
@ -20,12 +21,13 @@ module Inspec
{ id: id, title: title, desc: desc, impact: impact, tests: tests.map(&:to_hash), tags: tags.map(&:to_hash) } { id: id, title: title, desc: desc, impact: impact, tests: tests.map(&:to_hash), tags: tags.map(&:to_hash) }
end end
def to_ruby def to_ruby # rubocop:disable Metrics/AbcSize
res = ["control #{id.inspect} do"] res = ["control #{id.inspect} do"]
res.push " title #{title.inspect}" unless title.to_s.empty? res.push " title #{title.inspect}" unless title.to_s.empty?
res.push " desc #{prettyprint_text(desc, 2)}" unless desc.to_s.empty? res.push " desc #{prettyprint_text(desc, 2)}" unless desc.to_s.empty?
res.push " impact #{impact}" unless impact.nil? res.push " impact #{impact}" unless impact.nil?
tags.each { |t| res.push(indent(t.to_ruby, 2)) } tags.each { |t| res.push(indent(t.to_ruby, 2)) }
refs.each { |t| res.push(" ref #{print_ref(t)}") }
tests.each { |t| res.push(indent(t.to_ruby, 2)) } tests.each { |t| res.push(indent(t.to_ruby, 2)) }
res.push 'end' res.push 'end'
res.join("\n") res.join("\n")
@ -33,6 +35,12 @@ module Inspec
private private
def print_ref(x)
return x.inspect if x.is_a?(String)
raise "Cannot process the ref: #{x}" unless x.is_a?(Hash)
'('+x.inspect+')'
end
# Pretty-print a text block of InSpec code # Pretty-print a text block of InSpec code
# #
# @param s [String] should not be empty # @param s [String] should not be empty

View file

@ -275,12 +275,15 @@ end
control.id = 'sample.control.id' control.id = 'sample.control.id'
control.title = 'Sample Control Important Title' control.title = 'Sample Control Important Title'
control.desc = 'The most critical control the world has ever seen' control.desc = 'The most critical control the world has ever seen'
control.refs = ['simple ref', {ref: 'title', url: 'my url'}]
control.impact = 1.0 control.impact = 1.0
control.to_ruby.must_equal ' control.to_ruby.must_equal '
control "sample.control.id" do control "sample.control.id" do
title "Sample Control Important Title" title "Sample Control Important Title"
desc "The most critical control the world has ever seen" desc "The most critical control the world has ever seen"
impact 1.0 impact 1.0
ref "simple ref"
ref ({:ref=>"title", :url=>"my url"})
describe command("ls /etc") do describe command("ls /etc") do
its("exit_status") { should eq 0 } its("exit_status") { should eq 0 }
end end