-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_sandbox.rb
148 lines (122 loc) · 3.28 KB
/
file_sandbox.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'fileutils'
require 'test/unit/assertions'
module FileSandbox
VERSION = "0.5"
attr_reader :sandbox
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
def setup_sandbox(path = '__sandbox')
@sandbox = Sandbox.new(path)
@__old_path_for_sandbox = Dir.pwd
sleep(1)
silence_warnings {Dir.chdir(@sandbox.root)}
end
def teardown_sandbox
if @sandbox
silence_warnings {Dir.chdir(@__old_path_for_sandbox)}
@sandbox.clean_up
@sandbox = nil
end
end
class Sandbox
include Test::Unit::Assertions
attr_reader :root
def initialize(path = '__sandbox')
@root = File.expand_path(path)
clean_up
FileUtils.mkdir_p @root
end
def [](name)
SandboxFile.new(File.join(@root, name))
end
# usage new :file=>'my file.rb', :with_contents=>'some stuff'
def new(options)
if options.has_key? :directory
dir = self[options.delete(:directory)]
FileUtils.mkdir_p dir.path
else
file = self[options.delete(:file)]
if (binary_content = options.delete(:with_binary_content) || options.delete(:with_binary_contents))
file.binary_content = binary_content
else
file.content = (options.delete(:with_content) || options.delete(:with_contents) || '')
end
end
raise "unexpected keys '#{options.keys.join(', ')}'" unless options.empty?
dir || file
end
def remove(options)
name = File.join(@root, options[:file])
FileUtils.remove_file name
end
# usage assert :file=>'my file.rb', :has_contents=>'some stuff'
def assert(options)
name = File.join(@root, options[:file])
if (expected_content = options[:has_content] || options[:has_contents])
assert_equal(expected_content, File.read(name))
else
fail('expected something to assert')
end
end
def clean_up
FileUtils.rm_rf @root
if File.exists? @root
raise "Could not remove directory #{@root.inspect}, something is probably still holding a lock on it"
end
end
end
class SandboxFile
attr_reader :path
def initialize(path)
@path = path
end
def exist?
File.exist? path
end
def content
File.read path
end
def content=(content)
FileUtils.mkdir_p File.dirname(@path)
File.open(@path, "w") {|f| f << content}
end
def binary_content=(content)
FileUtils.mkdir_p File.dirname(@path)
File.open(@path, "wb") {|f| f << content}
end
def create
self.content = ''
end
alias exists? exist?
alias contents content
alias contents= content=
alias binary_contents= binary_content=
end
private
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end
end