Skip to content

Commit

Permalink
chore: RHICOMPL-3871 Fix rubocop errors
Browse files Browse the repository at this point in the history
  • Loading branch information
marleystipich2 committed Jul 7, 2023
1 parent 2a24af9 commit 045ad46
Show file tree
Hide file tree
Showing 52 changed files with 440 additions and 350 deletions.
16 changes: 16 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ Metrics/BlockLength:
Exclude:
- 'test/**/*'
- 'spec/**/*'
- 'openscap_parser.gemspec'

Metrics/MethodLength:
AllowedMethods: ['included']

Metrics/AbcSize:
AllowedMethods: ['included']

Metrics/CyclomaticComplexity:
AllowedMethods: ['included']

Metrics/PerceivedComplexity:
AllowedMethods: ['included']

Gemspec/DevelopmentDependencies:
EnforcedStyle: gemspec

Layout/LineEndStringConcatenationIndentation:
Enabled: false
Expand Down
7 changes: 3 additions & 4 deletions lib/openscap_parser/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Mimics openscap-ruby Benchmark interface
module OpenscapParser
# A class for parsing Benchmark information
class Benchmark < XmlNode
include OpenscapParser::Util
include OpenscapParser::Rules
Expand All @@ -23,8 +24,7 @@ def id
end

def title
@title ||= @parsed_xml.xpath('title') &&
@parsed_xml.xpath('title').text
@title ||= @parsed_xml.xpath('title')&.text
end

def description
Expand All @@ -35,8 +35,7 @@ def description
end

def version
@version ||= @parsed_xml.xpath('version') &&
@parsed_xml.xpath('version').text
@version ||= @parsed_xml.xpath('version')&.text
end
end
end
2 changes: 1 addition & 1 deletion lib/openscap_parser/benchmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def benchmark
@benchmark ||= OpenscapParser::Benchmark.new(parsed_xml: benchmark_node)
end

def benchmark_node(xpath = ".//Benchmark")
def benchmark_node(xpath = './/Benchmark')
xpath_node(xpath)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/openscap_parser/datastream_file.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'openscap_parser/xml_file'
require 'openscap_parser/benchmarks'

Expand All @@ -9,6 +10,7 @@ class DatastreamFile < XmlFile

def valid?
return true if @parsed_xml.root.name == 'data-stream-collection' && namespaces.keys.include?('xmlns:ds')

false
end
end
Expand Down
18 changes: 10 additions & 8 deletions lib/openscap_parser/fix.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require 'openscap_parser/xml_node'
require 'openscap_parser/subs'

module OpenscapParser
# A class for parsing Fix information
class Fix < XmlNode
include OpenscapParser::Subs

Expand All @@ -27,7 +29,7 @@ def strategy
end

def full_text(set_values)
full_text_lines(set_values).join('')
full_text_lines(set_values).join
end

def full_text_lines(set_values)
Expand All @@ -42,13 +44,13 @@ def map_child_nodes(set_values = [])

def to_h
{
:id => id,
:system => system,
:complexity => complexity,
:disruption => disruption,
:strategy => strategy,
:text => text,
:subs => subs.map(&:to_h)
id:,
system:,
complexity:,
disruption:,
strategy:,
text:,
subs: subs.map(&:to_h)
}
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/openscap_parser/fixes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'openscap_parser/fix'

module OpenscapParser
# Methods related to finding and saving Fixes
module Fixes
def self.included(base)
base.class_eval do
Expand All @@ -12,7 +13,7 @@ def fixes
end
end

def fix_nodes(xpath = ".//fix")
def fix_nodes(xpath = './/fix')
xpath_nodes(xpath)
end
end
Expand Down
44 changes: 22 additions & 22 deletions lib/openscap_parser/group.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module OpenscapParser
# A class for parsing Group information
class Group < XmlNode
include OpenscapParser::Util

Expand All @@ -8,8 +10,7 @@ def id
end

def title
@title ||= parsed_xml.at_css('title') &&
parsed_xml.at_css('title').text
@title ||= parsed_xml.at_css('title')&.text
end

def description
Expand All @@ -28,16 +29,16 @@ def rationale

def requires
@requires ||= parsed_xml.xpath('./requires') &&
parsed_xml.xpath('./requires/@idref').flat_map do |r|
r.to_s&.split
end
parsed_xml.xpath('./requires/@idref').flat_map do |r|
r.to_s&.split
end
end

def conflicts
@conflicts ||= parsed_xml.xpath('./conflicts') &&
parsed_xml.xpath('./conflicts/@idref').flat_map do |c|
c.to_s&.split
end
parsed_xml.xpath('./conflicts/@idref').flat_map do |c|
c.to_s&.split
end
end

def selected
Expand All @@ -53,24 +54,23 @@ def parent_ids
end

def parent_type
if parsed_xml.xpath("name(..)='Group'")
@parent_type = 'Group'
else
@parent_type = 'Benchmark'
end
@parent_type = if parsed_xml.xpath("name(..)='Group'")
'Group'
else
'Benchmark'
end
end

