From 8a7c92a40de53f72431db56750d34a307d35c624 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 12 Jun 2009 11:00:04 -0700 Subject: [PATCH] [Unit Tests] Extracted rails tests to their own test suite. --- test/command_line_helper.rb | 104 +++++++++++++++++++++++++++++++ test/command_line_test.rb | 108 +-------------------------------- test/rails_integration_test.rb | 33 ++++++++++ test/test_helper.rb | 1 + 4 files changed, 140 insertions(+), 106 deletions(-) create mode 100644 test/command_line_helper.rb create mode 100644 test/rails_integration_test.rb diff --git a/test/command_line_helper.rb b/test/command_line_helper.rb new file mode 100644 index 00000000..5601873e --- /dev/null +++ b/test/command_line_helper.rb @@ -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 \ No newline at end of file diff --git a/test/command_line_test.rb b/test/command_line_test.rb index a94ee220..6552c3be 100644 --- a/test/command_line_test.rb +++ b/test/command_line_test.rb @@ -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 \ No newline at end of file diff --git a/test/rails_integration_test.rb b/test/rails_integration_test.rb new file mode 100644 index 00000000..0b01e259 --- /dev/null +++ b/test/rails_integration_test.rb @@ -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 \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 6e0ed21d..30ee1245 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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')