Skip to content

Commit

Permalink
rake tasks for testing
Browse files Browse the repository at this point in the history
E.g. shotgun-powered reloadable server:

  $ rake test:reloadable

If you don't want to use this, install the bundle without it:

 $ bundle install --without reloadable
  • Loading branch information
mislav committed Jan 7, 2011
1 parent fbbefa0 commit 1008c33
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
source 'http://rubygems.org'

gem "json"

gem "sinatra", "= 1.0"
gem 'sinatra', '~> 1.0'
gem 'shotgun', :group => :reloadable
gem 'thin', :group => :reloadable
gem 'json'
16 changes: 14 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
GEM
remote: http://rubygems.org/
specs:
daemons (1.1.0)
eventmachine (0.12.10)
json (1.4.6)
rack (1.2.1)
sinatra (1.0)
shotgun (0.8)
rack (>= 1.0)
sinatra (1.1.2)
rack (~> 1.1)
tilt (~> 1.2)
thin (1.2.7)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.2.1)

PLATFORMS
ruby

DEPENDENCIES
json
sinatra (= 1.0)
shotgun
sinatra (~> 1.0)
thin
47 changes: 47 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
desc %(Starts the test server and opens it in a web browser)
multitask :default => ['test:server', 'test:open']

PORT = 4567

namespace :test do
desc %(Starts the test server)
task :server do
system 'bundle exec ruby test/server.rb'
end

desc %(Starts the test server which reloads everything on each refresh)
task :reloadable do
exec "bundle exec shotgun test/config.ru -p #{PORT} --server thin"
end

task :open do
url = "http://localhost:#{PORT}"
puts "Opening test app at #{url} ..."
sleep 3
system( *browse_cmd(url) )
end
end

# Returns an array e.g.: ['open', 'http://example.com']
def browse_cmd(url)
require 'rbconfig'
browser = ENV['BROWSER'] ||
(RbConfig::CONFIG['host_os'].include?('darwin') && 'open') ||
(RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/ && 'start') ||
%w[xdg-open x-www-browser firefox opera mozilla netscape].find { |comm| which comm }

abort('ERROR: no web browser detected') unless browser
Array(browser) << url
end

# which('ruby') #=> /usr/bin/ruby
def which cmd
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = "#{path}/#{cmd}#{ext}"
return exe if File.executable? exe
}
end
return nil
end
3 changes: 3 additions & 0 deletions test/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
require 'server'
run Sinatra::Application

0 comments on commit 1008c33

Please sign in to comment.