[Unit Tests] Extracted rails tests to their own test suite.

This commit is contained in:
Chris Eppstein 2009-06-12 11:00:04 -07:00
parent 91832127a3
commit 8a7c92a40d
4 changed files with 140 additions and 106 deletions

104
test/command_line_helper.rb Normal file
View File

@ -0,0 +1,104 @@
module Compass::CommandLineHelper
def compass(*arguments)
if block_given?
responder = Responder.new
yield responder
IO.popen("-", "w+") do |io|
if io
#parent process
output = ""
eof_at = nil
while !eof_at || (Time.now - eof_at < 1)
if io.eof?
eof_at ||= Time.now
sleep 0.1
else
eof_at = nil
timeout(1) do
output << io.readpartial(1024)
end
prompt = output.split("\n").last
if response = responder.response_for(prompt)
io.puts response
end
end
end
responder.assert_required_responses!
@last_result = output
else
#child process
execute *arguments
end
end
else
@last_result = capture_output do
execute *arguments
end
end
rescue Timeout::Error
fail "Read from child process timed out"
end
class Responder
Response = Struct.new(:prompt, :text, :required, :responded)
def initialize
@responses = []
end
def respond_to(prompt, options = {})
@responses << Response.new(prompt, options[:with], options[:required])
end
def response_for(prompt)
response = @responses.detect{|r| r.prompt == prompt}
if response
response.responded = true
response.text
end
end
def assert_required_responses!
@responses.each do |response|
if response.required && !response.responded
raise "Prompt not encountered: \"#{response.prompt}\""
end
end
end
end
def assert_action_performed(action, path)
actions_found = []
@last_result.split("\n").each do |line|
line = line.split
return if line.first == action.to_s && line.last == path
actions_found << line.first if line.last == path
end
message = "Action #{action.inspect} was not performed on: #{path}."
message += "The following actions were performed: #{actions_found.join(", ")}" if actions_found.any?
puts @last_result
fail message
end
def within_tmp_directory(dir = "tmp")
d = absolutize(dir)
FileUtils.mkdir_p(d)
Dir.chdir(d) do
yield
end
ensure
FileUtils.rm_r(d)
end
def capture_output
real_stdout, $stdout = $stdout, StringIO.new
yield
$stdout.string
ensure
$stdout = real_stdout
end
def execute(*arguments)
Compass::Exec::Compass.new(arguments).run!
end
def generate_rails_app(name)
`rails #{name}`
end
end

View File

@ -6,6 +6,7 @@ require 'timeout'
class CommandLineTest < Test::Unit::TestCase
include Compass::TestCaseHelper
include Compass::CommandLineHelper
def teardown
Compass.configuration.reset!
@ -18,11 +19,7 @@ class CommandLineTest < Test::Unit::TestCase
def test_list_frameworks
compass "--list-frameworks"
assert_equal(<<-FRAMEWORKS, @last_result)
blueprint
compass
yui
FRAMEWORKS
assert_equal("blueprint\ncompass\nyui\n", @last_result)
end
def test_basic_install
@ -66,105 +63,4 @@ FRAMEWORKS
end
end
def test_rails_install
within_tmp_directory do
generate_rails_app("compass_rails")
Dir.chdir "compass_rails" do
compass("--rails", '--trace', ".") do |responder|
responder.respond_to "Is this OK? (Y/n) ", :with => "Y"
responder.respond_to "Emit compiled stylesheets to public/stylesheets/compiled/? (Y/n) ", :with => "Y"
end
# puts @last_result
assert_action_performed :create, "./app/stylesheets/screen.sass"
assert_action_performed :create, "./config/initializers/compass.rb"
end
end
rescue LoadError
puts "Skipping rails test. Couldn't Load rails"
end
protected
def compass(*arguments)
if block_given?
responder = Responder.new
yield responder
IO.popen("-", "w+") do |io|
if io
#parent process
output = ""
while !io.eof?
timeout(1) do
output << io.readpartial(512)
end
prompt = output.split("\n").last
if response = responder.response_for(prompt)
io.puts response
end
end
@last_result = output
else
#child process
execute *arguments
end
end
else
@last_result = capture_output do
execute *arguments
end
end
rescue Timeout::Error
fail "Read from child process timed out"
end
class Responder
def initialize
@responses = []
end
def respond_to(prompt, options = {})
@responses << [prompt, options[:with]]
end
def response_for(prompt)
pair = @responses.detect{|r| r.first == prompt}
pair.last if pair
end
end
def assert_action_performed(action, path)
actions_found = []
@last_result.split("\n").each do |line|
line = line.split
return if line.first == action.to_s && line.last == path
actions_found << line.first if line.last == path
end
message = "Action #{action.inspect} was not performed on: #{path}."
message += "The following actions were performed: #{actions_found.join(", ")}" if actions_found.any?
puts @last_result
fail message
end
def within_tmp_directory(dir = "tmp")
d = absolutize(dir)
FileUtils.mkdir_p(d)
Dir.chdir(d) do
yield
end
ensure
FileUtils.rm_r(d)
end
def capture_output
real_stdout, $stdout = $stdout, StringIO.new
yield
$stdout.string
ensure
$stdout = real_stdout
end
def execute(*arguments)
Compass::Exec::Compass.new(arguments).run!
end
def generate_rails_app(name)
`rails #{name}`
end
end

View File

@ -0,0 +1,33 @@
require File.join(File.dirname(__FILE__),'test_helper')
require File.join(File.dirname(__FILE__),'test_rails_helper')
require 'fileutils'
require 'compass'
require 'compass/exec'
require 'timeout'
class RailsIntegrationTest < Test::Unit::TestCase
include Compass::TestCaseHelper
include Compass::CommandLineHelper
def setup
Compass.configuration.reset!
end
def test_rails_install
within_tmp_directory do
generate_rails_app("compass_rails")
Dir.chdir "compass_rails" do
compass("--rails", '--trace', ".") do |responder|
responder.respond_to "Is this OK? (Y/n) ", :with => "Y", :required => true
responder.respond_to "Emit compiled stylesheets to public/stylesheets/compiled/? (Y/n) ", :with => "Y", :required => true
end
# puts ">>>#{@last_result}<<<"
assert_action_performed :create, "./app/stylesheets/screen.sass"
assert_action_performed :create, "./config/initializers/compass.rb"
end
end
rescue LoadError
puts "Skipping rails test. Couldn't Load rails"
end
end

View File

@ -18,3 +18,4 @@ require 'compass'
require 'test/unit'
require File.join(File.dirname(__FILE__), 'test_case_helper')
require File.join(File.dirname(__FILE__), 'command_line_helper')