#!/usr/bin/env ruby JSPEC_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) $:.unshift JSPEC_ROOT require 'rubygems' require 'commander' require 'fileutils' RHINO = 'java org.mozilla.javascript.tools.shell.Main' program :name, 'JSpec' program :version, '2.0.1' program :description, 'JavaScript BDD Testing Framework' default_command :bind command :init do |c| c.syntax = 'jspec init [dest]' c.summary = 'Initialize a JSpec project template' c.description = 'Initialize a JSpec project template. Defaults to the current directory when [dest] is not specified. Currently three templates are available: "default" : runs specs in browser(s) using the DOM formatter. "rhino" : runs specs with Rhino using the Terminal formatter. "server" : runs specs using a Ruby server/client solution which starts each browser and reports back to the terminal.' c.example 'Create a directory foo, initialized with a jspec template', 'jspec init foo' c.example 'Initialize a rhino based project in the current directory', 'jspec init --template rhino' c.example 'Initialize a server based project in foo', 'jspec init foo --template server' c.option '-T', '--template name', 'Template to use. Valid choices are default, rhino, and server.' c.when_called do |args, options| dest = args.shift || '.' options.default :template => 'default' unless Dir[dest + '/*'].empty? abort unless agree "'#{dest}' is not empty; continue? " end template = File.join JSPEC_ROOT, 'templates', options.template, '.' abort "template #{options.template} does not exist" unless File.exists? template FileUtils.mkdir_p dest FileUtils.cp_r template, dest path = case options.template when 'default' ; 'spec.html' when 'rhino' ; 'spec.js' else end if path spec = File.join dest, 'spec', path contents = File.read(spec).gsub 'JSPEC_ROOT', JSPEC_ROOT File.open(spec, 'w') { |file| file.write contents } end say "Template initialized at '#{dest}'" end end command :update do |c| c.syntax = 'jspec update [path ...]' c.summary = 'Update JSpec releases' c.description = 'Update JSpec release in [paths], this will allow you to utilize the latest JSpec features. If you have suites running at a path other than the regular spec/spec.html simply pass them as arguments to this sub-command. This is only needed when using the default, or rhino project templates.' c.when_called do |args, options| args = %w( spec/spec.html ) if args.empty? args.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} to JSpec #{program(:version)}" end end end command :run do |c| c.syntax = 'jspec run [path] [options]' c.summary = 'Run specifications' c.description = 'Run specifications, defaulting [path] to spec/spec.html. You will need supply [path] if your specs do not reside in this location. `run --bind` is the default sub-command of jspec so you may simply execute `jspec` in order to bind execution of your specs when a file is altered. JSpec supports Rhino execution when installed. The [path] is assumed to be spec/spec.js unless specified. See examples below for using the --rhino switch.' c.example 'Run once in Safari', 'jspec run' c.example 'Run once in Safari and Firefox', 'jspec run --browsers Safari,Firefox' 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 once', 'jspec run --rhino' c.option '-b', '--browsers BROWSERS', Array, 'Specify browsers to test, defaults to Safari' 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.when_called do |args, options| begin require 'bind' options.default :browsers => %w( Safari ), :paths => ['lib/**/*.js', 'spec/**/*.js'] # Actions if options.rhino spec = args.shift || 'spec/spec.js' action = lambda { rhino spec } elsif options.server spec = args.shift || 'spec/spec.html' action = lambda { start_server options, spec } else spec = args.shift || 'spec/spec.html' action = Bind::Actions::RefreshBrowsers.new spec, *options.browsers 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) end rescue LoadError abort "jspec run requires the visionmedia-bind gem; http://visionmedia.github.com/bind/" end end end alias_command :bind, :run, '--bind' def rhino file abort "#{file} not found" unless File.exists? file system "#{RHINO} #{file}" end def start_server options, spec require 'server/server' JSpec::Server.start options, spec end