Skip to content

Commit 9c0bd68

Browse files
committed
Import target_rails_version spec from RuboCop core
This PR imports `target_rails_version` spec from RuboCop core. The following warning will be resolved with rubocop/rubocop#7095. ```console % bundle exec rake (snip) WARNING: Shared example group 'with Rails 3' has been previously defined at: /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/rspec/shared_contexts.rb:85 ...and you are now defining it at: /Users/koic/src/github.com/rubocop-hq/rubocop-rails/spec/support/shared_contexts.rb:3 The new definition will overwrite the original one. WARNING: Shared example group 'with Rails 4' has been previously defined at: /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/rspec/shared_contexts.rb:89 ...and you are now defining it at: /Users/koic/src/github.com/rubocop-hq/rubocop-rails/spec/support/shared_contexts.rb:7 The new definition will overwrite the original one. WARNING: Shared example group 'with Rails 5' has been previously defined at: /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/rspec/shared_contexts.rb:93 ...and you are now defining it at: /Users/koic/src/github.com/rubocop-hq/rubocop-rails/spec/support/shared_contexts.rb:11 The new definition will overwrite the original one. /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rspec-core-3.8.0/lib/rspec/core/memoized_helpers.rb:291: warning: method redefined; discarding old rails_version /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/rspec/shared_contexts.rb:86: warning: previous definition of rails_version was here /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rspec-core-3.8.0/lib/rspec/core/memoized_helpers.rb:298: warning: method redefined; discarding old rails_version /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rspec-core-3.8.0/lib/rspec/core/memoized_helpers.rb:298: warning: previous definition of rails_version was here /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rspec-core-3.8.0/lib/rspec/core/memoized_helpers.rb:291: warning: method redefined; discarding old rails_version /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/rspec/shared_contexts.rb:90: warning: previous definition of rails_version was here /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rspec-core-3.8.0/lib/rspec/core/memoized_helpers.rb:298: warning: method redefined; discarding old rails_version /Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rspec-core-3.8.0/lib/rspec/core/memoized_helpers.rb:298: warning: previous definition of rails_version was Here ``` spec/support/file_helper.rb was imported from the following. https://github.com/rubocop-hq/rubocop/blob/v0.71.0/spec/support/file_helper.rb
1 parent 71915e3 commit 9c0bd68

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed

