Class: AbideDevUtils::XCCDF::Diff::BenchmarkDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/xccdf/diff.rb

Overview

Class for benchmark diffs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml1, xml2, opts = {}) ⇒ BenchmarkDiff

Returns a new instance of BenchmarkDiff.

Parameters:

  • xml1 (String)

    path to the first benchmark XCCDF xml file

  • xml2 (String)

    path to the second benchmark XCCDF xml file

  • opts (Hash) (defaults to: {})

    options hash



29
30
31
32
33
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 29

def initialize(xml1, xml2, opts = {})
  @this = new_benchmark(xml1)
  @other = new_benchmark(xml2)
  @opts = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



24
25
26
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 24

def opts
  @opts
end

#otherObject (readonly)

Returns the value of attribute other.



24
25
26
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 24

def other
  @other
end

#thisObject (readonly)

Returns the value of attribute this.



24
25
26
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 24

def this
  @this
end

Instance Method Details

#diffObject



39
40
41
42
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 39

def diff
  warn 'Full benchmark diff is not yet implemented, return rules diff for now'
  diff_rules
end

#diff_rawObject



35
36
37
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 35

def diff_raw
  @diff_raw ||= @this.diff(@other)
end

#diff_rulesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 48

def diff_rules
  return diff_rules_raw if opts[:raw]
  return [] if diff_rules_raw.all? { |r| r.type == :equal }

  diff_hash = {
    from: @this.to_s,
    to: @other.to_s,
    rules: {}
  }
  diff_rules_raw.each do |rule|
    diff_hash[:rules][rule.type] ||= []
    case rule.type
    when :added
      diff_hash[:rules][rule.type] << { number: rule.new_value.number.to_s, title: rule.new_value.title.to_s }
    when :removed
      diff_hash[:rules][rule.type] << { number: rule.old_value.number.to_s, title: rule.old_value.title.to_s }
    else
      rd_hash = {}
      rd_hash[:from] = "#{rule.old_value&.number} #{rule.old_value&.title}" if rule.old_value
      rd_hash[:to] = "#{rule.new_value&.number} #{rule.new_value&.title}" if rule.new_value
      changes = rule.details.transform_values { |v| v.is_a?(Array) ? v.map(&:to_s) : v.to_s }
      if opts[:ignore_changed_properties]
        changes.delete_if { |k, _| opts[:ignore_changed_properties].include?(k.to_s) }
        next if changes.empty? # Skip entirely if all changed filtered out
      end
      rd_hash[:changes] = changes unless changes.empty?
      diff_hash[:rules][rule.type] << rd_hash
    end
  end
  unless opts[:no_stats]
    stats_hash = {}
    diff_hash[:rules].each do |type, rules|
      stats_hash[type] = rules.size
    end
    diff_hash[:stats] = stats_hash unless stats_hash.empty?
  end
  diff_hash
end

#diff_rules_rawObject



44
45
46
# File 'lib/abide_dev_utils/xccdf/diff.rb', line 44

def diff_rules_raw
  @diff_rules_raw ||= @this.diff_only_rules(@other)
end