|
| 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 |
0 commit comments