Skip to content

Commit

Permalink
[Fix #5694] Match Rails versions with multiple digits
Browse files Browse the repository at this point in the history
  • Loading branch information
roberts1000 authored and bbatsov committed Mar 18, 2018
1 parent 862a594 commit 902491c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@
* [#5436](https://github.com/bbatsov/rubocop/issues/5436): Allow empty kwrest args in UncommunicativeName cops. ([@pocke][])
* [#5674](https://github.com/bbatsov/rubocop/issues/5674): Fix auto-correction of `Layout/EmptyComment` when the empty comment appears on the same line as code. ([@rrosenblum][])
* [#5679](https://github.com/bbatsov/rubocop/pull/5679): Fix a false positive for `Style/EmptyLineAfterGuardClause` when guard clause is before `rescue` or `ensure`. ([@koic][])
* [#5694](https://github.com/bbatsov/rubocop/issues/5694): Match Rails versions with multiple digits when reading the TargetRailsVersion from the bundler lock files. ([@roberts1000][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config.rb
Expand Up @@ -626,7 +626,7 @@ def read_rails_version_from_bundler_lock_file
File.foreach(lock_file_path) do |line|
# If rails is in Gemfile.lock or gems.lock, there should be a line like:
# rails (X.X.X)
result = line.match(/^\s+rails\s+\((\d\.\d\.\d)/)
result = line.match(/^\s+rails\s+\((\d+\.\d+)/)
return result.captures.first.to_f if result
end
end
Expand Down
60 changes: 59 additions & 1 deletion spec/rubocop/config_spec.rb
Expand Up @@ -678,7 +678,7 @@ def cop_enabled(cop_class)
let(:base_path) { configuration.base_dir_for_path_parameters }
let(:lock_file_path) { File.join(base_path, file_name) }

it 'uses the Rails version when Rails is present in the lock file' do
it "uses the single digit Rails version in #{file_name}" do
content =
<<-HEREDOC
GEM
Expand Down Expand Up @@ -712,6 +712,64 @@ def cop_enabled(cop_class)
expect(configuration.target_rails_version).to eq 4.1
end

it "uses the multi digit Rails version in #{file_name}" do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
mail (~> 2.5.4)
rails (400.33.22)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
activemodel (= 4.1.0)
activerecord (= 4.1.0)
activesupport (= 4.1.0)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
sprockets-rails (~> 2.0)
PLATFORMS
ruby
DEPENDENCIES
rails (= 900.88.77)
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(configuration.target_rails_version).to eq 400.33
end

it "does not use the DEPENDENCIES Rails version in #{file_name}" do
content =
<<-HEREDOC
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
mail (~> 2.5.4)
PLATFORMS
ruby
DEPENDENCIES
rails (= 900.88.77)
BUNDLED WITH
1.16.1
HEREDOC
create_file(lock_file_path, content)
expect(configuration.target_rails_version).not_to eq 900.88
end

it "uses the default Rails when Rails is not in #{file_name}" do
content =
<<-HEREDOC
Expand Down

0 comments on commit 902491c

Please sign in to comment.