file_sandbox 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.txt +16 -0
- data/Manifest.txt +5 -2
- data/README.txt +54 -0
- data/Rakefile +25 -5
- data/examples/rspec_example.rb +15 -0
- data/examples/test_unit_example.rb +17 -0
- data/lib/file_sandbox.rb +41 -19
- data/lib/file_sandbox_behavior.rb +15 -0
- data/test/file_sandbox_test.rb +34 -7
- metadata +18 -12
- data/CHANGES +0 -6
- data/README +0 -40
data/CHANGES.txt
ADDED
data/Manifest.txt
CHANGED
data/README.txt
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= Project: File Sandbox v0.3
|
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. By cleaning up after them.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
in rspec :
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'spec'
|
13
|
+
require 'file_sandbox_behavior'
|
14
|
+
|
15
|
+
describe File do
|
16
|
+
include FileSandbox
|
17
|
+
|
18
|
+
it 'should read the contents of a file' do
|
19
|
+
sandbox.new :file => 'b/a.txt', :with_contents => 'some stuff'
|
20
|
+
|
21
|
+
sandbox['/b/a.txt'].contents.should == 'some stuff'
|
22
|
+
File.read(sandbox.root + "/b/a.txt").should == 'some stuff'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
in test unit :
|
27
|
+
|
28
|
+
require 'rubygems'
|
29
|
+
require 'test/unit'
|
30
|
+
require 'file_sandbox'
|
31
|
+
|
32
|
+
class MyFileTest < Test::Unit::TestCase
|
33
|
+
include FileSandbox
|
34
|
+
|
35
|
+
def test_read
|
36
|
+
in_sandbox do |sandbox|
|
37
|
+
sandbox.new :file => 'b/a.txt', :with_contents => 'some stuff'
|
38
|
+
|
39
|
+
assert_equal 'some stuff', File.read(sandbox.root + '/b/a.txt')
|
40
|
+
assert_equal 'some stuff', sandbox['/b/a.txt'].contents
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
== Contact
|
47
|
+
|
48
|
+
Author:: Jeremy Stell-Smith
|
49
|
+
Email:: jeremystellsmith@gmail.com
|
50
|
+
License:: LGPL License
|
51
|
+
|
52
|
+
== Home Page
|
53
|
+
|
54
|
+
http://onemanswalk.com
|
data/Rakefile
CHANGED
@@ -1,14 +1,34 @@
|
|
1
|
-
# -*- ruby -*-
|
2
|
-
|
3
1
|
require 'rubygems'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'spec/rake/spectask'
|
4
5
|
require 'hoe'
|
5
6
|
require './lib/file_sandbox.rb'
|
6
7
|
|
8
|
+
desc "Default Task"
|
9
|
+
task :default => [:test, :generate_readme]
|
10
|
+
|
11
|
+
task :generate_readme do
|
12
|
+
`erb README.in > README.txt`
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Run all tests"
|
16
|
+
task :test => [:unit, :spec]
|
17
|
+
|
18
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
19
|
+
t.spec_files = FileList['examples/*_example.rb']
|
20
|
+
end
|
21
|
+
|
22
|
+
Rake::TestTask.new(:unit) do |t|
|
23
|
+
t.test_files = FileList['test/**/*test.rb']
|
24
|
+
t.verbose = false
|
25
|
+
end
|
26
|
+
|
7
27
|
Hoe.new('file_sandbox', FileSandbox::VERSION) do |p|
|
8
28
|
p.rubyforge_name = 'filesandbox'
|
9
|
-
p.summary = p.description = p.paragraphs_of('README', 2).first
|
10
|
-
p.url = p.paragraphs_of('README', -1).first.strip
|
29
|
+
p.summary = p.description = p.paragraphs_of('README.txt', 2).first
|
30
|
+
p.url = p.paragraphs_of('README.txt', -1).first.strip
|
11
31
|
p.author = 'Jeremy Stell-Smith'
|
12
32
|
p.email = 'jeremystellsmith@gmail.com'
|
13
|
-
p.changes = p.paragraphs_of('CHANGES', 0..1).join("\n\n")
|
33
|
+
p.changes = p.paragraphs_of('CHANGES.txt', 0..1).join("\n\n")
|
14
34
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'spec'
|
4
|
+
require 'file_sandbox_behavior'
|
5
|
+
|
6
|
+
describe File do
|
7
|
+
include FileSandbox
|
8
|
+
|
9
|
+
it 'should read the contents of a file' do
|
10
|
+
sandbox.new :file => 'b/a.txt', :with_contents => 'some stuff'
|
11
|
+
|
12
|
+
sandbox['/b/a.txt'].contents.should == 'some stuff'
|
13
|
+
File.read(sandbox.root + "/b/a.txt").should == 'some stuff'
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
|
2
|
+
require 'rubygems'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'file_sandbox'
|
5
|
+
|
6
|
+
class MyFileTest < Test::Unit::TestCase
|
7
|
+
include FileSandbox
|
8
|
+
|
9
|
+
def test_read
|
10
|
+
in_sandbox do |sandbox|
|
11
|
+
sandbox.new :file => 'b/a.txt', :with_contents => 'some stuff'
|
12
|
+
|
13
|
+
assert_equal 'some stuff', File.read(sandbox.root + '/b/a.txt')
|
14
|
+
assert_equal 'some stuff', sandbox['/b/a.txt'].contents
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/file_sandbox.rb
CHANGED
@@ -3,7 +3,10 @@ require 'fileutils'
|
|
3
3
|
require 'test/unit/assertions'
|
4
4
|
|
5
5
|
module FileSandbox
|
6
|
-
VERSION = "0.
|
6
|
+
VERSION = "0.3"
|
7
|
+
|
8
|
+
attr_reader :sandbox
|
9
|
+
|
7
10
|
def in_sandbox(&block)
|
8
11
|
raise "I expected to create a sandbox as you passed in a block to me" if !block_given?
|
9
12
|
|
@@ -42,10 +45,6 @@ module FileSandbox
|
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
|
-
def file(name)
|
46
|
-
SandboxFile.new(File.join(@sandbox.root, name))
|
47
|
-
end
|
48
|
-
|
49
48
|
class Sandbox
|
50
49
|
include Test::Unit::Assertions
|
51
50
|
attr_reader :root
|
@@ -55,20 +54,28 @@ module FileSandbox
|
|
55
54
|
clean_up
|
56
55
|
FileUtils.mkdir_p @root
|
57
56
|
end
|
57
|
+
|
58
|
+
def [](name)
|
59
|
+
SandboxFile.new(File.join(@root, name))
|
60
|
+
end
|
58
61
|
|
59
62
|
# usage new :file=>'my file.rb', :with_contents=>'some stuff'
|
60
63
|
def new(options)
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
if (binary_content = options.delete(:with_binary_content) || options.delete(:with_binary_contents))
|
66
|
-
File.open(name, "wb") {|f| f << binary_content }
|
64
|
+
if options.has_key? :directory
|
65
|
+
dir = self[options.delete(:directory)]
|
66
|
+
FileUtils.mkdir_p dir.path
|
67
67
|
else
|
68
|
-
|
68
|
+
file = self[options.delete(:file)]
|
69
|
+
if (binary_content = options.delete(:with_binary_content) || options.delete(:with_binary_contents))
|
70
|
+
file.binary_content = binary_content
|
71
|
+
else
|
72
|
+
file.content = (options.delete(:with_content) || options.delete(:with_contents) || '')
|
73
|
+
end
|
69
74
|
end
|
70
75
|
|
71
76
|
raise "unexpected keys '#{options.keys.join(', ')}'" unless options.empty?
|
77
|
+
|
78
|
+
dir || file
|
72
79
|
end
|
73
80
|
|
74
81
|
def remove(options)
|
@@ -95,22 +102,37 @@ module FileSandbox
|
|
95
102
|
end
|
96
103
|
|
97
104
|
class SandboxFile
|
98
|
-
attr_reader :
|
105
|
+
attr_reader :path
|
99
106
|
|
100
|
-
def initialize(
|
101
|
-
@
|
107
|
+
def initialize(path)
|
108
|
+
@path = path
|
102
109
|
end
|
103
110
|
|
104
111
|
def exist?
|
105
|
-
File.exist?
|
112
|
+
File.exist? path
|
106
113
|
end
|
107
114
|
|
108
|
-
alias exists? exist?
|
109
|
-
|
110
115
|
def content
|
111
|
-
File.read
|
116
|
+
File.read path
|
117
|
+
end
|
118
|
+
|
119
|
+
def content=(content)
|
120
|
+
FileUtils.mkdir_p File.dirname(@path)
|
121
|
+
File.open(@path, "w") {|f| f << content}
|
122
|
+
end
|
123
|
+
|
124
|
+
def binary_content=(content)
|
125
|
+
FileUtils.mkdir_p File.dirname(@path)
|
126
|
+
File.open(@path, "wb") {|f| f << content}
|
127
|
+
end
|
128
|
+
|
129
|
+
def create
|
130
|
+
self.content = ''
|
112
131
|
end
|
113
132
|
|
133
|
+
alias exists? exist?
|
114
134
|
alias contents content
|
135
|
+
alias contents= content=
|
136
|
+
alias binary_contents= binary_content=
|
115
137
|
end
|
116
138
|
end
|
data/test/file_sandbox_test.rb
CHANGED
@@ -17,24 +17,24 @@ class FileSandboxTest < Test::Unit::TestCase
|
|
17
17
|
|
18
18
|
def test_file_exist
|
19
19
|
in_sandbox do |sandbox|
|
20
|
-
assert !
|
20
|
+
assert !sandbox['a.txt'].exists?
|
21
21
|
File.open(sandbox.root + "/a.txt", "w") {|f| f << "something"}
|
22
|
-
assert
|
22
|
+
assert sandbox['a.txt'].exist?
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_create_file
|
27
27
|
in_sandbox do |sandbox|
|
28
|
-
assert !
|
28
|
+
assert !sandbox['a.txt'].exists?
|
29
29
|
|
30
30
|
sandbox.new :file => 'a.txt'
|
31
|
-
assert
|
31
|
+
assert sandbox['a.txt'].exist?
|
32
32
|
|
33
33
|
sandbox.new :file => 'b', :with_contents => 'some'
|
34
|
-
assert_equal 'some',
|
34
|
+
assert_equal 'some', sandbox['b'].contents
|
35
35
|
|
36
36
|
sandbox.new :file => 'c', :with_contents => 'thing'
|
37
|
-
assert_equal 'thing',
|
37
|
+
assert_equal 'thing', sandbox['c'].contents
|
38
38
|
|
39
39
|
assert_raises("unexpected keys 'contents'") {
|
40
40
|
sandbox.new :file => 'd', :contents => 'crap'
|
@@ -42,14 +42,41 @@ class FileSandboxTest < Test::Unit::TestCase
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
+
def test_create_file_with_file_dot_contents_equal_syntax
|
46
|
+
in_sandbox do |sandbox|
|
47
|
+
sandbox['a/b/c'].contents = 'crap'
|
48
|
+
assert_equal 'crap', sandbox['a/b/c'].contents
|
49
|
+
|
50
|
+
sandbox['foo.bin'].binary_contents = '123\n456'
|
51
|
+
assert_equal '123\n456', sandbox['foo.bin'].contents
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
45
55
|
def test_remove_file
|
46
56
|
in_sandbox do |sandbox|
|
47
57
|
sandbox.new :file => 'foo'
|
48
58
|
sandbox.remove :file => 'foo'
|
49
59
|
|
50
|
-
assert !
|
60
|
+
assert !sandbox['foo'].exists?
|
51
61
|
end
|
52
62
|
end
|
63
|
+
|
64
|
+
def test_create_directory
|
65
|
+
in_sandbox do |sandbox|
|
66
|
+
sandbox.new :directory => 'foo/bar'
|
67
|
+
|
68
|
+
assert File.directory?(sandbox.root + "/foo")
|
69
|
+
assert File.directory?(sandbox.root + "/foo/bar")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_new_file_returns_file
|
74
|
+
in_sandbox do |sandbox|
|
75
|
+
assert_equal SandboxFile, sandbox.new(:file => 'foo').class
|
76
|
+
assert_equal SandboxFile, sandbox.new(:file => 'foo2', :with_content => 'crap').class
|
77
|
+
assert_equal SandboxFile, sandbox.new(:directory => 'foo3').class
|
78
|
+
end
|
79
|
+
end
|
53
80
|
|
54
81
|
private
|
55
82
|
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: file_sandbox
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "0.
|
7
|
-
date: 2007-
|
8
|
-
summary: 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
|
+
version: "0.3"
|
7
|
+
date: 2007-07-06 00:00:00 -07:00
|
8
|
+
summary: File sandbox creates a space for your tests to safely hit the file system. It also makes it easier for them to do so. By cleaning up after them.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: jeremystellsmith@gmail.com
|
12
12
|
homepage: http://onemanswalk.com
|
13
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.
|
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. By cleaning up after them.
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|
@@ -29,18 +29,24 @@ post_install_message:
|
|
29
29
|
authors:
|
30
30
|
- Jeremy Stell-Smith
|
31
31
|
files:
|
32
|
-
- CHANGES
|
32
|
+
- CHANGES.txt
|
33
33
|
- Manifest.txt
|
34
|
-
- README
|
34
|
+
- README.txt
|
35
35
|
- Rakefile
|
36
|
+
- examples/rspec_example.rb
|
37
|
+
- examples/test_unit_example.rb
|
36
38
|
- lib/file_sandbox.rb
|
39
|
+
- lib/file_sandbox_behavior.rb
|
37
40
|
- test/file_sandbox_test.rb
|
38
41
|
test_files: []
|
39
42
|
|
40
|
-
rdoc_options:
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
rdoc_options:
|
44
|
+
- --main
|
45
|
+
- README.txt
|
46
|
+
extra_rdoc_files:
|
47
|
+
- CHANGES.txt
|
48
|
+
- Manifest.txt
|
49
|
+
- README.txt
|
44
50
|
executables: []
|
45
51
|
|
46
52
|
extensions: []
|
@@ -55,5 +61,5 @@ dependencies:
|
|
55
61
|
requirements:
|
56
62
|
- - ">="
|
57
63
|
- !ruby/object:Gem::Version
|
58
|
-
version: 1.2.
|
64
|
+
version: 1.2.1
|
59
65
|
version:
|
data/CHANGES
DELETED
data/README
DELETED
@@ -1,40 +0,0 @@
|
|
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
|
-
License:: LGPL License
|
37
|
-
|
38
|
-
== Home Page
|
39
|
-
|
40
|
-
http://onemanswalk.com
|