Skip to content

Commit

Permalink
Support passing the target ruby version through an environment variable
Browse files Browse the repository at this point in the history
Add a new search method for determining the target ruby version: the
RUBOCOP_TARGET_RUBY_VERSION environment variable. This is now the first
source checked, allowing it to override all other sources of the target
ruby version.
  • Loading branch information
trevor-stripe authored and bbatsov committed Dec 20, 2024
1 parent 90593d4 commit f9e9c01
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13607](/~https://github.com/rubocop/rubocop/pull/13607): Support passing the target ruby version through an environment variable. ([@elliottt][])
7 changes: 7 additions & 0 deletions docs/modules/ROOT/pages/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,13 @@ check your project for a series of other files where the Ruby version may be
specified already. The files that will be checked are (in this order):
`*.gemspec`, `.ruby-version`, `.tool-versions`, and `Gemfile.lock`.

The target ruby version may also be specified by setting the
`RUBOCOP_TARGET_RUBY_VERSION` environment variable to the desired version: for
example, running `RUBOCOP_TARGET_RUBY_VERSION=3.3 rubocop` will
run rubocop with a target ruby version of 3.3. Using this environment variable
will override all other sources of version information, including
`.rubocop.yml`.

If a target Ruby version cannot be found via any of the above sources, then a
default target Ruby version will be used.

Expand Down
15 changes: 15 additions & 0 deletions lib/rubocop/target_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def to_s
end
end

# The target ruby version may be configured by setting the
# `RUBOCOP_TARGET_RUBY_VERSION` environment variable.
class RuboCopEnvVar < Source
def name
'`RUBOCOP_TARGET_RUBY_VERSION` environment variable'
end

private

def find_version
ENV.fetch('RUBOCOP_TARGET_RUBY_VERSION', nil)&.to_f
end
end

# The target ruby version may be configured in RuboCop's config.
# @api private
class RuboCopConfig < Source
Expand Down Expand Up @@ -246,6 +260,7 @@ def self.supported_versions
end

SOURCES = [
RuboCopEnvVar,
RuboCopConfig,
GemspecFile,
RubyVersionFile,
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/target_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,41 @@
end
end

context 'when RUBOCOP_TARGET_RUBY_VERSION is set' do
it 'uses RUBOCOP_TARGET_RUBY_VERSION' do
old_version = ENV.fetch('RUBOCOP_TARGET_RUBY_VERSION', nil)
begin
ENV['RUBOCOP_TARGET_RUBY_VERSION'] = '2.7'
expect(target_ruby.version).to eq 2.7
ensure
ENV['RUBOCOP_TARGET_RUBY_VERSION'] = old_version
end
end

it 'does not read .ruby-version' do
expect(File).not_to receive(:file?).with('.ruby-version')
old_version = ENV.fetch('RUBOCOP_TARGET_RUBY_VERSION', nil)
begin
ENV['RUBOCOP_TARGET_RUBY_VERSION'] = '2.7'
expect(target_ruby.version).to eq 2.7
ensure
ENV['RUBOCOP_TARGET_RUBY_VERSION'] = old_version
end
end

it 'does not read Gemfile.lock or gems.locked' do
expect(File).not_to receive(:file?).with('Gemfile')
expect(File).not_to receive(:file?).with('gems.locked')
old_version = ENV.fetch('RUBOCOP_TARGET_RUBY_VERSION', nil)
begin
ENV['RUBOCOP_TARGET_RUBY_VERSION'] = '2.7'
expect(target_ruby.version).to eq 2.7
ensure
ENV['RUBOCOP_TARGET_RUBY_VERSION'] = old_version
end
end
end

context 'when TargetRubyVersion is not set' do
context 'when gemspec file is present' do
let(:base_path) { configuration.base_dir_for_path_parameters }
Expand Down

0 comments on commit f9e9c01

Please sign in to comment.