Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 1e41d6e

Browse files
committed
Use cached Source instance even when extracting a single line
1 parent 3c17206 commit 1e41d6e

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

lib/rspec/core/formatters/snippet_extractor.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ class SnippetExtractor
99
NoSuchLineError = Class.new(StandardError)
1010

1111
def self.extract_line_at(file_path, line_number)
12-
raise_error_if_file_does_not_exist!(file_path)
13-
line = File.readlines(file_path)[line_number - 1]
12+
source = source_from_file(file_path)
13+
line = source.lines[line_number - 1]
1414
raise NoSuchLineError unless line
15-
line.chomp
15+
line
1616
end
1717

18-
def self.raise_error_if_file_does_not_exist!(path)
18+
def self.source_from_file(path)
1919
raise NoSuchFileError unless File.exist?(path)
20+
RSpec.world.source_cache.source_from_file(path)
2021
end
2122

2223
if RSpec::Support::RubyFeatures.ripper_supported?
@@ -40,11 +41,6 @@ def self.extract_expression_lines_at(file_path, beginning_line_number, max_line_
4041
end
4142
end
4243

43-
def self.source_from_file(path)
44-
raise_error_if_file_does_not_exist!(path)
45-
RSpec.world.source_cache.source_from_file(path)
46-
end
47-
4844
def initialize(source, beginning_line_number, max_line_count=nil)
4945
@source = source
5046
@beginning_line_number = beginning_line_number

0 commit comments

Comments
 (0)