Skip to content

Instantly share code, notes, and snippets.

@0x000def42
Created February 28, 2024 21:11
Show Gist options
  • Save 0x000def42/56415347effeecbce100e25feb492107 to your computer and use it in GitHub Desktop.
Save 0x000def42/56415347effeecbce100e25feb492107 to your computer and use it in GitHub Desktop.
# Add somewhere inside you spec file
require 'ruby_parser'
RSpec.configure do |config|
config.after(:suite) do
# Calling compare_traces after all suites complete
compare_traces('trace_with_feature_on.txt', 'trace_with_feature_off.txt')
end
end
SKIP_FILES = Set.new([
'/app/config/initializers/active_record_custom_preloader.rb',
# you can add more file to skip them from trace tracker
]);
def trace_diff(file_name)
traces = []
trace = TracePoint.new(:line) do |tp|
next unless tp.path.include?('/app/')
next if tp.path.include?('/app/spec')
next if SKIP_FILES.include?(tp.path)
traces << [tp.path, tp.lineno]
end
trace.enable
yield
trace.disable
files = {}
File.open(file_name, 'w') do |file|
width = 60
traces.each do |tp|
files[tp[0]] ||= File.readlines(tp[0])
line_content = files[tp[0]][tp[1] - 1].strip
offset = ' ' * [width - tp[0].size - tp[1].to_s.size, 1].max
file.puts "#{tp[0]}:#{tp[1]}#{offset}#{line_content}:"
end
end
end
def compare_traces(file1, file2)
return if !File.exists?(file1) || !File.exists?(file2)
trace1 = File.readlines(file1)
trace2 = File.readlines(file2)
trace1.zip(trace2).each_with_index do |(line1, line2), index|
if line1 != line2
start_line = [0, index - 5].max
middle_line = index
end_line = [index + 5, trace1.size - 1].min
puts "Расхождение обнаружено на строке #{index + 1}"
puts "Фрагмент из файла #{file1}:"
puts trace1[start_line...middle_line].join
puts ">>>#{trace1[middle_line]}"
puts trace1[1 + middle_line..end_line].join
puts "\nФрагмент из файла #{file2}:"
puts trace2[start_line...middle_line].join
puts ">>>#{trace2[middle_line]}"
puts trace2[1 + middle_line..end_line].join
break
end
end
end
it do
trace_diff('trace_with_feature_off.txt) do
action
end
end
it do
Flipper[...].enable
trace_diff('trace_with_feature_on.txt) do
action
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment