class Sauce::Config

Constants

BROWSERS
DEFAULT_BROWSERS
DEFAULT_OPTIONS
ENVIRONMENT_VARIABLES
PLATFORMS
POTENTIAL_PORTS
SAUCE_OPTIONS

Attributes

opts[R]

Public Class Methods

called_from_integrations() click to toggle source
# File lib/sauce/config.rb, line 87
def self.called_from_integrations
  Sauce.logger.debug "Sauce config accessed from an integration"
  @called_from_integrations = true
end
called_from_integrations?() click to toggle source
# File lib/sauce/config.rb, line 83
def self.called_from_integrations?
  @called_from_integrations || false
end
get_application_port() click to toggle source
# File lib/sauce/config.rb, line 77
def self.get_application_port
  port_index = ENV["TEST_ENV_NUMBER"].to_i
  port_to_use = POTENTIAL_PORTS[port_index]
  return port_to_use
end
new(opts={}) click to toggle source

Creates a new instance of Sauce::Config

@param [Hash, Boolean] opts Any value you'd set with [:option], as a hash. If false, skip loading default options @option opts [Boolean] :without_defaults Set true to skip loading default values

@return [Sauce::Config]

# File lib/sauce/config.rb, line 98
def initialize(opts={})
  @opts = {}
  @undefaulted_opts = {}
  if opts != false
    if (!opts[:without_defaults]) 
      @opts.merge! DEFAULT_OPTIONS
      @opts.merge! DEFAULT_BROWSERS
      @opts.merge!({:application_port => Sauce::Config.get_application_port})

      @undefaulted_opts.merge! load_options_from_yaml
      @undefaulted_opts.merge! load_options_from_environment
      @undefaulted_opts.merge! load_options_from_heroku unless ENV["SAUCE_DISABLE_HEROKU_CONFIG"]
      
      global_config = Sauce.get_config
      @undefaulted_opts.merge! global_config.opts if global_config.opts
      @whitelisted_capabilities = global_config.whitelisted_capabilities
    end

    @undefaulted_opts.merge! opts
    @opts.merge! @undefaulted_opts
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/sauce/config.rb, line 121
def [](key)
  @opts[key]
end
[]=(key, value) click to toggle source
# File lib/sauce/config.rb, line 125
def []=(key, value)
  if(key == :browsers)
    value = [value] unless value.first.instance_of?(Array)
  end
  @undefaulted_opts.merge!({key => value})
  @opts[key] = value
end
access_key() click to toggle source
# File lib/sauce/config.rb, line 287
def access_key
  @opts[:access_key]
end
after_job(hook, &block) click to toggle source
# File lib/sauce/config.rb, line 299
def after_job(hook, &block)
  hooks = @opts[:after_job_hooks] || {}
  hooks[hook] = block unless hooks[hook]
  @opts[:after_job_hooks] = hooks
end
browser() click to toggle source
# File lib/sauce/config.rb, line 241
def browser
  if single_browser_set?
    return @undefaulted_opts[:browser]
  end
  if !ENV["TEST_ENV_NUMBER"] && @opts[:browsers]
    @opts[:browsers][0][1]
  else
    raise StandardError, no_browser_message
  end
end
browser_version() click to toggle source
# File lib/sauce/config.rb, line 263
def browser_version
  if single_browser_set?
    return @undefaulted_opts[:browser_version] || @undefaulted_opts[:version]
  end
  if !ENV["TEST_ENV_NUMBER"] && @opts[:browsers]
    @opts[:browsers][0][2]
  else
    @opts[:browser_version]
  end
end
browsers() click to toggle source
# File lib/sauce/config.rb, line 207
def browsers
  if @undefaulted_opts[:browser]
    # If a specific browser was requested, ignore :browsers and
    # use that one. This allows a setup with :browsers to launch
    # sub-processes pointed just at each browser in the list.
    return [[os, browser, browser_version]]
  end

  return @opts[:browsers] if @opts.include? :browsers
  return [[os, browser, browser_version]]
end
caps_for_location(file, linenumber=nil) click to toggle source
# File lib/sauce/config.rb, line 219
def caps_for_location(file, linenumber=nil)
  Sauce.logger.debug "Finding capabilities for #{file}"
  Sauce.logger.debug "  Line #{linenumber}" unless linenumber.nil?
  Sauce::Config.called_from_integrations
  perfile_browsers = @opts[:perfile_browsers]
  
  if perfile_browsers
    platforms = []
    test_location = "#{file}:#{linenumber}"
    if linenumber && (perfile_browsers.include? test_location)
      platforms =  perfile_browsers[test_location]
    else
      platforms = perfile_browsers[file]
    end
    Sauce.logger.debug "  Found - #{platforms}"
    platforms.map { |p| [p['os'], p['browser'], p['version'], (p['caps'] || {})] }
  else
    Sauce.logger.debug "No perfile browsers; Using browsers array instead."
    browsers
  end
