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 b9bf09a commit 4fa3b4e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
37 changes: 14 additions & 23 deletions lib/rspecq/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,24 @@ 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
reset_rspec_state!

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
2 changes: 1 addition & 1 deletion test/test_scheduling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_scheduling_with_timings_and_splitting
worker = new_worker("scheduling")
worker.populate_timings = true
worker.file_split_threshold = 0.2
silent { worker.try_publish_queue!(worker.queue) }
worker.try_publish_queue!(worker.queue)

assert_equal [
"./test/sample_suites/scheduling/spec/foo_spec.rb[1:2:1]",
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 4fa3b4e

Please sign in to comment.