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



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

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



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

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



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

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/guard/jasmine/server.rb', line 135

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



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

def start(options)
  port = options[:port]

  case options[:server]
  when :webrick, :mongrel, :thin, :puma
    start_rack_server(options[:server], port, options)
  when :unicorn
    start_unicorn_server(port, options)
  when :jasmine_gem
    start_rake_server(port, 'jasmine', options)
  when :none # noop
  else
    start_rake_server(port, options[:server], options)
  end

  wait_for_server(port, options[:server_timeout]) unless options[: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)



102
103
104
105
106
# File 'lib/guard/jasmine/server.rb', line 102

def start_rack_server(server, port, options)
  coverage = options[:coverage] ? 'on' : 'off'
  Compat::UI.info "Guard::Jasmine starts #{server} spec server on port #{port} in #{options[:server_env]} environment (coverage #{coverage})."
  execute(options, ['rackup', '-E', options[:server_env].to_s, '-p', port.to_s, '-s', server.to_s, options[: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



128
129
130
131
# File 'lib/guard/jasmine/server.rb', line 128

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



116
117
118
119
120
# File 'lib/guard/jasmine/server.rb', line 116

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

.stopObject

Stop the server thread.



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

def stop
  return unless process
  Compat::UI.info 'Guard::Jasmine stops server.'
  process.stop(5)
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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/guard/jasmine/server.rb', line 158

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