Skip to content

Commit

Permalink
Allow a custom server to be started.
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Mar 7, 2012
1 parent 7eb3db0 commit 6c9a95a
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 43 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ guard 'jasmine', :all_on_start => false, :specdoc => :always do
end
```

### General options
### Server options

The general options configures the environment that is needed to run Guard::Jasmine:
The server options configures the server environment that is needed to run Guard::Jasmine:

```ruby
:server => :jasmine_gem # Jasmine server to use, either :auto, :none,
Expand Down Expand Up @@ -244,6 +244,14 @@ The reason why the Server environment is set to `development` by default is that
the asset pipeline doesn't concatenate the JavaScripts and you'll see the line number in the real file,
instead of a ridiculous high line number in a single, very large JavaScript.

#### Use a custom server

If you supply an unknown server name as the `:server` option, then Guard::Jasmine will execute
a `rake` task with the given server name as task in a child process. For example, if you configure
`:server => 'start_my_server'`, then the command `rake start_my_server` will be executed and
you have to make sure the server starts on the port that you can get from the `JASMINE_PORT`
environment variable.

### Spec runner options

The spec runner options configures the behavior driven development (or BDD) cycle:
Expand Down
2 changes: 1 addition & 1 deletion lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def initialize(watchers = [], options = { })
def start
if Jasmine.phantomjs_bin_valid?(options[:phantomjs_bin])

Server.start(options[:server], options[:port], options[:server_env]) unless options[:server] == :none
Server.start(options[:server], options[:port], options[:server_env], options[:spec_dir]) unless options[:server] == :none

if Jasmine.runner_available?(options[:jasmine_url])
run_all if options[:all_on_start]
Expand Down
4 changes: 2 additions & 2 deletions lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ def spec(*paths)
runner[:server_env] = options.server_env
runner[:spec_dir] = options.spec_dir
runner[:console] = [:always, :never, :failure].include?(options.console.to_sym) ? options.console.to_sym : :failure
runner[:server] = [:auto, :none, :webrick, :mongrel, :thin, :jasmine_gem].include?(options.server.to_sym) ? options.server.to_sym : :auto
runner[:server] = options.server.to_sym

runner[:notification] = false
runner[:hide_success] = true
runner[:max_error_notify] = 0
runner[:specdoc] = :always

if CLI.phantomjs_bin_valid?(runner[:phantomjs_bin])
::Guard::Jasmine::Server.start(runner[:server], runner[:port], runner[:server_env]) unless runner[:server] == :none
::Guard::Jasmine::Server.start(runner[:server], runner[:port], runner[:server_env], runner[:spec_dir]) unless runner[:server] == :none

if CLI.runner_available?(runner[:jasmine_url])
result = ::Guard::Jasmine::Runner.run(paths, runner)
Expand Down
19 changes: 12 additions & 7 deletions lib/guard/jasmine/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ class << self
# @param [String] strategy the server strategy to use
# @param [Number] port the server port
# @param [String] environment the Rails environment
# @param [String] spec_dir the spec directory
#
def start(strategy, port, environment)
strategy = detect_server if strategy == :auto
def start(strategy, port, environment, spec_dir)
strategy = detect_server(spec_dir) if strategy == :auto

case strategy
when :webrick, :mongrel, :thin
start_rack_server(port, environment, strategy)
when :jasmine_gem
start_jasmine_gem_server(port)
start_rake_server(port, 'jasmine')
else
start_rake_server(port, strategy.to_s) unless strategy == :none
end

wait_for_server(port) unless strategy == :none
Expand Down Expand Up @@ -63,11 +66,12 @@ def start_rack_server(port, environment, server)
# Start the Jasmine gem server of the current project.
#
# @param [Number] port the server port
# @param [String] task the rake task name
#
def start_jasmine_gem_server(port)
def start_rake_server(port, task)
::Guard::UI.info "Guard::Jasmine starts Jasmine Gem test server on port #{ port }."

self.process = ChildProcess.build('rake', 'jasmine', "JASMINE_PORT=#{ port }")
self.process = ChildProcess.build('rake', task, "JASMINE_PORT=#{ port }")
self.process.io.inherit! if ::Guard.respond_to?(:options) && ::Guard.options[:verbose]
self.process.start

Expand All @@ -77,12 +81,13 @@ def start_jasmine_gem_server(port)

# Detect the server to use
#
# @param [String] spec_dir the spec directory
# @return [Symbol] the server strategy
#
def detect_server
def detect_server(spec_dir)
if File.exists?('config.ru')
:webrick
elsif File.exists?(File.join('spec', 'javascripts', 'support', 'jasmine.yml'))
elsif File.exists?(File.join(spec_dir, 'support', 'jasmine.yml'))
:jasmine_gem
else
:none
Expand Down
11 changes: 8 additions & 3 deletions spec/guard/jasmine/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
context 'with specified options' do
context 'for the server' do
it 'sets the server type' do
server.should_receive(:start).with(:thin, 8888, 'test')
server.should_receive(:start).with(:thin, 8888, 'test', 'spec/javascripts')
cli.start(['spec', '--server', 'thin'])
end

it 'sets the server port' do
server.should_receive(:start).with(:auto, 4321, 'test')
server.should_receive(:start).with(:auto, 4321, 'test', 'spec/javascripts')
cli.start(['spec', '--port', '4321'])
end

