diff --git a/Rakefile b/Rakefile index e69de29..70a846d 100644 --- a/Rakefile +++ b/Rakefile @@ -0,0 +1,5 @@ +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task :default => :spec diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e69de29..6a2f3ca 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -0,0 +1,61 @@ + + + +# Load all spec files +Dir["./spec/support/**/*.rb"].each {|f| require f} + +RSpec.configure do |config| + # Allows using build(), create(), etc. without the "FactoryGirl." part. + config.include FactoryGirl::Syntax::Methods + + config.before(:each) do + $0 = "kook" # Pretend we're running as 'kook' + ARGV.clear # Make sure no args are passed to the commands. + @directory = Dir.mktmpdir('kook-spec-') # Create a temp directory to work in. + @orig_directory = Dir.pwd # Save the original directory. + Dir.chdir(@directory) # Change to it. pwd() is the temp directory in the examples. + end + + config.after(:each) do + Dir.chdir(@orig_directory) # Change back to the origin directory. + FileUtils.rmtree(@directory) # Remove the temp directory. + end + + + # Captures the output for analysis later + # + # @example Capture `$stderr` + # + # output = capture(:stderr) { $stderr.puts "this is captured" } + # + # @param [Symbol] stream `:stdout` or `:stderr` + # @yield The block to capture stdout/stderr for. + # @return [String] The contents of $stdout or $stderr + def capture(stream) + begin + stream = stream.to_s + eval "$#{stream} = StringIO.new" + yield + result = eval("$#{stream}").string + ensure + eval("$#{stream} = #{stream.upcase}") + end + + result + end + + # Silences the output stream + # + # @example Silence `$stdout` + # + # silence(:stdout) { $stdout.puts "hi" } + # + # @param [IO] stream The stream to use such as $stderr or $stdout + # @return [nil] + alias :silence :capture + + +end + +EXAMPLES_DIR = Pathname.new(__FILE__).dirname + 'examples' +