Raise errors for large and binary files
This commit is contained in:
parent
f2f844693e
commit
18398152fa
|
@ -10,8 +10,12 @@ module Gitlab
|
||||||
class MissingEndDelimiter < ParserError
|
class MissingEndDelimiter < ParserError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class UnmergeableFile < ParserError
|
||||||
|
end
|
||||||
|
|
||||||
def parse(text, our_path:, their_path:, parent: nil)
|
def parse(text, our_path:, their_path:, parent: nil)
|
||||||
return [] if text.blank?
|
raise UnmergeableFile if text.blank? # Typically a binary file
|
||||||
|
raise UnmergeableFile if text.length > 102400
|
||||||
|
|
||||||
line_obj_index = 0
|
line_obj_index = 0
|
||||||
line_old = 1
|
line_old = 1
|
||||||
|
|
|
@ -4,6 +4,10 @@ describe Gitlab::Conflict::Parser, lib: true do
|
||||||
let(:parser) { Gitlab::Conflict::Parser.new }
|
let(:parser) { Gitlab::Conflict::Parser.new }
|
||||||
|
|
||||||
describe '#parse' do
|
describe '#parse' do
|
||||||
|
def parse_text(text)
|
||||||
|
parser.parse(text, our_path: 'README.md', their_path: 'README.md')
|
||||||
|
end
|
||||||
|
|
||||||
context 'when the file has valid conflicts' do
|
context 'when the file has valid conflicts' do
|
||||||
let(:text) do
|
let(:text) do
|
||||||
<<CONFLICT
|
<<CONFLICT
|
||||||
|
@ -116,12 +120,6 @@ CONFLICT
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the file contents include conflict delimiters' do
|
context 'when the file contents include conflict delimiters' do
|
||||||
let(:path) { 'README.md' }
|
|
||||||
|
|
||||||
def parse_text(text)
|
|
||||||
parser.parse(text, our_path: path, their_path: path)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'raises UnexpectedDelimiter when there is a non-start delimiter first' do
|
it 'raises UnexpectedDelimiter when there is a non-start delimiter first' do
|
||||||
expect { parse_text('=======') }.
|
expect { parse_text('=======') }.
|
||||||
to raise_error(Gitlab::Conflict::Parser::UnexpectedDelimiter)
|
to raise_error(Gitlab::Conflict::Parser::UnexpectedDelimiter)
|
||||||
|
@ -172,9 +170,19 @@ CONFLICT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when lines is blank' do
|
context 'other file types' do
|
||||||
it { expect(parser.parse('', our_path: 'README.md', their_path: 'README.md')).to eq([]) }
|
it 'raises UnmergeableFile when lines is blank, indicating a binary file' do
|
||||||
it { expect(parser.parse(nil, our_path: 'README.md', their_path: 'README.md')).to eq([]) }
|
expect { parse_text('') }.
|
||||||
|
to raise_error(Gitlab::Conflict::Parser::UnmergeableFile)
|
||||||
|
|
||||||
|
expect { parse_text(nil) }.
|
||||||
|
to raise_error(Gitlab::Conflict::Parser::UnmergeableFile)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises UnmergeableFile when the file is over 100 KB' do
|
||||||
|
expect { parse_text('a' * 102401) }.
|
||||||
|
to raise_error(Gitlab::Conflict::Parser::UnmergeableFile)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue