From a9aa79d9a7f8115a2224cc37955e3b51f7ded749 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Mon, 19 Jan 2009 10:05:59 -0800 Subject: [PATCH] Support for testing the compass command line when it expects stdin responses. --- test/command_line_test.rb | 44 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/test/command_line_test.rb b/test/command_line_test.rb index 612b92b2..779d809f 100644 --- a/test/command_line_test.rb +++ b/test/command_line_test.rb @@ -2,6 +2,7 @@ require File.dirname(__FILE__)+'/test_helper' require 'fileutils' require 'compass' require 'compass/exec' +require 'timeout' class CommandLineTest < Test::Unit::TestCase include Compass::TestCaseHelper @@ -55,8 +56,47 @@ class CommandLineTest < Test::Unit::TestCase protected def compass(*arguments) - @last_result = capture_output do - execute *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