Sha256: 4b6c685cb15e842790f7f03ea1cefdea907da00d34ed45dbba68caf6b7e6fbd0

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require_relative "rspec/version"

require "stack_trace/viz"
require "pathname"
require "securerandom"

RSpec.configuration.after(:suite) do
  StackTrace::Rspec.finish_tracing
end

RSpec.configuration.around(:each) do |example|
  StackTrace.trace { example.run }

  StackTrace::Rspec.store_trace(StackTrace.current, example)
end

module StackTrace
  module Rspec
    EXAMPLE_META_KEYS = %i[file_path line_number scoped_id description full_description].freeze
    FINAL_MESSAGE = <<~TEXT
      \e[1m
      StackTrace:

      Trace information is saved into \e[32m%<file_path>s\e[0m
      \e[22m
    TEXT

    class << self
      def finish_tracing
        ensure_path_is_created!

        html.save(file_path)
            .then { |path| print_message(path) }
      end

      def store_trace(trace, example)
        html.add(trace, **example.metadata.slice(*EXAMPLE_META_KEYS))
      end

      private

      def ensure_path_is_created!
        Dir.mkdir(file_dir) unless File.directory?(file_dir)
      end

      def html
        @html ||= StackTrace::Viz::HTML.new
      end

      def print_message(path)
        puts format(FINAL_MESSAGE, file_path: path)
      end

      def file_path
        file_dir.join("#{SecureRandom.uuid}.html")
      end

      def file_dir
        Pathname.new(RSpec.configuration.default_path)
                .join("stack_trace")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
stack_trace-rspec-0.6.0 lib/stack_trace/rspec.rb
stack_trace-rspec-0.5.0 lib/stack_trace/rspec.rb
stack_trace-rspec-0.4.0 lib/stack_trace/rspec.rb
stack_trace-rspec-0.3.0 lib/stack_trace/rspec.rb
stack_trace-rspec-0.2.0 lib/stack_trace/rspec.rb
stack_trace-rspec-0.1.4 lib/stack_trace/rspec.rb