Class: ParallelRSpec::Runner

Inherits:
RSpec::Core::Runner
  • Object
show all
Defined in:
lib/parallel_rspec/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.invokeObject

Runs the suite of specs and exits the process with an appropriate exit code.



21
22
23
24
# File 'lib/parallel_rspec/runner.rb', line 21

def self.invoke
  status = run(ARGV, $stderr, $stdout).to_i
  exit(status) if status != 0
end

.run(args, err = $stderr, out = $stdout) ⇒ Fixnum

Run a suite of RSpec examples. Does not exit.

This is used internally by RSpec to run a suite, but is available for use by any other automation tool.

If you want to run this multiple times in the same process, and you want files like ‘spec_helper.rb` to be reloaded, be sure to load `load` instead of `require`.

Parameters:

  • args (Array)

    command-line-supported arguments

  • err (IO) (defaults to: $stderr)

    error stream

  • out (IO) (defaults to: $stdout)

    output stream

Returns:

  • (Fixnum)

    exit status code. 0 if all specs passed, or the configured failure exit code (1 by default) if specs failed.



41
42
43
44
45
# File 'lib/parallel_rspec/runner.rb', line 41

def self.run(args, err=$stderr, out=$stdout)
  RSpec::Core::Runner.trap_interrupt
  options = RSpec::Core::ConfigurationOptions.new(args)
  new(options).run(err, out)
end

Instance Method Details

#run_in_parallel(example_groups, reporter) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/parallel_rspec/runner.rb', line 64

def run_in_parallel(example_groups, reporter)
  server = Server.new(reporter)
  workers = Workers.new
  workers.run_test_workers_with_server(server) do |worker, channel_to_server|
    client = Client.new(channel_to_server)
    index = 0
    RSpec.world.filtered_examples.each do |group, examples|
      examples.reject! do |example|
        index += 1
        (index % workers.number_of_workers) != (worker % workers.number_of_workers)
      end
    end
    success = example_groups.map { |g| g.run(client) }.all?
    channel_to_server.write([:result, success])
  end
  server.success?
end

#run_specs(example_groups) ⇒ Fixnum

Runs the provided example groups.

Parameters:

Returns:

  • (Fixnum)

    exit status code. 0 if all specs passed, or the configured failure exit code (1 by default) if specs failed.



53
54
55
56
57
58
59
60
61
62
# File 'lib/parallel_rspec/runner.rb', line 53

def run_specs(example_groups)
  @configuration.reporter.report(@world.example_count(example_groups)) do |reporter|
    @configuration.with_suite_hooks do
      with_context_hooks, without_context_hooks = example_groups.partition(&:any_context_hooks?)
      success = run_in_parallel(without_context_hooks, reporter)
      success &&= with_context_hooks.map { |g| g.run(reporter) }.all?
      success ? 0 : @configuration.failure_exit_code
    end
  end
end