Permalink
Browse files

add SandboxFileUtils; fix Mocha/FileUtils issue

  • Loading branch information...
1 parent 8dc7425 commit c7f731e53d08a55c7c77b972718ca17989fab8bf Brian D. Burns committed Mar 3, 2013
Showing with 299 additions and 44 deletions.
  1. +1 −1 Gemfile
  2. +10 −7 spec/cleaner_spec.rb
  3. +13 −11 spec/cli_spec.rb
  4. +2 −10 spec/logger/logfile_spec.rb
  5. +7 −8 spec/spec_helper.rb
  6. +12 −7 spec/splitter_spec.rb
  7. +254 −0 spec/support/sandbox_file_utils.rb
View
@@ -13,7 +13,7 @@ Backup::Dependency.all.each do |dep|
end
gem 'rspec'
-gem 'mocha', '0.12.7'
+gem 'mocha'
gem 'timecop'
# Omitted from Travis CI Environment
View
@@ -217,17 +217,19 @@
describe '#packaging_folder_dirty?' do
before do
+ @tmpdir = Dir.mktmpdir('backup_spec')
+ SandboxFileUtils.activate!(@tmpdir)
cleaner.instance_variable_set(:@model, model)
- FileUtils.unstub(:mkdir_p)
end
after do
+ FileUtils.rm_r(@tmpdir, :force => true, :secure => true)
Backup::Config.send(:reset!)
end
context 'when files exist in the packaging folder' do
it 'should return true' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
Backup::Config.update(:root_path => path)
FileUtils.mkdir_p(
File.join(Backup::Config.tmp_path, 'test_trigger', 'archives')
@@ -239,7 +241,7 @@
context 'when files do not exist in the packaging folder' do
it 'should return false' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
Backup::Config.update(:root_path => path)
FileUtils.mkdir_p(
File.join(Backup::Config.tmp_path, 'test_trigger')
@@ -252,18 +254,19 @@
describe '#tmp_path_package_files' do
before do
+ @tmpdir = Dir.mktmpdir('backup_spec')
+ SandboxFileUtils.activate!(@tmpdir)
cleaner.instance_variable_set(:@model, model)
- FileUtils.unstub(:mkdir_p)
- FileUtils.unstub(:touch)
end
after do
+ FileUtils.rm_r(@tmpdir, :force => true, :secure => true)
Backup::Config.send(:reset!)
end
context 'when packaging files exist in the tmp_path' do
it 'should return the files' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
Backup::Config.update(:root_path => path)
FileUtils.mkdir_p(Backup::Config.tmp_path)
@@ -290,7 +293,7 @@
context 'when no packaging files exist in the tmp_path' do
it 'should return an empty array' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
Backup::Config.update(:root_path => path)
FileUtils.mkdir_p(Backup::Config.tmp_path)
View
@@ -359,18 +359,19 @@
describe '#generate:model' do
before do
- FileUtils.unstub(:mkdir_p)
- FileUtils.unstub(:touch)
+ @tmpdir = Dir.mktmpdir('backup_spec')
+ SandboxFileUtils.activate!(@tmpdir)
end
after do
+ FileUtils.rm_r(@tmpdir, :force => true, :secure => true)
Backup::Config.send(:reset!)
end
context 'when given a config_path' do
context 'when no config file exists' do
it 'should create both a config and a model under the given path' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
model_file = File.join(path, 'custom', 'models', 'my_test_trigger.rb')
config_file = File.join(path, 'custom', 'config.rb')
@@ -392,7 +393,7 @@
context 'when a config file already exists' do
it 'should only create a model under the given path' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
model_file = File.join(path, 'custom', 'models', 'my_test_trigger.rb')
config_file = File.join(path, 'custom', 'config.rb')
FileUtils.mkdir_p(File.join(path, 'custom'))
@@ -416,7 +417,7 @@
#
# context 'when a model file already exists' do
# it 'should prompt to overwrite the model under the given path' do
-# Dir.mktmpdir do |path|
+# Dir.chdir(@tmpdir) do |path|
# model_file = File.join(path, 'models', 'test_trigger.rb')
# config_file = File.join(path, 'config.rb')
#
@@ -441,7 +442,7 @@
context 'when not given a config_path' do
it 'should create both a config and a model under the root path' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
Backup::Config.update(:root_path => path)
model_file = File.join(path, 'models', 'test_trigger.rb')
config_file = File.join(path, 'config.rb')
@@ -517,17 +518,18 @@
describe '#generate:config' do
before do
- FileUtils.unstub(:mkdir_p)
- FileUtils.unstub(:touch)
+ @tmpdir = Dir.mktmpdir('backup_spec')
+ SandboxFileUtils.activate!(@tmpdir)
end
after do
+ FileUtils.rm_r(@tmpdir, :force => true, :secure => true)
Backup::Config.send(:reset!)
end
context 'when given a config_path' do
it 'should create a config file in the given path' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
config_file = File.join(path, 'custom', 'config.rb')
out, err = capture_io do
@@ -545,7 +547,7 @@
context 'when not given a config_path' do
it 'should create a config file in the root path' do
- Dir.mktmpdir do |path|
+ Dir.chdir(@tmpdir) do |path|
Backup::Config.update(:root_path => path)
config_file = File.join(path, 'config.rb')
@@ -564,7 +566,7 @@
#
# context 'when a config file already exists' do
# it 'should prompt to overwrite the config file' do
-# Dir.mktmpdir do |path|
+# Dir.chdir(@tmpdir) do |path|
# Backup::Config.update(:root_path => path)
# config_file = File.join(path, 'config.rb')
#
@@ -6,6 +6,8 @@ module Backup
describe Logger::Logfile do
before do
@tmpdir = Dir.mktmpdir('backup_spec')
+ SandboxFileUtils.activate!(@tmpdir)
+
@log_path_absolute = File.join(@tmpdir, 'log_path')
@logfile_absolute = File.join(@log_path_absolute, 'backup.log')
@root_path = File.join(@tmpdir, 'root_dir')
@@ -25,7 +27,6 @@ module Backup
end
after do
- FileUtils.unstub(:rm_r)
FileUtils.rm_r(@tmpdir, :force => true, :secure => true)
end
@@ -67,7 +68,6 @@ module Backup
logfile.log_path = 'log'
end
- FileUtils.unstub(:mkdir_p)
Logger.start!
File.exist?(@log_path_default).should be_false
@@ -77,10 +77,6 @@ module Backup
end
describe '#initialize' do
- before do
- FileUtils.unstub(:mkdir_p)
- end
-
describe 'log_path creation' do
context 'when log_path is not set' do
before do
@@ -136,9 +132,6 @@ module Backup
context 'when log file is larger than max_bytes' do
before do
- FileUtils.unstub(:mv)
- FileUtils.unstub(:rm)
- FileUtils.unstub(:rm_f)
FileUtils.mkdir_p(@log_path_default)
end
@@ -187,7 +180,6 @@ module Backup
let(:timestamp) { Time.now.strftime("%Y/%m/%d %H:%M:%S") }
before do
- FileUtils.unstub(:mkdir_p)
Logger.start!
end
View
@@ -1,16 +1,17 @@
# encoding: utf-8
-##
-# Use Bundler
require 'rubygems' if RUBY_VERSION < '1.9'
require 'bundler/setup'
-
-##
-# Load Backup
require 'backup'
require 'timecop'
+# ::FileUtils will always be either SandboxFileUtils or FileUtils::NoWrite.
+require File.expand_path('../support/sandbox_file_utils.rb', __FILE__)
+# SandboxFileUtils.deactivate!(:noop) will be called before each example,
+# which will set ::FileUtils to FileUtils::NoWrite if SandboxFileUtils is active.
+SandboxFileUtils.activate!
+
module Backup::ExampleHelpers
# ripped from MiniTest :)
# RSpec doesn't have a method for this? Am I missing something?
@@ -47,9 +48,7 @@ def capture_io
##
# Actions to perform before each example
config.before(:each) do
- FileUtils.collect_method(:noop).each do |method|
- FileUtils.stubs(method).raises("Unexpected call to FileUtils.#{ method }")
- end
+ SandboxFileUtils.deactivate!(:noop)
Open4.stubs(:popen4).raises('Unexpected call to Open4::popen4()')
View
@@ -103,20 +103,25 @@
describe '#chunks' do
before do
+ @tmpdir = Dir.mktmpdir('backup_spec')
+ SandboxFileUtils.activate!(@tmpdir)
splitter.instance_variable_set(:@package, package)
package.expects(:basename).returns('base_filename')
- FileUtils.unstub(:touch)
+ end
+
+ after do
+ FileUtils.rm_r(@tmpdir, :force => true, :secure => true)
end
it 'should return a sorted array of chunked file paths' do
- Dir.mktmpdir do |dir|
- Backup::Config.expects(:tmp_path).returns(dir)
- FileUtils.touch(File.join(dir, 'base_filename-aa'))
- FileUtils.touch(File.join(dir, 'base_filename-ab'))
+ Dir.chdir(@tmpdir) do |path|
+ Backup::Config.expects(:tmp_path).returns(path)
+ FileUtils.touch(File.join(path, 'base_filename-aa'))
+ FileUtils.touch(File.join(path, 'base_filename-ab'))
splitter.send(:chunks).should == [
- File.join(dir, 'base_filename-aa'),
- File.join(dir, 'base_filename-ab')
+ File.join(path, 'base_filename-aa'),
+ File.join(path, 'base_filename-ab')
]
end
end
Oops, something went wrong.

0 comments on commit c7f731e

Please sign in to comment.