Class: Autospec::SimpleRunner

Inherits:
RspecRunner show all
Defined in:
lib/autospec/simple_runner.rb

Constant Summary

Constants inherited from RspecRunner

RspecRunner::RELOADERS, RspecRunner::WATCHERS

Instance Method Summary collapse

Methods inherited from RspecRunner

#failed_specs, reload, #reloaders, watch, #watchers

Methods inherited from BaseRunner

#failed_specs, #reload, #running?, #start

Constructor Details

#initializeSimpleRunner

Returns a new instance of SimpleRunner.



7
8
9
# File 'lib/autospec/simple_runner.rb', line 7

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#abortObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/autospec/simple_runner.rb', line 44

def abort
  if pid = @pid
    begin
      Process.kill("TERM", pid)
    rescue StandardError
      nil
    end
    wait_for_done(pid)
    pid = nil
  end
end

#run(specs) ⇒ Object



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
# File 'lib/autospec/simple_runner.rb', line 11

def run(specs)
  puts "Running Rspec: #{specs}"
  # kill previous rspec instance
  @mutex.synchronize { self.abort }
  # we use our custom rspec formatter
  args = ["-r", "#{File.dirname(__FILE__)}/formatter.rb", "-f", "Autospec::Formatter"]

  command =
    begin
      line_specified = specs.split.any? { |s| s =~ /\:/ } # Parallel spec can't run specific line
      multiple_files = specs.split.count > 1 || specs == "spec" # Only parallelize multiple files
      if ENV["PARALLEL_SPEC"] == "1" && multiple_files && !line_specified
        "bin/turbo_rspec #{args.join(" ")} #{specs.split.join(" ")}"
      else
        "bin/rspec #{args.join(" ")} #{specs.split.join(" ")}"
      end
    end

  # launch rspec
  Dir.chdir(Rails.root) do # rubocop:disable Discourse/NoChdir because this is not part of the app
    env = { "RAILS_ENV" => "test" }
    if specs.split(" ").any? { |s| s =~ %r{\A(./)?plugins} }
      env["LOAD_PLUGINS"] = "1"
      puts "Loading plugins while running specs"
    end
    pid = @mutex.synchronize { @pid = Process.spawn(env, command) }

    _, status = Process.wait2(pid)

    status.exitstatus
  end
end

#stopObject



56
57
58
59
60
61
# File 'lib/autospec/simple_runner.rb', line 56

def stop
  # assume sigint on child will take care of this?
  if pid = @pid
    wait_for_done(pid)
  end
end

#wait_for_done(pid) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/autospec/simple_runner.rb', line 63

def wait_for_done(pid)
  i = 3000
  while (
          begin
            i > 0 && Process.getpgid(pid)
          rescue StandardError
            nil
          end
        )
    sleep 0.001
    i -= 1
  end
  if (
       begin
         Process.getpgid(pid)
       rescue StandardError
         nil
       end
     )
    STDERR.puts "Terminating rspec #{pid} by force cause it refused graceful termination"
    Process.kill("KILL", pid)
  end
end