Sha256: 9ab3655cf2caaa2a7f1eb89743c9a0b336b483d6cafeccd3461f046e254bf21e

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'facter'
require 'tempfile'
require 'phantomjs-mac' if RUBY_PLATFORM.downcase.include?('darwin')

class Jasmine::Runners::Phantom
  attr_accessor :suites

  def initialize(port, results_processor, result_batch_size)
    @port = port
    @results_processor = results_processor
    @result_batch_size = result_batch_size
  end

  def run
    load_suite_info
    @results_processor.process(results_hash, suites)
  end

  private

  def load_suite_info
    tmpfile = Tempfile.new('count')
    pid = Process.spawn "#{Phantomjs.executable_path} '#{File.join(File.dirname(__FILE__), 'phantom_jasmine_count.js')}' #{@port}", :out => tmpfile.path
    Process.wait pid
    json = JSON.parse(tmpfile.read, :max_nesting => 100).tap { tmpfile.close }
    @suites = json['suites']
    @top_level_suites = json['top_level_suites']
  end

  def run_suites(suites)
    tmpfile = Tempfile.new('run')
    commands = suites.map do |suite|
      "#{Phantomjs.executable_path} '#{File.join(File.dirname(__FILE__), 'phantom_jasmine_run.js')}' #{@port} '#{suite['description']}'"
    end.join(';echo ,;')

    pid = Process.spawn "echo [;#{commands};echo ]", :out => tmpfile.path
    [pid, tmpfile]
  end

  def results_hash
    spec_results = {}

    @top_level_suites.group_by { |suite| suite['id'] % processor_count }.map { |(k, suites)| run_suites(suites) }.each do |pid, tmpfile|
      Process.wait pid

      JSON.parse(tmpfile.read).each do |result|
        result.each do |spec_id, spec_result|
          spec_results.merge! spec_id => spec_result unless spec_result['messages'].empty?
        end
      end

      tmpfile.close
    end

    spec_results
  end

  def processor_count
    @processor_count ||= begin
      ENV['JASMINE_PARALLEL_COUNT'] || [Facter.sp_number_processors.to_i, 4].min
    end.to_i
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phantom_jasmine-0.0.6 lib/jasmine/runners/phantom.rb
phantom_jasmine-0.0.5 lib/jasmine/runners/phantom.rb
phantom_jasmine-0.0.4 lib/jasmine/runners/phantom.rb
phantom_jasmine-0.0.3 lib/jasmine/runners/phantom.rb