Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requested additions for :server_mount option #138

Merged
merged 4 commits into from
Aug 6, 2013
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Ruby.
* Fast headless testing on [PhantomJS][], a full featured WebKit browser with native support for
various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

* Runs the standard Jasmine test runner, so you can use [Jasminerice][] for integrating [Jasmine][] into the
* Runs the standard Jasmine test runner, so you can use [Jasminerice][] or [jasmine-rails][] for integrating [Jasmine][] into the
[Rails asset pipeline][] and write your specs in [CoffeeScript][].

* Integrates [Istanbul](https://github.com/gotwarlost/istanbul) to instrument your code in the asset pipeline and
Expand Down Expand Up @@ -143,6 +143,34 @@ It also creates an empty `spec/javascripts/spec.css` file as it is always reques

Now you can access `/jasmine` when you start your Rails server normally.

### Jasmine-Rails

[jasmine-rails][] is another option for integrating your [Jasmine][] tests with an asset pipeline-enabled Rails application. The quick-and-dirty recipe for this is:

1. Add `jasmine-rails` to your `Gemfile`:

```ruby
group :test do
gem "jasmine-rails"
end
```

2. Configure a mount point in your application's `routes.rb` (please refer to the [jasmine-rails][] documentation for more details):

```ruby
mount JasmineRails::Engine => '/spec' if defined?(JasmineRails)
```

3. Configure **Guard::Jasmine** to reference the mount point in your `Guardfile`:

```ruby
guard 'jasmine', :server => :webrick, :server_mount => '/specs' do
# watch stuff
end
```

4. Profit! Seriously, you should be able to access the Jasmine runner at `/specs` within your Rails application, *and* **Guard::Jasmine** should run the same specs. Now no more excuses, get that javascript tested!

### Jasmine Stories acceptance tests

[Jasmine Stories](https://github.com/DominikGuzei/jasmine-stories) is a Jasminerice clone and that serves
Expand Down Expand Up @@ -884,6 +912,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[PhantomJS build instructions]: http://code.google.com/p/phantomjs/wiki/BuildInstructions
[Brad Phelan]: http://twitter.com/#!/bradgonesurfing
[Jasminerice]: https://github.com/bradphelan/jasminerice
[jasmine-rails]: https://github.com/searls/jasmine-rails
[Pivotal Labs]: http://pivotallabs.com/
[Jasmine]: http://pivotal.github.com/jasmine/
[the Jasmine Gem]: https://github.com/pivotal/jasmine-gem
Expand Down
5 changes: 4 additions & 1 deletion lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Jasmine < Guard
server: :auto,
server_env: ENV['RAILS_ENV'] || 'development',
server_timeout: 60,
server_mount: '/jasmine',
server_mount: '/jasmine', # set here for documnetation puprposes; actually determiend at runtime by presence (or lack thereof) of the JasmineRails constant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typos

port: nil,
rackup_config: nil,
jasmine_url: nil,
Expand Down Expand Up @@ -61,6 +61,7 @@ class Jasmine < Guard
# @option options [Integer] :server_timeout the number of seconds to wait for the Jasmine spec server
# @option options [String] :port the port for the Jasmine test server
# @option options [String] :rackup_config custom rackup config to use
# @option options [String] :server_mount custom mount point to use; defaults to '/specs' if JasmineRails is on the load path, otherwise '/jasmine'
# @option options [String] :jasmine_url the url of the Jasmine test runner
# @option options [String] :phantomjs_bin the location of the PhantomJS binary
# @option options [Integer] :timeout the maximum time in seconds to wait for the spec runner to finish
Expand All @@ -86,6 +87,8 @@ class Jasmine < Guard
# @option options [Hash] :run_all options overwrite options when run all specs
#
def initialize(watchers = [], options = { })
options[:server_mount] ||= defined?(JasmineRails) ? '/specs' : '/jasmine'

options = DEFAULT_OPTIONS.merge(options)

options[:spec_dir] ||= File.exists?(File.join('spec', 'javascripts')) ? File.join('spec', 'javascripts') : 'spec'
Expand Down
6 changes: 6 additions & 0 deletions lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class CLI < Thor
aliases: '-u',
desc: 'The url of the Jasmine test runner'

method_option :mount,
type: :string,
aliases: '-m',
desc: 'The mount point of the Jasmine test runner'

method_option :timeout,
type: :numeric,
aliases: '-t',
Expand Down Expand Up @@ -148,6 +153,7 @@ def spec(*paths)
runner_options[:spec_dir] = options.spec_dir || (File.exists?(File.join('spec', 'javascripts')) ? File.join('spec', 'javascripts') : 'spec')
runner_options[:server] = options.server.to_sym == :auto ? ::Guard::Jasmine::Server.detect_server(runner_options[:spec_dir]) : options.server.to_sym
runner_options[:jasmine_url] = options.url || "http://localhost:#{ runner_options[:port] }#{ options.server.to_sym == :jasmine_gem ? '/' : '/jasmine' }"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this line also need to interpolate options.mount instead of hard-coding /jasmine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point... I had intentionally not added that in, figuring that to leave the default mount point to be /jasmine in the interest of minimum disruption, but it would be more internally consistent. Anyone with add'l thoughts on the matter?

runner_options[:server_mount] = options.mount || '/jasmine'
runner_options[:phantomjs_bin] = options.bin || CLI.which('phantomjs')
runner_options[:timeout] = options.timeout
runner_options[:verbose] = options.verbose
Expand Down
5 changes: 5 additions & 0 deletions spec/guard/jasmine/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
cli.start(['spec', '--url', 'http://smackaho.st:3000/jasmine'])
end

it 'sets the jasmine mount point' do
runner.should_receive(:run).with(anything(), hash_including(server_mount: '/foo')).and_return [true, []]
cli.start(['spec', '--mount', '/foo'])
end

it 'sets the PhantomJS binary' do
runner.should_receive(:run).with(anything(), hash_including(phantomjs_bin: '/bin/phantomjs')).and_return [true, []]
cli.start(['spec', '--bin', '/bin/phantomjs'])
Expand Down
26 changes: 20 additions & 6 deletions spec/guard/jasmine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
guard.options[:server_timeout].should eql 60
end

it 'sets a default :server_mount option' do
it 'otherwise the default should be /jasmine' do
guard.options[:server_mount].should eql defaults[:server_mount]
end

Expand Down Expand Up @@ -312,14 +312,28 @@
guard.options[:jasmine_url].should eql 'http://localhost:4321/'
end

it 'sets the jasminerice url by default' do
guard = Guard::Jasmine.new(nil, { server: :thin, port: 4321 })
guard.options[:jasmine_url].should eql 'http://localhost:4321/jasmine'
context 'sets the url automatically' do
it 'when jasmine-rails is in the load path, it sets the jasmine-rails url by default' do
# module JasmineRails; end
Kernel.const_set('JasmineRails', Module.new)
defined?(JasmineRails).should be_true

guard = Guard::Jasmine.new(nil, { server: :thin, port: 4321 })
guard.options[:jasmine_url].should eql 'http://localhost:4321/specs'

Kernel.send(:remove_const, 'JasmineRails')
defined?(JasmineRails).should_not be_true
end

it 'otherwise, it sets the jasminerice url by default' do
guard = Guard::Jasmine.new(nil, { server: :thin, port: 4321 })
guard.options[:jasmine_url].should eql 'http://localhost:4321/jasmine'
end
end

it 'sets the jasmine runner url as configured' do
guard = Guard::Jasmine.new(nil, { server: :thin, port: 4321, server_mount: '/specs' })
guard.options[:jasmine_url].should eql 'http://localhost:4321/specs'
guard = Guard::Jasmine.new(nil, { server: :thin, port: 4321, server_mount: '/foo' })
guard.options[:jasmine_url].should eql 'http://localhost:4321/foo'
end
end

Expand Down