2019-11-21 04:06:16 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-04 13:43:06 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2022-07-14 20:08:49 -04:00
|
|
|
RSpec.describe Gitlab::Git::AttributesParser do
|
|
|
|
let(:data) { fixture_file('gitlab/git/gitattributes') }
|
2017-01-04 13:43:06 -05:00
|
|
|
|
2018-01-11 18:12:34 -05:00
|
|
|
subject { described_class.new(data) }
|
2017-01-04 13:43:06 -05:00
|
|
|
|
|
|
|
describe '#attributes' do
|
|
|
|
context 'using a path with attributes' do
|
|
|
|
it 'returns the attributes as a Hash' do
|
|
|
|
expect(subject.attributes('test.txt')).to eq({ 'text' => true })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing multiple attributes' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.attributes('test.sh'))
|
|
|
|
.to eq({ 'eol' => 'lf', 'gitlab-language' => 'shell' })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing attributes for a file with multiple extensions' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.attributes('test.haml.html'))
|
|
|
|
.to eq({ 'gitlab-language' => 'haml' })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing attributes for a file in a directory' do
|
|
|
|
expect(subject.attributes('foo/bar.txt')).to eq({ 'foo' => true })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing attributes with query string parameters' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.attributes('foo.cgi'))
|
|
|
|
.to eq({ 'key' => 'value?p1=v1&p2=v2' })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing the attributes for an absolute path' do
|
|
|
|
expect(subject.attributes('/test.txt')).to eq({ 'text' => true })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a Hash containing the attributes when a pattern is defined using an absolute path' do
|
|
|
|
# When a path is given without a leading slash it should still match
|
|
|
|
# patterns defined with a leading slash.
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.attributes('foo.png'))
|
|
|
|
.to eq({ 'gitlab-language' => 'png' })
|
2017-01-04 13:43:06 -05:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.attributes('/foo.png'))
|
|
|
|
.to eq({ 'gitlab-language' => 'png' })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an empty Hash for a defined path without attributes' do
|
|
|
|
expect(subject.attributes('bla/bla.txt')).to eq({})
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the "binary" option is set for a path' do
|
|
|
|
it 'returns true for the "binary" option' do
|
|
|
|
expect(subject.attributes('test.binary')['binary']).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false for the "diff" option' do
|
|
|
|
expect(subject.attributes('test.binary')['diff']).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'using a path without any attributes' do
|
|
|
|
it 'returns an empty Hash' do
|
|
|
|
expect(subject.attributes('test.foo')).to eq({})
|
|
|
|
end
|
|
|
|
end
|
2018-01-11 18:12:34 -05:00
|
|
|
|
|
|
|
context 'when attributes data is nil' do
|
|
|
|
let(:data) { nil }
|
|
|
|
|
|
|
|
it 'returns an empty Hash' do
|
|
|
|
expect(subject.attributes('test.foo')).to eq({})
|
|
|
|
end
|
|
|
|
end
|
2020-05-11 05:09:45 -04:00
|
|
|
|
|
|
|
context 'when attributes data has binary data' do
|
|
|
|
let(:data) { "\xFF\xFE*\u0000.\u0000c\u0000s".b }
|
|
|
|
|
|
|
|
it 'returns an empty Hash' do
|
|
|
|
expect(subject.attributes('test.foo')).to eq({})
|
|
|
|
end
|
|
|
|
end
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#patterns' do
|
|
|
|
it 'parses a file with entries' do
|
|
|
|
expect(subject.patterns).to be_an_instance_of(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses an entry that uses a tab to separate the pattern and attributes' do
|
2018-01-11 18:12:34 -05:00
|
|
|
expect(subject.patterns[File.join('/', '*.md')])
|
2017-06-21 09:48:12 -04:00
|
|
|
.to eq({ 'gitlab-language' => 'markdown' })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores patterns in reverse order' do
|
|
|
|
first = subject.patterns.to_a[0]
|
|
|
|
|
2018-01-11 18:12:34 -05:00
|
|
|
expect(first[0]).to eq(File.join('/', 'bla/bla.txt'))
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# It's a bit hard to test for something _not_ being processed. As such we'll
|
|
|
|
# just test the number of entries.
|
|
|
|
it 'ignores any comments and empty lines' do
|
2018-09-05 05:50:38 -04:00
|
|
|
expect(subject.patterns.length).to eq(12)
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#parse_attributes' do
|
|
|
|
it 'parses a boolean attribute' do
|
|
|
|
expect(subject.parse_attributes('text')).to eq({ 'text' => true })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a negated boolean attribute' do
|
|
|
|
expect(subject.parse_attributes('-text')).to eq({ 'text' => false })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses a key-value pair' do
|
|
|
|
expect(subject.parse_attributes('foo=bar')).to eq({ 'foo' => 'bar' })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses multiple attributes' do
|
|
|
|
input = 'boolean key=value -negated'
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.parse_attributes(input))
|
|
|
|
.to eq({ 'boolean' => true, 'key' => 'value', 'negated' => false })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses attributes with query string parameters' do
|
2017-06-21 09:48:12 -04:00
|
|
|
expect(subject.parse_attributes('foo=bar?baz=1'))
|
|
|
|
.to eq({ 'foo' => 'bar?baz=1' })
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#each_line' do
|
|
|
|
it 'iterates over every line in the attributes file' do
|
2018-09-05 05:50:38 -04:00
|
|
|
args = [String] * 16 # the number of lines in the file
|
2017-01-04 13:43:06 -05:00
|
|
|
|
|
|
|
expect { |b| subject.each_line(&b) }.to yield_successive_args(*args)
|
|
|
|
end
|
|
|
|
|
2022-07-14 20:08:49 -04:00
|
|
|
context 'unsupported encoding' do
|
|
|
|
let(:data) { fixture_file('gitlab/git/gitattributes_invalid') }
|
2017-01-04 13:43:06 -05:00
|
|
|
|
2022-07-14 20:08:49 -04:00
|
|
|
it 'does not yield' do
|
|
|
|
expect { |b| subject.each_line(&b) }.not_to yield_control
|
|
|
|
end
|
2017-01-04 13:43:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|