it 'sets the spec dir' do
server.should_receive(:start).with(:auto, 4321, 'test', 'specs')
cli.start(['spec', '--port', '4321', '-d', 'specs'])
end
end

context 'for the runner' do
Expand Down Expand Up @@ -81,7 +86,7 @@
context 'without specified options' do
context 'for the server' do
it 'sets the server type' do
server.should_receive(:start).with(:auto, 8888, 'test')
server.should_receive(:start).with(:auto, 8888, 'test', 'spec/javascripts')
cli.start(['spec'])
end
end
Expand Down
110 changes: 83 additions & 27 deletions spec/guard/jasmine/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

before do
server.stub(:start_rack_server)
server.stub(:start_jasmine_gem_server)
server.stub(:start_rake_server)
server.stub(:wait_for_server)
end

Expand All @@ -20,29 +20,48 @@

it 'chooses the rack server strategy' do
server.should_receive(:start_rack_server)
server.start(:auto, 8888, 'test')
server.start(:auto, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:auto, 8888, 'test')
server.start(:auto, 8888, 'test', 'spec/javascripts')
end
end

context 'with a jasmine config file' do
before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('spec', 'javascripts', 'support', 'jasmine.yml')).and_return true
context 'with the default spec dir' do
before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('spec', 'javascripts', 'support', 'jasmine.yml')).and_return true
end

it 'chooses the jasmine_gem server strategy' do
server.should_receive(:start_rake_server)
server.start(:auto, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:auto, 8888, 'test', 'spec/javascripts')
end
end

it 'chooses the jasmine_gem server strategy' do
server.should_receive(:start_jasmine_gem_server)
server.start(:auto, 8888, 'test')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:auto, 8888, 'test')
context 'with a custom spec dir' do
before do
File.should_receive(:exists?).with('config.ru').and_return false
File.should_receive(:exists?).with(File.join('specs', 'support', 'jasmine.yml')).and_return true
end

it 'chooses the jasmine_gem server strategy' do
server.should_receive(:start_rake_server)
server.start(:auto, 8888, 'test', 'specs')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:auto, 8888, 'test', 'specs')
end
end
end

Expand All @@ -54,72 +73,109 @@

it 'does not start a server' do
server.should_not_receive(:start_rack_server)
server.should_not_receive(:start_jasmine_gem_server)
server.should_not_receive(:start_rake_server)
server.should_not_receive(:wait_for_server)
server.start(:auto, 8888, 'test')
server.start(:auto, 8888, 'test', 'spec/javascripts')
end
end
end

context 'with the :thin strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:thin, 8888, 'test')
server.start(:thin, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:thin, 8888, 'test')
server.start(:thin, 8888, 'test', 'spec/javascripts')
end

it 'starts a :thin rack server' do
server.should_receive(:start_rack_server).with(8888, 'test', :thin)
server.start(:thin, 8888, 'test', 'spec/javascripts')
end
end

context 'with the :mongrel strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:mongrel, 8888, 'test')
server.start(:mongrel, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:mongrel, 8888, 'test')
server.start(:mongrel, 8888, 'test', 'spec/javascripts')
end

it 'starts a :mongrel rack server' do
server.should_receive(:start_rack_server).with(8888, 'test', :mongrel)
server.start(:mongrel, 8888, 'test', 'spec/javascripts')
end
end

context 'with the :webrick strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:webrick, 8888, 'test')
server.start(:webrick, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:webrick, 8888, 'test')
server.start(:webrick, 8888, 'test', 'spec/javascripts')
end

it 'starts a :webrick rack server' do
server.should_receive(:start_rack_server).with(8888, 'test', :webrick)
server.start(:webrick, 8888, 'test', 'spec/javascripts')
end
end

context 'with the :jasmine_gem strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:jasmine_gem, 8888, 'test')
server.start(:jasmine_gem, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:jasmine_gem, 8888, 'test')
server.start(:jasmine_gem, 8888, 'test', 'spec/javascripts')
end

it 'starts the :jasmine rake task server' do
server.should_receive(:start_rake_server).with(8888, 'jasmine')
server.start(:jasmine_gem, 8888, 'test', 'spec/javascripts')
end
end

context 'with a custom rake strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:start_server, 8888, 'test', 'spec/javascripts')
end

it 'does wait for the server' do
server.should_receive(:wait_for_server)
server.start(:start_server, 8888, 'test', 'spec/javascripts')
end

it 'starts a custom rake task server' do
server.should_receive(:start_rake_server).with(8888, 'start_server')
server.start(:start_server, 8888, 'test', 'spec/javascripts')
end
end

context 'with the :none strategy' do
it 'does not auto detect a server' do
server.should_not_receive(:detect_server)
server.start(:none, 8888, 'test')
server.start(:none, 8888, 'test', 'spec/javascripts')
end

it 'does not start a server' do
server.should_not_receive(:start_rack_server)
server.should_not_receive(:start_jasmine_gem_server)
server.should_not_receive(:start_rake_server)
server.should_not_receive(:wait_for_server)
server.start(:none, 8888, 'test')
server.start(:none, 8888, 'test', 'spec/javascripts')
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/guard/jasmine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
end

it 'does start a server' do
server.should_receive(:start).with(:jasmine_gem, 3333, 'test')
server.should_receive(:start).with(:jasmine_gem, 3333, 'test', 'spec/javascripts')
guard.start
end
end
Expand Down

0 comments on commit 6c9a95a

Please sign in to comment.