Skip to content

Commit

Permalink
Split spec files to examples 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
faster and more robust, since we avoid common shell shenanigans.

Closes #6
  • Loading branch information
agis committed Sep 21, 2020
1 parent c89c87b commit f4bbe3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
38 changes: 16 additions & 22 deletions lib/rspecq/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,39 +163,33 @@ def try_publish_queue!(queue)
puts "Published queue (size=#{queue.publish(jobs)})"
end

# Split a list of spec files into their individual examples.
#
# NOTE: RSpec has to load the files before we can split them as individual
# examples. In case a file to be splitted fails to be loaded
# (e.g. contains a syntax error), we return the files unchanged, thereby
# 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(' ')}"
out, err, cmd_result = Open3.capture3(cmd)

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.\n #{err}",
"error",
rspec_stdout: rspec_output,
rspec_stderr: err,
cmd_result: cmd_result.inspect,
)
out = StringIO.new

pp rspec_output
reset_rspec_state!

return files
end
dry_run_before = RSpec.configuration.dry_run

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 = dry_run_before
reset_rspec_state!
end


private

def reset_rspec_state!
Expand Down
4 changes: 2 additions & 2 deletions test/test_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_files_to_example_ids
]
)

assert_equal expected, actual
assert_equal expected.sort, actual.sort
end

def test_files_to_example_ids_failure_fallback
Expand All @@ -36,6 +36,6 @@ def test_files_to_example_ids_failure_fallback
]
)

assert_equal expected, actual
assert_equal expected.sort, actual.sort
end
end

0 comments on commit f4bbe3d

Please sign in to comment.