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



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

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



94
95
96
97
98
99
100
101
102
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 94

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



86
87
88
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 86

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

#_launch_tests(device_id, reuse_build) ⇒ Object

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



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
78
79
80
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 51

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
  @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}" : ''}")
    UI.message("Test #{count} ended with code '#{_get_exit_code(status)}'")
    count += 1
  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



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

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]



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
133
# File 'lib/fastlane/plugin/flutter_tests/helper/flutter_integration_test_helper.rb', line 108

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) ⇒ Object

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



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

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