file_sandbox 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +6 -0
- data/README +37 -0
- data/Rakefile +112 -0
- data/lib/file_sandbox.rb +106 -0
- data/test/file_sandbox_test.rb +36 -0
- metadata +56 -0
data/CHANGES
ADDED
data/README
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= Project: File Sandbox
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
File sandbox creates a space for your tests to safely hit the file system. It also makes it easier for them to do so.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
require 'test/unit'
|
10
|
+
require_gem 'file_sandbox'
|
11
|
+
|
12
|
+
class MyFileTest < Test::Unit::TestCase
|
13
|
+
include FileSandbox
|
14
|
+
|
15
|
+
def test_read
|
16
|
+
in_sandbox do |sandbox|
|
17
|
+
sandbox.new :file => 'b/a.txt', :with_contents => 'some stuff'
|
18
|
+
|
19
|
+
assert_equal 'some_stuff', File.read(sandbox.root + '/b/a.txt')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
== Features
|
26
|
+
|
27
|
+
* Create a file
|
28
|
+
|
29
|
+
sandbox.new :file => 'b/a.txt', :with_contents => 'some stuff'
|
30
|
+
|
31
|
+
|
32
|
+
== Contact
|
33
|
+
|
34
|
+
Author:: Jeremy Stell-Smith
|
35
|
+
Email:: jeremystellsmith@gmail.com
|
36
|
+
Home Page:: http://onemanswalk.com
|
37
|
+
License:: LGPL License
|
data/Rakefile
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'rake/clean'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'rubygems'
|
7
|
+
require 'rake/gempackagetask'
|
8
|
+
require 'rake/gempackagetask'
|
9
|
+
require 'rake/contrib/rubyforgepublisher'
|
10
|
+
rescue
|
11
|
+
puts "caught error : #{$!}"
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
PKG_NAME = 'file_sandbox'
|
16
|
+
PKG_VERSION = '0.1'
|
17
|
+
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
18
|
+
|
19
|
+
RELEASE_NAME = "REL #{PKG_VERSION}"
|
20
|
+
|
21
|
+
RUBY_FORGE_PROJECT = "filesandbox"
|
22
|
+
RUBY_FORGE_USER = "stellsmi"
|
23
|
+
# RUBY_FORGE_GROUP = 2944
|
24
|
+
# RUBY_FORGE_PACKAGE = 3495
|
25
|
+
|
26
|
+
SRC_RB = FileList['lib/**/*.rb']
|
27
|
+
|
28
|
+
desc "Default Task"
|
29
|
+
task :default => :test
|
30
|
+
|
31
|
+
#################################################
|
32
|
+
# Test
|
33
|
+
|
34
|
+
desc "Run all tests"
|
35
|
+
task :test => [:test_units]
|
36
|
+
|
37
|
+
Rake::TestTask.new("test_units") do |t|
|
38
|
+
t.test_files = FileList['test/**/*test.rb']
|
39
|
+
t.verbose = false
|
40
|
+
end
|
41
|
+
|
42
|
+
#################################################
|
43
|
+
# RDoc
|
44
|
+
|
45
|
+
rd = Rake::RDocTask.new("rdoc") { |rdoc|
|
46
|
+
rdoc.rdoc_dir = 'html'
|
47
|
+
rdoc.title = "File Sandbox"
|
48
|
+
rdoc.options << '--line-numbers' << '--inline-source' << '--main' << 'README'
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb', '[A-Z]*', 'doc/**/*.rdoc')
|
50
|
+
# rdoc.template = 'doc/jamis.rb'
|
51
|
+
}
|
52
|
+
|
53
|
+
#################################################
|
54
|
+
# Package
|
55
|
+
|
56
|
+
PKG_FILES = FileList[
|
57
|
+
'CHANGES', 'Rakefile', 'README', 'lib/**/*.rb', 'test/**/*.rb',
|
58
|
+
]
|
59
|
+
|
60
|
+
spec = Gem::Specification.new do |s|
|
61
|
+
s.name = PKG_NAME
|
62
|
+
s.summary = "A File Sandbox for testing against the file system."
|
63
|
+
s.description = <<-EOS
|
64
|
+
File sandbox creates a space for your tests to safely hit the file system.
|
65
|
+
It also makes it easier for them to do so..
|
66
|
+
EOS
|
67
|
+
s.version = PKG_VERSION
|
68
|
+
|
69
|
+
s.files = PKG_FILES.to_a
|
70
|
+
s.require_path = 'lib'
|
71
|
+
s.autorequire = 'file_sandbox'
|
72
|
+
s.test_files = PKG_FILES.select { |fn| fn =~ /^test\/.*_test.rb$/ }
|
73
|
+
|
74
|
+
s.has_rdoc = true
|
75
|
+
s.extra_rdoc_files = rd.rdoc_files.reject { |fn| fn =~ /\.rb$/ }.to_a
|
76
|
+
s.rdoc_options <<
|
77
|
+
'--title' << 'Builder -- Easy XML Building' <<
|
78
|
+
'--main' << 'README' <<
|
79
|
+
'--line-numbers'
|
80
|
+
|
81
|
+
s.author = "Jeremy Stell-Smith"
|
82
|
+
s.email = "jeremystellsmith@gmail.com"
|
83
|
+
s.rubyforge_project = RUBY_FORGE_PROJECT
|
84
|
+
s.homepage = "http://rubyforge.org/projects/#{RUBY_FORGE_PROJECT}"
|
85
|
+
end
|
86
|
+
|
87
|
+
Rake::GemPackageTask.new(spec) do |p|
|
88
|
+
p.gem_spec = spec
|
89
|
+
p.need_tar = true
|
90
|
+
p.need_zip = true
|
91
|
+
end
|
92
|
+
|
93
|
+
################################################
|
94
|
+
# release
|
95
|
+
|
96
|
+
task :release => [:test, :package, :upload_release]
|
97
|
+
|
98
|
+
task :upload_release do
|
99
|
+
require 'rubyforge'
|
100
|
+
|
101
|
+
# puts "Enter rubyforge password:"
|
102
|
+
# rubyforge = RubyForge.new(File.dirname(__FILE__) + "/config/rubyforge.yml",
|
103
|
+
# :cookie_jar => RubyForge::COOKIE_F, :password => $stdin.gets.strip)
|
104
|
+
rubyforge = RubyForge.new
|
105
|
+
p rubyforge.class.instance_methods(false).sort
|
106
|
+
rubyforge.login
|
107
|
+
rubyforge.autoconfig
|
108
|
+
|
109
|
+
files = %w(gem tgz zip).collect {|ext| "pkg/#{PKG_FILE_NAME}.#{ext}"}
|
110
|
+
puts "Releasing #{files.collect{|f| File.basename(f)}.join(", ")}..."
|
111
|
+
rubyforge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, *files)
|
112
|
+
end
|
data/lib/file_sandbox.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'ftools'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'test/unit/assertions'
|
4
|
+
|
5
|
+
module FileSandbox
|
6
|
+
def in_sandbox(&block)
|
7
|
+
raise "I expected to create a sandbox as you passed in a block to me" if !block_given?
|
8
|
+
|
9
|
+
setup_sandbox
|
10
|
+
original_error = nil
|
11
|
+
|
12
|
+
begin
|
13
|
+
Dir.chdir(@sandbox.root) do
|
14
|
+
yield @sandbox
|
15
|
+
end
|
16
|
+
rescue => e
|
17
|
+
original_error = e
|
18
|
+
raise
|
19
|
+
ensure
|
20
|
+
begin
|
21
|
+
teardown_sandbox
|
22
|
+
rescue
|
23
|
+
if original_error
|
24
|
+
STDERR.puts "ALERT: a test raised an error and failed to release some lock(s) in the sandbox directory"
|
25
|
+
raise(original_error)
|
26
|
+
else
|
27
|
+
raise
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def setup_sandbox
|
34
|
+
@sandbox = Sandbox.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def teardown_sandbox
|
38
|
+
@sandbox.clean_up
|
39
|
+
@sandbox = nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def file(name)
|
43
|
+
SandboxFile.new(File.join(@sandbox.root, name))
|
44
|
+
end
|
45
|
+
|
46
|
+
class Sandbox
|
47
|
+
include Test::Unit::Assertions
|
48
|
+
attr_reader :root
|
49
|
+
|
50
|
+
def initialize
|
51
|
+
@root = File.expand_path("__sandbox")
|
52
|
+
clean_up
|
53
|
+
FileUtils.mkdir_p @root
|
54
|
+
end
|
55
|
+
|
56
|
+
# usage new :file=>'my file.rb', :with_contents=>'some stuff'
|
57
|
+
def new(options)
|
58
|
+
name = File.join(@root, options[:file])
|
59
|
+
dir = File.dirname(name)
|
60
|
+
FileUtils.mkdir_p dir
|
61
|
+
|
62
|
+
if (binary_content = options[:with_binary_content] || options[:with_binary_contents])
|
63
|
+
File.open(name, "wb") {|f| f << binary_content }
|
64
|
+
else
|
65
|
+
File.open(name, "w") {|f| f << (options[:with_content] || options[:with_contents] || '')}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# usage assert :file=>'my file.rb', :has_contents=>'some stuff'
|
70
|
+
def assert(options)
|
71
|
+
name = File.join(@root, options[:file])
|
72
|
+
if (expected_content = options[:has_content] || options[:has_contents])
|
73
|
+
assert_equal(expected_content, File.read(name))
|
74
|
+
else
|
75
|
+
fail('expected something to assert')
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def clean_up
|
80
|
+
FileUtils.rm_rf @root
|
81
|
+
if File.exists? @root
|
82
|
+
raise "Could not remove directory #{@root.inspect}, something is probably still holding a lock on it"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class SandboxFile
|
88
|
+
attr_reader :name
|
89
|
+
|
90
|
+
def initialize(name)
|
91
|
+
@name = name
|
92
|
+
end
|
93
|
+
|
94
|
+
def exist?
|
95
|
+
File.exist? name
|
96
|
+
end
|
97
|
+
|
98
|
+
alias exists? exist?
|
99
|
+
|
100
|
+
def content
|
101
|
+
File.read name
|
102
|
+
end
|
103
|
+
|
104
|
+
alias contents content
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/file_sandbox')
|
3
|
+
|
4
|
+
class FileSandboxTest < Test::Unit::TestCase
|
5
|
+
include FileSandbox
|
6
|
+
|
7
|
+
def test_sandbox_cleans_up_file
|
8
|
+
in_sandbox do |sandbox|
|
9
|
+
name = sandbox.root + "/a.txt"
|
10
|
+
|
11
|
+
File.open(name, "w") {|f| f << "something"}
|
12
|
+
|
13
|
+
assert File.exist?(name)
|
14
|
+
end
|
15
|
+
assert !File.exist?(name)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_file_exist
|
19
|
+
in_sandbox do |sandbox|
|
20
|
+
assert !file('a.txt').exists?
|
21
|
+
File.open(sandbox.root + "/a.txt", "w") {|f| f << "something"}
|
22
|
+
assert file('a.txt').exist?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_create_file
|
27
|
+
in_sandbox do |sandbox|
|
28
|
+
assert !file('a.txt').exists?
|
29
|
+
|
30
|
+
sandbox.new :file => 'a.txt'
|
31
|
+
|
32
|
+
assert file('a.txt').exist?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.1
|
3
|
+
specification_version: 1
|
4
|
+
name: file_sandbox
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.1"
|
7
|
+
date: 2007-02-08 00:00:00 -08:00
|
8
|
+
summary: A File Sandbox for testing against the file system.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: jeremystellsmith@gmail.com
|
12
|
+
homepage: http://rubyforge.org/projects/filesandbox
|
13
|
+
rubyforge_project: filesandbox
|
14
|
+
description: File sandbox creates a space for your tests to safely hit the file system. It also makes it easier for them to do so..
|
15
|
+
autorequire: file_sandbox
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Jeremy Stell-Smith
|
31
|
+
files:
|
32
|
+
- CHANGES
|
33
|
+
- Rakefile
|
34
|
+
- README
|
35
|
+
- lib/file_sandbox.rb
|
36
|
+
- test/file_sandbox_test.rb
|
37
|
+
test_files:
|
38
|
+
- test/file_sandbox_test.rb
|
39
|
+
rdoc_options:
|
40
|
+
- --title
|
41
|
+
- Builder -- Easy XML Building
|
42
|
+
- --main
|
43
|
+
- README
|
44
|
+
- --line-numbers
|
45
|
+
extra_rdoc_files:
|
46
|
+
- CHANGES
|
47
|
+
- Rakefile
|
48
|
+
- README
|
49
|
+
executables: []
|
50
|
+
|
51
|
+
extensions: []
|
52
|
+
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
dependencies: []
|
56
|
+
|