test/helper.rb in nanoc-3.6.6 vs test/helper.rb in nanoc-3.6.7
- old
+ new
@@ -19,14 +19,17 @@
require 'nanoc'
require 'nanoc/cli'
require 'nanoc/tasks'
# Load miscellaneous requirements
+require 'tmpdir'
require 'stringio'
module Nanoc::TestHelpers
+ LIB_DIR = File.expand_path(File.dirname(__FILE__) + '/../lib')
+
def if_have(*libs)
libs.each do |lib|
begin
require lib
rescue LoadError
@@ -111,18 +114,21 @@
# Clean up
GC.start
# Go quiet
unless ENV['QUIET'] == 'false'
+ @orig_stdout = $stdout
+ @orig_stderr = $stderr
+
$stdout = StringIO.new
$stderr = StringIO.new
end
# Enter tmp
- FileUtils.mkdir_p('tmp')
+ @tmp_dir = Dir.mktmpdir('nanoc-test')
@orig_wd = FileUtils.pwd
- FileUtils.cd('tmp')
+ FileUtils.cd(@tmp_dir)
# Let us get to the raw errors
Nanoc::CLI::ErrorHandler.disable
end
@@ -130,16 +136,16 @@
# Restore normal error handling
Nanoc::CLI::ErrorHandler.enable
# Exit tmp
FileUtils.cd(@orig_wd)
- FileUtils.rm_rf('tmp')
+ FileUtils.rm_rf(@tmp_dir)
# Go unquiet
unless ENV['QUIET'] == 'false'
- $stdout = STDOUT
- $stderr = STDERR
+ $stdout = @orig_stdout
+ $stderr = @orig_stderr
end
end
def capturing_stdio(&block)
# Store
@@ -209,9 +215,34 @@
orig_env_hash = ENV.to_hash
hash.each_pair { |k,v| ENV[k] = v }
yield
ensure
orig_env_hash.each_pair { |k,v| ENV[k] = v }
+ end
+
+ def on_windows?
+ Nanoc.on_windows?
+ end
+
+ def have_command?(cmd)
+ which, null = on_windows? ? ["where", "NUL"] : ["which", "/dev/null"]
+ system("#{which} #{cmd} > #{null} 2>&1")
+ end
+
+ def have_symlink?
+ File.symlink nil, nil
+ rescue NotImplementedError
+ return false
+ rescue
+ return true
+ end
+
+ def skip_unless_have_command(cmd)
+ skip "Could not find external command \"#{cmd}\"" unless have_command?(cmd)
+ end
+
+ def skip_unless_have_symlink
+ skip "Symlinks are not supported by Ruby on Windows" unless have_symlink?
end
end
class Nanoc::TestCase < MiniTest::Unit::TestCase