Class: Specwrk::Worker::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/specwrk/worker/executor.rb

Instance Method Summary collapse

Instance Method Details

#completion_formatterObject



71
72
73
# File 'lib/specwrk/worker/executor.rb', line 71

def completion_formatter
  @completion_formatter ||= CompletionFormatter.new
end

#examplesObject



14
15
16
# File 'lib/specwrk/worker/executor.rb', line 14

def examples
  completion_formatter.examples
end

#final_outputObject



18
19
20
# File 'lib/specwrk/worker/executor.rb', line 18

def final_output
  progress_formatter.final_output
end

#flush_logObject



75
76
77
# File 'lib/specwrk/worker/executor.rb', line 75

def flush_log
  completion_formatter.examples.each { |example| json_log_file.puts JSON.generate(example) }
end

#json_log_fileObject



79
80
81
82
83
84
85
86
87
# File 'lib/specwrk/worker/executor.rb', line 79

def json_log_file
  @json_log_file ||= if json_log_file_path
    FileUtils.mkdir_p(File.dirname(json_log_file_path))
    File.truncate(json_log_file_path, 0) if File.exist?(json_log_file_path)
    File.open(json_log_file_path, "a", sync: true)
  else
    File.open(File::NULL, "a")
  end
end

#json_log_file_pathObject



89
90
91
92
93
# File 'lib/specwrk/worker/executor.rb', line 89

def json_log_file_path
  return unless ENV["SPECWRK_OUT"]

  @json_log_file_path ||= File.join(ENV["SPECWRK_OUT"], ENV["SPECWRK_RUN"], "#{ENV["SPECWRK_FORKED"]}.ndjson")
end

#progress_formatterObject

We want to persist this object between example runs



67
68
69
# File 'lib/specwrk/worker/executor.rb', line 67

def progress_formatter
  @progress_formatter ||= ProgressFormatter.new($stdout)
end

#reset!Object



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
# File 'lib/specwrk/worker/executor.rb', line 32

def reset!
  flush_log
  completion_formatter.examples.clear

  RSpec.clear_examples
  RSpec.configuration.backtrace_formatter.filter_gem "specwrk"

  # see https://github.com/rspec/rspec-core/pull/2723
  if Gem::Version.new(RSpec::Core::Version::STRING) <= Gem::Version.new("3.9.1")
    RSpec.world.instance_variable_set(
      :@example_group_counts_by_spec_file, Hash.new(0)
    )
  end

  # RSpec.clear_examples does not reset those, which causes issues when
  # a non-example error occurs (subsequent jobs are not executed)
  RSpec.world.non_example_failure = false

  # we don't want an error that occured outside of the examples (which
  # would set this to `true`) to stop the worker
  RSpec.world.wants_to_quit = Specwrk.force_quit

  RSpec.configuration.silence_filter_announcements = true

  RSpec.configuration.add_formatter progress_formatter
  RSpec.configuration.add_formatter completion_formatter

  # This formatter may be specified by the runner options so
  # it will be initialized by RSpec
  RSpec.configuration.add_formatter NullFormatter

  true
end

#run(examples) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/specwrk/worker/executor.rb', line 22

def run(examples)
  reset!

  example_ids = examples.map { |example| example[:id] }

  options = RSpec::Core::ConfigurationOptions.new ["--format", "Specwrk::Worker::NullFormatter"] + example_ids
  RSpec::Core::Runner.new(options).run($stderr, $stdout)
end