Class: Towel::Minitest::Reporter

Inherits:
Minitest::AbstractReporter
  • Object
show all
Defined in:
lib/towel/minitest/reporter.rb

Overview

Reports Minitest results to Towel.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Reporter

Returns a new instance of Reporter.



5
6
7
8
9
10
11
12
# File 'lib/towel/minitest/reporter.rb', line 5

def initialize(options)
  @session = Towel::Session.new
  @stdout = nil
  @stderr = nil
  @previous_stdout = nil
  @previous_stderr = nil
  @cancelled = false
end

Instance Method Details

#prerecord(klass, name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/towel/minitest/reporter.rb', line 35

def prerecord(klass, name)
  context = @session.create_result(
    klass.to_s,
    name,
    display_name: result_name(klass, name)
  )

  @stdout.context = context
  @stderr.context = context
end

#record(result) ⇒ Object



46
47
48
49
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
# File 'lib/towel/minitest/reporter.rb', line 46

def record(result)
  @stdout.context = nil
  @stderr.context = nil

  if result.passed?
    state = Towel::V1alpha::ResultState::SUCCESS
  elsif result.skipped?
    state = Towel::V1alpha::ResultState::SKIPPED
  elsif result.error?
    state = Towel::V1alpha::ResultState::ERROR
  else
    state = Towel::V1alpha::ResultState::FAILURE
  end

  duration = nil
  if !result.skipped?
    duration = result.time
  end

  description = nil
  if !result.failures.empty?
    description = result.failures.first.message
  end

  @session.update_result(
    result.klass,
    result.name,
    state: state,
    duration: duration,
    description: description
  )
end

#reportObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/towel/minitest/reporter.rb', line 79

def report
  $stdout = @previous_stdout
  $stderr = @previous_stderr

  if @cancelled
    @session.cancel_invocation
  else
    @session.finish_invocation
  end
end

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/towel/minitest/reporter.rb', line 14

def start
  url = @session.create_invocation
  puts "View test results at #{url}"

  # Register a cancellation handler
  old_handler = Signal.trap("INT") do
    @cancelled = true
    if old_handler.respond_to?(:call)
      old_handler.call
    else
      raise Interrupt
    end
  end

  # Capture logs
  @previous_stdout = $stdout
  $stdout = @stdout = Towel::LogIO.new(@session.create_log("STDOUT"))
  @previous_stderr = $stderr
  $stderr = @stderr = Towel::LogIO.new(@session.create_log("STDERR"))
end