Skip to content

Commit 4a6ea88

Browse files
committed
feat: default KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true
1 parent 5585ad9 commit 4a6ea88

File tree

5 files changed

+47
-54
lines changed

5 files changed

+47
-54
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
### Unreleased (Major)
4+
5+
* Enable [`KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES`](https://docs.knapsackpro.com/ruby/split-by-test-examples/) by default
6+
* This should improve the speed of your builds, but you can disable it with [`KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=false`](https://docs.knapsackpro.com/ruby/reference/#knapsack_pro_rspec_split_by_test_examples-rspec)
7+
8+
https://github.com/KnapsackPro/knapsack_pro-ruby/pull/288
9+
310
### 7.14.0
411

512
* Improve debugging for hanging CI nodes: show hanging spec files in the RSpec output and a command to reproduce the current batch of tests.

lib/knapsack_pro/config/env.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ def cucumber_queue_prefix
191191
def rspec_split_by_test_examples?
192192
return @rspec_split_by_test_examples if defined?(@rspec_split_by_test_examples)
193193

194-
split = ENV.fetch('KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES', false).to_s == 'true'
194+
env = ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES']
195+
196+
if defined?(::Turnip) && env.nil?
197+
KnapsackPro.logger.warn("You have required turnip so SBTE is off. If you don't use turnip you can enable SBTE. Read more: LINK")
198+
return (@rspec_split_by_test_examples = false)
199+
end
200+
201+
split = (env || true).to_s == 'true'
195202

196203
if split && ci_node_total < 2
197204
KnapsackPro.logger.debug('Skipping split of test files by test examples because you are running tests on a single CI node (no parallelism)')

lib/knapsack_pro/formatters/time_tracker.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def path_for(example)
121121
def rspec_split_by_test_example?(file)
122122
return false unless KnapsackPro::Config::Env.rspec_split_by_test_examples?
123123
return false unless KnapsackPro::Adapters::RSpecAdapter.slow_test_file?(KnapsackPro::Adapters::RSpecAdapter, file)
124+
124125
true
125126
end
126127

spec/integration/runners/queue/rspec_runner_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,16 +1961,13 @@ def when_first_matching_example_defined(type:)
19611961

19621962
context 'when the RSpec split by examples is enabled' do
19631963
before do
1964-
ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES'] = 'true'
1965-
19661964
# remember to stub Queue API batches to include test examples (example: a_spec.rb[1:1])
19671965
# for the following slow test files
19681966
ENV['KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN'] = "#{SPEC_DIRECTORY}/a_spec.rb"
19691967

19701968
ENV['KNAPSACK_PRO_CI_NODE_TOTAL'] = '2'
19711969
end
19721970
after do
1973-
ENV.delete('KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES')
19741971
ENV.delete('KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN')
19751972
ENV.delete('KNAPSACK_PRO_CI_NODE_TOTAL')
19761973
end
@@ -2060,16 +2057,13 @@ def when_first_matching_example_defined(type:)
20602057

20612058
context 'when the RSpec split by examples is enabled AND --tag is set' do
20622059
before do
2063-
ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES'] = 'true'
2064-
20652060
# remember to stub Queue API batches to include test examples (example: a_spec.rb[1:1])
20662061
# for the following slow test files
20672062
ENV['KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN'] = "#{SPEC_DIRECTORY}/a_spec.rb"
20682063

20692064
ENV['KNAPSACK_PRO_CI_NODE_TOTAL'] = '2'
20702065
end
20712066
after do
2072-
ENV.delete('KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES')
20732067
ENV.delete('KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN')
20742068
ENV.delete('KNAPSACK_PRO_CI_NODE_TOTAL')
20752069
end
@@ -2141,16 +2135,13 @@ def when_first_matching_example_defined(type:)
21412135
let(:json_file) { "#{SPEC_DIRECTORY}/rspec.json" }
21422136

21432137
before do
2144-
ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES'] = 'true'
2145-
21462138
# remember to stub Queue API batches to include test examples (example: a_spec.rb[1:1])
21472139
# for the following slow test files
21482140
ENV['KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN'] = "#{SPEC_DIRECTORY}/a_spec.rb"
21492141

21502142
ENV['KNAPSACK_PRO_CI_NODE_TOTAL'] = '2'
21512143
end
21522144
after do
2153-
ENV.delete('KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES')
21542145
ENV.delete('KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN')
21552146
ENV.delete('KNAPSACK_PRO_CI_NODE_TOTAL')
21562147
end
@@ -2243,16 +2234,13 @@ def when_first_matching_example_defined(type:)
22432234
let(:xml_file) { "#{SPEC_DIRECTORY}/rspec.xml" }
22442235

22452236
before do
2246-
ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES'] = 'true'
2247-
22482237
# remember to stub Queue API batches to include test examples (example: a_spec.rb[1:1])
22492238
# for the following slow test files
22502239
ENV['KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN'] = "#{SPEC_DIRECTORY}/a_spec.rb"
22512240

22522241
ENV['KNAPSACK_PRO_CI_NODE_TOTAL'] = '2'
22532242
end
22542243
after do
2255-
ENV.delete('KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES')
22562244
ENV.delete('KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN')
22572245
ENV.delete('KNAPSACK_PRO_CI_NODE_TOTAL')
22582246
end
@@ -2343,16 +2331,13 @@ def when_first_matching_example_defined(type:)
23432331
let(:coverage_file) { "#{coverage_dir}/index.html" }
23442332

23452333
before do
2346-
ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES'] = 'true'
2347-
23482334
# remember to stub Queue API batches to include test examples (example: a_spec.rb[1:1])
23492335
# for the following slow test files
23502336
ENV['KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN'] = "#{SPEC_DIRECTORY}/a_spec.rb"
23512337

23522338
ENV['KNAPSACK_PRO_CI_NODE_TOTAL'] = '2'
23532339
end
23542340
after do
2355-
ENV.delete('KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES')
23562341
ENV.delete('KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN')
23572342
ENV.delete('KNAPSACK_PRO_CI_NODE_TOTAL')
23582343
end

spec/knapsack_pro/config/env_spec.rb

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,53 +1014,46 @@
10141014
end
10151015

10161016
describe '.rspec_split_by_test_examples?' do
1017-
subject { described_class.rspec_split_by_test_examples? }
1018-
10191017
after(:each) do
10201018
described_class.remove_instance_variable(:@rspec_split_by_test_examples)
10211019
end
10221020

1023-
context 'when KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true AND KNAPSACK_PRO_CI_NODE_TOTAL >= 2' do
1024-
before do
1025-
stub_const("ENV", { 'KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES' => 'true', 'KNAPSACK_PRO_CI_NODE_TOTAL' => '2' })
1026-
expect(KnapsackPro).not_to receive(:logger)
1027-
end
1028-
1029-
it { should be true }
1030-
end
1031-
1032-
context 'when KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=false AND KNAPSACK_PRO_CI_NODE_TOTAL >= 2' do
1033-
before do
1034-
stub_const("ENV", { 'KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES' => 'false', 'KNAPSACK_PRO_CI_NODE_TOTAL' => '2' })
1035-
expect(KnapsackPro).not_to receive(:logger)
1036-
end
1037-
1038-
it { should be false }
1039-
end
1040-
1041-
context 'when KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=true AND KNAPSACK_PRO_CI_NODE_TOTAL < 2' do
1042-
before { stub_const("ENV", { 'KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES' => 'true', 'KNAPSACK_PRO_CI_NODE_TOTAL' => '1' }) }
1043-
1044-
it { should be false }
1021+
[
1022+
[ "false", "2", :turnip, false ],
1023+
[ "false", "2", nil, false ],
1024+
[ "true", "2", :turnip, true ],
1025+
[ "true", "2", nil, true ],
1026+
[ nil, "2", :turnip, false, :warn, "You have required turnip so SBTE is off. If you don't use turnip you can enable SBTE. Read more: LINK" ],
1027+
[ nil, "2", nil, true ],
1028+
[ "false", "1", :turnip, false ],
1029+
[ "false", "1", nil, false ],
1030+
[ "true", "1", :turnip, false, :debug, "Skipping split of test files by test examples because you are running tests on a single CI node (no parallelism)" ],
1031+
[ "true", "1", nil, false, :debug, "Skipping split of test files by test examples because you are running tests on a single CI node (no parallelism)" ],
1032+
[ nil, "1", :turnip, false, :warn, "You have required turnip so SBTE is off. If you don't use turnip you can enable SBTE. Read more: LINK" ],
1033+
[ nil, "1", nil, false, :debug, "Skipping split of test files by test examples because you are running tests on a single CI node (no parallelism)" ],
1034+
1035+
].each do |sbte, node_total, turnip, expected, log_level, log_message|
1036+
context "KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES=#{sbte.inspect} AND KNAPSACK_PRO_CI_NODE_TOTAL=#{node_total.inspect} AND :Turnip is #{turnip ? "defined" : "not defined"}" do
1037+
before do
1038+
stub_const("ENV", { 'KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES' => sbte, 'KNAPSACK_PRO_CI_NODE_TOTAL' => node_total }.compact)
1039+
module Turnip; end if turnip
10451040

1046-
context 'when called twice' do
1047-
it 'logs a debug message only once' do
1048-
logger = instance_double(Logger)
1049-
expect(KnapsackPro).to receive(:logger).and_return(logger)
1050-
expect(logger).to receive(:debug).with('Skipping split of test files by test examples because you are running tests on a single CI node (no parallelism)')
1041+
if log_level && log_message
1042+
logger = instance_double(Logger)
1043+
expect(KnapsackPro).to receive(:logger).and_return(logger)
1044+
expect(logger).to receive(log_level).once.with(log_message)
1045+
end
1046+
end
10511047

1052-
2.times { described_class.rspec_split_by_test_examples? }
1048+
after do
1049+
Object.send(:remove_const, :Turnip) if turnip
10531050
end
1054-
end
1055-
end
10561051

1057-
context "when ENV doesn't exist" do
1058-
before do
1059-
stub_const("ENV", {})
1060-
expect(KnapsackPro).not_to receive(:logger)
1052+
it do
1053+
expect(described_class.rspec_split_by_test_examples?).to eq(expected)
1054+
expect(described_class.rspec_split_by_test_examples?).to eq(expected)
1055+
end
10611056
end
1062-
1063-
it { should be false }
10641057
end
10651058
end
10661059

0 commit comments

Comments
 (0)