end
domain() click to toggle source
# File lib/sauce/config.rb, line 274
def domain
  return @opts[:domain] if @opts.include? :domain
  return URI.parse(@opts[:browser_url]).host
end
has_key?(key) click to toggle source
# File lib/sauce/config.rb, line 133
def has_key?(key)
  @opts.has_key? key
end
host() click to toggle source
# File lib/sauce/config.rb, line 291
def host
  @opts[:host]
end
is_defined?(top_mod, sub_mod = nil) click to toggle source

Only here to be stubbed for testing. Gross.

# File lib/sauce/config.rb, line 325
def is_defined? (top_mod, sub_mod = nil)
  return_value = Object.const_defined? top_mod
  unless !return_value || sub_mod.nil?
    return_value = Object.const_get(top_mod).const_defined? sub_mod
  end

  return_value
end
local?() click to toggle source
# File lib/sauce/config.rb, line 279
def local?
  return ENV['LOCAL_SELENIUM']
end
method_missing(meth, *args) click to toggle source
# File lib/sauce/config.rb, line 141
def method_missing(meth, *args)
  unless self.silence_warnings
    warn "[DEPRECATED] This method (#{meth}) is deprecated, please use the [] and []= accessors instead"
  end
  if meth.to_s =~ /(.*)=$/
    self[$1.to_sym] = args[0]
    return args[0]
  elsif meth.to_s =~ /(.*)\?$/
    return self[$1.to_sym]
  else
    return self[meth]
  end
end
os() click to toggle source
# File lib/sauce/config.rb, line 252
def os
  if single_browser_set?
    return @undefaulted_opts[:os]
  end
  if !ENV["TEST_ENV_NUMBER"] && @opts[:browsers]
    @opts[:browsers][0][0]
  else
    @opts[:os]
  end
end
port() click to toggle source
# File lib/sauce/config.rb, line 295
def port
  @opts[:port]
end
run_post_job_hooks(job_id, platform, job_name, job_success) click to toggle source
# File lib/sauce/config.rb, line 305
def run_post_job_hooks(job_id, platform, job_name, job_success)
  Sauce.logger.debug "Running post job hook for #{job_id}"
  Sauce.logger.debug " - Job name #{job_name}"
  Sauce.logger.debug " - Successful? #{job_success}"
  @opts[:after_job_hooks].each do |key, hook|
    Sauce.logger.debug "Running #{hook}"
    hook.call job_id, platform, job_name, job_success
  end
end
silence_warnings() click to toggle source
# File lib/sauce/config.rb, line 137
def silence_warnings
  false
end
to_browser_string() click to toggle source
# File lib/sauce/config.rb, line 166
def to_browser_string
  browser_options = {
    'username' => @opts[:username],
    'access-key' => @opts[:access_key],
    'os' => os,
    'browser' => browser,
    'browser-version' => browser_version,
    'name' => @opts[:name] || @opts[:job_name]}

  SAUCE_OPTIONS.each do |opt|
    [opt, opt.gsub("-", "_")].map(&:to_sym).each do |sym|
      browser_options[opt] = @opts[sym] if @opts.include? sym
    end
  end
  return browser_options.to_json
end
to_desired_capabilities() click to toggle source
# File lib/sauce/config.rb, line 183
def to_desired_capabilities
  desired_capabilities = {
    :browserName => BROWSERS[browser] || browser,
    :version => browser_version,
    :platform => PLATFORMS[os] || os,
    :name =>@opts[:job_name],
    :client_version => client_version
  }

  allowed_options = whitelisted_capabilities + SAUCE_OPTIONS

  allowed_options.each do |opt|
    [opt, opt.gsub("-", "_")].map(&:to_sym).each do |sym|
      if @opts.include? sym
        desired_capabilities[opt.to_sym] = @opts[sym]
      elsif @opts.include? sym.to_s
        desired_capabilities[opt.to_sym] = @opts[sym.to_s]
      end
    end
  end

  desired_capabilities
end
tools() click to toggle source
# File lib/sauce/config.rb, line 315
def tools
  tools = []
  tools << "Rspec" if is_defined? "RSpec"
  tools << "Capybara" if is_defined? "Capybara"
  tools << "Cucumber" if is_defined? "Cucumber"
  tools << "Test::Unit" if is_defined?("Test", "Unit")
  tools
end
username() click to toggle source
# File lib/sauce/config.rb, line 283
def username
  @opts[:username]
end
whitelist(capability) click to toggle source
# File lib/sauce/config.rb, line 159
def whitelist capability
  Sauce.logger.info "Whitelisting #{capability}"
  cap = capability.to_s
  wl = whitelisted_capabilities || Set.new
  @whitelisted_capabilities = wl.add cap
