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



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

def cmd
  @cmd
end

.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.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

Parameters:

  • array (Array<string>)

    of arguments to send to ChildProcess



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/guard/jasmine/server.rb', line 143

def execute(options, cmd)
  if RUBY_PLATFORM == "java"
    cmd.unshift("jruby", "-S")
  else
    cmd.unshift("ruby", "-S")
  end
  self.cmd = cmd
  if options[:debug]
    puts "Starting server using: #{cmd.join(' ')}"
  end
  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
  Compat::UI.error "Cannot start server using command #{ cmd.join(' ') }."
  Compat::UI.error "Error was: #{ e.message }"
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
# 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'

  Compat::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.

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



136
137
138
139
# File 'lib/guard/jasmine/server.rb', line 136

def start_rake_server(port, task, options)
  Compat::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.

Parameters:

  • options (Hash)

    the server options

Options Hash (options):

  • server_env (String)

    the Rails environment

  • port (Number)

    the server port



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

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

  Compat::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.



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

def stop
  if process
    Compat::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.

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
185
186
187
188
189
190
# File 'lib/guard/jasmine/server.rb', line 168

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

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

  throw :task_has_failed
end