From 0dd78219f01c88b2c1479f705d831549d06eebba Mon Sep 17 00:00:00 2001 From: Brian Nystrom and Kyle Annen Date: Mon, 4 Feb 2019 10:26:14 -0600 Subject: [PATCH] add exclude pattern support --- Readme.md | 1 + lib/parallel_tests/cli.rb | 1 + lib/parallel_tests/test/runner.rb | 15 ++++++++++++--- spec/fixtures/rails51/Gemfile.lock | 4 ++-- spec/fixtures/rails52/Gemfile.lock | 4 ++-- spec/integration_spec.rb | 10 ++++++++++ spec/parallel_tests/cli_spec.rb | 4 ++++ 7 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Readme.md b/Readme.md index 1c1f6e0b..34befbf1 100644 --- a/Readme.md +++ b/Readme.md @@ -195,6 +195,7 @@ Options are: -n [PROCESSES] How many processes to use, default: available CPUs -p, --pattern [PATTERN] run tests matching this regex pattern + --exclude-pattern [PATTERN] exclude tests matching this regex pattern --group-by [TYPE] group tests by: found - order of finding files steps - number of cucumber/spinach steps diff --git a/lib/parallel_tests/cli.rb b/lib/parallel_tests/cli.rb index bb506e15..a7bda591 100644 --- a/lib/parallel_tests/cli.rb +++ b/lib/parallel_tests/cli.rb @@ -164,6 +164,7 @@ def parse_options!(argv) BANNER opts.on("-n [PROCESSES]", Integer, "How many processes to use, default: available CPUs") { |n| options[:count] = n } opts.on("-p", "--pattern [PATTERN]", "run tests matching this regex pattern") { |pattern| options[:pattern] = /#{pattern}/ } + opts.on("--exclude-pattern", "--exclude-pattern [PATTERN]", "exclude tests matching this regex pattern") { |pattern| options[:exclude_pattern] = /#{pattern}/ } opts.on("--group-by [TYPE]", <<-TEXT.gsub(/^ /, '') group tests by: found - order of finding files diff --git a/lib/parallel_tests/test/runner.rb b/lib/parallel_tests/test/runner.rb index e7b379e1..bd355b48 100644 --- a/lib/parallel_tests/test/runner.rb +++ b/lib/parallel_tests/test/runner.rb @@ -209,14 +209,23 @@ def sort_by_filesize(tests) end def find_tests(tests, options = {}) - (tests || []).map do |file_or_folder| + files_list = (tests || []).map do |file_or_folder| if File.directory?(file_or_folder) files = files_in_folder(file_or_folder, options) - files.grep(options[:suffix]||test_suffix).grep(options[:pattern]||//) + files.grep(options[:suffix] || test_suffix) + .grep(options[:pattern] || //) else file_or_folder end - end.flatten.uniq + end + + flat_files_list = files_list.uniq.flat_map { |i| i } + + if regex = options[:exclude_pattern] + flat_files_list.reject! { |file| file =~ regex } + end + + flat_files_list end def files_in_folder(folder, options={}) diff --git a/spec/fixtures/rails51/Gemfile.lock b/spec/fixtures/rails51/Gemfile.lock index c8346cd3..fd386b0b 100644 --- a/spec/fixtures/rails51/Gemfile.lock +++ b/spec/fixtures/rails51/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../../.. specs: - parallel_tests (2.26.0) + parallel_tests (2.27.1) parallel GEM @@ -65,7 +65,7 @@ GEM nio4r (2.3.1) nokogiri (1.8.5) mini_portile2 (~> 2.3.0) - parallel (1.12.1) + parallel (1.13.0) rack (2.0.5) rack-test (1.1.0) rack (>= 1.0, < 3) diff --git a/spec/fixtures/rails52/Gemfile.lock b/spec/fixtures/rails52/Gemfile.lock index b204541b..c1035324 100644 --- a/spec/fixtures/rails52/Gemfile.lock +++ b/spec/fixtures/rails52/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../../.. specs: - parallel_tests (2.26.0) + parallel_tests (2.27.1) parallel GEM @@ -72,7 +72,7 @@ GEM nio4r (2.3.1) nokogiri (1.8.5) mini_portile2 (~> 2.3.0) - parallel (1.12.1) + parallel (1.13.0) rack (2.0.5) rack-test (1.1.0) rack (>= 1.0, < 3) diff --git a/spec/integration_spec.rb b/spec/integration_spec.rb index 7ffc7de4..2c636663 100644 --- a/spec/integration_spec.rb +++ b/spec/integration_spec.rb @@ -287,6 +287,16 @@ def test_unicode expect(result).to include('ZZZ') end + it "excludes test by given pattern and relative paths" do + write "spec/x_spec.rb", "puts 'XXX'" + write "spec/acceptance/y_spec.rb", "puts 'YYY'" + write "spec/integration/z_spec.rb", "puts 'ZZZ'" + result = run_tests "spec", :add => "--exclude-pattern 'spec/(integration|acceptance)'", :type => "rspec" + expect(result).to include('XXX') + expect(result).not_to include('YYY') + expect(result).not_to include('ZZZ') + end + it "can wait_for_other_processes_to_finish" do skip if RUBY_PLATFORM == "java" # just too slow ... write "test/a_test.rb", "require 'parallel_tests'; sleep 0.5 ; ParallelTests.wait_for_other_processes_to_finish; puts 'OutputA'" diff --git a/spec/parallel_tests/cli_spec.rb b/spec/parallel_tests/cli_spec.rb index aa14023c..27b513e5 100644 --- a/spec/parallel_tests/cli_spec.rb +++ b/spec/parallel_tests/cli_spec.rb @@ -21,6 +21,10 @@ def call(*args) expect(call(["--exec", "echo"])).to eq(execute: "echo") end + it "parses excludes pattern" do + expect(call(["--exclude-pattern", "spec/"])).to eq(exclude_pattern: /spec\//) + end + it "parses regular count" do expect(call(["test", "-n3"])).to eq(defaults.merge(:count => 3)) end