forked from josevalim/enginex
/
test_helper.rb
77 lines (63 loc) · 1.83 KB
/
test_helper.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
require 'rubygems'
begin
gem "test-unit"
rescue LoadError
end
ENV["RAILS_ENV"] ||= "test"
require 'test/unit'
require 'active_support'
require 'active_support/test_case'
$counter = 0
LIB_PATH = File.expand_path('../../lib', __FILE__)
BIN_PATH = File.expand_path('../../bin/enginex', __FILE__)
DESTINATION_ROOT = File.expand_path('../enginex', __FILE__)
FileUtils.rm_rf(DESTINATION_ROOT)
$:.unshift LIB_PATH
require 'enginex'
class ActiveSupport::TestCase
def run_enginex
$counter += 1
`ruby -I#{LIB_PATH} -rrubygems #{BIN_PATH} #{destination_root}`
yield
FileUtils.rm_rf(File.dirname(destination_root))
rescue Exception => e
puts "Error happened. #{destination_root.inspect} left for inspecting."
raise e
end
def destination_root
File.join(DESTINATION_ROOT, $counter.to_s, "demo_engine")
end
def capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
eval("$#{stream}").string
ensure
eval("$#{stream} = #{stream.upcase}")
end
end
alias :silence :capture
def assert_file(relative, *contents)
absolute = File.expand_path(relative, destination_root)
assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not"
read = File.read(absolute) if block_given? || !contents.empty?
yield read if block_given?
contents.each do |content|
assert_match content, read
end
end
alias :assert_directory :assert_file
def assert_no_file(relative)
absolute = File.expand_path(relative, destination_root)
assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does"
end
alias :assert_no_directory :assert_no_file
def execute(command)
current_path = Dir.pwd
FileUtils.cd destination_root
`#{command}`
ensure
FileUtils.cd current_path
end
end