sandbox 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +39 -0
- data/VERSION.yml +4 -0
- data/lib/sandbox.rb +54 -0
- data/spec/sandbox_spec.rb +49 -0
- data/spec/spec_helper.rb +10 -0
- metadata +60 -0
data/README.rdoc
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
= sandbox
|
2
|
+
Sandbox is a very small library that creates a folder in tmp for you to be able to perform tests. Your temporary path will be available while the play block is executing. When you're done, it will clean it up for you.
|
3
|
+
|
4
|
+
Sandbox.play do |path|
|
5
|
+
FileUtils.touch(File.join(path, 'foo'))
|
6
|
+
end
|
7
|
+
|
8
|
+
# path has been deleted
|
9
|
+
|
10
|
+
It will clean up even if you have an exception:
|
11
|
+
|
12
|
+
Sandbox.play do |path|
|
13
|
+
# do stuff in path
|
14
|
+
raise "boom"
|
15
|
+
end
|
16
|
+
|
17
|
+
# path has been deleted
|
18
|
+
|
19
|
+
You can also use the Sandbox object directly if necessary:
|
20
|
+
|
21
|
+
sbox = Sandbox.new
|
22
|
+
# do stuff
|
23
|
+
sbox.close # deletes sandbox
|
24
|
+
|
25
|
+
This could be useful for RSpec:
|
26
|
+
|
27
|
+
before(:each) do
|
28
|
+
@sbox = Sandbox.new
|
29
|
+
end
|
30
|
+
|
31
|
+
after(:each) do
|
32
|
+
@sbox.close
|
33
|
+
end
|
34
|
+
|
35
|
+
# specs using @sbox (with a new sandbox for each spec)
|
36
|
+
|
37
|
+
= Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2009 Brandon Dimcheff. See LICENSE for details.
|
data/VERSION.yml
ADDED
data/lib/sandbox.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'digest/sha1'
|
3
|
+
|
4
|
+
# Represents a temporary sandbox for testing that relies on the
|
5
|
+
# filesystem.
|
6
|
+
class Sandbox
|
7
|
+
attr_accessor :path
|
8
|
+
|
9
|
+
# Executes the block and yields the path to the sandbox directory.
|
10
|
+
# Cleans up the sandbox after the block is complete.
|
11
|
+
# == Options
|
12
|
+
# [+:path+] the path to use. default: generate one in Dir.tmpdir
|
13
|
+
# [+:cd+] change directory with Dir.chdir to the temp directory
|
14
|
+
def self.play(options = {}, &block)
|
15
|
+
sandbox = Sandbox.new(options[:path])
|
16
|
+
|
17
|
+
begin
|
18
|
+
if options[:cd]
|
19
|
+
Dir.chdir(sandbox.path) do
|
20
|
+
yield sandbox.path
|
21
|
+
end
|
22
|
+
else
|
23
|
+
yield sandbox.path
|
24
|
+
end
|
25
|
+
ensure
|
26
|
+
sandbox.close
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Creates a new Sandbox with an optional path.
|
31
|
+
# == Parameters
|
32
|
+
# [+path+] The path to use. default: generate one in Dir.tmpdir
|
33
|
+
def initialize(path = nil)
|
34
|
+
self.path = path || generate_path
|
35
|
+
|
36
|
+
FileUtils.mkdir_p(self.path)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Cleans up the sandbox by removing the path
|
40
|
+
def close
|
41
|
+
FileUtils.rm_r(path)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def generate_path
|
46
|
+
File.join(Dir.tmpdir, sandbox_dir)
|
47
|
+
end
|
48
|
+
|
49
|
+
def sandbox_dir
|
50
|
+
sha = Digest::SHA1.hexdigest("--#{rand(20000)}---#{Time.now}--")
|
51
|
+
|
52
|
+
"sandbox-#{sha}"
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "Sandbox" do
|
4
|
+
context "when there are no errors in the block" do
|
5
|
+
it "cleans up after itself" do
|
6
|
+
path = Sandbox.play do |p|
|
7
|
+
File.exist?(p).should be_true
|
8
|
+
File.ftype(p).should == 'directory'
|
9
|
+
p
|
10
|
+
end
|
11
|
+
|
12
|
+
File.exist?(path).should be_false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "changes directories when :cd => true is specified" do
|
16
|
+
path = Sandbox.play(:cd => true) do |p|
|
17
|
+
File.exist?(p).should be_true
|
18
|
+
File.ftype(p).should == 'directory'
|
19
|
+
File::Stat.new(Dir.pwd).ino.should == File::Stat.new(p).ino
|
20
|
+
p
|
21
|
+
end
|
22
|
+
|
23
|
+
File.exist?(path).should be_false
|
24
|
+
end
|
25
|
+
|
26
|
+
it "uses the specified path when :path is specified" do
|
27
|
+
tmp = Dir.tmpdir
|
28
|
+
path = File.join(tmp, 'sandbox-test')
|
29
|
+
|
30
|
+
Sandbox.play(:path => path) do |p|
|
31
|
+
p.should == path
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when an exception is raised" do
|
37
|
+
it "cleans up after itself" do
|
38
|
+
lambda {
|
39
|
+
Sandbox.play do |p|
|
40
|
+
@path = p
|
41
|
+
File.exist?(p).should be_true
|
42
|
+
raise "boom"
|
43
|
+
end
|
44
|
+
}.should raise_error
|
45
|
+
|
46
|
+
File.exist?(@path).should be_false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sandbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Dimcheff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-23 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A really small filesystem sandbox
|
17
|
+
email: bdimchef-git@wieldim.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/sandbox.rb
|
28
|
+
- spec/sandbox_spec.rb
|
29
|
+
- spec/spec_helper.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/bdimcheff/sandbox
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.4
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: Automatically manages a filesystem sandbox area for your tests!
|
59
|
+
test_files: []
|
60
|
+
|