Class: Specwrk::CLI::Watch

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/specwrk/cli.rb

Instance Method Summary collapse

Instance Method Details

#call(count:, watchfile:, **args) ⇒ Object



293
294
295
296
297
298
299
300
301
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/specwrk/cli.rb', line 293

def call(count:, watchfile:, **args)
  $stdout.sync = true

  # nil this env var if it exists to prevent never-ending workers
  ENV["SPECWRK_SRV_URI"] = nil

  # Start on a random open port to not conflict with another server
  ENV["SPECWRK_SRV_PORT"] = find_open_port.to_s
  ENV["SPECWRK_SRV_URI"] = "http://localhost:#{ENV.fetch("SPECWRK_SRV_PORT", "5138")}"

  ENV["SPECWRK_SEED_WAITS"] = "0"
  ENV["SPECWRK_MAX_BUCKET_SIZE"] = "1"
  ENV["SPECWRK_COUNT"] = count.to_s
  ENV["SPECWRK_RUN"] = "watch"

  web_pid

  return if Specwrk.force_quit

  seed_pid

  start_watcher(watchfile)

  require "specwrk/cli_reporter"

  title "šŸ‘€ for changes"

  loop do
    status "šŸ‘€ Watching for file changes..."

    @worker_pids = nil
    Thread.pass until file_queue.length.positive? || Specwrk.force_quit

    break if Specwrk.force_quit

    files = []
    files.push(file_queue.pop) until file_queue.length.zero?
    status "Running specs for #{files.join(" ")}..."
    ipc.write(files.join(" "))

    example_count = ipc.read.to_i
    if example_count.positive?
      puts "\n🌱 Seeded #{example_count} examples for execution\n"
    else
      puts "\nšŸ™… No examples to seed for execution\n"
    end

    next if example_count.zero?
    title "šŸ‘· on #{example_count} examples"

    return if Specwrk.force_quit
    start_workers

    Specwrk.wait_for_pids_exit(@worker_pids)

    drain_outputs
    return if Specwrk.force_quit

    reporter = Specwrk::CLIReporter.new

    status = reporter.report
    puts

    if status.zero?
      title "🟢 #{reporter.example_count} examples passed"
    else
      title " šŸ”“ #{reporter.failure_count}/#{reporter.example_count} examples failed"
    end

    $stdout.flush
  end

  ipc.write "INT" # wakes the socket
  Specwrk.wait_for_pids_exit([web_pid, seed_pid])
end