Skip to content

Commit

Permalink
Make TargetRubyVersion take precedence over .ruby-version (rubocop#3261)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjwp authored and Neodelf committed Oct 15, 2016
1 parent ea8b38a commit 6e22e48
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#3248](https://github.com/bbatsov/rubocop/issues/3248): Support 'ruby-' prefix in `.ruby-version`. ([@tjwp][])
* [#3250](https://github.com/bbatsov/rubocop/pull/3250): Make regexp for cop names less restrictive in CommentConfig lines. ([@tjwp][])
* [#3261](https://github.com/bbatsov/rubocop/pull/3261): Prefer `TargetRubyVersion` to `.ruby-version`. ([@tjwp][])

## 0.41.1 (2016-06-26)

Expand Down
7 changes: 5 additions & 2 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ AllCops:
# cache location is secure from this kind of attack, and wish to use a
# symlinked cache location, set this value to "true".
AllowSymlinksInCacheRootDirectory: false
# What version of the Ruby interpreter is the inspected code intended to
# What MRI version of the Ruby interpreter is the inspected code intended to
# run on? (If there is more than one, set this to the lowest version.)
TargetRubyVersion: 2.0
# If a value is specified for TargetRubyVersion then it is used.
# Else if .ruby-version exists and it contains an MRI version it is used.
# Otherwise we fallback to the oldest officially supported Ruby version (2.0).
TargetRubyVersion: ~

# Indent private/protected/public as deep as method definitions
Style/AccessModifierIndentation:
Expand Down
18 changes: 11 additions & 7 deletions lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Config < Hash

COMMON_PARAMS = %w(Exclude Include Severity
AutoCorrect StyleGuide Details).freeze
# 2.0 is the oldest officially supported Ruby version.
DEFAULT_RUBY_VERSION = 2.0
KNOWN_RUBIES = [1.9, 2.0, 2.1, 2.2, 2.3].freeze
OBSOLETE_COPS = {
'Style/TrailingComma' =>
Expand Down Expand Up @@ -196,16 +198,18 @@ def base_dir_for_path_parameters

def target_ruby_version
@target_ruby_version ||=
if File.file?('.ruby-version')
@target_ruby_version_source = :dot_ruby_version

/(ruby-)?(?<ruby_version>\d.\d)/ =~ File.read('.ruby-version')

ruby_version.to_f if ruby_version
else
if for_all_cops['TargetRubyVersion']
@target_ruby_version_source = :rubocop_yml

for_all_cops['TargetRubyVersion']
elsif File.file?('.ruby-version') &&
/\A(ruby-)?(?<version>\d+\.\d+)/ =~ File.read('.ruby-version')

@target_ruby_version_source = :dot_ruby_version

version.to_f
else
DEFAULT_RUBY_VERSION
end
end

Expand Down
118 changes: 88 additions & 30 deletions spec/rubocop/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,34 +376,8 @@
end

describe '#target_ruby_version' do
context 'when .ruby-version is present' do
let(:ruby_version) { '2.2.4' }
let(:ruby_version_to_f) { 2.2 }

before do
allow(File).to receive(:file?).with('.ruby-version').and_return true
allow(File)
.to receive(:read)
.with('.ruby-version')
.and_return ruby_version
end

it 'reads it to determine the target ruby version' do
expect(configuration.target_ruby_version).to eq ruby_version_to_f
end

context 'when ruby version is prefixed by "ruby-"' do
let(:ruby_version) { 'ruby-2.3.0' }
let(:ruby_version_to_f) { 2.3 }

it 'correctly determines the target ruby version' do
expect(configuration.target_ruby_version).to eq ruby_version_to_f
end
end
end

context 'when .ruby-version is not present' do
let(:ruby_version) { 2.0 }
context 'when TargetRubyVersion is set' do
let(:ruby_version) { 2.1 }

let(:hash) do
{
Expand All @@ -414,12 +388,96 @@
end

before do
allow(File).to receive(:file?).with('.ruby-version').and_return false
allow(File).to receive(:file?).with('.ruby-version')
end

it 'falls back to TargetRubyVersion' do
it 'uses TargetRubyVersion' do
expect(configuration.target_ruby_version).to eq ruby_version
end

it 'does not read .ruby-version' do
configuration.target_ruby_version
expect(File).not_to have_received(:file?).with('.ruby-version')
end
end

context 'when TargetRubyVersion is not set' do
context 'when .ruby-version is present' do
before do
allow(File).to receive(:file?).with('.ruby-version').and_return true
allow(File)
.to receive(:read)
.with('.ruby-version')
.and_return ruby_version
end

context 'when .ruby-version contains an MRI version' do
let(:ruby_version) { '2.2.4' }
let(:ruby_version_to_f) { 2.2 }

it 'reads it to determine the target ruby version' do
expect(configuration.target_ruby_version).to eq ruby_version_to_f
end
end

context 'when the MRI version contains multiple digits' do
let(:ruby_version) { '10.11.0' }
let(:ruby_version_to_f) { 10.11 }

it 'reads it to determine the target ruby version' do
expect(configuration.target_ruby_version).to eq ruby_version_to_f
end
end

context 'when .ruby-version contains a version prefixed by "ruby-"' do
let(:ruby_version) { 'ruby-2.3.0' }
let(:ruby_version_to_f) { 2.3 }

it 'correctly determines the target ruby version' do
expect(configuration.target_ruby_version).to eq ruby_version_to_f
end
end

context 'when .ruby-version contains a JRuby version' do
let(:ruby_version) { 'jruby-9.1.2.0' }

it 'uses the default target ruby version' do
expect(configuration.target_ruby_version)
.to eq described_class::DEFAULT_RUBY_VERSION
end
end

context 'when .ruby-version contains a Rbx version' do
let(:ruby_version) { 'rbx-3.42' }

it 'uses the default target ruby version' do
expect(configuration.target_ruby_version)
.to eq described_class::DEFAULT_RUBY_VERSION
end
end

context 'when .ruby-version contains "system" version' do
let(:ruby_version) { 'system' }

it 'uses the default target ruby version' do
expect(configuration.target_ruby_version)
.to eq described_class::DEFAULT_RUBY_VERSION
end
end
end

context 'when .ruby-version is not present' do
let(:ruby_version) { described_class::DEFAULT_RUBY_VERSION }

before do
allow(File).to receive(:file?).with('.ruby-version').and_return false
end

it 'uses the default target ruby version' do
expect(configuration.target_ruby_version)
.to eq described_class::DEFAULT_RUBY_VERSION
end
end
end
end
end

0 comments on commit 6e22e48

Please sign in to comment.