Class: Spring::Commands::OpalRSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/spring/commands/version.rb,
lib/spring/commands/opal_rspec.rb

Constant Summary collapse

VERSION =
'1.0.0'
PORT =
9999
URL =
"http://localhost:#{PORT}/"

Instance Method Summary collapse

Instance Method Details

#callObject



18
19
20
21
# File 'lib/spring/commands/opal_rspec.rb', line 18

def call
  wait_for_server
  launch_phantom
end

#descriptionObject



82
83
84
# File 'lib/spring/commands/opal_rspec.rb', line 82

def description
  'Execute opal::rspec tests'
end

#envObject



8
9
10
# File 'lib/spring/commands/opal_rspec.rb', line 8

def env(*)
  'test'
end

#launch_phantom(timeout_value = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spring/commands/opal_rspec.rb', line 26

def launch_phantom(timeout_value=nil)
  rspec_path = Gem.loaded_specs['opal-rspec'].full_gem_path
  runner_path = File.join(rspec_path, 'vendor/spec_runner.js')

  if `phantomjs -v`.strip.to_i >= 2
    warn <<-WARN.gsub(/^              /, '')
                      Only PhantomJS v1 is currently supported,
                      if you're using homebrew on OSX you can switch version with:

                        brew switch phantomjs 1.9.8

    WARN
    exit 1
  end
  command_line = %Q{phantomjs #{runner_path} "#{URL}"#{timeout_value ? " #{timeout_value}" : ''}}
  puts "Running #{command_line}"
  system command_line
  success = $?.success?
  exit 1 unless success
end

#server_running?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
# File 'lib/spring/commands/opal_rspec.rb', line 58

def server_running?
  uri = URI(URL)
  begin
    socket = TCPSocket.new uri.hostname, uri.port
    socket.close
    true
  rescue Errno::ECONNREFUSED
    false
  end
end

#setupObject



12
13
14
15
16
# File 'lib/spring/commands/opal_rspec.rb', line 12

def setup
  unless server_running?
    start_server
  end
end

#start_serverObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/spring/commands/opal_rspec.rb', line 47

def start_server
  Process.spawn({
                    'RAILS_ROOT' => ::Rails.root.to_s
                },
                'ruby',
                '-I',
                File.expand_path('../..', __FILE__),
                '-e',
                'require "spring/commands/opal_rack_boot"')
end

#wait_for_serverObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/spring/commands/opal_rspec.rb', line 69

def wait_for_server
  # avoid retryable dependency
  tries = 0
  up = false
  max_tries = 50
  while tries < max_tries && !up
    tries += 1
    sleep 0.1
    up = server_running?
  end
  raise "Tried #{max_tries} times to contact Rack server and not up!" unless up
end