Skip to content

Commit 1008c33

Browse files
committed
rake tasks for testing
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
1 parent fbbefa0 commit 1008c33

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

Diff for: Gemfile

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
source 'http://rubygems.org'
22

3-
gem "json"
4-
5-
gem "sinatra", "= 1.0"
3+
gem 'sinatra', '~> 1.0'
4+
gem 'shotgun', :group => :reloadable
5+
gem 'thin', :group => :reloadable
6+
gem 'json'

Diff for: Gemfile.lock

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4+
daemons (1.1.0)
5+
eventmachine (0.12.10)
46
json (1.4.6)
57
rack (1.2.1)
6-
sinatra (1.0)
8+
shotgun (0.8)
79
rack (>= 1.0)
10+
sinatra (1.1.2)
11+
rack (~> 1.1)
12+
tilt (~> 1.2)
13+
thin (1.2.7)
14+
daemons (>= 1.0.9)
15+
eventmachine (>= 0.12.6)
16+
rack (>= 1.0.0)
17+
tilt (1.2.1)
818

919
PLATFORMS
1020
ruby
1121

1222
DEPENDENCIES
1323
json
14-
sinatra (= 1.0)
24+
shotgun
25+
sinatra (~> 1.0)
26+
thin

Diff for: Rakefile

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
desc %(Starts the test server and opens it in a web browser)
2+
multitask :default => ['test:server', 'test:open']
3+
4+
PORT = 4567
5+
6+
namespace :test do
7+
desc %(Starts the test server)
8+
task :server do
9+
system 'bundle exec ruby test/server.rb'
10+
end
11+
12+
desc %(Starts the test server which reloads everything on each refresh)
13+
task :reloadable do
14+
exec "bundle exec shotgun test/config.ru -p #{PORT} --server thin"
15+
end
16+
17+
task :open do
18+
url = "http://localhost:#{PORT}"
19+
puts "Opening test app at #{url} ..."
20+
sleep 3
21+
system( *browse_cmd(url) )
22+
end
23+
end
24+
25+
# Returns an array e.g.: ['open', 'http://example.com']
26+
def browse_cmd(url)
27+
require 'rbconfig'
28+
browser = ENV['BROWSER'] ||
29+
(RbConfig::CONFIG['host_os'].include?('darwin') && 'open') ||
30+
(RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/ && 'start') ||
31+
%w[xdg-open x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
32+
33+
abort('ERROR: no web browser detected') unless browser
34+
Array(browser) << url
35+
end
36+
37+
# which('ruby') #=> /usr/bin/ruby
38+
def which cmd
39+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
40+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
41+
exts.each { |ext|
42+
exe = "#{path}/#{cmd}#{ext}"
43+
return exe if File.executable? exe
44+
}
45+
end
46+
return nil
47+
end

Diff for: test/config.ru

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
2+
require 'server'
3+
run Sinatra::Application

0 commit comments

Comments
 (0)