Module: Guard::Jasmine::Server

Defined in:
lib/guard/jasmine/server.rb

Overview

Start and stop a Jasmine test server for requesting the specs from PhantomJS.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cmdObject

Returns the value of attribute cmd



16
17
18
# File 'lib/guard/jasmine/server.rb', line 16

def cmd
  @cmd
end

.processObject

Returns the value of attribute process



15
16
17
# File 'lib/guard/jasmine/server.rb', line 15

def process
  @process
end

Class Method Details

.choose_server_port(options) ⇒ Integer

A port was not specified, therefore we attempt to detect the best port to use

Options Hash (options):

  • server (Symbol)

    the rack server to use



59
60
61
62
63
64
65
# File 'lib/guard/jasmine/server.rb', line 59

def choose_server_port(options)
  if options[:server] == :jasmine_gem
    ::Jasmine.config.port(:server)
  else
    ::Guard::Jasmine.find_free_server_port
  end
end

.detect_server(spec_dir) ⇒ Symbol

Detect the server to use



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/guard/jasmine/server.rb', line 72

def detect_server(spec_dir)
  if spec_dir && File.exist?(File.join(spec_dir, 'support', 'jasmine.yml'))
    :jasmine_gem
  elsif File.exist?('config.ru')
    %w(unicorn thin mongrel puma).each do |server|
      begin
        require server
        return server.to_sym
      rescue LoadError
        # Ignore missing server and try next
      end
    end
    :webrick
  else
    :none
  end
end

.execute(options, cmd) ⇒ Object (private)

Builds a child process with the given command and arguments



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/guard/jasmine/server.rb', line 141

def execute(options, cmd)
  if RUBY_PLATFORM == "java"
    cmd.unshift("jruby", "-S")
  else
    cmd.unshift("ruby", "-S")
  end
  self.cmd = cmd
  self.process = ChildProcess.build(*cmd.compact)
  process.environment['COVERAGE'] = options[:coverage].to_s
  process.environment['IGNORE_INSTRUMENTATION'] = options[:ignore_instrumentation].to_s
  process.io.inherit! if options[:verbose]
  process.start
rescue => e
  ::Guard::UI.error "Cannot start server using command #{ cmd.join(' ') }."
  ::Guard::UI.error "Error was: #{ e.message }"
end

.start(options) ⇒ Object

Start the internal test server for getting the Jasmine runner.

Options Hash (options):

  • server (String)

    the server to use

  • port (Number)

    the server port

  • server_env (String)

    the Rails environment

  • server_timeout (Number)

    the server start timeout

  • spec_dir (String)

    the spec directory

  • rackup_config (String)

    custom rackup config to use (i.e. spec/dummy/config.ru for mountable engines)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/guard/jasmine/server.rb', line 27

def start(options)
  server  = options[:server]
  port    = options[:port]
  timeout = options[:server_timeout]

  case server
  when :webrick, :mongrel, :thin, :puma
    start_rack_server(server, port, options)
  when :unicorn
    start_unicorn_server(port, options)
  when :jasmine_gem
    start_rake_server(port, 'jasmine', options)
  else
    start_rake_server(port, server.to_s, options) unless server == :none
  end

  wait_for_server(port, timeout) unless server == :none
end

.start_rack_server(server, port, options) ⇒ Object (private)

Start the Rack server of the current project. This will simply start a server that uses the config.ru in the current directory.

Options Hash (options):

  • server (Symbol)

    the rack server to use

  • server_env (String)

    the Rails environment

  • rackup_config (String)

    custom rackup config to use (i.e. spec/dummy/config.ru for mountable engines)



103
104
105
106
107
108
109
110
# File 'lib/guard/jasmine/server.rb', line 103

def start_rack_server(server, port, options)
  environment   = options[:server_env]
  rackup_config = options[:rackup_config]
  coverage      = options[:coverage] ? 'on' : 'off'

  ::Guard::UI.info "Guard::Jasmine starts #{ server } spec server on port #{ port } in #{ environment } environment (coverage #{ coverage })."
  execute(options, ['rackup', '-E', environment.to_s, '-p', port.to_s, '-s', server.to_s, rackup_config])
end

.start_rake_server(port, task, options) ⇒ Object (private)

Start the Jasmine gem server of the current project.

Options Hash (options):

  • server (Symbol)

    the rack server to use



134
135
136
137
# File 'lib/guard/jasmine/server.rb', line 134

def start_rake_server(port, task, options)
  ::Guard::UI.info "Guard::Jasmine starts Jasmine Gem test server on port #{ port }."
  execute(options, ['rake', task, "JASMINE_PORT=#{ port }"])
end

.start_unicorn_server(port, options) ⇒ Object (private)

Start the Rack server of the current project. This will simply start a server that uses the config.ru in the current directory.

Options Hash (options):

  • server_env (String)

    the Rails environment

  • port (Number)

    the server port



120
121
122
123
124
125
126
# File 'lib/guard/jasmine/server.rb', line 120

def start_unicorn_server(port, options)
  environment = options[:server_env]
  coverage    = options[:coverage] ? 'on' : 'off'

  ::Guard::UI.info "Guard::Jasmine starts Unicorn spec server on port #{ port } in #{ environment } environment (coverage #{ coverage })."
  execute(options, ['unicorn_rails', '-E', environment.to_s, '-p', port.to_s])
end

.stopObject

Stop the server thread.



48
49
50
51
52
53
# File 'lib/guard/jasmine/server.rb', line 48

def stop
  if process
    ::Guard::UI.info 'Guard::Jasmine stops server.'
    process.stop(5)
  end
end

.wait_for_server(port, timeout) ⇒ Object (private)

Wait until the Jasmine test server is running.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/guard/jasmine/server.rb', line 163

def wait_for_server(port, timeout)
  Timeout.timeout(timeout) do
    loop do
      begin
        ::TCPSocket.new('127.0.0.1', port).close
        break
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        # Ignore, server still not available
      end
      sleep 0.1
    end
  end

rescue Timeout::Error
  ::Guard::UI.warning "Timeout while waiting for the server to startup"
  ::Guard::UI.warning "Most likely there is a configuration error that's preventing the server from starting"
  ::Guard::UI.warning "You may need to increase the `:server_timeout` option."
  ::Guard::UI.warning "The commandline that was used to start the server was:"
  ::Guard::UI.warning cmd.join(' ')
  ::Guard::UI.warning "You should attempt to run that and see if any errors occur"

  throw :task_has_failed
end