spec/rubocop/config_spec.rb

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Config do
4+
include FileHelper
5+
6+
subject(:configuration) { described_class.new(hash, loaded_path) }
7+
8+
let(:loaded_path) { 'example/.rubocop.yml' }
9+
10+
describe '#target_rails_version' do
11+
context 'when TargetRailsVersion is set' do
12+
let(:hash) do
13+
{
14+
'AllCops' => {
15+
'TargetRailsVersion' => rails_version
16+
}
17+
}
18+
end
19+
20+
context 'with patch version' do
21+
let(:rails_version) { '5.1.4' }
22+
let(:rails_version_to_f) { 5.1 }
23+
24+
it 'truncates the patch part and converts to a float' do
25+
expect(configuration.target_rails_version).to eq rails_version_to_f
26+
end
27+
end
28+
29+
context 'correctly' do
30+
let(:rails_version) { 4.0 }
31+
32+
it 'uses TargetRailsVersion' do
33+
expect(configuration.target_rails_version).to eq rails_version
34+
end
35+
end
36+
end
37+
38+
context 'when TargetRailsVersion is not set', :isolated_environment do
39+
let(:hash) do
40+
{
41+
'AllCops' => {}
42+
}
43+
end
44+
45+
context 'and lock files do not exist' do
46+
it 'uses the default rails version' do
47+
default = RuboCop::Config::DEFAULT_RAILS_VERSION
48+
expect(configuration.target_rails_version).to eq default
49+
end
50+
end
51+
52+
['Gemfile.lock', 'gems.locked'].each do |file_name|
53+
context "and #{file_name} exists" do
54+
let(:base_path) { configuration.base_dir_for_path_parameters }
55+
let(:lock_file_path) { File.join(base_path, file_name) }
56+
57+
it "uses the single digit Rails version in #{file_name}" do
58+
content =
59+
<<-HEREDOC
60+
GEM
61+
remote: https://rubygems.org/
62+
specs:
63+
actionmailer (4.1.0)
64+
actionpack (= 4.1.0)
65+
actionview (= 4.1.0)
66+
mail (~> 2.5.4)
67+
rails (4.1.0)
68+
actionmailer (= 4.1.0)
69+
actionpack (= 4.1.0)
70+
actionview (= 4.1.0)
71+
activemodel (= 4.1.0)
72+
activerecord (= 4.1.0)
73+
activesupport (= 4.1.0)
74+
bundler (>= 1.3.0, < 2.0)
75+
railties (= 4.1.0)
76+
sprockets-rails (~> 2.0)
77+
78+
PLATFORMS
79+
ruby
80+
81+
DEPENDENCIES
82+
rails (= 4.1.0)
83+
84+
BUNDLED WITH
85+
1.16.1
86+
HEREDOC
87+
create_file(lock_file_path, content)
88+
expect(configuration.target_rails_version).to eq 4.1
89+
end
90+
91+
it "uses the multi digit Rails version in #{file_name}" do
92+
content =
93+
<<-HEREDOC
94+
GEM
95+
remote: https://rubygems.org/
96+
specs:
97+
actionmailer (4.1.0)
98+
actionpack (= 4.1.0)
99+
actionview (= 4.1.0)
100+
mail (~> 2.5.4)
101+
rails (400.33.22)
102+
actionmailer (= 4.1.0)
103+
actionpack (= 4.1.0)
104+
actionview (= 4.1.0)
105+
activemodel (= 4.1.0)
106+
activerecord (= 4.1.0)
107+
activesupport (= 4.1.0)
108+
bundler (>= 1.3.0, < 2.0)
109+
railties (= 4.1.0)
110+
sprockets-rails (~> 2.0)
111+
112+
PLATFORMS
113+
ruby
114+
115+
DEPENDENCIES
116+
rails (= 900.88.77)
117+
118+
BUNDLED WITH
119+
1.16.1
120+
HEREDOC
121+
create_file(lock_file_path, content)
122+
expect(configuration.target_rails_version).to eq 400.33
123+
end
124+
125+
it "does not use the DEPENDENCIES Rails version in #{file_name}" do
126+
content =
127+
<<-HEREDOC
128+
GEM
129+
remote: https://rubygems.org/
130+
specs:
131+
actionmailer (4.1.0)
132+
actionpack (= 4.1.0)
133+
actionview (= 4.1.0)
134+
mail (~> 2.5.4)
135+
136+
PLATFORMS
137+
ruby
138+
139+
DEPENDENCIES
140+
rails (= 900.88.77)
141+
142+
BUNDLED WITH
143+
1.16.1
144+
HEREDOC
145+
create_file(lock_file_path, content)
146+
expect(configuration.target_rails_version).not_to eq 900.88
147+
end
148+
149+
it "uses the default Rails when Rails is not in #{file_name}" do
150+
content =
151+
<<-HEREDOC
152+
GEM
153+
remote: https://rubygems.org/
154+
specs:
155+
addressable (2.5.2)
156+
public_suffix (>= 2.0.2, < 4.0)
157+
ast (2.4.0)
158+
bump (0.5.4)
159+
160+
PLATFORMS
161+
ruby
162+
163+
DEPENDENCIES
164+
bump
165+
bundler (~> 1.3)
166+
167+
BUNDLED WITH
168+
1.16.1
169+
HEREDOC
170+
create_file(lock_file_path, content)
171+
default = RuboCop::Config::DEFAULT_RAILS_VERSION
172+
expect(configuration.target_rails_version).to eq default
173+
end
174+
end
175+
end
176+
end
177+
end
178+
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require 'rubocop-rails'
22
require 'rubocop/rspec/support'
3+
require_relative 'support/file_helper'
4+
require_relative 'support/shared_contexts'
35

46
if ENV['COVERAGE'] == 'true'
57
require 'simplecov'

spec/support/file_helper.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require 'fileutils'
4+
5+
module FileHelper
6+
def create_file(file_path, content)
7+
file_path = File.expand_path(file_path)
8+
9+
dir_path = File.dirname(file_path)
10+
FileUtils.makedirs dir_path unless File.exist?(dir_path)
11+
12+
File.open(file_path, 'w') do |file|
13+
case content
14+
when String
15+
file.puts content
16+
when Array
17+
file.puts content.join("\n")
18+
end
19+
end
20+
end
21+
22+
def create_empty_file(file_path)
23+
create_file(file_path, '')
24+
end
25+
end

spec/support/shared_contexts.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_context 'with Rails 3', :rails3 do
4+
let(:rails_version) { 3.0 }
5+
end
6+
7+
RSpec.shared_context 'with Rails 4', :rails4 do
8+
let(:rails_version) { 4.0 }
9+
end
10+
11+
RSpec.shared_context 'with Rails 5', :rails5 do
12+
let(:rails_version) { 5.0 }
13+
end

0 commit comments

Comments
 (0)