def to_h
{
:id => id,
:title => title,
:description => description,
:requires => requires,
:conflicts => conflicts,
:rationale => rationale,
:selected => selected,
:parent_id => parent_id,
:parent_type => parent_type
id:, title:,
description:,
requires:,
conflicts:,
rationale:,
selected:,
parent_id:,
parent_type:
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openscap_parser/groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'openscap_parser/group'

module OpenscapParser
# Methods related to finding and saving rule references
# Methods related to finding and saving Groups
module Groups
def self.included(base)
base.class_eval do
Expand Down
6 changes: 4 additions & 2 deletions lib/openscap_parser/oval_report.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# frozen_string_literal: true

require 'openscap_parser/xml_file'
require 'oval/definition_result'
require 'oval/definition'

module OpenscapParser
# A class for parsing Oval report information
class OvalReport < XmlFile
def definition_results
@definition_results ||= definition_result_nodes.map { |node| ::Oval::DefinitionResult.new parsed_xml: node }
end

def definition_result_nodes(xpath = "./oval_results/results/system/definitions/definition")
def definition_result_nodes(xpath = './oval_results/results/system/definitions/definition')
xpath_nodes(xpath)
end

def definitions
@definitions ||= definition_nodes.map { |node| Oval::Definition.new parsed_xml: node }
end

def definition_nodes(xpath = "./oval_results/oval_definitions/definitions/definition")
def definition_nodes(xpath = './oval_results/oval_definitions/definitions/definition')
xpath_nodes(xpath)
end
end
Expand Down
42 changes: 18 additions & 24 deletions lib/openscap_parser/profile.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,47 @@
# frozen_string_literal: true

require 'openscap_parser/regex_handler'

module OpenscapParser
# A class for parsing Profile information
class Profile < XmlNode
def id
@id ||= @parsed_xml['id']
end

def extends_profile_id
@extends ||= @parsed_xml['extends']
@extends_profile_id ||= @parsed_xml['extends']
end

def title
@title ||= @parsed_xml.at_css('title') &&
@parsed_xml.at_css('title').text
@title ||= @parsed_xml.at_css('title')&.text
end
alias :name :title
alias name title

def description
@description ||= @parsed_xml.at_css('description') &&
@parsed_xml.at_css('description').text
@description ||= @parsed_xml.at_css('description')&.text
end

def selected_rule_ids
# Look for selected rule ids where the idref contains '_rule_' that is not preceded by 'group'
@selected_rule_ids ||= @parsed_xml.xpath("select[@selected='true']
[regex(@idref, '^((?!_group_).)*?(_rule_).*$')]
/@idref", RegexHandler) &&
@parsed_xml.xpath("select[@selected='true']
[regex(@idref, '^((?!_group_).)*?(_rule_).*$')]
/@idref", RegexHandler).map(&:text)
/@idref", RegexHandler)&.map(&:text)
end

def unselected_group_ids
# Look for group ids that are not selected where the idref contains '_group_' that is not preceded by 'rule'
@unselected_group_ids ||= @parsed_xml.xpath("select[@selected='false']
[regex(@idref, '^((?!_rule_).)*?(_group_).*$')]
/@idref", RegexHandler) &&
@parsed_xml.xpath("select[@selected='false']
[regex(@idref, '^((?!_rule_).)*?(_group_).*$')]
/@idref", RegexHandler).map(&:text)
/@idref", RegexHandler)&.map(&:text)
end

def selected_entity_ids
@selected_entity_ids ||= @parsed_xml.xpath("select[@selected='true']/@idref") &&
@parsed_xml.xpath("select[@selected='true']/@idref").map(&:text)
@selected_entity_ids ||= @parsed_xml.xpath("select[@selected='true']/@idref")&.map(&:text)
end

def refined_values
@refined_values ||= @parsed_xml.xpath("refine-value").each_with_object({}) do |element, rv|
@refined_values ||= @parsed_xml.xpath('refine-value').each_with_object({}) do |element, rv|
rv[element.at_xpath('@idref').text] = element.at_xpath('@selector').text
end
end
Expand All @@ -72,13 +66,13 @@ def refined_rule_weight

def to_h
{
:id => id,
:title => title,
:description => description,
:refined_values => refined_values,
:refined_rule_severity => refined_rule_severity,
:refined_rule_role => refined_rule_role,
:refined_rule_weight => refined_rule_weight
id:,
title:,
description:,
refined_values:,
refined_rule_severity:,
refined_rule_role:,
refined_rule_weight:
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openscap_parser/profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def profiles
end
end

def profile_nodes(xpath = ".//Profile")
def profile_nodes(xpath = './/Profile')
xpath_nodes(xpath)
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/openscap_parser/regex_handler.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

module OpenscapParser
# Methods for handling regex
class RegexHandler < XmlNode
def self.regex node_set, regex
def self.regex(node_set, regex)
node_set.find_all { |node| node.to_s =~ /#{regex}/ }
end
end
Expand Down
Loading

0 comments on commit 045ad46

Please sign in to comment.