-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass.rb
executable file
·103 lines (87 loc) · 2.19 KB
/
pass.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
#!/usr/bin/env ruby
# Description
# Starts the rails environment in a long running process that forks a test
# runner. The test begins running immediately as the rails environment
# has already been loaded. Useful when running a single spec over and over.
# Type CTRL-\ (Quit signal) to enter the rails console
# Type CTRL-D (or exit) to exit the rails console
# Usage
# chmod +x pass
# In one terminal window, run ./pass.rb
# In another window, run the actual test, i.e. ./pass.rb spec/models/model_spec.rb
FIFO_FILE = ".pass_ipc"
`mkfifo #{FIFO_FILE}` unless test(?e, FIFO_FILE)
if ARGV.any?
File.open(FIFO_FILE, 'w') do |f|
f.print ARGV.join(" ")
end
File.open(FIFO_FILE) do |f|
until f.eof?
print f.getc
end
end
exit
end
class Pass
def initialize
trap('QUIT') { print("\r"); launch_console }
end
def trap_int
trap('INT') { abort("\rExit\n") }
end
def start
load_environment
trap_int
wait_and_run
end
def wait_and_run
while args = File.read(FIFO_FILE)
puts "got args #{args.inspect}"
run args unless args.empty?
end
rescue Errno::EINTR
puts 'retrying'
retry
end
def run(args)
args = args.split(' ')
puts "running #{args.inspect}"
benchmark('reload') { reload_stack }
benchmark('fork') do
fork do
benchmark('establish connection') do
ActiveRecord::Base.establish_connection
end
out = File.open(FIFO_FILE, 'w')
::RSpec::Core::Runner.run args.unshift('--tty'), $stderr, out
exit
end
end
Process.waitall
end
def load_environment
benchmark('rails loaded') do
require File.expand_path('spec/spec_helper')
ActiveRecord::Base.remove_connection
::RSpec.configuration.backtrace_clean_patterns << %r(#{__FILE__})
end
end
def launch_console
require 'rails/commands/console'
Rails::Console.start(Rails.application)
trap_int
print "\r\n"
end
def reload_stack
Rails.application.reloaders.each do |reloader|
reloader.execute_if_updated
end
end
private
def benchmark(msg, &block)
t = Benchmark.realtime &block
puts "#{msg} in #{t}s"
end
end
require 'benchmark'
Pass.new.start