Skip to content

Commit

Permalink
change check errors on summary+title to warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
arlimus authored and srenatus committed Dec 23, 2015
1 parent d2509f7 commit b2e0fac
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/inspec/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def supports(sth, version = nil)

def valid?
is_valid = true
%w{ name title version summary }.each do |field|
%w{ name version }.each do |field|
next unless params[field.to_sym].nil?
@logger.error("Missing profile #{field} in metadata.rb")
is_valid = false
end
%w{ maintainer copyright }.each do |field|
%w{ title summary maintainer copyright }.each do |field|
next unless params[field.to_sym].nil?
@logger.warn("Missing profile #{field} in metadata.rb")
is_valid = false
Expand Down
20 changes: 4 additions & 16 deletions lib/inspec/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'inspec/metadata'

module Inspec
class Profile # rubocop:disable Metrics/ClassLength
class Profile
def self.from_path(path, options = nil)
opt = {}
options.each { |k, v| opt[k.to_sym] = v } unless options.nil?
Expand Down Expand Up @@ -35,6 +35,7 @@ def initialize(options = nil)
id: @profile_id,
backend: :mock,
)

@runner.add_tests([@path])
@runner.rules.each do |id, rule|
file = rule.instance_variable_get(:@__file)
Expand Down Expand Up @@ -91,24 +92,11 @@ def check # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metri
}

@logger.info "Checking profile in #{@path}"

if @params[:name].to_s.empty?
error.call('No profile name defined')
elsif !(@params[:name].to_s =~ %r{^\S+\/\S+$})
error.call('Profile name must be defined as: OWNER/ID')
end

warn.call('No version defined') if @params[:name].to_s.empty?
warn.call('No title defined') if @params[:title].to_s.empty?
warn.call('No maintainer defined') if @params[:maintainer].to_s.empty?
if Array(@params[:supports]).empty?
warn.call('No supports defined (supported operating systems)')
end
@logger.info 'Metadata OK.' if no_warnings
@logger.info 'Metadata OK.' if @metadata.valid?

no_warnings = true
if @params[:rules].empty?
warn.call('No rules were found.')
warn.call('No controls or tests were defined.')
else
@logger.debug "Found #{@params[:rules].length} rules."
end
Expand Down
7 changes: 7 additions & 0 deletions test/unit/mock/profiles/complete-meta/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name 'name'
version '1.2.3'
maintainer 'bob'
title 'title'
copyright 'left'
summary 'nothing'
supports nil
Empty file.
45 changes: 40 additions & 5 deletions test/unit/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

require 'helper'

def load_profile(name)
pwd = File.dirname(__FILE__)
Inspec::Profile.from_path("#{pwd}/mock/profiles/#{name}")
end

describe Inspec::Profile do
before {
# mock up the profile runner
Expand All @@ -23,6 +18,13 @@ def rules
end
}

let(:logger) { Minitest::Mock.new }
let(:home) { File.dirname(__FILE__) }

def load_profile(name, opts = {})
Inspec::Profile.from_path("#{home}/mock/profiles/#{name}", opts)
end

describe 'with empty profile' do
let(:profile) { load_profile('empty') }

Expand All @@ -46,4 +48,37 @@ def rules
profile.params[:rules].must_equal({})
end
end

describe 'when checking' do
describe 'an empty profile' do
let(:profile) { load_profile('empty', {logger: logger}) }

it 'prints loads of warnings' do
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/empty"]
logger.expect :error, nil, ['Missing profile name in metadata.rb']
logger.expect :error, nil, ['Missing profile version in metadata.rb']
logger.expect :warn, nil, ['Missing profile title in metadata.rb']
logger.expect :warn, nil, ['Missing profile summary in metadata.rb']
logger.expect :warn, nil, ['Missing profile maintainer in metadata.rb']
logger.expect :warn, nil, ['Missing profile copyright in metadata.rb']
logger.expect :warn, nil, ['No controls or tests were defined.']

profile.check
logger.verify
end
end

describe 'a complete metadata profile' do
let(:profile) { load_profile('complete-meta', {logger: logger}) }

it 'prints ok messages' do
logger.expect :info, nil, ["Checking profile in #{home}/mock/profiles/complete-meta"]
logger.expect :info, nil, ['Metadata OK.']
logger.expect :warn, nil, ['No controls or tests were defined.']

profile.check
logger.verify
end
end
end
end

0 comments on commit b2e0fac

Please sign in to comment.