-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from koic/import_target_rails_version_spec_fro…
…m_rubocop_core Import `target_rails_version` spec from RuboCop core
- Loading branch information
Showing
4 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Config do | ||
include FileHelper | ||
|
||
subject(:configuration) { described_class.new(hash, loaded_path) } | ||
|
||
let(:loaded_path) { 'example/.rubocop.yml' } | ||
|
||
describe '#target_rails_version' do | ||
context 'when TargetRailsVersion is set' do | ||
let(:hash) do | ||
{ | ||
'AllCops' => { | ||
'TargetRailsVersion' => rails_version | ||
} | ||
} | ||
end | ||
|
||
context 'with patch version' do | ||
let(:rails_version) { '5.1.4' } | ||
let(:rails_version_to_f) { 5.1 } | ||
|
||
it 'truncates the patch part and converts to a float' do | ||
expect(configuration.target_rails_version).to eq rails_version_to_f | ||
end | ||
end | ||
|
||
context 'correctly' do | ||
let(:rails_version) { 4.0 } | ||
|
||
it 'uses TargetRailsVersion' do | ||
expect(configuration.target_rails_version).to eq rails_version | ||
end | ||
end | ||
end | ||
|
||
context 'when TargetRailsVersion is not set', :isolated_environment do | ||
let(:hash) do | ||
{ | ||
'AllCops' => {} | ||
} | ||
end | ||
|
||
context 'and lock files do not exist' do | ||
it 'uses the default rails version' do | ||
default = RuboCop::Config::DEFAULT_RAILS_VERSION | ||
expect(configuration.target_rails_version).to eq default | ||
end | ||
end | ||
|
||
['Gemfile.lock', 'gems.locked'].each do |file_name| | ||
context "and #{file_name} exists" do | ||
let(:base_path) { configuration.base_dir_for_path_parameters } | ||
let(:lock_file_path) { File.join(base_path, file_name) } | ||
|
||
it "uses the single 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 (4.1.0) | ||
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 (= 4.1.0) | ||
BUNDLED WITH | ||
1.16.1 | ||
HEREDOC | ||
create_file(lock_file_path, content) | ||
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 | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
addressable (2.5.2) | ||
public_suffix (>= 2.0.2, < 4.0) | ||
ast (2.4.0) | ||
bump (0.5.4) | ||
PLATFORMS | ||
ruby | ||
DEPENDENCIES | ||
bump | ||
bundler (~> 1.3) | ||
BUNDLED WITH | ||
1.16.1 | ||
HEREDOC | ||
create_file(lock_file_path, content) | ||
default = RuboCop::Config::DEFAULT_RAILS_VERSION | ||
expect(configuration.target_rails_version).to eq default | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'fileutils' | ||
|
||
module FileHelper | ||
def create_file(file_path, content) | ||
file_path = File.expand_path(file_path) | ||
|
||
dir_path = File.dirname(file_path) | ||
FileUtils.makedirs dir_path unless File.exist?(dir_path) | ||
|
||
File.open(file_path, 'w') do |file| | ||
case content | ||
when String | ||
file.puts content | ||
when Array | ||
file.puts content.join("\n") | ||
end | ||
end | ||
end | ||
|
||
def create_empty_file(file_path) | ||
create_file(file_path, '') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.shared_context 'with Rails 3', :rails3 do | ||
let(:rails_version) { 3.0 } | ||
end | ||
|
||
RSpec.shared_context 'with Rails 4', :rails4 do | ||
let(:rails_version) { 4.0 } | ||
end | ||
|
||
RSpec.shared_context 'with Rails 5', :rails5 do | ||
let(:rails_version) { 5.0 } | ||
end |