Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle fuubar #51

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ Options:
--seed SEED Seed for rspec
```

To pass any options supported by paralell_tests, use `--`:

```bash
bundle exec turbo_tests -n 4 -- --only-group 1 --pattern spec/system
```

`turbo_tests` supports custom formatter such as Fuubar, but you might need to require it:

```bash
bundle exec turbo_tests -r fuubar -f Fuubar spec/whatever
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
12 changes: 8 additions & 4 deletions lib/turbo_tests/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,20 @@ def run
end
end

success = TurboTests::Runner.run(
parallel_options = ParallelTests::CLI.new.send(:parse_options!, @argv.unshift("--type", "rspec"))
files = parallel_options.fetch(:files, ["spec"])
inkstak marked this conversation as resolved.
Show resolved Hide resolved

success = TurboTests::Runner.run({
formatters: formatters,
tags: tags,
files: @argv.empty? ? ["spec"] : @argv,
files: files,
runtime_log: runtime_log,
verbose: verbose,
fail_fast: fail_fast,
count: count,
seed: seed
)
seed: seed,
parallel_options: parallel_options
})

if success
exit 0
Expand Down
25 changes: 22 additions & 3 deletions lib/turbo_tests/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module TurboTests
class Reporter
attr_writer :load_time

def self.from_config(formatter_config, start_time)
reporter = new(start_time)
def self.from_config(formatter_config, start_time, files, parallel_options)
reporter = new(start_time, files, parallel_options)

formatter_config.each do |config|
name, outputs = config.values_at(:name, :outputs)
Expand All @@ -23,7 +23,7 @@ def self.from_config(formatter_config, start_time)
attr_reader :pending_examples
attr_reader :failed_examples

def initialize(start_time)
def initialize(start_time, files, parallel_options)
@formatters = []
@pending_examples = []
@failed_examples = []
Expand All @@ -32,9 +32,12 @@ def initialize(start_time)
@start_time = start_time
@load_time = 0
@errors_outside_of_examples_count = 0
@files = files
@parallel_options = parallel_options
end

def add(name, outputs)
custom_formatters = false
outputs.each do |output|
formatter_class =
case name
Expand All @@ -43,11 +46,18 @@ def add(name, outputs)
when "d", "documentation"
RSpec::Core::Formatters::DocumentationFormatter
else
custom_formatters = true
Kernel.const_get(name)
end

@formatters << formatter_class.new(output)
end

start if custom_formatters
ilyazub marked this conversation as resolved.
Show resolved Hide resolved
end

def start
delegate_to_formatters(:start, RSpec::Core::Notifications::StartNotification.new(examples_count))
end

def group_started(notification)
Expand Down Expand Up @@ -121,6 +131,15 @@ def seed_notification(seed, seed_used)

protected

def examples_count
files = ParallelTests::RSpec::Runner.send(:find_tests, @files, @parallel_options)
output_file = Tempfile.new("rspec-summary")
`#{ENV.fetch("BUNDLE_BIN_PATH")} exec rspec --dry-run --format json --out='#{output_file.path}' #{files.join(" ")}`

json_summary = JSON.parse(output_file.read, symbolize_names: true)
json_summary.dig(:summary, :example_count) || 0
end

def delegate_to_formatters(method, *args)
@formatters.each do |formatter|
formatter.send(method, *args) if formatter.respond_to?(method)
Expand Down
43 changes: 13 additions & 30 deletions lib/turbo_tests/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,67 @@ class Runner
using CoreExtensions

def self.run(opts = {})
files = opts[:files]
formatters = opts[:formatters]
tags = opts[:tags]

# SEE: https://bit.ly/2NP87Cz
start_time = opts.fetch(:start_time) { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
runtime_log = opts.fetch(:runtime_log, nil)
verbose = opts.fetch(:verbose, false)
fail_fast = opts.fetch(:fail_fast, nil)
count = opts.fetch(:count, nil)
seed = opts.fetch(:seed) || rand(0xFFFF).to_s
seed_used = !opts[:seed].nil?

if verbose
warn "VERBOSE"
end

reporter = Reporter.from_config(formatters, start_time)

new(
reporter: reporter,
files: files,
tags: tags,
runtime_log: runtime_log,
**opts,
start_time: start_time,
verbose: verbose,
fail_fast: fail_fast,
count: count,
seed: seed,
seed_used: seed_used
).run
end

def initialize(opts)
@reporter = opts[:reporter]
def initialize(**opts)
@formatters = opts[:formatters]
@files = opts[:files]
@tags = opts[:tags]
@runtime_log = opts[:runtime_log] || "tmp/turbo_rspec_runtime.log"
@verbose = opts[:verbose]
@fail_fast = opts[:fail_fast]
@start_time = opts[:start_time]
@count = opts[:count]
@load_time = 0
@load_count = 0
@failure_count = 0
@seed = opts[:seed]
@seed_used = opts[:seed_used]

@runtime_log = opts[:runtime_log] || "tmp/turbo_rspec_runtime.log"
@parallel_options = opts.fetch(:parallel_options, {})
@parallel_options[:runtime_log] = @runtime_log

@messages = Thread::Queue.new
@threads = []
@error = false
end

def run
@reporter = Reporter.from_config(@formatters, @start_time, @files, @parallel_options)

@num_processes = [
ParallelTests.determine_number_of_processes(@count),
ParallelTests::RSpec::Runner.tests_with_size(@files, {}).size
].min

use_runtime_info = @files == ["spec"]

group_opts = {}

if use_runtime_info
group_opts[:runtime_log] = @runtime_log
else
group_opts[:group_by] = :filesize
end

tests_in_groups =
ParallelTests::RSpec::Runner.tests_in_groups(
@files,
@num_processes,
**group_opts
@parallel_options
)

setup_tmp_dir

subprocess_opts = {
record_runtime: use_runtime_info
record_runtime: @parallel_options[:group_by] == :runtime
}

report_number_of_tests(tests_in_groups)
Expand Down