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



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

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



78
79
80
# File 'lib/specwrk/worker/executor.rb', line 78

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

#json_log_fileObject



82
83
84
85
86
87
88
89
90
# File 'lib/specwrk/worker/executor.rb', line 82

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



92
93
94
95
96
# File 'lib/specwrk/worker/executor.rb', line 92

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



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

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

#reset!Object



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

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
30
31
32
# 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
  Hooks.run(:before_worker_examples_execute, examples)
  RSpec::Core::Runner.new(options).run($stderr, $stdout).tap do
    Hooks.run(:after_worker_examples_execute, examples)
  end
end