diff --git a/changelog/fix_fix_pattern_to_match_toolversion_files.md b/changelog/fix_fix_pattern_to_match_toolversion_files.md new file mode 100644 index 00000000000..dbd2ee0a93e --- /dev/null +++ b/changelog/fix_fix_pattern_to_match_toolversion_files.md @@ -0,0 +1 @@ +* [#11181](https://github.com/rubocop/rubocop/pull/11181): Fix pattern to match .tool-versions files that specify multiple runtimes. ([@noelblaschke][]) diff --git a/lib/rubocop/target_ruby.rb b/lib/rubocop/target_ruby.rb index 174d448e1b4..8ef58312a77 100644 --- a/lib/rubocop/target_ruby.rb +++ b/lib/rubocop/target_ruby.rb @@ -84,7 +84,7 @@ def version_file # @api private class ToolVersionsFile < RubyVersionFile TOOL_VERSIONS_FILENAME = '.tool-versions' - TOOL_VERSIONS_PATTERN = /\Aruby (?:ruby-)?(?\d+\.\d+)/.freeze + TOOL_VERSIONS_PATTERN = /^(?:ruby )(?\d+\.\d+)/.freeze def name "`#{TOOL_VERSIONS_FILENAME}`" diff --git a/spec/rubocop/target_ruby_spec.rb b/spec/rubocop/target_ruby_spec.rb index c5b7fef874f..9580862d89c 100644 --- a/spec/rubocop/target_ruby_spec.rb +++ b/spec/rubocop/target_ruby_spec.rb @@ -104,8 +104,16 @@ create_file(File.join(dir, '.tool-versions'), tool_versions) end - context 'when .tool-versions contains a ruby version' do - let(:tool_versions) { ['ruby 3.0.0', 'nodejs 14.9.0'] } + context 'when .tool-versions does not contain a ruby version' do + let(:tool_versions) { ['nodejs 14.9.0'] } + + it 'uses the default ruby version' do + expect(target_ruby.version).to eq default_version + end + end + + context 'when .tool-versions contains only a ruby version' do + let(:tool_versions) { ['ruby 3.0.0'] } let(:ruby_version_to_f) { 3.0 } it 'reads it to determine the target ruby version' do @@ -119,12 +127,18 @@ end end - context 'when .tool-versions does not contain a ruby version' do - let(:tool_versions) { ['nodejs 14.9.0'] } + context 'when .tool-versions contains different runtimes' do + let(:tool_versions) { ['nodejs 14.9.0', 'ruby 3.0.0'] } let(:ruby_version_to_f) { 3.0 } - it 'uses the default ruby version' do - expect(target_ruby.version).to eq default_version + it 'reads it to determine the target ruby version' do + expect(target_ruby.version).to eq ruby_version_to_f + end + + it 'does not read Gemfile.lock, gems.locked' do + expect(File).not_to receive(:file?).with(/Gemfile/) + expect(File).not_to receive(:file?).with(/gems\.locked/) + target_ruby.version end end end