module NdrError::Fuzzing

Mixin to help with fuzzing of exception messages/traces.

Public Instance Methods

fuzz(description, backtrace, parent_print = nil) click to toggle source
# File lib/ndr_error/fuzzing.rb, line 4
def fuzz(description, backtrace, parent_print = nil)
  Digest::MD5.hexdigest([
    fuzz_description(description),
    fuzz_backtrace(backtrace),
    parent_print
  ].compact.join)
end

Private Instance Methods

fuzz_backtrace(backtrace) click to toggle source

Fuzz a backtrace:

* independent of deployment paths
* independent of line numbers
# File lib/ndr_error/fuzzing.rb, line 23
def fuzz_backtrace(backtrace)
  backtrace.map { |line| fuzz_line(line) }.join("\n")
end
fuzz_description(description) click to toggle source

Prepare a fuzzed description:

# File lib/ndr_error/fuzzing.rb, line 15
def fuzz_description(description)
  # Apply the fuzzers sequentially:
  NdrError.description_fuzzers.inject(description) { |a, e| e.call(a) }
end
fuzz_line(line) click to toggle source
# File lib/ndr_error/fuzzing.rb, line 27
def fuzz_line(line)
  line = line.sub(Rails.root.to_s, 'Rails.root')

  # Remove gem version numbers from backtrace
  line.sub!(Regexp.new('/gems/([^/]*-)[0-9][0-9.]*/'), '/gems/\1/')

  # Remove GEM_PATH / LOAD_PATH differences:
  sub_paths!(line)

  # Remove Rails compiled callback & template identifiers:
  line.gsub!(Regexp.new('___?[0-9][0-9_]*[0-9]'), '__COMPILED_ID')

  # Remove line numbers:
  line.gsub!(/:(\d+):/, '')

  line
end
sub_paths!(line) click to toggle source
# File lib/ndr_error/fuzzing.rb, line 45
def sub_paths!(line)
  Gem.path.each do |path|
    line.sub!(Regexp.new('^' + Regexp.escape(path)), 'Gem.path')
  end

  $LOAD_PATH.each do |path|
    line.sub!(Regexp.new('^' + Regexp.escape(path)), '$LOAD_PATH')
  end
end