end
whitelisted_capabilities() click to toggle source
# File lib/sauce/config.rb, line 155
def whitelisted_capabilities
  @whitelisted_capabilities ||= Set.new 
end

Private Instance Methods

client_version() click to toggle source
# File lib/sauce/config.rb, line 336
def client_version
  "Ruby: #{RUBY_ENGINE} #{RUBY_VERSION} (#{RUBY_PLATFORM}) Sauce gem: #{Sauce.version} Tools: #{tools.to_s}"
end
extract_options_from_hash(env) click to toggle source
# File lib/sauce/config.rb, line 391
def extract_options_from_hash(env)
  hash = Hash[env]
  opts = {}

  on_demand = hash.delete "SAUCE_ONDEMAND_BROWSERS"
  env_browsers = hash.delete "SAUCE_BROWSERS"
  username = hash.delete("SAUCE_USERNAME") || hash.delete("SAUCE_USER_NAME")
  access_key = hash.delete("SAUCE_ACCESS_KEY") || hash.delete("SAUCE_API_KEY")

  hash.select {|k,v| k.start_with? "SAUCE_"}.each do |k,v|
    opts[k.downcase.sub("sauce_", "").to_sym] = v
  end

  opts[:job_name] = hash['SAUCE_JOB_NAME'] || hash['JOB_NAME']
  opts[:build] = (hash['BUILD_TAG'] ||
                  hash['BUILD_NUMBER'] ||
                  hash['TRAVIS_BUILD_NUMBER'] ||
                  hash['CIRCLE_BUILD_NUM'])

  if hash.include? 'URL'
    opts['SAUCE_BROWSER_URL'] = "http://#{hash['URL']}/"
  end

  if on_demand
    browsers = JSON.parse(on_demand)
    opts[:browsers] = browsers.map { |x| [x['os'], x['browser'], x['browser-version']] }
  end

  if env_browsers
    browsers = JSON.parse(env_browsers)
    opts[:browsers] = browsers.map { |x| [x['os'], x['browser'], x['version'], x['caps']] }
  end

  if hash.include? 'SAUCE_PERFILE_BROWSERS'
    opts[:perfile_browsers] = JSON.parse(hash['SAUCE_PERFILE_BROWSERS'])
  end

  opts[:username] = username if username
  opts[:access_key] = access_key if access_key

  return opts.delete_if {|key, value| value.nil?}
end
load_options_from_environment() click to toggle source
# File lib/sauce/config.rb, line 340
def load_options_from_environment
  return extract_options_from_hash(ENV)
end
load_options_from_heroku() click to toggle source

Heroku supports multiple apps per $PWD. Specify $SAUCE_HEROKU_APP if needed otherwise this can still time out.

# File lib/sauce/config.rb, line 346
def load_options_from_heroku
  @@heroku_environment ||= begin
    if File.exists?(File.expand_path('~/.heroku'))
      Sauce.logger.debug "Heroku config found."
      heroku_app = ENV['SAUCE_HEROKU_APP']
      cmd = "heroku config #{heroku_app ? "--app #{heroku_app}": ''}"
      cmd += "--shell 2>/dev/null"
      buffer = IO.popen(cmd) { |out| out.read }
      if $?.exitstatus == 0
        env = {}
        buffer.split("\n").each do |line|
          key, value = line.split("=")
          env[key] = value
        end
        extract_options_from_hash(env)
      else
        {}
      end
    else
      {}
    end
  rescue Errno::ENOENT
    {} # not a Heroku environment
  end
  return @@heroku_environment
end
load_options_from_yaml() click to toggle source
# File lib/sauce/config.rb, line 373
def load_options_from_yaml
    paths = [
        "ondemand.yml",
        File.join("config", "ondemand.yml"),
        File.expand_path("../../../ondemand.yml", __FILE__),
        File.join(File.expand_path("~"), ".sauce", "ondemand.yml")
    ]

    paths.each do |path|
        if File.exist? path
            Sauce.logger.info "Loading Sauce config from yaml file at #{path}"
            conf = YAML.load_file(path)
            return conf.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
        end
    end
    return {}
end
no_browser_message() click to toggle source
# File lib/sauce/config.rb, line 440
    def no_browser_message
      <<-MESSAGE
No browser has been configured.

It seems you're trying to run your tests in parallel, but haven't configured your specs/tests to use the Sauce integration.

To fix this, add :sauce => true to your specs or make your tests subclasses of Sauce::TestCase or Sauce::RailsTestCase.

For more details check the gem readme at https://github.com/saucelabs/sauce_ruby/blob/master/README.markdown
      MESSAGE
    end
single_browser_set?() click to toggle source
# File lib/sauce/config.rb, line 436
def single_browser_set?
  @undefaulted_opts[:browser] || @undefaulted_opts[:os] || @undefaulted_opts[:version]
end