Skip to content

Commit

Permalink
Add --require rails_helper to cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacyfik committed Mar 7, 2024
1 parent 87394d7 commit a965543
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 deletions.
8 changes: 8 additions & 0 deletions lib/knapsack_pro/adapters/rspec_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def self.has_format_option?(cli_args)
!!parsed_options(cli_args)&.[](:formatters)
end

def self.has_require_rails_helper_option?(cli_args)
(parsed_options(cli_args)&.[](:requires) || []).include?("rails_helper")
end

def self.order_option(cli_args)
parsed_options(cli_args)&.[](:order)
end
Expand All @@ -76,6 +80,10 @@ def self.parse_file_path(id)
id.match(/\A(.*?)(?:\[([\d\s:,]+)\])?\z/).captures.first
end

def self.rails_helper_exists?
File.exist?('spec/rails_helper.rb')
end

# private
def self.top_level_group(example)
group = example.metadata[:example_group]
Expand Down
10 changes: 9 additions & 1 deletion lib/knapsack_pro/pure/queue/rspec_pure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ def args_with_seed_option_added_when_viable(order_option, seed, args)
args + ['--seed', seed.value]
end

def prepare_cli_args(args, has_format_option, test_dir)
def prepare_cli_args(args, has_format_option, has_require_rails_helper_option, rails_helper_exists, test_dir)
(args || '').split
.yield_self { args_with_at_least_one_formatter(_1, has_format_option) }
.yield_self { args_with_require_rails_helper_if_needed(_1, has_require_rails_helper_option, rails_helper_exists) }
.yield_self { args_with_default_options(_1, test_dir) }
end

Expand Down Expand Up @@ -75,6 +76,13 @@ def args_with_at_least_one_formatter(cli_args, has_format_option)
cli_args + ['--format', 'progress']
end

def args_with_require_rails_helper_if_needed(cli_args, has_require_rails_helper_option, rails_helper_exists)
return cli_args if has_require_rails_helper_option
return cli_args unless rails_helper_exists

cli_args + ['--require', 'rails_helper']
end

def args_with_default_options(cli_args, test_dir)
new_cli_args = cli_args + [
'--default-path', test_dir,
Expand Down
4 changes: 3 additions & 1 deletion lib/knapsack_pro/runners/queue/rspec_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def initialize(adapter_class, rspec_pure, args, stream_error, stream_out)
@adapter_class = adapter_class
@rspec_pure = rspec_pure
has_format_option = @adapter_class.has_format_option?((args || '').split)
@cli_args = rspec_pure.prepare_cli_args(args, has_format_option, test_dir)
has_require_rails_helper_option = @adapter_class.has_require_rails_helper_option?((args || '').split)
rails_helper_exists = @adapter_class.rails_helper_exists?
@cli_args = rspec_pure.prepare_cli_args(args, has_format_option, has_require_rails_helper_option, rails_helper_exists, test_dir)
@stream_error = stream_error
@stream_out = stream_out
@node_test_file_paths = []
Expand Down
48 changes: 48 additions & 0 deletions spec/knapsack_pro/adapters/rspec_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,54 @@
end
end

describe '.has_require_rails_helper_option?' do
subject { described_class.has_require_rails_helper_option?(cli_args) }

context 'when require option is provided as -r' do
let(:cli_args) { ['-r', 'rails_helper'] }

it { expect(subject).to be true }
end

context 'when require option is provided as --require' do
let(:cli_args) { ['--require', 'rails_helper'] }

it { expect(subject).to be true }
end

context 'when require option is provided without delimiter' do
let(:cli_args) { ['-rrails_helper'] }

it { expect(subject).to be true }
end

context 'when require option is not provided' do
let(:cli_args) { ['--fake', 'value'] }

it { expect(subject).to be false }
end
end

describe '.rails_helper_exists?' do
subject { described_class.rails_helper_exists? }

before do
expect(File).to receive(:exist?).with('spec/rails_helper.rb').and_return file_exists
end

context 'when rails_helper exists' do
let(:file_exists) { true }

it { expect(subject).to be true }
end

context 'when rails_helper does not exist' do
let(:file_exists) { false }

it { expect(subject).to be false }
end
end

describe '.order_option' do
subject { described_class.order_option(cli_args) }

Expand Down
42 changes: 33 additions & 9 deletions spec/knapsack_pro/pure/queue/rspec_pure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,52 @@
end

describe '#prepare_cli_args' do
subject { rspec_pure.prepare_cli_args(args, has_format_option, test_dir) }
subject { rspec_pure.prepare_cli_args(args, has_format_option, has_require_rails_helper_option, rails_helper_exists, test_dir) }

context 'when no args' do
let(:args) { nil }
let(:has_format_option) { false }
let(:has_require_rails_helper_option) { false }
let(:test_dir) { 'spec' }

it 'adds the default progress formatter and the default path and the time tracker formatter' do
expect(subject).to eq [
'--format', 'progress',
'--default-path', 'spec',
'--format', 'KnapsackPro::Formatters::TimeTracker',
]
context "when rails_helper doesn't exist" do
let(:rails_helper_exists) { false }

it 'adds the default progress formatter, the default path and the time tracker formatter, does not add require rails_helper' do
expect(subject).to eq [
'--format', 'progress',
'--default-path', 'spec',
'--format', 'KnapsackPro::Formatters::TimeTracker',
]
end
end

context "when rails_helper exists" do
let(:rails_helper_exists) { true }

it 'adds the default progress formatter, require rails_helper, the default path and the time tracker formatter' do
expect(subject).to eq [
'--format', 'progress',
'--require', 'rails_helper',
'--default-path', 'spec',
'--format', 'KnapsackPro::Formatters::TimeTracker',
]
end
end
end

context 'when args are present and a custom test directory is set' do
let(:args) { '--color --profile' }
let(:args) { '--color --profile --require rails_helper' }
let(:has_format_option) { false }
let(:has_require_rails_helper_option) { true }
let(:rails_helper_exists) { true }
let(:test_dir) { 'custom_spec_dir' }

it do
expect(subject).to eq [
'--color',
'--profile',
'--require', 'rails_helper',
'--format', 'progress',
'--default-path', 'custom_spec_dir',
'--format', 'KnapsackPro::Formatters::TimeTracker',
Expand All @@ -150,15 +171,18 @@
end

context 'when args are present and has format option' do
let(:args) { '--color --profile --format d' }
let(:args) { '--color --profile --format d --require rails_helper' }
let(:has_format_option) { true }
let(:has_require_rails_helper_option) { true }
let(:rails_helper_exists) { true }
let(:test_dir) { 'spec' }

it 'uses the format option from args instead of the default formatter' do
expect(subject).to eq [
'--color',
'--profile',
'--format', 'd',
'--require', 'rails_helper',
'--default-path', 'spec',
'--format', 'KnapsackPro::Formatters::TimeTracker',
]
Expand Down

0 comments on commit a965543

Please sign in to comment.