bin/jspec in visionmedia-jspec-2.8.4 vs bin/jspec in visionmedia-jspec-2.9.0
- old
+ new
@@ -1,19 +1,20 @@
#!/usr/bin/env ruby
JSPEC_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
$:.unshift JSPEC_ROOT
-
+
require 'rubygems'
require 'commander'
require 'bind'
require 'fileutils'
+require 'server/server'
RHINO = 'java org.mozilla.javascript.tools.shell.Main'
program :name, 'JSpec'
-program :version, '2.8.4'
+program :version, '2.9.0'
program :description, 'JavaScript BDD Testing Framework'
default_command :bind
command :init do |c|
c.syntax = 'jspec init [dest]'
@@ -24,13 +25,13 @@
c.example 'Create a directory foo, initialized with a jspec template', 'jspec init foo'
c.option '-R', '--rails', 'Initialize rails template'
c.when_called do |args, options|
dest = args.shift || '.'
if options.rails
- initialize_rails_to dest
+ initialize_rails_at dest
else
- initialize_to dest
+ initialize_at dest
end
say "Template initialized at '#{dest}'"
end
end
@@ -69,114 +70,173 @@
JSpec\'s server is also available via --server, which defaults
the [path] to spec/server.html'
c.example 'Run once in Safari', 'jspec run'
c.example 'Run once in Safari and Firefox', 'jspec run --browsers Safari,Firefox'
+ c.example 'Run once in Opera, Firefox, and Chrome', 'jspec run --browsers opera,ff,chrome'
c.example 'Run custom spec file', 'jspec run foo.html'
c.example 'Auto-run browsers when a file is altered', 'jspec run --bind --browsers Safari,Firefox'
c.example 'Shortcut for the previous example', 'jspec --browsers Safari,Firefox'
c.example 'Auto-run rhino when a file is altered', 'jspec --rhino'
+ c.example 'Run Rhino specs at spec/rhino.js', 'jspec run --rhino'
c.example 'Run Rhino specs once', 'jspec run specs/something.js --rhino'
- c.option '-b', '--browsers BROWSERS', Array, 'Specify browsers to test, defaults to Safari'
+ c.option '-b', '--browsers BROWSERS', Array, 'Specify browsers to test'
c.option '-p', '--paths PATHS', Array, 'Specify paths when binding, defaults to javascript within ./lib and ./spec'
c.option '-B', '--bind', 'Auto-run specs when source files or specs are altered'
c.option '-R', '--rhino', 'Run specs using Rhino'
c.option '-S', '--server', 'Run specs using the JSpec server'
- c.option '-s', '--server-only', 'Start JSpec server without running browsers'
+ c.option '-P', '--port NUMBER', Integer, 'Start JSpec server using the given port number'
c.when_called do |args, options|
# Rails
if rails?
- options.default :browsers => %w( Safari ), :paths => ['public/javascripts/**/*.js', 'jspec/**/*.js']
- else
- options.default :browsers => %w( Safari ), :paths => ['lib/**/*.js', 'spec/**/*.js']
+ options.default :paths => ['public/javascripts/**/*.js', 'jspec/**/*.js'], :port => 4444
+ else
+ options.default :paths => ['lib/**/*.js', 'spec/**/*.js'], :port => 4444
end
# Actions
if options.rhino
- spec = args.shift || path_to('spec.rhino.js')
- action = lambda { rhino spec }
- elsif options.server_only
- start_server options, nil
+ suite = args.shift || path_to('spec.rhino.js')
+ action = lambda { rhino suite }
elsif options.server
- spec = args.shift || path_to('spec.server.html')
- action = lambda { start_server options, spec }
+ raise 'Cannot use --server with --bind' if options.bind
+ suite = args.shift || path_to('spec.server.html')
+ action = lambda { start_server suite, options }
else
- spec = args.shift || path_to('spec.dom.html')
- action = Bind::Actions::RefreshBrowsers.new spec, *options.browsers
+ suite = args.shift || path_to('spec.dom.html')
+ browsers = browsers_for options.browsers || ['safari']
+ action = lambda do
+ browsers.each do |browser|
+ browser.visit File.expand_path(suite)
+ end
+ end
end
# Binding
if options.bind
listener = Bind::Listener.new :paths => options.paths, :interval => 1, :actions => [action], :debug => $stdout
listener.run!
else
- action.call File.new(spec)
+ action.call File.new(suite)
end
end
end
alias_command :bind, :run, '--bind'
-def initialize_to dest
+##
+# Initialize template at _dest_.
+
+def initialize_at dest
unless Dir[dest + '/*'].empty?
abort unless agree "'#{dest}' is not empty; continue? "
end
copy_template_to 'default', dest
replace_root_in dest, 'spec/spec.dom.html', 'spec/spec.rhino.js'
end
-def initialize_rails_to dest
+##
+# Initialize rails template at _dest_.
+
+def initialize_rails_at dest
unless looks_like_rails_root?(dest)
abort unless agree "'#{dest}' does not look like root of a rails project; continue? "
end
copy_template_to 'rails', "#{dest}/jspec"
replace_root_in "#{dest}/jspec", 'spec.dom.html', 'spec.rhino.js'
end
+##
+# Copy template _name_ to _dest_.
+
def copy_template_to name, dest
FileUtils.mkdir_p dest
FileUtils.cp_r path_to_template(name), dest
end
+##
+# Return path to template _name_.
+
def path_to_template name
File.join JSPEC_ROOT, 'templates', name, '.'
end
+##
+# Resolve path to _file_. Supports rails and unbound projects.
+
def path_to file
rails? ? "jspec/#{file}" : "spec/#{file}"
end
+##
+# Execute _file_ with Rhino.
+
def rhino file
- abort "#{file} not found" unless File.exists? file
+ raise "#{file} not found" unless File.exists? file
system "#{RHINO} #{file}"
end
-def start_server options, spec
- require 'server/server'
- JSpec::Server.start options, spec
+##
+# Start server with _suite_ html and _options_.
+
+def start_server suite, options
+ set :port, options.port
+ set :server, 'Mongrel'
+ enable :sessions
+ disable :logging
+ hook = File.expand_path path_to('server.rb')
+ load hook if File.exists? hook
+ JSpec::Server.new(suite, options.port).start(options.browsers ? browsers_for(options.browsers) : nil)
end
+##
+# Return array of browser instances for the given _names_.
+
+def browsers_for names
+ names.map do |name|
+ begin
+ Browser.subclasses.find do |browser|
+ browser.matches_name? name
+ end.new
+ rescue
+ raise "Unsupported browser `#{name}'"
+ end
+ end
+end
+
+##
+# Check if the current directory looks like a rails app.
+
def rails?
File.directory? 'jspec'
end
+##
+# Replace JSPEC_ROOT placeholder in _paths_ relative to _dest_.
+
def replace_root_in dest, *paths
paths.each do |path|
path = File.join dest, path
contents = File.read(path).gsub 'JSPEC_ROOT', JSPEC_ROOT
File.open(path, 'w') { |file| file.write contents }
end
end
+##
+# Update JSpec version in _paths_. Matches visionmedia-jspec-TRIPLE
+
def update_version_in *paths
paths.each do |path|
next unless File.exists? path
contents = File.read(path).gsub /visionmedia-jspec-(\d+\.\d+\.\d+)/, "visionmedia-jspec-#{program(:version)}"
File.open(path, 'r+'){ |file| file.write contents }
say "Updated #{path}; #{$1} -> #{program(:version)}"
end
say "Finished updating JSpec"
end
+
+##
+# Check if _path_ looks like a rails root directory.
def looks_like_rails_root? path = '.'
File.directory? "#{path}/vendor"
end