]> git.wincent.com - wikitext.git/commitdiff
Add low-level "run" method to Rails specs
authorWincent Colaiuta <win@wincent.com>
Mon, 5 Jan 2009 16:43:09 +0000 (17:43 +0100)
committerWincent Colaiuta <win@wincent.com>
Mon, 5 Jan 2009 16:48:42 +0000 (17:48 +0100)
First step in allowing us to run these specs silently; the run method
is a low-level method that runs a command with the given arguments,
silently recording the stdout, stderr, and exit status for later
retrieval.

Signed-off-by: Wincent Colaiuta <win@wincent.com>
spec/rails_spec.rb

index d6cf2750c51b7b66f1a611cba8215696c95247e0..df53d9c060b8d05cca1125f194e6726c6ca286c0 100644 (file)
@@ -17,14 +17,33 @@ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
 require 'wikitext/version'
 require 'fileutils'
 require 'pathname'
+require 'wopen3'
+require 'ostruct'
 
-# TODO: silence stdout for these commands; want the specs to run silently unless errors occur
 module RailsSpecs
   TRASH_PATH    = Pathname.new(__FILE__).dirname + 'trash'
   CLONE_PATH    = TRASH_PATH + 'rails.git'
   RAILS_PATH    = CLONE_PATH + 'railties' + 'bin' + 'rails'
   EDGE_APP_PATH = TRASH_PATH + 'edge-app'
 
+  def run cmd, *args
+    result = OpenStruct.new
+    result.stdout = ''
+    result.stderr = ''
+    Wopen3.popen3(*([cmd] + args)) do |stdin, stdout, stderr|
+      threads = []
+      threads << Thread.new(stdout) do |out|
+        out.each { |line| result.stdout << line }
+      end
+      threads << Thread.new(stderr) do |err|
+        err.each { |line| result.stderr << line }
+      end
+      threads.each { |thread| thread.join }
+    end
+    result.status = $?.exitstatus
+    result
+  end
+
   def clone
     if File.exist? CLONE_PATH
       system "cd #{CLONE_PATH} && git fetch"