Sha256: 30f1c206c224e9505afcd92fe89f82211c47abacb85fe9bfe3aa0c3655be224c

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# frozen-string-literal: true

require "json"

RSpec.configuration.before(:suite) do
  StackTrace::Integration::Rspec.create_tracing_directory
end

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

RSpec.configuration.around(:each) do |example|
  StackTrace.trace do
    example.run
    StackTrace::Integration::Rspec.save_trace_for(example)
  end
end

module StackTrace
  module Integration
    class Rspec
      EXAMPLE_META_KEYS = %i(file_path line_number scoped_id description full_description)
      FINAL_MESSAGE = <<~TEXT
        \e[1m
        StackTrace:

        Trace information is saved into \e[32m%{file_path}\e[0m
        \e[22m
      TEXT

      class << self
        def create_tracing_directory
          Dir.mkdir(tracing_dir_path) unless Dir.exist?(tracing_dir_path)
        end

        def finish_tracing
          save_examples
          print_message
        end

        def save_trace_for(example)
          examples << example_data(example)
        end

        private

        def examples
          @examples ||= []
        end

        def save_examples
          File.write(tracing_file_path, examples.to_json)
        end

        def tracing_file_path
          File.join(tracing_dir_path, trace_file_name)
        end

        def tracing_dir_path
          File.expand_path("stack_trace")
        end

        def trace_file_name
          @trace_file_name ||= Time.now.strftime('%d_%m_%Y %H_%M_%S.json')
        end

        def example_data(example)
          example.metadata.slice(*EXAMPLE_META_KEYS)
                          .merge!(trace: Trace.current.as_json)
        end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stack_trace-0.2.1 lib/stack_trace/integration/rspec.rb
stack_trace-0.2.0 lib/stack_trace/integration/rspec.rb