Skip to content

Commit

Permalink
Add TEST_ENV_NUMBER and PARALLEL_TEST_GROUPS environment variables to…
Browse files Browse the repository at this point in the history
… command output in --verbose (#884)
  • Loading branch information
adis-io committed Nov 5, 2022
1 parent bd8ed08 commit fc09051
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Breaking Changes
- The `--verbose-process-command` and `--verbose-rerun-command` are combined into `--verbose-command`. See [#884](https://github.com/grosser/parallel_tests/pull/884).

### Added

Expand Down
7 changes: 3 additions & 4 deletions lib/parallel_tests/cli.rb
Expand Up @@ -131,12 +131,12 @@ def report_failure_rerun_commmand(test_results, options)
failing_sets = test_results.reject { |r| r[:exit_status] == 0 }
return if failing_sets.none?

if options[:verbose] || options[:verbose_rerun_command]
if options[:verbose] || options[:verbose_command]
puts "\n\nTests have failed for a parallel_test group. Use the following command to run the group again:\n\n"
failing_sets.each do |failing_set|
command = failing_set[:command]
command = @runner.command_with_seed(command, failing_set[:seed]) if failing_set[:seed]
puts Shellwords.shelljoin(command)
@runner.print_command(command, failing_set[:env] || {})
end
end
end
Expand Down Expand Up @@ -261,8 +261,7 @@ def parse_options!(argv)
opts.on("--first-is-1", "Use \"1\" as TEST_ENV_NUMBER to not reuse the default test environment") { options[:first_is_1] = true }
opts.on("--fail-fast", "Stop all groups when one group fails (best used with --test-options '--fail-fast' if supported") { options[:fail_fast] = true }
opts.on("--verbose", "Print debug output") { options[:verbose] = true }
opts.on("--verbose-process-command", "Displays only the command that will be executed by each process") { options[:verbose_process_command] = true }
opts.on("--verbose-rerun-command", "When there are failures, displays the command executed by each process that failed") { options[:verbose_rerun_command] = true }
opts.on("--verbose-command", "Displays the command that will be executed by each process and when there are failures displays the command executed by each process that failed") { options[:verbose_command] = true }
opts.on("--quiet", "Print only tests output") { options[:quiet] = true }
opts.on("-v", "--version", "Show Version") do
puts ParallelTests::VERSION
Expand Down
11 changes: 8 additions & 3 deletions lib/parallel_tests/test/runner.rb
Expand Up @@ -97,11 +97,16 @@ def execute_command(cmd, process_number, num_processes, options)
# being able to run with for example `-output foo-$TEST_ENV_NUMBER` worked originally and is convenient
cmd.map! { |c| c.gsub("$TEST_ENV_NUMBER", number).gsub("${TEST_ENV_NUMBER}", number) }

puts Shellwords.shelljoin(cmd) if report_process_command?(options) && !options[:serialize_stdout]
print_command(cmd, env) if report_process_command?(options) && !options[:serialize_stdout]

execute_command_and_capture_output(env, cmd, options)
end

def print_command(command, env)
env_str = ['TEST_ENV_NUMBER', 'PARALLEL_TEST_GROUPS'].map { |e| "#{e}=#{env[e]}" }.join(' ')
puts [env_str, Shellwords.shelljoin(command)].compact.join(' ')
end

def execute_command_and_capture_output(env, cmd, options)
pid = nil

Expand All @@ -119,7 +124,7 @@ def execute_command_and_capture_output(env, cmd, options)

output = "#{Shellwords.shelljoin(cmd)}\n#{output}" if report_process_command?(options) && options[:serialize_stdout]

{ stdout: output, exit_status: exitstatus, command: cmd, seed: seed }
{ env: env, stdout: output, exit_status: exitstatus, command: cmd, seed: seed }
end

def find_results(test_output)
Expand Down Expand Up @@ -286,7 +291,7 @@ def set_unknown_runtime(tests, options)
end

def report_process_command?(options)
options[:verbose] || options[:verbose_process_command]
options[:verbose] || options[:verbose_command]
end
end
end
Expand Down
24 changes: 5 additions & 19 deletions spec/integration_spec.rb
Expand Up @@ -66,8 +66,8 @@ def self.it_runs_the_default_folder_if_it_exists(type, test_folder)
end
end

let(:printed_commands) { /specs? per process\nbundle exec rspec/ }
let(:printed_rerun) { "run the group again:\n\nbundle exec rspec" }
let(:printed_commands) { /specs? per process\nTEST_ENV_NUMBER=(\d+)? PARALLEL_TEST_GROUPS=\d+ bundle exec rspec/ }
let(:printed_rerun) { /run the group again:\n\nTEST_ENV_NUMBER=(\d+)? PARALLEL_TEST_GROUPS=\d+ bundle exec rspec/ }

context "running tests sequentially" do
it "exits with 0 when each run is successful" do
Expand Down Expand Up @@ -231,30 +231,16 @@ def test_unicode
expect(result).to include('Took')
end

it "shows command and rerun with --verbose" do
it "shows command and rerun with --verbose-command" do
write 'spec/xxx_spec.rb', 'describe("it"){it("should"){puts "TEST1"}}'
write 'spec/xxx2_spec.rb', 'describe("it"){it("should"){expect(1).to eq(2)}}'
result = run_tests ["spec", "--verbose"], type: 'rspec', fail: true
result = run_tests ["spec", "--verbose-command"], type: 'rspec', fail: true
expect(result).to match printed_commands
expect(result).to include printed_rerun
expect(result).to match printed_rerun
expect(result).to include "bundle exec rspec spec/xxx_spec.rb"
expect(result).to include "bundle exec rspec spec/xxx2_spec.rb"
end

it "shows only rerun with --verbose-rerun-command" do
write 'spec/xxx_spec.rb', 'describe("it"){it("should"){expect(1).to eq(2)}}'
result = run_tests ["spec", "--verbose-rerun-command"], type: 'rspec', fail: true
expect(result).to include printed_rerun
expect(result).to_not match printed_commands
end

it "shows only process with --verbose-process-command" do
write 'spec/xxx_spec.rb', 'describe("it"){it("should"){expect(1).to eq(2)}}'
result = run_tests ["spec", "--verbose-process-command"], type: 'rspec', fail: true
expect(result).to_not include printed_rerun
expect(result).to match printed_commands
end

it "fails when tests fail" do
write 'spec/xxx_spec.rb', 'describe("it"){it("should"){puts "TEST1"}}'
write 'spec/xxx2_spec.rb', 'describe("it"){it("should"){expect(1).to eq(2)}}'
Expand Down
29 changes: 13 additions & 16 deletions spec/parallel_tests/cli_spec.rb
Expand Up @@ -55,15 +55,9 @@ def call(*args)
expect(call(["test", "--verbose"])).to eq(defaults.merge(verbose: true))
end

it "parses --verbose-process-command" do
expect(call(['test', '--verbose-process-command'])).to eq(
defaults.merge(verbose_process_command: true)
)
end

it "parses --verbose-rerun-command" do
expect(call(['test', '--verbose-rerun-command'])).to eq(
defaults.merge(verbose_rerun_command: true)
it "parses --verbose-command" do
expect(call(['test', '--verbose-command'])).to eq(
defaults.merge(verbose_command: true)
)
end

Expand Down Expand Up @@ -225,6 +219,10 @@ def self.it_prints_nothing_about_rerun_commands(options)
end

describe "failure" do
before do
subject.instance_variable_set(:@runner, ParallelTests::Test::Runner)
end

context 'without options' do
it_prints_nothing_about_rerun_commands({})
end
Expand All @@ -233,19 +231,19 @@ def self.it_prints_nothing_about_rerun_commands(options)
it_prints_nothing_about_rerun_commands(verbose: false)
end

context "with verbose rerun" do
context "with verbose command" do
it "prints command if there is a failure" do
expect do
subject.send(:report_failure_rerun_commmand, single_failed_command, verbose_rerun_command: true)
end.to output("\n\nTests have failed for a parallel_test group. Use the following command to run the group again:\n\nfoo\n").to_stdout
subject.send(:report_failure_rerun_commmand, single_failed_command, verbose_command: true)
end.to output("\n\nTests have failed for a parallel_test group. Use the following command to run the group again:\n\nTEST_ENV_NUMBER= PARALLEL_TEST_GROUPS= foo\n").to_stdout
end
end

context 'with verbose' do
it "prints a message and the command if there is a failure" do
expect do
subject.send(:report_failure_rerun_commmand, single_failed_command, verbose: true)
end.to output("\n\nTests have failed for a parallel_test group. Use the following command to run the group again:\n\nfoo\n").to_stdout
end.to output("\n\nTests have failed for a parallel_test group. Use the following command to run the group again:\n\nTEST_ENV_NUMBER= PARALLEL_TEST_GROUPS= foo\n").to_stdout
end

it "prints multiple commands if there are multiple failures" do
Expand All @@ -259,7 +257,7 @@ def self.it_prints_nothing_about_rerun_commands(options)
],
{ verbose: true }
)
end.to output(/foo\nbar\nbaz/).to_stdout
end.to output(/\sfoo\n.+?\sbar\n.+?\sbaz/).to_stdout
end

it "only includes failures" do
Expand All @@ -273,14 +271,13 @@ def self.it_prints_nothing_about_rerun_commands(options)
],
{ verbose: true }
)
end.to output(/foo --color\nbaz/).to_stdout
end.to output(/\sfoo --color\n.+?\sbaz/).to_stdout
end

it "prints the command with the seed added by the runner" do
command = ['rspec', '--color', 'spec/foo_spec.rb']
seed = 555

subject.instance_variable_set(:@runner, ParallelTests::Test::Runner)
expect(ParallelTests::Test::Runner).to receive(:command_with_seed).with(command, seed)
.and_return(['my', 'seeded', 'command', 'result', '--seed', seed])
single_failed_command[0].merge!(seed: seed, command: command)
Expand Down

0 comments on commit fc09051

Please sign in to comment.