2015-06-07 19:41:54 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
2015-06-19 14:45:36 +00:00
|
|
|
require 'verify/specfile'
|
2015-06-19 13:06:44 +00:00
|
|
|
require 'vulcano/log'
|
2015-06-07 19:41:54 +00:00
|
|
|
|
|
|
|
module Vulcano
|
|
|
|
# Handle Vulcano Profiles
|
|
|
|
class Profiles
|
|
|
|
attr_reader :profiles
|
|
|
|
def initialize opts = {}
|
|
|
|
@profiles = {}
|
|
|
|
@log = Log.new(opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_folder f
|
|
|
|
path = File::expand_path(f)
|
|
|
|
if File.directory? path
|
|
|
|
add_specs_in_folder path
|
|
|
|
else
|
|
|
|
@log.error "Path is not a folder: #{path}"
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_folder? f
|
|
|
|
path = File::expand_path(f)
|
|
|
|
if !File.directory? path
|
|
|
|
return @log.error "This is not a folder: #{path}"
|
|
|
|
else
|
|
|
|
@log.ok "Valid directory"
|
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
metadata = Metadata.for_path(path)
|
|
|
|
@log.ok "vmetadata.rb" unless metadata.nil? or !metadata.valid?
|
2015-06-07 19:41:54 +00:00
|
|
|
|
|
|
|
specs = Dir["#{path}/spec/*_spec.rb"]
|
|
|
|
if specs.empty?
|
|
|
|
@log.warn "No tests found in #{path}"
|
|
|
|
end
|
2015-06-19 23:36:27 +00:00
|
|
|
specs.each{|s| valid_spec? s, metadata }
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
def valid_spec? f, metadata
|
2015-06-07 19:41:54 +00:00
|
|
|
return @log.error "Can't find spec file #{f}" unless File::file? f
|
2015-06-19 23:36:27 +00:00
|
|
|
# validation tracking
|
2015-06-07 19:41:54 +00:00
|
|
|
valid = true
|
2015-06-19 23:36:27 +00:00
|
|
|
invalid = lambda {|msg|
|
|
|
|
@log.error "#{msg} (#{File::basename f})"
|
2015-06-07 19:41:54 +00:00
|
|
|
valid = false
|
2015-06-19 23:36:27 +00:00
|
|
|
}
|
|
|
|
# Load the spec file
|
|
|
|
specs = SpecFile.from_file(f, metadata)
|
|
|
|
# find all errors during parsing
|
|
|
|
specs.errors.each do |err|
|
|
|
|
invalid.(err)
|
|
|
|
end
|
|
|
|
# detect missing metadata
|
|
|
|
meta = specs.metadata
|
|
|
|
if meta['title'].nil?
|
|
|
|
invalid.("Missing title in spec file")
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
if meta['copyright'].nil?
|
2015-06-19 23:36:27 +00:00
|
|
|
invalid.("Missing copyright in spec file")
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
2015-06-19 23:36:27 +00:00
|
|
|
# detect empty rules
|
2015-06-16 21:20:24 +00:00
|
|
|
unless meta['rules'][''].nil?
|
2015-06-19 23:36:27 +00:00
|
|
|
invalid.("Please configure IDs for all rules.")
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-16 21:20:24 +00:00
|
|
|
meta['rules'].each do |k,v|
|
2015-06-10 15:36:30 +00:00
|
|
|
if v['impact'].nil?
|
|
|
|
invalid.("Missing impact for rule #{k}")
|
|
|
|
else
|
|
|
|
invalid.("Impact cannot be larger than 1.0 for rule #{k}") if v['impact'] > 1.0
|
|
|
|
invalid.("Impact cannot be less than 0.0 for rule #{k}") if v['impact'] < 0.0
|
|
|
|
end
|
2015-06-10 15:21:03 +00:00
|
|
|
invalid.("Missing title for rule #{k}") if v['title'].nil?
|
2015-06-10 15:16:05 +00:00
|
|
|
invalid.("Missing description for rule #{k}") if v['desc'].nil?
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-16 21:33:27 +00:00
|
|
|
if valid && specs.instance_variable_get(:@invalid_calls).empty?
|
|
|
|
@log.ok "Valid spec file in #{f} with #{meta['rules'].length} rules"
|
|
|
|
end
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_specs_in_folder path
|
2015-06-16 21:20:24 +00:00
|
|
|
allrules = {}
|
2015-06-19 23:41:48 +00:00
|
|
|
meta = Metadata.for_path(path, @log)
|
2015-06-07 19:41:54 +00:00
|
|
|
|
|
|
|
Dir["#{path}/spec/*_spec.rb"].each do |specfile|
|
|
|
|
rel_path = specfile.sub(File.join(path,''), '')
|
2015-06-19 23:41:48 +00:00
|
|
|
specs = SpecFile.from_file(specfile, meta)
|
|
|
|
allrules[rel_path] = sanitize_specfile_json(specs.metadata)
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 23:41:48 +00:00
|
|
|
meta.dict['rules'] = allrules
|
|
|
|
@profiles = meta.dict
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-10 15:21:03 +00:00
|
|
|
def sanitize_specfile_json j
|
2015-06-16 21:20:24 +00:00
|
|
|
j['rules'].each do |k,v|
|
2015-06-10 15:21:03 +00:00
|
|
|
v['title'] = k if v['title'].nil?
|
|
|
|
v['desc'] = "" if v['desc'].nil?
|
|
|
|
v['impact'] = 1.0 if v['impact'].nil?
|
|
|
|
end
|
2015-06-12 10:32:10 +00:00
|
|
|
j
|
2015-06-10 15:21:03 +00:00
|
|
|
end
|
|
|
|
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
end
|