Class: Maze::Option::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/option/validator.rb

Overview

Validates command line options

Instance Method Summary collapse

Instance Method Details

#validate(options) ⇒ Object

Validates all provided options

Parameters:

  • options (Hash)

    Parsed command line options



12
13
14
15
16
17
18
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
# File 'lib/maze/option/validator.rb', line 12

def validate(options)
  errors = []

  #
  # Common options
  #

  # --farm
  farm = options[Option::FARM]
  if farm && !%w[bs local bb].include?(farm)
    errors << "--#{Option::FARM} must be 'bs', 'bb' or 'local' if provided"
  end

  # --capabilities
  begin
    JSON.parse(options[Option::CAPABILITIES])
  rescue JSON::ParserError
    errors << "--#{Option::CAPABILITIES} must be valid JSON (given #{options[Option::CAPABILITIES]})"
  end

  # --repeater-api-key
  key = options[Option::BUGSNAG_REPEATER_API_KEY]
  key_regex = /^[0-9a-fA-F]{32}$/
  if key && !key_regex.match?(key)
    errors << "--#{Option::BUGSNAG_REPEATER_API_KEY} must be set to a 32-character hex value"
  end

  # Farm specific options
  validate_bs options, errors if farm == 'bs'
  validate_bitbar options, errors if farm == 'bb'
  validate_local options, errors if farm == 'local'

  errors
end

#validate_bitbar(options, errors) ⇒ Object

Validates BitBar device options



89
90
91
92
93
94
95
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
# File 'lib/maze/option/validator.rb', line 89

def validate_bitbar(options, errors)
  browser = options[Option::BROWSER]
  device = options[Option::DEVICE]
  
  errors << "--#{Option::USERNAME} must be specified" if options[Option::USERNAME].nil?
  errors << "--#{Option::ACCESS_KEY} must be specified" if options[Option::ACCESS_KEY].nil?

  # Device
  if browser.nil? && device.empty?
    errors << "Either --#{Option::BROWSER} or --#{Option::DEVICE} must be specified"
  elsif browser
    browsers = YAML.safe_load(File.read("#{__dir__}/../client/selenium/bb_browsers.yml"))

    if browsers.include? browser
      if options[Option::BROWSER_VERSION].nil? && !browsers[browser].include?('version')
        errors << "--#{Option::BROWSER_VERSION} must be specified for browser '#{browser}'"
      end
    else
      browser_list = browsers.keys.join ', '
      errors << "Browser type '#{browser}' unknown on BitBar.  Must be one of: #{browser_list}."
    end
  elsif device
    app = Maze::Helper.read_at_arg_file options[Option::APP]
    if app.nil?
      errors << "--#{Option::APP} must be provided when running on a device"
    else
      uuid_regex = /\A[0-9]+\z/
      unless uuid_regex.match? app
        app = Maze::Helper.expand_path app
        errors << "app file '#{app}' not found" unless File.exist?(app)
      end
    end
  end
end

#validate_bs(options, errors) ⇒ Object

Validates BrowserStack options



48
49
50
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
81
82
83
84
85
86
# File 'lib/maze/option/validator.rb', line 48

def validate_bs(options, errors)
  # BS local binary
  bs_local = Maze::Helper.expand_path options[Option::BS_LOCAL]
  errors << "BrowserStack local binary '#{bs_local}' not found" unless File.exist? bs_local

  # Device
  browser = options[Option::BROWSER]
  device = options[Option::DEVICE]
  if browser.nil? && device.empty?
    errors << "Either --#{Option::BROWSER} or --#{Option::DEVICE} must be specified"
  elsif browser

    browsers = YAML.safe_load(File.read("#{__dir__}/../client/selenium/bs_browsers.yml"))

    unless browsers.include? browser
      browser_list = browsers.keys.join ', '
      errors << "Browser type '#{browser}' unknown on BrowserStack.  Must be one of: #{browser_list}."
    end
  elsif device
    device.each do |device_key|
      next if Maze::Client::Appium::BrowserStackDevices::DEVICE_HASH.key? device_key
      errors << "Device type '#{device_key}' unknown on BrowserStack.  Must be one of #{Maze::Client::Appium::BrowserStackDevices::DEVICE_HASH.keys}"
    end
    # App
    app = Maze::Helper.read_at_arg_file options[Option::APP]
    if app.nil?
      errors << "--#{Option::APP} must be provided when running on a device"
    else
      unless app.start_with?('bs://')
        app = Maze::Helper.expand_path app
        errors << "app file '#{app}' not found" unless File.exist?(app)
      end
    end
  end

  # Credentials
  errors << "--#{Option::USERNAME} must be specified" if options[Option::USERNAME].nil?
  errors << "--#{Option::ACCESS_KEY} must be specified" if options[Option::ACCESS_KEY].nil?
end

#validate_local(options, errors) ⇒ Object

Validates Local device options



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
151
152
# File 'lib/maze/option/validator.rb', line 125

def validate_local(options, errors)
  if options[Option::BROWSER].nil?
    errors << "--#{Option::APP} must be specified" if options[Option::APP].nil?

    # OS
    if options[Option::OS].nil?
      errors << "--#{Option::OS} must be specified"
    else
      os = options[Option::OS].downcase
      errors << 'os must be android, ios, macos or windows' unless %w[android ios macos windows].include? os
      if os == 'ios'
        errors << "--#{Option::APPLE_TEAM_ID} must be specified for iOS" if options[Option::APPLE_TEAM_ID].nil?
        errors << "--#{Option::UDID} must be specified for iOS" if options[Option::UDID].nil?
      end
    end

    # OS Version
    unless options[Option::OS_VERSION].nil?
      # Ensure OS version is a valid float so that notifier tests can perform numeric checks
      # e.g 'Maze.config.os_version > 7'
      unless /^[1-9][0-9]*(\.[0-9])?/.match? options[Option::OS_VERSION]
        errors << "--#{Option::OS_VERSION} must be a valid version matching '/^[1-9][0-9]*(\\.[0-9])?/'"
      end
    end
  else
    # TODO Validate browser options
  end
end