Module: RSpec::Interactive
- Defined in:
- lib/rspec-interactive.rb,
lib/rspec-interactive/stdio.rb,
lib/rspec-interactive/config.rb,
lib/rspec-interactive/runner.rb,
lib/rspec-interactive/version.rb,
lib/rspec-interactive/client_output.rb,
lib/rspec-interactive/rspec_command.rb,
lib/rspec-interactive/string_output.rb,
lib/rspec-interactive/input_completer.rb,
lib/rspec-interactive/refresh_command.rb,
lib/rspec-interactive/threaded_output.rb,
lib/rspec-interactive/rubo_cop_command.rb
Defined Under Namespace
Classes: ClientOutput, ConfigCache, Configuration, InputCompleter, RSpecCommand, RefreshCommand, RuboCopCommand, Runner, Stdio, StringOutput, ThreadedOutput
Constant Summary
collapse
- DEFAULT_HISTORY_FILE =
'.rspec_interactive_history'.freeze
- DEFAULT_PORT =
5678
- VERSION =
"0.9.17"
Class Attribute Summary collapse
Class Method Summary
collapse
-
.await_startup(output: @output_stream) ⇒ Object
-
.check_rails ⇒ Object
-
.configure(&block) ⇒ Object
-
.configure_pry ⇒ Object
-
.configure_watched_files ⇒ Object
-
.eval(line, options, &block) ⇒ Object
-
.get_updated_files ⇒ Object
-
.get_watched_files ⇒ Object
-
.log_exception(output, e) ⇒ Object
-
.maybe_trap_interrupt ⇒ Object
-
.parse_args(args) ⇒ Object
-
.print_startup_output(output: @output_stream) ⇒ Object
-
.refresh(output: @output_stream) ⇒ Object
-
.rspec(args) ⇒ Object
-
.rspec_for_server(client, args) ⇒ Object
-
.rubo_cop(args) ⇒ Object
-
.start(config_file: nil, server: false, port: DEFAULT_PORT, history_file: DEFAULT_HISTORY_FILE, input_stream: STDIN, output_stream: STDOUT, error_stream: STDERR) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
33
34
35
|
# File 'lib/rspec-interactive.rb', line 33
def configuration
@configuration
end
|
Class Method Details
.await_startup(output: @output_stream) ⇒ Object
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
# File 'lib/rspec-interactive.rb', line 302
def self.await_startup(output: @output_stream)
return true unless @startup_thread
waited = false
if @startup_thread.alive?
output.puts 'waiting for configure_rspec...'
waited = true
end
begin
@startup_thread.join
@startup_thread = nil
if waited
if @startup_duration == 1
output.puts("configure_rspec took 1 second")
else
output.puts("configure_rspec took #{@startup_duration} seconds")
end
end
print_startup_output(output: output)
true
rescue Interrupt
false
rescue StandardError => e
print_startup_output(output: output)
output.puts 'configure_rspec failed'
log_exception(output, e)
false
end
end
|
.check_rails ⇒ Object
118
119
120
121
122
123
124
|
# File 'lib/rspec-interactive.rb', line 118
def self.check_rails
if defined?(::Rails)
if ::Rails.application.config.cache_classes
@error_stream.puts "warning: Rails.application.config.cache_classes enabled. Disable to ensure code is reloaded."
end
end
end
|
36
37
38
|
# File 'lib/rspec-interactive.rb', line 36
def self.configure(&block)
block.call(@configuration)
end
|
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/rspec-interactive.rb', line 145
def self.configure_pry
Pry.config.input = Readline
Pry.config.output = @output_stream
Readline.output = @output_stream
Readline.input = @input_stream
Pry.config.completer = RSpec::Interactive::InputCompleter
Pry.config.history_file = @history_file
end
|
348
349
350
|
# File 'lib/rspec-interactive.rb', line 348
def self.configure_watched_files
@watched_files = get_watched_files
end
|
.eval(line, options, &block) ⇒ Object
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/rspec-interactive.rb', line 290
def self.eval(line, options, &block)
return yield if line.nil? return yield if line.empty?
if await_startup
yield
else
@output_stream.puts
true
end
end
|
.get_updated_files ⇒ Object
364
365
366
367
368
369
|
# File 'lib/rspec-interactive.rb', line 364
def self.get_updated_files
new_watched_files = get_watched_files
difference = new_watched_files - @watched_files
@watched_files = new_watched_files
difference.map(&:first)
end
|
.get_watched_files ⇒ Object
352
353
354
355
356
357
358
359
360
361
362
|
# File 'lib/rspec-interactive.rb', line 352
def self.get_watched_files
return Set.new if @configuration.watch_dirs.empty?
entries = Find.find(*@configuration.watch_dirs).flat_map do |file|
if FileTest.file?(file)
[[file, File.mtime(file).to_i]]
else
[]
end
end
entries.to_set
end
|
.log_exception(output, e) ⇒ Object
334
335
336
337
|
# File 'lib/rspec-interactive.rb', line 334
def self.log_exception(output, e)
output.puts "#{e.backtrace[0]}: #{e.message} (#{e.class})"
e.backtrace[1..-1].each { |b| output.puts "\t#{b}" }
end
|
.maybe_trap_interrupt ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/rspec-interactive.rb', line 126
def self.maybe_trap_interrupt
Pry.config.should_trap_interrupts = false
trap('INT') do
if @runner
@runner&.quit
else
raise Interrupt
end
end
end
|
.parse_args(args) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/rspec-interactive.rb', line 172
def self.parse_args(args)
i = 0
parsed_args = []
until i == args.length
case args[i]
when /[\*\?\[]/
glob = Dir.glob(args[i])
parsed_args.concat(glob.empty? ? args[i] : glob)
when '--pattern'
parsed_args.concat(args[i..(i + 1)])
i += 1
else
parsed_args << args[i]
end
i += 1
end
parsed_args
end
|
.print_startup_output(output: @output_stream) ⇒ Object
339
340
341
342
343
344
345
346
|
# File 'lib/rspec-interactive.rb', line 339
def self.print_startup_output(output: @output_stream)
return if @startup_output.nil?
unless @startup_output.string.empty?
output.puts(@startup_output.string)
end
@startup_output = nil
end
|
.refresh(output: @output_stream) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/rspec-interactive.rb', line 158
def self.refresh(output: @output_stream)
get_updated_files.each do |filename|
output.puts "changed: #{filename}"
trace = TracePoint.new(:class) do |tp|
@configuration.on_class_load.call(tp.self)
end
trace.enable
load filename
trace.disable
output.puts
end
@configuration.refresh.call
end
|
.rspec(args) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/rspec-interactive.rb', line 193
def self.rspec(args)
@rspec_mutex.synchronize do
begin
@runner = RSpec::Interactive::Runner.new(parse_args(args))
refresh
Pry.config.history_save = false
RSpec.configure do |config|
config.error_stream = @error_stream
config.output_stream = @output_stream
config.start_time = RSpec::Core::Time.now
end
@runner.run
ensure
@runner = nil
Pry.config.history_save = true
RSpec.clear_examples
RSpec.reset
@config_cache.replay_configuration
end
end
end
|
.rspec_for_server(client, args) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/rspec-interactive.rb', line 226
def self.rspec_for_server(client, args)
@rspec_mutex.synchronize do
disable_pry = ENV['DISABLE_PRY']
output = ClientOutput.new(client)
ENV['TEAMCITY_RAKE_RUNNER_DEBUG_OUTPUT_CAPTURER_ENABLED'] = 'false'
Rake::TeamCity::RunnerCommon.class_variable_set(:@@original_stdout, output)
return unless await_startup(output: output)
Stdio.capture(stdout: output, stderr: output) do
ENV['DISABLE_PRY'] = 'true'
runner = RSpec::Interactive::Runner.new(parse_args(args))
refresh(output: output)
RSpec.configure do |config|
config.error_stream = output
config.output_stream = output
config.start_time = RSpec::Core::Time.now
end
RSpec.configuration.instance_variable_set(
:@formatter_loader,
RSpec::Core::Formatters::Loader.new(RSpec::Core::Reporter.new(RSpec.configuration)))
RSpec.configuration.formatter = Spec::Runner::Formatter::TeamcityFormatter
runner.run
rescue Errno::EPIPE, IOError
ensure
ENV['DISABLE_PRY'] = disable_pry
runner = nil
RSpec.clear_examples
RSpec.reset
@config_cache.replay_configuration
end
end
end
|
.rubo_cop(args) ⇒ Object
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/rspec-interactive.rb', line 279
def self.rubo_cop(args)
begin
require 'rubocop'
rescue LoadError
@error_stream.puts "fatal: RuboCop not found. Is the gem installed in this project?"
return
end
RuboCop::CLI.new.run args
end
|
.start(config_file: nil, server: false, port: DEFAULT_PORT, history_file: DEFAULT_HISTORY_FILE, input_stream: STDIN, output_stream: STDOUT, error_stream: STDERR) ⇒ Object
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
|
# File 'lib/rspec-interactive.rb', line 40
def self.start(
config_file: nil,
server: false,
port: DEFAULT_PORT,
history_file: DEFAULT_HISTORY_FILE,
input_stream: STDIN,
output_stream: STDOUT,
error_stream: STDERR)
@history_file = history_file
@updated_files = []
@stty_save = %x`stty -g`.chomp
@rspec_mutex = Mutex.new
@output_stream = output_stream
@input_stream = input_stream
@error_stream = error_stream
@config_cache = RSpec::Interactive::ConfigCache.new
@configuration = Configuration.new
if !config_file && File.exist?('spec/rspec_interactive.rb')
config_file = 'spec/rspec_interactive.rb'
end
load config_file if config_file
check_rails
maybe_trap_interrupt
configure_pry
configure_watched_files
@startup_thread = Thread.start do
Thread.current.report_on_exception = false
if server
@server_thread = Thread.start do
begin
server = TCPServer.new port
rescue StandardError => e
log_exception(@output_stream, e)
exit 1
end
while true
break unless client = server.accept
begin
request = client.gets
args = Shellwords.split(request)
rspec_for_server(client, args)
rescue StandardError => e
@output_stream.puts
@output_stream.puts 'error handling client request'
log_exception(@output_stream, e)
ensure
client.close
end
end
end
end
@startup_output = StringOutput.new
output = ThreadedOutput.new(thread_map: { Thread.current => @startup_output }, default: @output_stream)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Stdio.capture(stdout: output, stderr: output) do
@config_cache.record_configuration { @configuration.configure_rspec.call }
end
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
@startup_duration = (finish - start).round
end
Pry.start
@server_thread.exit if @server_thread
@startup_thread.exit if @startup_thread
0
end
|