summaryrefslogtreecommitdiff
path: root/spec/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-03-27 18:07:48 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-03-27 18:07:48 +0000
commite20baee820ea2c76ee16980a98e8080f255d9035 (patch)
tree6e13a73bee42b7ef310850d03982faebea17a0b1 /spec/rubocop
parent71c5863d7b1ca9836a7d7703f35750cd726a9846 (diff)
downloadgitlab-ce-e20baee820ea2c76ee16980a98e8080f255d9035.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r--spec/rubocop/cop/performance/readlines_each_spec.rb77
1 files changed, 77 insertions, 0 deletions
diff --git a/spec/rubocop/cop/performance/readlines_each_spec.rb b/spec/rubocop/cop/performance/readlines_each_spec.rb
new file mode 100644
index 00000000000..5b3691e2342
--- /dev/null
+++ b/spec/rubocop/cop/performance/readlines_each_spec.rb
@@ -0,0 +1,77 @@
+# frozen_string_literal: true
+
+require 'fast_spec_helper'
+require_relative '../../../support/helpers/expect_offense'
+require_relative '../../../../rubocop/cop/performance/readlines_each'
+
+describe RuboCop::Cop::Performance::ReadlinesEach do
+ include CopHelper
+ include ExpectOffense
+
+ subject(:cop) { described_class.new }
+
+ let(:message) { 'Avoid `IO.readlines.each`, since it reads contents into memory in full. Use `IO.each_line` or `IO.each` instead.' }
+
+ shared_examples_for(:class_read) do |klass|
+ context "and it is called as a class method on #{klass}" do
+ # We can't use `expect_offense` here because indentation changes based on `klass`
+ it 'flags it as an offense' do
+ inspect_source "#{klass}.readlines(file_path).each { |line| puts line }"
+
+ expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ReadlinesEach')
+ end
+ end
+
+ context 'when just using readlines without each' do
+ it 'does not flag it as an offense' do
+ expect_no_offenses "contents = #{klass}.readlines(file_path)"
+ end
+ end
+ end
+
+ context 'when reading all lines using IO.readlines.each' do
+ %w(IO File).each do |klass|
+ it_behaves_like(:class_read, klass)
+ end
+
+ context 'and it is called as an instance method on a return value' do
+ it 'flags it as an offense' do
+ expect_offense <<~SOURCE
+ get_file.readlines.each { |line| puts line }
+ ^^^^ #{message}
+ SOURCE
+ end
+ end
+
+ context 'and it is called as an instance method on an assigned variable' do
+ it 'flags it as an offense' do
+ expect_offense <<~SOURCE
+ file = File.new(path)
+ file.readlines.each { |line| puts line }
+ ^^^^ #{message}
+ SOURCE
+ end
+ end
+
+ context 'and it is called as an instance method on a new object' do
+ it 'flags it as an offense' do
+ expect_offense <<~SOURCE
+ File.new(path).readlines.each { |line| puts line }
+ ^^^^ #{message}
+ SOURCE
+ end
+ end
+
+ it 'autocorrects `readlines.each` to `each_line`' do
+ expect(autocorrect_source('obj.readlines.each { |line| line }')).to(
+ eq('obj.each_line { |line| line }')
+ )
+ end
+ end
+
+ context 'when just using readlines without each' do
+ it 'does not flag it as an offense' do
+ expect_no_offenses 'contents = my_file.readlines'
+ end
+ end
+end