diff --git a/lib/inspec/rspec_json_formatter.rb b/lib/inspec/rspec_json_formatter.rb index 0dbb1057b..ab7aad8b4 100644 --- a/lib/inspec/rspec_json_formatter.rb +++ b/lib/inspec/rspec_json_formatter.rb @@ -3,6 +3,7 @@ # author: Christoph Hartmann require 'rspec/core' +require 'rspec/core/formatters/json_formatter' # Extend the basic RSpec JSON Formatter # to give us an ID in its output @@ -21,10 +22,34 @@ module RSpec::Core::Formatters run_time: example.execution_result.run_time, pending_message: example.execution_result.pending_message, id: example.metadata[:id], - impact: example.metadata[:impact], - title: example.metadata[:title], - desc: example.metadata[:desc], } end end end + +class InspecRspecFormatter < RSpec::Core::Formatters::JsonFormatter + RSpec::Core::Formatters.register self, :message, :dump_summary, :dump_profile, :stop, :close + + private + + def format_example(example) + res = { + id: example.metadata[:id], + title: example.metadata[:title], + desc: example.metadata[:desc], + code: example.metadata[:code], + impact: example.metadata[:impact], + status: example.execution_result.status.to_s, + code_desc: example.full_description, + ref: example.metadata['file_path'], + ref_line: example.metadata['line_number'], + run_time: example.execution_result.run_time, + start_time: example.execution_result.started_at.to_s, + } + + # pending messages are embedded in the resources description + res[:pending] = example.metadata[:description] if res[:status] == 'pending' + + res + end +end diff --git a/lib/inspec/runner_rspec.rb b/lib/inspec/runner_rspec.rb index cd5fed00f..efe86cba5 100644 --- a/lib/inspec/runner_rspec.rb +++ b/lib/inspec/runner_rspec.rb @@ -81,7 +81,9 @@ module Inspec RSpec.configuration.output_stream = @conf['output'] end - RSpec.configuration.add_formatter(@conf['format'] || 'progress') + format = @conf['format'] || 'progress' + format = 'InspecRspecFormatter' if format == 'fulljson' + RSpec.configuration.add_formatter(format) RSpec.configuration.color = @conf['color'] setup_reporting if @conf['report']