2011-04-10 22:57:22 -04:00
|
|
|
require 'backports'
|
|
|
|
require_relative 'spec_helper'
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
describe Sinatra::Reloader do
|
2011-04-14 12:00:09 -04:00
|
|
|
# Returns the temporary directory.
|
2011-04-10 22:57:22 -04:00
|
|
|
def tmp_dir
|
|
|
|
File.expand_path('../../tmp', __FILE__)
|
|
|
|
end
|
|
|
|
|
2011-04-14 12:00:09 -04:00
|
|
|
# Returns the path of the Sinatra application file created by
|
|
|
|
# +setup_example_app+.
|
2011-04-10 22:57:22 -04:00
|
|
|
def app_file_path
|
2011-04-12 20:03:52 -04:00
|
|
|
File.join(tmp_dir, "example_app_#{@@example_app_counter}.rb")
|
|
|
|
end
|
|
|
|
|
2011-04-14 12:00:09 -04:00
|
|
|
# Returns the name of the Sinatra application created by
|
|
|
|
# +setup_example_app+: 'ExampleApp1' for the first application,
|
|
|
|
# 'ExampleApp2' fo the second one, and so on...
|
2011-04-12 20:03:52 -04:00
|
|
|
def app_name
|
|
|
|
"ExampleApp#{@@example_app_counter}"
|
|
|
|
end
|
|
|
|
|
2011-04-14 12:00:09 -04:00
|
|
|
# Returns the (constant of the) Sinatra application created by
|
|
|
|
# +setup_example_app+.
|
2011-04-12 20:03:52 -04:00
|
|
|
def app_const
|
|
|
|
Module.const_get(app_name)
|
2011-04-10 22:57:22 -04:00
|
|
|
end
|
|
|
|
|
2011-04-14 12:00:09 -04:00
|
|
|
# Writes a file with a Sinatra application using the template
|
|
|
|
# located at <tt>specs/reloader/app.rb.erb</tt>. It expects an
|
|
|
|
# +options+ hash, with an array of strings containing the
|
|
|
|
# application's routes (+:routes+ key), a hash with the inline
|
|
|
|
# template's names as keys and the bodys as values
|
|
|
|
# (+:inline_templates+ key) and an optional application name
|
|
|
|
# (+:name+) otherwise +app_name+ is used.
|
2011-04-16 14:38:00 -04:00
|
|
|
#
|
|
|
|
# It ensures to change the written file's mtime when it already
|
|
|
|
# exists.
|
2011-04-10 22:57:22 -04:00
|
|
|
def write_app_file(options={})
|
|
|
|
options[:routes] ||= ['get("/foo") { erb :foo }']
|
|
|
|
options[:inline_templates] ||= nil
|
2011-04-12 20:03:52 -04:00
|
|
|
options[:name] ||= app_name
|
2011-04-10 22:57:22 -04:00
|
|
|
|
2011-04-16 14:38:00 -04:00
|
|
|
update_file(app_file_path) do |f|
|
2011-04-10 22:57:22 -04:00
|
|
|
template_path = File.expand_path('../reloader/app.rb.erb', __FILE__)
|
|
|
|
template = Tilt.new(template_path, nil, :trim => '<>')
|
|
|
|
f.write template.render(Object.new, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-16 14:38:00 -04:00
|
|
|
alias update_app_file write_app_file
|
2011-04-13 14:38:35 -04:00
|
|
|
|
2011-04-16 14:38:00 -04:00
|
|
|
# It calls <tt>File.open(path, 'w', &block)</tt> all the times
|
|
|
|
# needed to change the file's mtime.
|
|
|
|
def update_file(path, &block)
|
2011-04-16 14:06:30 -04:00
|
|
|
original_mtime = File.exist?(path) ? File.mtime(path) : Time.at(0)
|
2011-04-10 22:57:22 -04:00
|
|
|
begin
|
2011-04-16 14:38:00 -04:00
|
|
|
File.open(path, 'w', &block)
|
2011-04-10 22:57:22 -04:00
|
|
|
sleep 0.1
|
2011-04-13 14:38:35 -04:00
|
|
|
end until original_mtime != File.mtime(path)
|
2011-04-10 22:57:22 -04:00
|
|
|
end
|
|
|
|
|
2011-04-14 12:00:09 -04:00
|
|
|
# Writes a Sinatra application to a file, requires the file, sets
|
|
|
|
# the new application as the one being tested and enables the
|
|
|
|
# reloader.
|
2011-04-13 14:38:35 -04:00
|
|
|
def setup_example_app(options={})
|
2011-04-12 20:03:52 -04:00
|
|
|
@@example_app_counter ||= 0
|
|
|
|
@@example_app_counter += 1
|
|
|
|
|
2011-04-10 22:57:22 -04:00
|
|
|
FileUtils.mkdir_p(tmp_dir)
|
2011-04-12 20:03:52 -04:00
|
|
|
write_app_file(options)
|
|
|
|
$LOADED_FEATURES.delete app_file_path
|
|
|
|
require app_file_path
|
|
|
|
self.app = app_const
|
2011-04-13 20:24:40 -04:00
|
|
|
app_const.enable :reloader
|
2011-04-12 20:03:52 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
after(:all) { FileUtils.rm_rf(tmp_dir) }
|
2011-04-12 20:03:52 -04:00
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
describe "default reloading mechanism" do
|
|
|
|
before(:each) do
|
|
|
|
setup_example_app(
|
|
|
|
:routes => ['get("/foo") { erb :foo }'],
|
|
|
|
:inline_templates => { :foo => 'foo' }
|
|
|
|
)
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
it "doesn't mess up the application" do
|
|
|
|
get('/foo').body.should == 'foo'
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
it "knows when a route has been modified" do
|
|
|
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
|
|
|
get('/foo').body.should == 'bar'
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
it "knows when a route has been added" do
|
|
|
|
update_app_file(
|
|
|
|
:routes => ['get("/foo") { "foo" }', 'get("/bar") { "bar" }']
|
|
|
|
)
|
|
|
|
get('/foo').body.should == 'foo'
|
|
|
|
get('/bar').body.should == 'bar'
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
it "knows when a route has been removed" do
|
|
|
|
update_app_file(:routes => ['get("/bar") { "bar" }'])
|
|
|
|
get('/foo').status.should == 404
|
|
|
|
end
|
|
|
|
|
2011-04-13 21:16:22 -04:00
|
|
|
it "reloads inline templates in the app file" do
|
2011-04-13 14:38:35 -04:00
|
|
|
update_app_file(
|
|
|
|
:routes => ['get("/foo") { erb :foo }'],
|
|
|
|
:inline_templates => { :foo => 'bar' }
|
|
|
|
)
|
|
|
|
get('/foo').body.should == 'bar'
|
|
|
|
end
|
2011-04-13 21:16:22 -04:00
|
|
|
|
|
|
|
it "reloads inline templates in other file" do
|
|
|
|
setup_example_app(:routes => ['get("/foo") { erb :foo }'])
|
|
|
|
template_file_path = File.join(tmp_dir, 'templates.rb')
|
|
|
|
File.open(template_file_path, 'w') do |f|
|
|
|
|
f.write "__END__\n\n@@foo\nfoo"
|
|
|
|
end
|
|
|
|
require template_file_path
|
|
|
|
app_const.inline_templates= template_file_path
|
|
|
|
get('/foo').body.should == 'foo'
|
2011-04-16 14:38:00 -04:00
|
|
|
update_file(template_file_path) do |f|
|
|
|
|
f.write "__END__\n\n@@foo\nbar"
|
2011-04-13 21:16:22 -04:00
|
|
|
end
|
|
|
|
get('/foo').body.should == 'bar'
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
describe ".dont_reload" do
|
|
|
|
before(:each) do
|
|
|
|
setup_example_app(
|
|
|
|
:routes => ['get("/foo") { erb :foo }'],
|
|
|
|
:inline_templates => { :foo => 'foo' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to specify a file to stop from being reloaded" do
|
|
|
|
app_const.dont_reload app_file_path
|
|
|
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
|
|
|
get('/foo').body.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to specify a glob to stop matching files from being reloaded" do
|
|
|
|
app_const.dont_reload '**/*.rb'
|
|
|
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
|
|
|
get('/foo').body.should == 'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't interfere with other application's reloading policy" do
|
|
|
|
app_const.dont_reload '**/*.rb'
|
|
|
|
setup_example_app(:routes => ['get("/foo") { "foo" }'])
|
|
|
|
update_app_file(:routes => ['get("/foo") { "bar" }'])
|
|
|
|
get('/foo').body.should == 'bar'
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 14:38:35 -04:00
|
|
|
describe ".also_reload" do
|
|
|
|
before(:each) do
|
|
|
|
setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
|
|
|
|
@foo_path = File.join(tmp_dir, 'foo.rb')
|
2011-04-16 14:38:00 -04:00
|
|
|
update_file(@foo_path) do |f|
|
|
|
|
f.write 'class Foo; def self.foo() "foo" end end'
|
2011-04-13 14:38:35 -04:00
|
|
|
end
|
|
|
|
$LOADED_FEATURES.delete @foo_path
|
|
|
|
require @foo_path
|
|
|
|
app_const.also_reload @foo_path
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to specify a file to be reloaded" do
|
|
|
|
get('/foo').body.should == 'foo'
|
2011-04-16 14:38:00 -04:00
|
|
|
update_file(@foo_path) do |f|
|
|
|
|
f.write 'class Foo; def self.foo() "bar" end end'
|
2011-04-13 14:38:35 -04:00
|
|
|
end
|
|
|
|
get('/foo').body.should == 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to specify glob to reaload matching files" do
|
|
|
|
get('/foo').body.should == 'foo'
|
2011-04-16 14:38:00 -04:00
|
|
|
update_file(@foo_path) do |f|
|
|
|
|
f.write 'class Foo; def self.foo() "bar" end end'
|
2011-04-13 14:38:35 -04:00
|
|
|
end
|
|
|
|
get('/foo').body.should == 'bar'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't interfere with other application's reloading policy" do
|
|
|
|
app_const.also_reload '**/*.rb'
|
|
|
|
setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
|
|
|
|
get('/foo').body.should == 'foo'
|
2011-04-16 14:38:00 -04:00
|
|
|
update_file(@foo_path) do |f|
|
|
|
|
f.write 'class Foo; def self.foo() "bar" end end'
|
2011-04-13 14:38:35 -04:00
|
|
|
end
|
|
|
|
get('/foo').body.should == 'foo'
|
|
|
|
end
|
2011-04-10 22:57:22 -04:00
|
|
|
end
|
|
|
|
end
|