From 4723a12a032e62f37d50b9d461536f3fba559e3d Mon Sep 17 00:00:00 2001 From: Sebastien Savater Date: Tue, 19 Sep 2023 15:54:12 +0200 Subject: [PATCH] Support parallel_tests options --- .rubocop_gradual.lock | 14 +++++++------- README.md | 6 ++++++ lib/turbo_tests/cli.rb | 6 +++++- lib/turbo_tests/runner.rb | 34 +++++++++++++++++++++------------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/.rubocop_gradual.lock b/.rubocop_gradual.lock index 6591d04..7527bd3 100644 --- a/.rubocop_gradual.lock +++ b/.rubocop_gradual.lock @@ -25,14 +25,14 @@ "lib/turbo_tests/reporter.rb:1386902608": [ [7, 5, 400, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2092085575] ], - "lib/turbo_tests/runner.rb:2204434148": [ + "lib/turbo_tests/runner.rb:1170052458": [ [13, 5, 271, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 21989008], - [20, 5, 917, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1582439743], - [203, 11, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], - [221, 47, 6, "Style/GlobalStdStream: Use `$stderr` instead of `STDERR`.", 3356712163], - [223, 21, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], - [232, 7, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], - [292, 9, 6, "Style/GlobalStdStream: Use `$stdout` instead of `STDOUT`.", 3356722952] + [20, 5, 1253, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1925027850], + [211, 11, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], + [229, 47, 6, "Style/GlobalStdStream: Use `$stderr` instead of `STDERR`.", 3356712163], + [231, 21, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], + [240, 7, 10, "ThreadSafety/NewThread: Avoid starting new threads.", 3411682361], + [300, 9, 6, "Style/GlobalStdStream: Use `$stdout` instead of `STDOUT`.", 3356722952] ], "spec/cli_spec.rb:3990998076": [ [1, 1, 30, "RSpec/SpecFilePathFormat: Spec path should end with `turbo_tests/cli*_spec.rb`.", 965721356], diff --git a/README.md b/README.md index 47ff3fb..a5e30f7 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,12 @@ Options: --print_failed_group Prints group that had failures in it ``` +To pass any options supported by paralell_tests, use `--` : + +```bash +bundle exec turbo_tests -n 4 -- --only-group 1 --pattern spec/system +``` + ## 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. diff --git a/lib/turbo_tests/cli.rb b/lib/turbo_tests/cli.rb index 33b2b17..18c7ac2 100644 --- a/lib/turbo_tests/cli.rb +++ b/lib/turbo_tests/cli.rb @@ -114,16 +114,20 @@ def run formatter[:outputs] << "-" if formatter[:outputs].empty? end + parallel_options = ParallelTests::CLI.new.send(:parse_options!, @argv.unshift("--type", "rspec")) + files = parallel_options.fetch(:files, ["spec"]) + exitstatus = 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, print_failed_group: print_failed_group, + parallel_options: parallel_options, ) # From https://github.com/serpapi/turbo_tests/pull/20/ diff --git a/lib/turbo_tests/runner.rb b/lib/turbo_tests/runner.rb index efab4b7..94fdfa1 100644 --- a/lib/turbo_tests/runner.rb +++ b/lib/turbo_tests/runner.rb @@ -21,6 +21,7 @@ def self.run(opts = {}) files = opts[:files] formatters = opts[:formatters] tags = opts[:tags] + parallel_options = opts[:parallel_options] || {} start_time = opts.fetch(:start_time) { RSpec::Core::Time.now } runtime_log = opts.fetch(:runtime_log, nil) @@ -31,6 +32,14 @@ def self.run(opts = {}) seed_used = !seed.nil? print_failed_group = opts.fetch(:print_failed_group, false) + use_runtime_info = files == ["spec"] + + if use_runtime_info + parallel_options[:runtime_log] = runtime_log + else + parallel_options[:group_by] = :filesize + end + warn("VERBOSE") if verbose reporter = Reporter.from_config(formatters, start_time, seed, seed_used) @@ -46,6 +55,8 @@ def self.run(opts = {}) seed: seed, seed_used: seed_used, print_failed_group: print_failed_group, + use_runtime_info: use_runtime_info, + parallel_options: parallel_options, ).run end @@ -53,17 +64,24 @@ def initialize(opts) @reporter = opts[:reporter] @files = opts[:files] @tags = opts[:tags] - @runtime_log = opts[:runtime_log] || "tmp/turbo_rspec_runtime.log" @verbose = opts[:verbose] @fail_fast = opts[:fail_fast] @count = opts[:count] @seed = opts[:seed] @seed_used = opts[:seed_used] + @use_runtime_info = opts[:use_runtime_info] @load_time = 0 @load_count = 0 @failure_count = 0 + # Supports runtime_log as a top level option, + # but also nested inside parallel_options + @runtime_log = opts[:runtime_log] || "tmp/turbo_rspec_runtime.log" + @parallel_options = opts.fetch(:parallel_options, {}) + @parallel_options[:runtime_log] ||= @runtime_log + @record_runtime = @parallel_options[:group_by] == :runtime + @messages = Thread::Queue.new @threads = [] @error = false @@ -76,27 +94,17 @@ def run 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: @record_runtime, } @reporter.report(tests_in_groups) do |_reporter|