Convert CLI reporter to plugin, RunData is still Hash-based

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2020-06-02 21:06:51 -04:00
parent afc6e59e95
commit 76033c03ff
7 changed files with 45 additions and 13 deletions

View file

@ -341,7 +341,6 @@ module Inspec
# Tracked on https://github.com/inspec/inspec/issues/3667
inspec_reporters_that_are_not_yet_plugins = %w{
automate
cli
json
json-automate
json-min

View file

@ -1,5 +1,4 @@
require "inspec/reporters/base"
require "inspec/reporters/cli"
require "inspec/reporters/json"
require "inspec/reporters/json_automate"
require "inspec/reporters/json_min"
@ -13,8 +12,6 @@ module Inspec::Reporters
name, config = reporter.dup
config[:run_data] = run_data
case name
when "cli"
reporter = Inspec::Reporters::CLI.new(config)
when "json"
reporter = Inspec::Reporters::Json.new(config)
# This reporter is only used for Chef internal. We reserve the

View file

@ -0,0 +1,12 @@
# cli reporter
This is the implementation of the "cli" reporter. It is the default reporter used if you do not specify a reporter.
## To Install This Plugin
This plugin is included with inspec. There is no need to install it separately.
## What This Plugin Does
This reporter generates a summary of output at the end of the run to STDOUT by default. By default it uses color and terminal graphics if possible, depending on the capabilities of the host platform.

View file

@ -0,0 +1,13 @@
require_relative "inspec-reporter-cli/version"
module InspecPlugins
module CliReporter
class Plugin < ::Inspec.plugin(2)
plugin_name :'inspec-reporter-cli'
reporter :cli do
require_relative "inspec-reporter-cli/reporter"
InspecPlugins::CliReporter::Reporter
end
end
end
end

View file

@ -1,5 +1,6 @@
module Inspec::Reporters
class CLI < Base
module InspecPlugins::CliReporter
class Reporter < Inspec.plugin(2, :reporter)
case RUBY_PLATFORM
when /windows|mswin|msys|mingw|cygwin/
# Most currently available Windows terminals have poor support
@ -40,6 +41,10 @@ module Inspec::Reporters
MULTI_TEST_CONTROL_SUMMARY_MAX_LEN = 60
def self.run_data_schema_constraints
"~> 0.0"
end
def render
run_data[:profiles].each do |profile|
if profile[:status] == "skipped"
@ -85,7 +90,7 @@ module Inspec::Reporters
def print_standard_control_results(profile)
standard_controls_from_profile(profile).each do |control_from_profile|
control = Control.new(control_from_profile)
next if control.results.nil?
next if control.results.empty?
output(format_control_header(control))
control.results.each do |result|
@ -99,7 +104,7 @@ module Inspec::Reporters
def print_anonymous_control_results(profile)
anonymous_controls_from_profile(profile).each do |control_from_profile|
control = Control.new(control_from_profile)
next if control.results.nil?
next if control.results.empty?
output(format_control_header(control))
control.results.each do |result|
@ -183,7 +188,7 @@ module Inspec::Reporters
all_unique_controls.each do |control|
next if control[:id].start_with? "(generated from "
next unless control[:results]
next if control[:results].empty?
if control[:results].any? { |r| r[:status] == "failed" }
failed += 1

View file

@ -0,0 +1,5 @@
module InspecPlugins
module CliReporter
VERSION = "0.1.0".freeze
end
end

View file

@ -1,16 +1,17 @@
require "helper"
require "inspec/reporters"
require_relative "../../../lib/plugins/inspec-reporter-cli/lib/inspec-reporter-cli"
require_relative "../../../lib/plugins/inspec-reporter-cli/lib/inspec-reporter-cli/reporter"
describe Inspec::Reporters::CLI do
describe InspecPlugins::CliReporter::Reporter do
WINDOWS = RUBY_PLATFORM =~ /windows|mswin|msys|mingw|cygwin/
let(:report) do
data = JSON.parse(File.read("test/fixtures/reporters/run_data.json"), symbolize_names: true)
cli = Inspec::Reporters::CLI
cli = InspecPlugins::CliReporter::Reporter
cli.new({ run_data: data })
end
let(:profile) { report.run_data[:profiles].first }
let(:control) { Inspec::Reporters::CLI::Control }
let(:control) { InspecPlugins::CliReporter::Reporter::Control }
before do
RSpec.configuration.color = true if defined?(RSpec.configuration)