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

.processObject

Returns the value of attribute process



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

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

Parameters:

  • options (Hash)

    the server options

Options Hash (options):

  • server (Symbol)

    the rack server to use

Returns:

  • (Integer)

    port number



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

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

Parameters:

  • spec_dir (String)

    the spec directory

Returns:

  • (Symbol)

    the server strategy



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

def detect_server(spec_dir)
  if spec_dir && File.exists?(File.join(spec_dir, 'support','jasmine.yml'))
    :jasmine_gem
  elsif File.exists?('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

.start(options) ⇒ Object

Start the internal test server for getting the Jasmine runner.

Parameters:

  • options (Hash)

    the server options

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)



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

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.

Parameters:

  • server (Symbol)

    the server name

  • port (Integer)

    the server port

  • options (Hash)

    the server options

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)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/guard/jasmine/server.rb', line 105

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 })."

  self.process = ChildProcess.build(*['rackup', '-E', environment.to_s, '-p', port.to_s, '-s', server.to_s, rackup_config].compact)
  self.process.environment['COVERAGE'] = options[:coverage].to_s
  self.process.environment['IGNORE_INSTRUMENTATION'] = options[:ignore_instrumentation].to_s
  self.process.io.inherit! if options[:verbose]
  self.process.start

rescue => e
  ::Guard::UI.error "Cannot start Rack server: #{ e.message }"
end

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

Start the Jasmine gem server of the current project.

Parameters:

  • port (Number)

    the server port

  • task (String)

    the rake task name

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • server (Symbol)

    the rack server to use



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/guard/jasmine/server.rb', line 151

def start_rake_server(port, task, options)

  ::Guard::UI.info "Guard::Jasmine starts Jasmine Gem test server on port #{ port }."

  self.process = ChildProcess.build('ruby', '-S', 'rake', task, "JASMINE_PORT=#{ port }")
  self.process.io.inherit! if options[:verbose]
  self.process.start

rescue => e
  ::Guard::UI.error "Cannot start Rake task server: #{ e.message }"
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.

Parameters:

  • options (Hash)

    the server options

Options Hash (options):

  • server_env (String)

    the Rails environment

  • port (Number)

    the server port



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/guard/jasmine/server.rb', line 130

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 })."

  self.process = ChildProcess.build('unicorn_rails', '-E', environment.to_s, '-p', port.to_s)
  self.process.environment['COVERAGE'] = options[:coverage].to_s
  self.process.io.inherit! if options[:verbose]
  self.process.start

rescue => e
  ::Guard::UI.error "Cannot start Unicorn server: #{ e.message }"
end

.stopObject

Stop the server thread.



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

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

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

Wait until the Jasmine test server is running.

Parameters:

  • port (Number)

    the server port

  • timeout (Number)

    the server wait timeout



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

def wait_for_server(port, timeout)
  Timeout::timeout(timeout) do
    while true
      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 startup. You may need to increase the `:server_timeout` option.'
  throw :task_has_failed
end