Class: Fastlane::Actions::FlutterTestsAction::TestRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/flutter_tests/actions/flutter_tests_action.rb

Instance Method Summary collapse

Constructor Details

#initializeTestRunner

Returns a new instance of TestRunner.



82
83
84
# File 'lib/fastlane/plugin/flutter_tests/actions/flutter_tests_action.rb', line 82

def initialize
  @launched_tests = Hash.new { |hash, key| hash[key] = nil }
end

Instance Method Details

#_colorize(message, color) ⇒ Object

Wraps the message to color it

Parameters:

  • message (String)

    the message that has to be wrapped

  • color (Integer)

    the color of the message (34 -> blue, 32 -> green, 31 -> red)



90
91
92
# File 'lib/fastlane/plugin/flutter_tests/actions/flutter_tests_action.rb', line 90

def _colorize(message, color)
  "\e[#{color}m#{message}\e[0m"
end

#parse_json_output(line, print_only_failed) ⇒ Object

Parses the json output given by [self.run]



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fastlane/plugin/flutter_tests/actions/flutter_tests_action.rb', line 135

def parse_json_output(line, print_only_failed)
  unless line.to_s.strip.empty?
    output = JSON.parse(line)
    unless output.kind_of?(Array)
      type = output['type']
      case type
      when 'testStart'
        id = output['test']['id']
        name = output['test']['name']
        if name.include?('loading')
          return
        end

        test_item = Test.new(id, name)
        @launched_tests[test_item.get_id] = test_item
      when 'testDone'
        test_id = output['testID']
        test_item = @launched_tests[test_id]
        if !test_item.nil? && test_item.can_print
          was_skipped = output['skipped']
          test_item.mark_as_done(output['result'], was_skipped, nil, nil)
          if was_skipped || !print_only_failed
            test_item.print
          end
        end
      when 'error'
        test_id = output['testID']
        test_item = @launched_tests[test_id]
        if !test_item.nil? && test_item.can_print
          test_item.mark_as_done('error', false, output['error'], output['stackTrace'])
          test_item.print
        end
      else
        # ignored
      end
    end
  end
rescue StandardError => e
  UI.error("Got error during parse_json: #{e.message}")
  UI.error(e.backtrace.join('\n'))
  exit(1)
end

#run(flutter_command, print_only_failed, print_stats) ⇒ Object

Launches all the unit tests contained in the project folder



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fastlane/plugin/flutter_tests/actions/flutter_tests_action.rb', line 96

def run(flutter_command, print_only_failed, print_stats)
  Open3.popen3("#{flutter_command} test --machine") do |stdin, stdout, stderr, thread|
    stdout.each_line do |line|
      parse_json_output(line, print_only_failed)
    end
  end

  if print_stats
    stats = Hash.new { |hash, key| hash[key] = 0 }
    @launched_tests.values.each do |item|
      unless item.nil?
        stats[item.get_status] += 1
      end
    end

    skipped_tests = stats['skipped'].nil? ? 0 : stats['skipped']
    failed_tests = stats['error'].nil? ? 0 : stats['error']
    successful_tests = stats['success'].nil? ? 0 : stats['success']
    table = [
      %w[Successful Failed Skipped],
      [successful_tests, failed_tests, skipped_tests]
    ]

    messages = ["Ran #{@launched_tests.values.count { |e| !e.nil? }} tests"]
    colors = { 0 => 32, 1 => 31, 2 => 34 }
    max_length = 0
    (0..2).each do |i|
      msg = "#{table[0][i]}:\t#{table[1][i]}"
      max_length = [max_length, msg.length].max
      messages.append(_colorize(msg, colors[i]))
    end

    UI.message('-' * max_length)
    messages.each { |m| UI.message(m) }
    UI.message('-' * max_length)
  end
end