Module: FileSandbox

Defined in:
lib/file_sandbox.rb,
lib/file_sandbox_behavior.rb

Defined Under Namespace

Classes: Sandbox, SandboxFile

Constant Summary collapse

VERSION =
"0.6"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sandboxObject (readonly)

Returns the value of attribute sandbox.



7
8
9
# File 'lib/file_sandbox.rb', line 7

def sandbox
  @sandbox
end

Class Method Details

.included(spec) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/file_sandbox_behavior.rb', line 4

def self.included(spec)
  return unless spec.respond_to? :before

  spec.before do
    setup_sandbox
  end

  spec.after do
    teardown_sandbox
  end
end

Instance Method Details

#in_sandbox(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/file_sandbox.rb', line 9

def in_sandbox(&block)
  raise "I expected to create a sandbox as you passed in a block to me" if !block_given?

  setup_sandbox
  original_error = nil

  begin
    yield @sandbox
  rescue => e
    original_error = e
    raise
  ensure
    begin
      teardown_sandbox
    rescue
      if original_error
        STDERR.puts "ALERT: a test raised an error and failed to release some lock(s) in the sandbox directory"
        raise(original_error)
      else
        raise
      end
    end
  end
end

#setup_sandbox(path = '__sandbox') ⇒ Object



34
35
36
37
38
39
# File 'lib/file_sandbox.rb', line 34

def setup_sandbox(path = '__sandbox')
  @sandbox = Sandbox.new(path)
  @__old_path_for_sandbox = Dir.pwd
  sleep(1)
  silence_warnings {Dir.chdir(@sandbox.root)}
end

#teardown_sandboxObject



41
42
43
44
45
46
47
# File 'lib/file_sandbox.rb', line 41

def teardown_sandbox
  if @sandbox
    silence_warnings {Dir.chdir(@__old_path_for_sandbox)}
    @sandbox.clean_up
    @sandbox = nil
  end
end