Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo committed Oct 10, 2017
1 parent 1eab0dd commit b19be87
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/airbrake-ruby/backtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def read_code_hunk(logger, frame)
return unless code_hunk

unless code_hunk.key?(0)
frame[:code_hunk] = code_hunk
frame[:code] = code_hunk
return
end

Expand Down
17 changes: 9 additions & 8 deletions lib/airbrake-ruby/code_hunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ class CodeHunk

##
# @return [Integer] how many lines should be read around the base line
INTERVAL = 3
NLINES = 2

def initialize(file, line, interval = INTERVAL)
def initialize(file, line, nlines = NLINES)
@file = file
@line = line

@start_line = [line - interval, 1].max
@end_line = line + interval
@start_line = [line - nlines, 1].max
@end_line = line + nlines

@code_hash = {}
@lines = {}
end

##
# @return [Hash{Integer=>String}, nil] code hunk around the base line. When
# an error occurrs, returns a zero key Hash
def to_h
return @code_hash unless @code_hash.empty?
return @lines if @lines.any?
return unless File.exist?(@file)

begin
Expand All @@ -34,7 +34,8 @@ def to_h
{ 0 => ex }
end

@code_hash
@lines = { 1 => '' } if @lines.empty?
@lines
end

private
Expand All @@ -44,7 +45,7 @@ def fetch_code
next if i < @start_line
break if i > @end_line

@code_hash[i] = line[0...MAX_LINE_LEN].rstrip
@lines[i] = line[0...MAX_LINE_LEN].rstrip
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/backtrace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,14 @@
file: File.join(fixture_path('code.rb')),
line: 94,
function: 'to_json',
code_hunk: {
91 => ' def to_json',
code: {
92 => ' loop do',
93 => ' begin',
94 => ' json = @payload.to_json',
95 => ' rescue *JSON_EXCEPTIONS => ex',
# rubocop:disable Metrics/LineLength,Lint/InterpolationCheck
96 => ' @config.logger.debug("#{LOG_LABEL} `notice.to_json` failed: #{ex.class}: #{ex}")',
# rubocop:enable Metrics/LineLength,Lint/InterpolationCheck
97 => ' else'
}
}
]
Expand Down
17 changes: 6 additions & 11 deletions spec/code_hunk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
context "when a file is empty" do
subject { described_class.new(fixture_path('empty_file.rb'), 1).to_h }

it { is_expected.to eql({}) }
it { is_expected.to eq(1 => '') }
end

context "when a file doesn't exist" do
Expand All @@ -14,7 +14,7 @@
it { is_expected.to be_nil }
end

context "when a file has less than INTERVAL lines before start line" do
context "when a file has less than NLINES lines before start line" do
subject { described_class.new(fixture_path('code.rb'), 1).to_h }

it do
Expand All @@ -25,28 +25,25 @@
# rubocop:disable Metrics/LineLength
3 => ' # Represents a chunk of information that is meant to be either sent to',
# rubocop:enable Metrics/LineLength
4 => ' # Airbrake or ignored completely.'
)
)
end
end

context "when a file has less than INTERVAL lines after end line" do
subject { described_class.new(fixture_path('code.rb'), 221).to_h }
context "when a file has less than NLINES lines after end line" do
subject { described_class.new(fixture_path('code.rb'), 222).to_h }

it do
is_expected.to(
eq(
218 => ' end',
219 => ' end',
220 => ' end',
221 => 'end'
)
)
end
end

context "when a file has less lines than a code hunk requests" do
context "when a file has less than NLINES lines before and after" do
subject { described_class.new(fixture_path('short_file.rb'), 2).to_h }

it do
Expand All @@ -60,19 +57,17 @@
end
end

context "when a line location is in the middle of a file" do
context "when a file has enough lines before and after" do
subject { described_class.new(fixture_path('code.rb'), 100).to_h }

it do
is_expected.to(
eq(
97 => ' else',
98 => ' return json if json && json.bytesize <= MAX_NOTICE_SIZE',
99 => ' end',
100 => '',
101 => ' break if truncate == 0',
102 => ' end',
103 => ' end'
)
)
end
Expand Down

0 comments on commit b19be87

Please sign in to comment.