Improve cross platform portability #101

Merged
merged 5 commits into from Apr 26, 2012
@@ -1,5 +1,4 @@
require 'fileutils'
-require 'open4'
# The Filesystem module runs commands, captures their output and exit status
# and lets the host know about it.
@@ -46,11 +45,25 @@ def in_current_dir(&block)
# @api public
def run(command)
in_current_dir do
- pid = Open4.popen4(command) do |pid, stdin, stdout, stderr|
- @stdout = stdout.readlines.join("\n")
- @stderr = stderr.readlines.join("\n")
- end
- @last_exit_status = pid.exitstatus
+ # stdout, stderr pipes
+ rout, wout = IO.pipe
+ rerr, werr = IO.pipe
+
+ pid = Process.spawn(command, :out => wout, :err => werr)
+ _, status = Process.wait2(pid)
+
+ # close write ends so we could read them
+ wout.close
+ werr.close
+
+ @stdout = rout.readlines.join("\n")
+ @stderr = rerr.readlines.join("\n")
+
+ # dispose the read ends of the pipes
+ rout.close
+ rerr.close
+
+ @last_exit_status = status.exitstatus
end
end
@@ -60,6 +73,10 @@ def mkdir(dirname)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
end
+ def rmdir(dirname)
+ FileUtils.rm_rf(dirname) unless File.directory?(dirname)
+ end
+
def current_dir
File.join(*dirs)
end
@@ -1,3 +1,4 @@
+require "rbconfig"
require_relative 'filesystem'
module Integration
@@ -8,7 +9,7 @@ def self.included(base)
Spinach.hooks.before_scenario do
if respond_to?(:in_current_dir)
in_current_dir do
- run "rm -fR rails_app"
+ rmdir "rails_app"
end
end
end
@@ -18,7 +19,15 @@ def run_feature(command, options={})
options[:framework] ||= :minitest
use_minitest if options[:framework] == :minitest
use_rspec if options[:framework] == :rspec
- run "../../bin/spinach #{command} #{options[:append]}"
+ run "#{ruby} ../../bin/spinach #{command} #{options[:append]}"
+ end
+
+ def ruby
+ return @ruby if defined?(@ruby)
+
+ config = RbConfig::CONFIG
+ @ruby = File.join(config["bindir"],
+ "#{config["ruby_install_name"]}#{config["EXEEXT"]}")
end
def use_minitest
View
@@ -1,5 +1,6 @@
require 'capybara'
require 'capybara/dsl'
+require 'rbconfig'
require_relative 'feature_steps'
module Spinach
@@ -33,12 +34,24 @@ def self.included(base)
def visit(*args)
stream = STDOUT
old_stream = stream.dup
- stream.reopen('/dev/null')
+ stream.reopen(null_device)
stream.sync = true
super
ensure
stream.reopen(old_stream)
end
+
+ def null_device
+ return @null_device if defined?(@null_device)
+
+ if RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
+ @null_device = "NUL"
+ else
+ @null_device = "/dev/null"
+ end
+
+ @null_device
+ end
end
end
end
View
@@ -15,11 +15,9 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'mocha'
gem.add_development_dependency 'sinatra'
gem.add_development_dependency 'capybara'
- gem.add_development_dependency 'open4'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'simplecov'
gem.add_development_dependency 'rspec'
- gem.add_development_dependency 'fakefs'
gem.add_development_dependency 'minitest', '~> 2.0'
gem.add_development_dependency 'turn'
@@ -63,28 +63,28 @@ module Spinach::Generators
describe "#store" do
it "stores the generated feature into a file" do
- FakeFS.activate!
- subject.store
- File.directory?("features/steps/").must_equal true
- File.exists?("features/steps/cheezburger_can_i_has.rb").must_equal true
- File.read("features/steps/cheezburger_can_i_has.rb").strip.must_equal(
- subject.generate.strip
- )
- FileUtils.rm_rf("features/steps")
- FakeFS.deactivate!
+ in_current_dir do
+ subject.store
+ File.directory?("features/steps/").must_equal true
+ File.exists?("features/steps/cheezburger_can_i_has.rb").must_equal true
+ File.read("features/steps/cheezburger_can_i_has.rb").strip.must_equal(
+ subject.generate.strip
+ )
+ FileUtils.rm_rf("features/steps")
+ end
end
it "raises an error if the file already exists and does nothing" do
file = "features/steps/cheezburger_can_i_has.rb"
- FakeFS.activate!
- FileUtils.mkdir_p "features/steps"
- File.open(file, 'w') do |f|
- f.write("Fake content")
+ in_current_dir do
+ FileUtils.mkdir_p "features/steps"
+ File.open(file, 'w') do |f|
+ f.write("Fake content")
+ end
+ Proc.new{subject.store}.must_raise(
+ Spinach::Generators::FeatureGeneratorException)
+ FileUtils.rm_rf("features/steps")
end
- Proc.new{subject.store}.must_raise(
- Spinach::Generators::FeatureGeneratorException)
- FileUtils.rm_rf("features/steps")
- FakeFS.deactivate!
end
end
end
@@ -32,19 +32,22 @@
describe "#generate_feature" do
it "generates a file" do
feature_data = data
- FakeFS.activate!
- subject.generate_feature(feature_data)
- File.exists?("features/steps/cheezburger_can_i_has.rb").must_equal true
- FileUtils.rm_rf("features/steps")
- FakeFS.deactivate!
+ in_current_dir do
+ subject.generate_feature(feature_data)
+ File.exists?("features/steps/cheezburger_can_i_has.rb").must_equal true
+ FileUtils.rm_rf("features/steps")
+ end
end
+
it "outputs a message if feature cannot be generated" do
- subject::FeatureGenerator.expects(:new).raises(
- Spinach::Generators::FeatureGeneratorException.new("File already exists"))
- capture_stdout do
- subject.generate_feature(data)
- end.must_include "File already exists"
- File.exists?("features/steps/cheezburger_can_i_has.rb").must_equal false
+ in_current_dir do
+ subject::FeatureGenerator.expects(:new).raises(
+ Spinach::Generators::FeatureGeneratorException.new("File already exists"))
+ capture_stdout do
+ subject.generate_feature(data)
+ end.must_include "File already exists"
+ File.exists?("features/steps/cheezburger_can_i_has.rb").must_equal false
+ end
end
end
end
View
@@ -0,0 +1,13 @@
+require "fileutils"
+
+module Filesystem
+ def in_current_dir(&block)
+ dir = "tmp/fs"
+ FileUtils.mkdir(dir)
+ Dir.chdir(dir, &block)
+ ensure
+ FileUtils.rm_rf(dir)
+ end
+end
+
+MiniTest::Spec.send(:include, Filesystem)
View
@@ -15,11 +15,12 @@
require 'ostruct'
require 'stringio'
require 'pry'
-require 'fakefs/safe'
require 'turn'
require 'spinach'
+require_relative "support/filesystem"
+
module Kernel
def capture_stdout
out = StringIO.new