bdimcheff-sandbox 0.0.1 → 0.1.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 +20 -2
- data/VERSION.yml +1 -1
- data/lib/sandbox.rb +34 -18
- data/spec/sandbox_spec.rb +12 -14
- metadata +2 -2
data/README.rdoc
CHANGED
|
@@ -15,7 +15,25 @@ It will clean up even if you have an exception:
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# path has been deleted
|
|
18
|
-
|
|
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
|
+
|
|
19
37
|
= Copyright
|
|
20
38
|
|
|
21
|
-
Copyright (c)
|
|
39
|
+
Copyright (c) 2009 Brandon Dimcheff. See LICENSE for details.
|
data/VERSION.yml
CHANGED
data/lib/sandbox.rb
CHANGED
|
@@ -1,28 +1,44 @@
|
|
|
1
1
|
require 'tmpdir'
|
|
2
2
|
require 'sha1'
|
|
3
3
|
|
|
4
|
+
# Represents a temporary sandbox for testing that relies on the
|
|
5
|
+
# filesystem.
|
|
4
6
|
class Sandbox
|
|
5
|
-
|
|
6
|
-
def play(&block)
|
|
7
|
-
path = generate_path
|
|
8
|
-
|
|
9
|
-
FileUtils.mkdir_p(path)
|
|
7
|
+
attr_accessor :path
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
end
|
|
16
|
-
end
|
|
9
|
+
# Executes the block and yields the path to the sandbox directory.
|
|
10
|
+
# Cleans up the sandbox after the block is complete.
|
|
11
|
+
def self.play(path = nil, &block)
|
|
12
|
+
sandbox = Sandbox.new(path)
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
begin
|
|
15
|
+
yield sandbox.path
|
|
16
|
+
ensure
|
|
17
|
+
sandbox.close
|
|
20
18
|
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Creates a new Sandbox with an optional path. Generates a random
|
|
22
|
+
# path in Dir.tmpdir if path is unspecified.
|
|
23
|
+
def initialize(path = nil)
|
|
24
|
+
self.path = path || generate_path
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
FileUtils.mkdir_p(self.path)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Cleans up the sandbox by removing the path
|
|
30
|
+
def close
|
|
31
|
+
FileUtils.rm_r(path)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
def generate_path
|
|
36
|
+
File.join(Dir.tmpdir, sandbox_dir)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def sandbox_dir
|
|
40
|
+
sha = Digest::SHA1.hexdigest("--#{rand(20000)}---#{Time.now}--")
|
|
41
|
+
|
|
42
|
+
"sandbox-#{sha}"
|
|
27
43
|
end
|
|
28
44
|
end
|
data/spec/sandbox_spec.rb
CHANGED
|
@@ -1,24 +1,11 @@
|
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
|
3
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
4
|
context "no errors" do
|
|
19
5
|
it "cleans up after itself" do
|
|
20
6
|
path = Sandbox.play do |p|
|
|
21
7
|
File.exist?(p).should be_true
|
|
8
|
+
File.ftype(p).should == 'directory'
|
|
22
9
|
p
|
|
23
10
|
end
|
|
24
11
|
|
|
@@ -39,4 +26,15 @@ describe "Sandbox" do
|
|
|
39
26
|
File.exist?(@path).should be_false
|
|
40
27
|
end
|
|
41
28
|
end
|
|
29
|
+
|
|
30
|
+
context "optional path supplied" do
|
|
31
|
+
it "uses the specified path" do
|
|
32
|
+
tmp = Dir.tmpdir
|
|
33
|
+
path = File.join(tmp, 'sandbox-test')
|
|
34
|
+
|
|
35
|
+
Sandbox.play(path) do |p|
|
|
36
|
+
p.should == path
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
42
40
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bdimcheff-sandbox
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brandon Dimcheff
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-02-
|
|
12
|
+
date: 2009-02-23 00:00:00 -08:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|