|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "yaml" |
| 4 | + |
| 5 | +class E2EMatrix |
| 6 | + attr_reader :config_path |
| 7 | + |
| 8 | + def self.load(config_path) |
| 9 | + new(config_path, YAML.safe_load_file(config_path)) |
| 10 | + end |
| 11 | + |
| 12 | + def initialize(config_path, config) |
| 13 | + @config_path = config_path |
| 14 | + @config = config || {} |
| 15 | + end |
| 16 | + |
| 17 | + def expand |
| 18 | + applications.flat_map do |application| |
| 19 | + os_tracks.flat_map do |os_track| |
| 20 | + suites.map do |suite| |
| 21 | + build_run(application, os_track, suite) |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + def run_at(index) |
| 28 | + runs = expand |
| 29 | + runs.fetch(index) |
| 30 | + end |
| 31 | + |
| 32 | + def validation_errors |
| 33 | + errors = [] |
| 34 | + errors << "version must be 1" unless @config.fetch("version", nil) == 1 |
| 35 | + validate_collection(errors, "applications", applications) |
| 36 | + validate_collection(errors, "os_tracks", os_tracks) |
| 37 | + validate_collection(errors, "suites", suites) |
| 38 | + validate_applications(errors) |
| 39 | + validate_os_tracks(errors) |
| 40 | + validate_suites(errors) |
| 41 | + errors |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + def build_run(application, os_track, suite) |
| 47 | + platform = application.fetch("platform") |
| 48 | + os_track_id = os_track_id(os_track) |
| 49 | + suite_id = suite.fetch("id") |
| 50 | + application_id = application.fetch("id") |
| 51 | + |
| 52 | + { |
| 53 | + "id" => "#{application_id}-#{os_track_id}-#{suite_id}", |
| 54 | + "application_id" => application_id, |
| 55 | + "target" => application.fetch("target"), |
| 56 | + "platform" => platform, |
| 57 | + "os_track" => os_track_id, |
| 58 | + "device_selector" => device_selector(platform, os_track), |
| 59 | + "app_id" => application.fetch("app_id"), |
| 60 | + "artifact_env" => application.fetch("artifact_env"), |
| 61 | + "execute" => suite.fetch("execute"), |
| 62 | + "ready_marker" => application.fetch("ready_marker"), |
| 63 | + "status_context" => "checkout-kit/e2e/#{application_id}/#{os_track_id}/#{suite_id}" |
| 64 | + } |
| 65 | + end |
| 66 | + |
| 67 | + def device_selector(platform, os_track) |
| 68 | + return os_track.fetch("device_selector") if os_track.is_a?(Hash) && os_track.key?("device_selector") |
| 69 | + |
| 70 | + "#{platform}:phone:#{os_track_id(os_track)}" |
| 71 | + end |
| 72 | + |
| 73 | + def os_track_id(os_track) |
| 74 | + os_track.is_a?(Hash) ? os_track.fetch("id") : os_track |
| 75 | + end |
| 76 | + |
| 77 | + def applications |
| 78 | + @config.fetch("applications", []) || [] |
| 79 | + end |
| 80 | + |
| 81 | + def os_tracks |
| 82 | + @config.fetch("os_tracks", []) || [] |
| 83 | + end |
| 84 | + |
| 85 | + def suites |
| 86 | + @config.fetch("suites", []) || [] |
| 87 | + end |
| 88 | + |
| 89 | + def validate_collection(errors, name, collection) |
| 90 | + errors << "#{name} must be a non-empty array" unless collection.is_a?(Array) && !collection.empty? |
| 91 | + end |
| 92 | + |
| 93 | + def validate_applications(errors) |
| 94 | + return unless applications.is_a?(Array) |
| 95 | + |
| 96 | + validate_unique_ids(errors, "application", applications) |
| 97 | + applications.each do |application| |
| 98 | + id = application.fetch("id", "<missing>") |
| 99 | + required_application_keys.each do |key| |
| 100 | + errors << "application #{id} missing #{key}" if application.fetch(key, "").to_s.empty? |
| 101 | + end |
| 102 | + platform = application.fetch("platform", nil) |
| 103 | + errors << "application #{id} platform must be ios or android" unless ["ios", "android"].include?(platform) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def validate_os_tracks(errors) |
| 108 | + return unless os_tracks.is_a?(Array) |
| 109 | + |
| 110 | + ids = os_tracks.map { |os_track| safe_os_track_id(os_track) } |
| 111 | + errors << "os_track ids must be unique" unless ids.compact.uniq.length == ids.compact.length |
| 112 | + os_tracks.each do |os_track| |
| 113 | + id = safe_os_track_id(os_track) |
| 114 | + errors << "os_track missing id" if id.to_s.empty? |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + def safe_os_track_id(os_track) |
| 119 | + os_track_id(os_track) |
| 120 | + rescue KeyError |
| 121 | + nil |
| 122 | + end |
| 123 | + |
| 124 | + def validate_suites(errors) |
| 125 | + return unless suites.is_a?(Array) |
| 126 | + |
| 127 | + validate_unique_ids(errors, "suite", suites) |
| 128 | + suites.each do |suite| |
| 129 | + id = suite.fetch("id", "<missing>") |
| 130 | + execute = suite.fetch("execute", "") |
| 131 | + errors << "suite #{id} missing execute" if execute.empty? |
| 132 | + next if execute.empty? |
| 133 | + |
| 134 | + errors << "suite #{id} execute path does not exist: #{execute}" unless File.exist?(File.join(e2e_root, execute)) |
| 135 | + end |
| 136 | + end |
| 137 | + |
| 138 | + def validate_unique_ids(errors, label, collection) |
| 139 | + ids = collection.map { |item| item.fetch("id", nil) } |
| 140 | + errors << "#{label} ids must be unique" unless ids.compact.uniq.length == ids.compact.length |
| 141 | + end |
| 142 | + |
| 143 | + def required_application_keys |
| 144 | + ["id", "target", "platform", "app_id", "artifact_env", "ready_marker"] |
| 145 | + end |
| 146 | + |
| 147 | + def e2e_root |
| 148 | + File.expand_path("..", File.dirname(config_path)) |
| 149 | + end |
| 150 | +end |
0 commit comments