lib/inspec/profile.rb in inspec-4.7.3 vs lib/inspec/profile.rb in inspec-4.7.18

- old
+ new

@@ -32,10 +32,11 @@ end content = Hash[cache.map { |x| [x, file_provider.binread(x)] }] keys = content.keys keys.each do |key| next if content[key].nil? + # remove prefix rel = Pathname.new(key).relative_path_from(Pathname.new("vendor")).to_s tar = Pathname.new(opts[:vendor_cache].path).join(rel) FileUtils.mkdir_p tar.dirname.to_s @@ -185,14 +186,16 @@ end def collect_tests(include_list = @controls) unless @tests_collected return unless supports_platform? + locked_dependencies.each(&:collect_tests) tests.each do |path, content| next if content.nil? || content.empty? + abs_path = source_reader.target.abs_path(path) @runner_context.load_control_file(content, abs_path, nil) end @tests_collected = true end @@ -205,10 +208,11 @@ # Check for anything that might be a regex in the list, and make it official include_list.each_with_index do |inclusion, index| next if inclusion.is_a?(Regexp) # Insist the user wrap the regex in slashes to demarcate it as a regex next unless inclusion.start_with?("/") && inclusion.end_with?("/") + inclusion = inclusion[1..-2] # Trim slashes begin re = Regexp.new(inclusion) include_list[index] = re rescue RegexpError => e @@ -269,10 +273,11 @@ def info(res = params.dup) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength # add information about the controls res[:controls] = res[:controls].map do |id, rule| next if id.to_s.empty? + data = rule.dup data.delete(:checks) data[:impact] ||= 0.5 data[:impact] = 1.0 if data[:impact] > 1.0 data[:impact] = 0.0 if data[:impact] < 0.0 @@ -282,11 +287,11 @@ if data[:code].empty? && parent_profile.nil? locked_dependencies.dep_list.each do |_name, dep| profile = dep.profile code = Inspec::MethodSource.code_at(data[:source_location], profile.source_reader) data[:code] = code unless code.nil? || code.empty? - break if !data[:code].empty? + break unless data[:code].empty? end end data end.compact @@ -375,12 +380,12 @@ @logger.info "Metadata OK." if m_errors.empty? && m_unsupported.empty? # only run the vendor check if the legacy profile-path is not used as argument if @legacy_profile_path == false # verify that a lockfile is present if we have dependencies - if !metadata.dependencies.empty? - error.call(meta_path, 0, 0, nil, "Your profile needs to be vendored with `inspec vendor`.") if !lockfile_exists? + unless metadata.dependencies.empty? + error.call(meta_path, 0, 0, nil, "Your profile needs to be vendored with `inspec vendor`.") unless lockfile_exists? end if lockfile_exists? # verify if metadata and lockfile are out of sync if lockfile.deps.size != metadata.dependencies.size @@ -391,11 +396,11 @@ metadata.dependencies.each do |dep| # Skip if the dependency does not specify a name next if dep[:name].nil? # TODO: should we also verify that the soure is the same? - if !lockfile.deps.map { |x| x[:name] }.include? dep[:name] + unless lockfile.deps.map { |x| x[:name] }.include? dep[:name] error.call(meta_path, 0, 0, nil, "Cannot find #{dep[:name]} in lockfile. Please re-vendor with `inspec vendor`.") end end end end @@ -415,10 +420,11 @@ params[:controls].each do |id, control| sfile = control[:source_location][:ref] sline = control[:source_location][:line] error.call(sfile, sline, nil, id, "Avoid controls with empty IDs") if id.nil? || id.empty? next if id.start_with? "(generated " + warn.call(sfile, sline, nil, id, "Control #{id} has no title") if control[:title].to_s.empty? warn.call(sfile, sline, nil, id, "Control #{id} has no descriptions") if control[:descriptions][:default].to_s.empty? warn.call(sfile, sline, nil, id, "Control #{id} has impact > 1.0") if control[:impact].to_f > 1.0 warn.call(sfile, sline, nil, id, "Control #{id} has impact < 0.0") if control[:impact].to_f < 0.0 warn.call(sfile, sline, nil, id, "Control #{id} has no tests defined") if control[:checks].nil? || control[:checks].empty? @@ -545,11 +551,11 @@ source_reader.data_files.to_a + [["inspec.yml", source_reader.metadata.content]] + [["inspec.lock.deps", YAML.dump(deps)]] files.sort_by { |a| a[0] } - .map { |f| res << f[0] << "\0" << f[1] << "\0" } + .map { |f| res << f[0] << "\0" << f[1] << "\0" } res.digest.unpack("H*")[0] end private @@ -589,10 +595,11 @@ params[:controls] = controls = {} params[:groups] = groups = {} prefix = @source_reader.target.prefix || "" tests&.each do |rule| next if rule.nil? + f = load_rule_filepath(prefix, rule) load_rule(rule, f, controls, groups) end params[:inputs] = Inspec::InputRegistry.list_inputs_for_profile(@profile_id) params @@ -621,10 +628,10 @@ # try and grab code text from merge locations if controls[id][:code].empty? && Inspec::Rule.merge_count(rule) > 0 Inspec::Rule.merge_changes(rule).each do |merge_location| code = Inspec::MethodSource.code_at(merge_location, source_reader) - if !code.empty? + unless code.empty? controls[id][:code] = code break end end end