Sha256: 08826b99697a3ec98fd293f4330324c2a67986b01199fe4dfb1f6865d7a07ba6

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'open-uri'
require 'teabag/environment'

module Teabag
  class Console

    def initialize(options = nil, files = [])
      @options = options || {}
      @suites = {}
      @files = []

      Teabag::Environment.load(@options)
      Rails.application.config.assets.debug = false if Teabag.configuration.driver == "phantomjs"

      start_server
      resolve(files)
    end

    def execute(options = {}, files = [])
      @options = @options.merge(options) if options.present?
      resolve(files)

      failure_count = 0
      suites.each do |suite|
        STDOUT.print "Teabag running #{suite} suite at #{url(suite)}\n" unless Teabag.configuration.suppress_log
        failure_count += run_specs(suite)
      end
      failure_count > 0
    rescue Teabag::Failure
      true
    rescue Teabag::RunnerException
      true
    end

    def run_specs(suite)
      driver.run_specs(suite, url(suite))
    end

    protected

    def resolve(files)
      return if files.length == 0
      @suites = {}
      @files = files
      files.uniq.each do |path|
        if result = Teabag::Suite.resolve_spec_for(path)
          suite = @suites[result[:suite]] ||= []
          suite << result[:path]
        end
      end
    end

    def start_server
      @server = Teabag::Server.new
      @server.start
    end

    def suites
      return [@options[:suite]] if @options[:suite].present?
      return @suites.keys if @suites.present?
      Teabag.configuration.suites.keys
    end

    def driver
      @driver ||= Teabag::Drivers.const_get("#{Teabag.configuration.driver.to_s.camelize}Driver").new
    end

    def filter(suite)
      parts = []
      parts << "grep=#{URI::encode(@options[:filter])}" if @options[:filter].present?
      (@suites[suite] || @files).each { |file| parts << "file[]=#{URI::encode(file)}" }
      "?#{parts.join('&')}" if parts.present?
    end

    def url(suite)
      ["#{@server.url}#{Teabag.configuration.mount_at}", suite, filter(suite)].compact.join("/")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
teabag-0.5.4 lib/teabag/console.rb