Module: RSpec::Hermetic::StableValue

Defined in:
lib/rspec/hermetic/stable_value.rb

Constant Summary collapse

MAX_DEPTH =
3

Class Method Summary collapse

Class Method Details

.capture(value, depth: MAX_DEPTH, seen: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec/hermetic/stable_value.rb', line 18

def capture(value, depth: MAX_DEPTH, seen: {})
  case value
  when nil, true, false, Symbol, Numeric
    value
  when String
    value.dup.freeze
  when Array
    return fingerprint(value) if depth <= 0

    value.map { |item| capture(item, depth: depth - 1, seen: seen) }.freeze
  when Hash
    return fingerprint(value) if depth <= 0

    value.keys.sort_by(&:to_s).to_h do |key|
      [
        capture(key, depth: depth - 1, seen: seen),
        capture(value[key], depth: depth - 1, seen: seen)
      ]
    end.freeze
  else
    object_fingerprint(value, depth, seen)
  end
end

.class_variables_for(value, depth, seen) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/rspec/hermetic/stable_value.rb', line 110

def class_variables_for(value, depth, seen)
  return {} unless value.is_a?(Module)

  value.class_variables.sort_by(&:to_s).to_h do |ivar|
    [ivar.to_s, capture(value.class_variable_get(ivar), depth: depth - 1, seen: seen)]
  rescue StandardError => error
    [ivar.to_s, "#{error.class}: #{error.message}"]
  end
end

.fingerprint(value) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rspec/hermetic/stable_value.rb', line 42

def fingerprint(value)
  dumped = Marshal.dump(value)
  "#{value.class}:marshal:#{Digest::SHA256.hexdigest(dumped)}"
rescue StandardError
  normalized = value.inspect.gsub(/0x[0-9a-f]+/i, "0x...")
  "#{value.class}:inspect:#{Digest::SHA256.hexdigest(normalized)}"
end

.graph_node(object, depth) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rspec/hermetic/stable_value.rb', line 120

def graph_node(object, depth)
  {
    class: object.class.name,
    depth: depth,
    value: immediate?(object) ? object : capture(object, depth: 1)
  }
rescue StandardError => error
  {
    class: object.class.name,
    depth: depth,
    error: "#{error.class}: #{error.message}"
  }
end

.immediate?(object) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/rspec/hermetic/stable_value.rb', line 134

def immediate?(object)
  object.nil? || object == true || object == false ||
    object.is_a?(Symbol) || object.is_a?(Numeric)
end

.instance_variables_for(value, depth, seen) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/rspec/hermetic/stable_value.rb', line 102

def instance_variables_for(value, depth, seen)
  value.instance_variables.sort_by(&:to_s).to_h do |ivar|
    [ivar.to_s, capture(value.instance_variable_get(ivar), depth: depth - 1, seen: seen)]
  rescue StandardError => error
    [ivar.to_s, "#{error.class}: #{error.message}"]
  end
end

.object_fingerprint(value, depth, seen) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rspec/hermetic/stable_value.rb', line 83

def object_fingerprint(value, depth, seen)
  return fingerprint(value) if depth <= 0
  return "#{value.class}:cycle:#{seen[value.object_id]}" if seen.key?(value.object_id)

  seen[value.object_id] = seen.length
  dumped = Marshal.dump(value)
  "#{value.class}:marshal:#{Digest::SHA256.hexdigest(dumped)}"
rescue StandardError
  payload = {
    class: value.class.name,
    inspect: value.inspect.gsub(/0x[0-9a-f]+/i, "0x..."),
    ivars: instance_variables_for(value, depth, seen),
    class_vars: class_variables_for(value, depth, seen)
  }
  "#{value.class}:graph:#{Digest::SHA256.hexdigest(Marshal.dump(payload))}"
ensure
  seen.delete(value.object_id)
end

.reachable_graph_fingerprint(value, max_depth:, max_nodes:) ⇒ Object



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
# File 'lib/rspec/hermetic/stable_value.rb', line 50

def reachable_graph_fingerprint(value, max_depth:, max_nodes:)
  return capture(value) unless ObjectSpace.respond_to?(:reachable_objects_from)

  seen = {}
  queue = [[value, 0]]
  nodes = []

  until queue.empty? || nodes.length >= max_nodes
    object, depth = queue.shift
    next if seen[object.object_id]

    begin
      seen[object.object_id] = true
      nodes << graph_node(object, depth)
      next if depth >= max_depth

      ObjectSpace.reachable_objects_from(object).each do |child|
        next if immediate?(child)

        queue << [child, depth + 1]
      end
    rescue StandardError
      next
    end
  end

  {
    graph: Digest::SHA256.hexdigest(Marshal.dump(nodes.sort_by(&:to_s))),
    nodes: nodes.length,
    truncated: !queue.empty?
  }
end