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

Run with line number #154

Merged
merged 5 commits into from
Nov 29, 2013
Merged
Changes from 1 commit
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
Next Next commit
ability to provide line number for spec
in the same manner as rspec, the line number may be provided directly after the
file name or by explicitely passing a cli argument
koffeinfrei committed Nov 2, 2013
commit 6dfe0f5dcb4622c3e9283224107326981d82e1ad
6 changes: 6 additions & 0 deletions lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
@@ -65,6 +65,11 @@ class CLI < Thor
aliases: '-d',
desc: 'The directory with the Jasmine specs'

method_option :line_number,
type: :numeric,
aliases: '-l',
desc: 'The line which identifies the spec to be run'

method_option :url,
type: :string,
aliases: '-u',
@@ -166,6 +171,7 @@ def spec(*paths)
runner_options = {}
runner_options[:port] = options.port || CLI.find_free_server_port
runner_options[:spec_dir] = options.spec_dir || (File.exists?(File.join('spec', 'javascripts')) ? File.join('spec', 'javascripts') : 'spec')
runner_options[:line_number] = options.line_number
runner_options[:server] = options.server.to_sym == :auto ? ::Guard::Jasmine::Server.detect_server(runner_options[:spec_dir]) : options.server.to_sym
runner_options[:server_mount] = options.mount || (defined?(JasmineRails) ? '/specs' : '/jasmine')
runner_options[:jasmine_url] = options.url || "http://localhost:#{ runner_options[:port] }#{ options.server.to_sym == :jasmine_gem ? '/' : runner_options[:server_mount] }"
22 changes: 18 additions & 4 deletions lib/guard/jasmine/runner.rb
Original file line number Diff line number Diff line change
@@ -155,10 +155,24 @@ def query_string_for_suite(file, options)

query_string = ''

File.foreach(file) do |line|
if line =~ /describe\s*[("']+(.*?)["')]+/
query_string = "?spec=#{ $1 }"
break
if options[:line_number].to_s =~ /\A(\d+)$/ || file =~/\A[^:]+:(\d+)$/
line_number = $1.to_i

spec = File.readlines(file)[0, line_number].
map(&:strip).
select { |x| x.start_with?('it') || x.start_with?('describe') }.
map { |x| x[/['"](.+?)['"]/, 1] }.
join(' ')

if spec && !spec.empty?
query_string = "?spec=#{ spec }"
end
else
File.foreach(file) do |line|
if line =~ /describe\s*[("']+(.*?)["')]+/
query_string = "?spec=#{ $1 }"
break
end
end
end

10 changes: 10 additions & 0 deletions spec/guard/jasmine/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -45,6 +45,11 @@
cli.start(['spec', '--spec-dir', 'specs'])
end

it 'sets the line number' do
runner.should_receive(:run).with(anything(), hash_including(line_number: 1)).and_return [true, []]
cli.start(['spec', '--line-number', 1])
end

it 'detects the server type' do
server.should_receive(:detect_server).with('specs')
cli.start(['spec', '--spec-dir', 'specs'])
@@ -274,6 +279,11 @@
cli.start(['spec'])
end

it 'sets the line number' do
runner.should_receive(:run).with(anything(), hash_including(line_number: nil)).and_return [true, []]
cli.start(['spec'])
end

it 'disables the focus mode' do
runner.should_receive(:run).with(anything(), hash_including(focus: false)).and_return [true, []]
cli.start(['spec', '-f', 'false'])
34 changes: 34 additions & 0 deletions spec/guard/jasmine/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -211,6 +211,40 @@
end
end

context 'when passed a line number' do
before do
File.stub(:readlines).and_return([
'describe "TestContext", ->',
' it "does something", ->',
' # some assertion'
])
end

context 'with the spec file name' do
it 'executes the example for line number on example' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20does%20something\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:2'], defaults)
end

it 'executes the example for line number within example' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20does%20something\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:3'], defaults)
end

it 'executes all examples within describe' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee:1'], defaults)
end
end

context 'with the cli argument' do
it 'executes the example for line number on example' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine?spec=TestContext%20does%20something\" 60000 failure true failure failure false true ''", "r:UTF-8")
runner.run(['spec/javascripts/a.js.coffee'], defaults.merge(line_number: 2))
end
end
end

context 'when passed the spec directory' do
it 'requests all jasmine specs from the server' do
IO.should_receive(:popen).with("#{ phantomjs_command } \"http://localhost:8888/jasmine\" 60000 failure true failure failure false true ''", "r:UTF-8")