Class: Fastlane::Helper::FlutterUnitTestHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/flutter_tests/helper/flutter_unit_test_helper.rb

Instance Method Summary collapse

Constructor Details

#initializeFlutterUnitTestHelper

Returns a new instance of FlutterUnitTestHelper.



8
9
10
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_unit_test_helper.rb', line 8

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

Instance Method Details

#parse_json_output(line, print_only_failed) ⇒ Object

Parses the json output given by [self.run]

Parameters:

  • line (String)

    The json as string that has to be parsed

  • print_only_failed (Boolean)

    See definition on run



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_unit_test_helper.rb', line 65

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
            UI.message(test_item.generate_message)
          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'])
          UI.message(test_item.generate_message)
        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

the launched tests

Parameters:

  • flutter_command (String)

    Contains the command to launch flutter

  • print_only_failed (Boolean)

    If true, prints only skipped and failed tests

  • print_stats (Boolean)

    If true, it prints a table containing the info about



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_unit_test_helper.rb', line 19

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 => 'green', 1 => 'red', 2 => 'blue' }
    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(Utilities.new.colorize(msg, colors[i]))
    end

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

    unless failed_tests == 0
      UI.user_error!("There are some unitTests that fail")
    end
  end
end