bdimcheff-sandbox 0.0.1
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/README.rdoc +21 -0
- data/VERSION.yml +4 -0
- data/lib/sandbox.rb +28 -0
- data/spec/sandbox_spec.rb +42 -0
- data/spec/spec_helper.rb +10 -0
- metadata +58 -0
data/README.rdoc
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
= Copyright
|
|
20
|
+
|
|
21
|
+
Copyright (c) 2008 Brandon Dimcheff. See LICENSE for details.
|
data/VERSION.yml
ADDED
data/lib/sandbox.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'tmpdir'
|
|
2
|
+
require 'sha1'
|
|
3
|
+
|
|
4
|
+
class Sandbox
|
|
5
|
+
class << self
|
|
6
|
+
def play(&block)
|
|
7
|
+
path = generate_path
|
|
8
|
+
|
|
9
|
+
FileUtils.mkdir_p(path)
|
|
10
|
+
|
|
11
|
+
begin
|
|
12
|
+
yield path
|
|
13
|
+
ensure
|
|
14
|
+
FileUtils.rm_r(path)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def generate_path
|
|
19
|
+
File.join(Dir.tmpdir, sandbox_dir)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def sandbox_dir
|
|
23
|
+
sha = Digest::SHA1.hexdigest("--#{rand(20000)}---#{Time.now}--")
|
|
24
|
+
|
|
25
|
+
"sandbox-#{sha}"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "Sandbox" do
|
|
4
|
+
it "returns a path in the tmp directory" do
|
|
5
|
+
flexmock(Sandbox, :sandbox_dir => 'foo')
|
|
6
|
+
flexmock(Dir, :tmpdir => '/bar')
|
|
7
|
+
|
|
8
|
+
Sandbox.generate_path.should == '/bar/foo'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "returns different paths for subsequent calls to sandbox_dir" do
|
|
12
|
+
dir1 = Sandbox.sandbox_dir
|
|
13
|
+
dir2 = Sandbox.sandbox_dir
|
|
14
|
+
|
|
15
|
+
dir1.should_not == dir2
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
context "no errors" do
|
|
19
|
+
it "cleans up after itself" do
|
|
20
|
+
path = Sandbox.play do |p|
|
|
21
|
+
File.exist?(p).should be_true
|
|
22
|
+
p
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
File.exist?(path).should be_false
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
context "exception raised" do
|
|
30
|
+
it "cleans up after itself" do
|
|
31
|
+
lambda {
|
|
32
|
+
Sandbox.play do |p|
|
|
33
|
+
@path = p
|
|
34
|
+
File.exist?(p).should be_true
|
|
35
|
+
raise "boom"
|
|
36
|
+
end
|
|
37
|
+
}.should raise_error
|
|
38
|
+
|
|
39
|
+
File.exist?(@path).should be_false
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bdimcheff-sandbox
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brandon Dimcheff
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-02-13 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: TODO
|
|
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
|
+
post_install_message:
|
|
33
|
+
rdoc_options:
|
|
34
|
+
- --inline-source
|
|
35
|
+
- --charset=UTF-8
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: "0"
|
|
43
|
+
version:
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: "0"
|
|
49
|
+
version:
|
|
50
|
+
requirements: []
|
|
51
|
+
|
|
52
|
+
rubyforge_project:
|
|
53
|
+
rubygems_version: 1.2.0
|
|
54
|
+
signing_key:
|
|
55
|
+
specification_version: 2
|
|
56
|
+
summary: TODO
|
|
57
|
+
test_files: []
|
|
58
|
+
|