Class: Fastlane::Helper::FlutterIntegrationTestHelper

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

Instance Method Summary collapse

Constructor Details

#initialize(driver, test_folder, flutter_command) ⇒ FlutterIntegrationTestHelper

Initialize the helper that launches the integration tests

integration tests

Parameters:

  • driver (String)

    the path to the file that will be used as driver

  • test_folder (String)

    the path to the folder that contains the

  • flutter_command (String)

    the command to launch flutter



14
15
16
17
18
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 14

def initialize(driver, test_folder, flutter_command)
  @driver = driver
  @integration_tests = _load_files(test_folder)
  @flutter_command = flutter_command
end

Instance Method Details

#_get_apk_path(message) ⇒ Object

Parse the flutter build output looking for a .apk path

Parameters:

  • message (String)

    the stdout of flutter build process

Returns:

  • the path to the apk that has been built



111
112
113
114
115
116
117
118
119
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 111

def _get_apk_path(message)
  components = message.split(/\n/).last.split(' ')
  if components.any? { |line| line.end_with? '.apk' }
    components.detect { |c| c.end_with? '.apk' }
  else
    UI.warn('Apk path not found in the stdout')
    nil
  end
end

#_get_exit_code(exit_status) ⇒ Object

Returns the exit code of a process

Parameters:

  • exit_status (String)

    status given back by [Open3]

Returns:

  • The exit code (0|1) as string



103
104
105
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 103

def _get_exit_code(exit_status)
  exit_status.to_s.split(' ').last
end

#_launch_tests(device_id, reuse_build) ⇒ Integer

Executes the tests found on the device_id

Parameters:

  • device_id (String)

    the id of the device previously found

  • reuse_build (Boolean)

    If it’s true, it will run the build only for the first integration test

Returns:

  • (Integer)

    Value 0 or 1 if all tests were run correctly or not



54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 54

def _launch_tests(device_id, reuse_build)
  apk_path = nil
  if reuse_build
    UI.message("Building apk")
    out, err, status = Open3.capture3("#{@flutter_command} build apk")
    if _get_exit_code(status) != '0'
      UI.error("Failed to build apk")
      puts err
      exit(1)
    else
      apk_path = _get_apk_path(out)
      if !apk_path.nil? && File.file?(apk_path)
        UI.message("Build apk at path #{apk_path}")
        #TODO
      else
        UI.error("Apk path not found or it's not accessible")
        exit(1)
      end

    end
  end

  count = 0

  tests = {
    "successful" => 0,
    "failed" => 0,
  }

  @integration_tests.each do |test|
    UI.message("Launching test #{count}/#{@integration_tests.length}: #{test.split("/").last}")
    _, __, status = Open3.capture3("#{@flutter_command} drive --target #{@driver} --driver #{test} -d #{device_id} #{reuse_build ? "--use-application-binary #{apk_path}" : ''}")
    successful = _get_exit_code(status) == '0'
    color = successful ? 'green' : 'red'
    tests[successful ? 'successful' : 'failed'] += 1

    UI.message(Utilities.new.colorize("Test #{test.split("/").last} #{successful ? 'terminated correctly' : 'failed'}", color))
    count += 1
  end

  unless tests['failed'] == 0
    UI.user_error!("There are some integration tests that fail")
  end
end

#_load_files(test_folder) ⇒ Array

Loads all the integration test files

Parameters:

  • test_folder (String)

    the path that contains the test files

Returns:

  • (Array)

    An array containing all the paths to the files found



24
25
26
27
28
29
30
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 24

def _load_files(test_folder)
  test_files = Dir.glob("#{test_folder}/**/*").reject do |f|
    File.directory?(f) || !f.end_with?('_test.dart')
  end
  UI.message("Found #{test_files.length} test files")
  test_files
end

#_run_test_device(platform, force_launch) ⇒ Object

Checks if there’s a device running and gets its id

Parameters:

  • platform (String)

    Specifies the type of device that should be found

  • force_launch (Boolean)

    If it’s true and there aren’t any devices ready, the plugin will try to start one for the given platform

Returns:

  • The deviceId if the device exists or [nil]



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 125

def _run_test_device(platform, force_launch)
  out, _ = Open3.capture2("#{@flutter_command} devices | grep #{platform}")
  device_id = nil
  if out.to_s.strip.empty? && force_launch
    out, _ = Open3.capture2("#{@flutter_command} emulators | grep #{platform}")
    if out.to_s.strip.empty?
      UI.error("No emulators found for platform #{platform}")
      exit(1)
    end

    emulator_id = out.to_s.split('')[0]
    Open3.capture2("#{@flutter_command} emulators --launch #{emulator_id}")

    out, _ = Open3.capture2("#{@flutter_command} devices | grep #{platform}")
  else
    device_id = (out.to_s.split("")[1]).strip
    UI.message("Found already running device: #{device_id}")
  end

  unless out.to_s.strip.empty?
    device_id = (out.to_s.split("")[1]).strip
    UI.message("Got device id #{device_id}")
  end

  device_id.nil? ? nil : device_id
end

#run(platform, force_launch, reuse_build) ⇒ Integer

Launches the tests sequentially

Parameters:

  • platform (String)

    Specifies on which platform the tests should be run

  • force_launch (Boolean)

    If it’s true and there aren’t any devices ready, the plugin will try to start one for the given platform

  • reuse_build (Boolean)

    If it’s true, it will run the build only for the first integration test

Returns:

  • (Integer)

    Value 0 or 1 if all tests were run correctly or not



38
39
40
41
42
43
44
45
46
47
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 38

def run(platform, force_launch, reuse_build)
  UI.message("Checking for running devices")
  device_id = _run_test_device(platform, force_launch)
  if !device_id.nil?
    _launch_tests(device_id, reuse_build)
  else
    UI.error("Failed to find a device to launch the tests on")
    exit(1)
  end
end