Merge pull request #1398 from maxmeyer/feature/switch_current_working_dir

Switch to current working directory during testing
This commit is contained in:
Thomas Reynolds 2014-11-03 08:02:48 -08:00
commit ebe2164fe9
2 changed files with 69 additions and 13 deletions

View file

@ -0,0 +1,33 @@
Feature: Honour working directory
Honour the working directory during testing
In order to support helpers which work with the current directories
Scenario: Set working directory for helpers in tests
Given a fixture app "empty-app"
And a file named "source/index.erb" with:
"""
<%= Dir.getwd %>
"""
And the Server is running
When I go to "/index.html"
Then I should see:
"""
aruba
"""
Scenario: Set working directory for config.rb in tests
Given a fixture app "empty-app"
And a file named "config.rb" with:
"""
set :my_working_directory, Dir.getwd
"""
And a file named "source/index.erb" with:
"""
<%= my_working_directory %>
"""
And the Server is running
When I go to "/index.html"
Then I should see:
"""
aruba
"""

View file

@ -34,6 +34,7 @@ end
Given /^the Server is running$/ do Given /^the Server is running$/ do
root_dir = File.expand_path(current_dir) root_dir = File.expand_path(current_dir)
if File.exists?(File.join(root_dir, 'source')) if File.exists?(File.join(root_dir, 'source'))
ENV['MM_SOURCE'] = 'source' ENV['MM_SOURCE'] = 'source'
else else
@ -48,11 +49,13 @@ Given /^the Server is running$/ do
set :show_exceptions, false set :show_exceptions, false
} }
in_current_dir do
@server_inst = Middleman::Application.server.inst do @server_inst = Middleman::Application.server.inst do
initialize_commands.each do |p| initialize_commands.each do |p|
instance_exec(&p) instance_exec(&p)
end end
end end
end
app_rack = @server_inst.class.to_rack_app app_rack = @server_inst.class.to_rack_app
@browser = ::Rack::Test::Session.new(::Rack::MockSession.new(app_rack)) @browser = ::Rack::Test::Session.new(::Rack::MockSession.new(app_rack))
@ -68,41 +71,61 @@ Given /^a template named "([^\"]*)" with:$/ do |name, string|
end end
When /^I go to "([^\"]*)"$/ do |url| When /^I go to "([^\"]*)"$/ do |url|
in_current_dir do
@browser.get(URI.escape(url)) @browser.get(URI.escape(url))
end
end end
Then /^going to "([^\"]*)" should not raise an exception$/ do |url| Then /^going to "([^\"]*)" should not raise an exception$/ do |url|
in_current_dir do
expect{ @browser.get(URI.escape(url)) }.to_not raise_exception expect{ @browser.get(URI.escape(url)) }.to_not raise_exception
end
end end
Then /^the content type should be "([^\"]*)"$/ do |expected| Then /^the content type should be "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.content_type).to start_with(expected) expect(@browser.last_response.content_type).to start_with(expected)
end
end end
Then /^I should see "([^\"]*)"$/ do |expected| Then /^I should see "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to include(expected) expect(@browser.last_response.body).to include(expected)
end
end end
Then /^I should see '([^\']*)'$/ do |expected| Then /^I should see '([^\']*)'$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to include(expected) expect(@browser.last_response.body).to include(expected)
end
end end
Then /^I should see:$/ do |expected| Then /^I should see:$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to include(expected) expect(@browser.last_response.body).to include(expected)
end
end end
Then /^I should not see "([^\"]*)"$/ do |expected| Then /^I should not see "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to_not include(expected) expect(@browser.last_response.body).to_not include(expected)
end
end end
Then /^I should not see:$/ do |expected| Then /^I should not see:$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to_not include(expected.chomp) expect(@browser.last_response.body).to_not include(expected.chomp)
end
end end
Then /^the status code should be "([^\"]*)"$/ do |expected| Then /^the status code should be "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.status).to eq expected.to_i expect(@browser.last_response.status).to eq expected.to_i
end
end end
Then /^I should see "([^\"]*)" lines$/ do |lines| Then /^I should see "([^\"]*)" lines$/ do |lines|
in_current_dir do
expect(@browser.last_response.body.chomp.split($/).length).to eq(lines.to_i) expect(@browser.last_response.body.chomp.split($/).length).to eq(lines.to_i)
end
end end