Skip to content

Commit

Permalink
Merge pull request #65 from koic/import_target_rails_version_spec_fro…
Browse files Browse the repository at this point in the history
…m_rubocop_core

Import `target_rails_version` spec from RuboCop core
  • Loading branch information
koic authored Jun 4, 2019
2 parents 71915e3 + 9c0bd68 commit f3ab72e
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
178 changes: 178 additions & 0 deletions spec/rubocop/config_spec.rb
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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rubocop-rails'
require 'rubocop/rspec/support'
require_relative 'support/file_helper'
require_relative 'support/shared_contexts'

if ENV['COVERAGE'] == 'true'
require 'simplecov'
Expand Down
25 changes: 25 additions & 0 deletions spec/support/file_helper.rb
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
13 changes: 13 additions & 0 deletions spec/support/shared_contexts.rb
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

0 comments on commit f3ab72e

Please sign in to comment.