Skip to content

Commit

Permalink
Split spec files programmatically
Browse files Browse the repository at this point in the history
Instead of shelling out to rspec, we now split spec files into
individual examples programmatically, using the RSpec Core API. This is
arguably faster and more robust.

Closes #6
  • Loading branch information
agis committed Sep 20, 2020
1 parent 0f35bd8 commit 7f90b6b
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions lib/rspecq/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,22 @@ def try_publish_queue!(queue)
# falling back to scheduling them as whole files. Their errors will be
# reported in the normal flow when they're eventually picked up by a worker.
def files_to_example_ids(files)
cmd = "DISABLE_SPRING=1 bundle exec rspec --dry-run --format json #{files.join(' ')} 2>&1"
out = `#{cmd}`
cmd_result = $?
out = StringIO.new

if !cmd_result.success?
rspec_output = begin
JSON.parse(out)
rescue JSON::ParserError
out
end

log_event(
"Failed to split slow files, falling back to regular scheduling",
"error",
rspec_output: rspec_output,
cmd_result: cmd_result.inspect,
)

pp rspec_output

return files
end

JSON.parse(out)["examples"].map { |e| e["id"] }
RSpec.configuration.add_formatter(RSpec::Core::Formatters::JsonFormatter.new(out))
RSpec.configuration.files_or_directories_to_run = files_or_dirs_to_run
RSpec.configuration.dry_run = true

opts = RSpec::Core::ConfigurationOptions.new(files)
result = RSpec::Core::Runner.new(opts).run($stderr, $stdout)
return files if result != 0
JSON.parse(out.string)["examples"].map { |e| e["id"] }
ensure
RSpec.configuration.dry_run = false
# clear formatters
reset_rspec_state!
end


private

def reset_rspec_state!
Expand Down

0 comments on commit 7f90b6b

Please sign in to comment.