debian-mirror-gitlab/spec/lib/gitlab/search/found_blob_spec.rb

170 lines
6.1 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2019-02-15 15:39:39 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Search::FoundBlob do
2019-09-30 21:07:59 +05:30
let(:project) { create(:project, :public, :repository) }
describe 'parsing content results' do
2019-02-15 15:39:39 +05:30
let(:results) { project.repository.search_files_by_content('feature', 'master') }
let(:search_result) { results.first }
subject { described_class.new(content_match: search_result, project: project) }
2019-09-30 21:07:59 +05:30
it 'returns a valid FoundBlob' do
2019-02-15 15:39:39 +05:30
is_expected.to be_an described_class
expect(subject.id).to be_nil
expect(subject.path).to eq('CHANGELOG')
expect(subject.basename).to eq('CHANGELOG')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(188)
expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n")
end
2019-09-30 21:07:59 +05:30
it 'does not parse content if not needed' do
2019-02-15 15:39:39 +05:30
expect(subject).not_to receive(:parse_search_result)
expect(subject.project_id).to eq(project.id)
2019-12-26 22:10:19 +05:30
expect(subject.binary_path).to eq('CHANGELOG')
2019-02-15 15:39:39 +05:30
end
2019-09-30 21:07:59 +05:30
it 'parses content only once when needed' do
2019-02-15 15:39:39 +05:30
expect(subject).to receive(:parse_search_result).once.and_call_original
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('CHANGELOG')
2019-02-15 15:39:39 +05:30
expect(subject.startline).to eq(188)
end
context 'when the matching filename contains a colon' do
let(:search_result) { "master:testdata/project::function1.yaml\x001\x00---\n" }
it 'returns a valid FoundBlob' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('testdata/project::function1.yaml')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('testdata/project::function1')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(1)
expect(subject.data).to eq("---\n")
end
end
context 'when the matching content contains a number surrounded by colons' do
let(:search_result) { "master:testdata/foo.txt\x001\x00blah:9:blah" }
it 'returns a valid FoundBlob' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('testdata/foo.txt')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('testdata/foo')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(1)
expect(subject.data).to eq('blah:9:blah')
end
end
context 'when the matching content contains multiple null bytes' do
let(:search_result) { "master:testdata/foo.txt\x001\x00blah\x001\x00foo" }
it 'returns a valid FoundBlob' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('testdata/foo.txt')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('testdata/foo')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(1)
expect(subject.data).to eq("blah\x001\x00foo")
end
end
context 'when the search result ends with an empty line' do
let(:results) { project.repository.search_files_by_content('Role models', 'master') }
it 'returns a valid FoundBlob that ends with an empty line' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('files/markdown/ruby-style-guide.md')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('files/markdown/ruby-style-guide')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(1)
expect(subject.data).to eq("# Prelude\n\n> Role models are important. <br/>\n> -- Officer Alex J. Murphy / RoboCop\n\n")
end
end
context 'when the search returns non-ASCII data' do
context 'with UTF-8' do
let(:results) { project.repository.search_files_by_content('файл', 'master') }
it 'returns results as UTF-8' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('encoding/russian.rb')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('encoding/russian')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(1)
expect(subject.data).to eq("Хороший файл\n")
end
end
context 'with UTF-8 in the filename' do
let(:results) { project.repository.search_files_by_content('webhook', 'master') }
it 'returns results as UTF-8' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('encoding/テスト.txt')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('encoding/テスト')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(3)
expect(subject.data).to include('WebHookの確認')
end
end
context 'with ISO-8859-1' do
2019-12-04 20:38:33 +05:30
let(:search_result) { (+"master:encoding/iso8859.txt\x001\x00\xC4\xFC\nmaster:encoding/iso8859.txt\x002\x00\nmaster:encoding/iso8859.txt\x003\x00foo\n").force_encoding(Encoding::ASCII_8BIT) }
2019-02-15 15:39:39 +05:30
it 'returns results as UTF-8' do
2019-12-26 22:10:19 +05:30
expect(subject.path).to eq('encoding/iso8859.txt')
2019-02-15 15:39:39 +05:30
expect(subject.basename).to eq('encoding/iso8859')
expect(subject.ref).to eq('master')
expect(subject.startline).to eq(1)
expect(subject.data).to eq("Äü\n\nfoo\n")
end
end
end
2019-09-30 21:07:59 +05:30
context 'when filename has extension' do
2019-02-15 15:39:39 +05:30
let(:search_result) { "master:CONTRIBUTE.md\x005\x00- [Contribute to GitLab](#contribute-to-gitlab)\n" }
it { expect(subject.path).to eq('CONTRIBUTE.md') }
it { expect(subject.basename).to eq('CONTRIBUTE') }
end
2019-09-30 21:07:59 +05:30
context 'when file is under directory' do
2019-02-15 15:39:39 +05:30
let(:search_result) { "master:a/b/c.md\x005\x00a b c\n" }
it { expect(subject.path).to eq('a/b/c.md') }
it { expect(subject.basename).to eq('a/b/c') }
end
end
2019-09-30 21:07:59 +05:30
describe 'parsing title results' do
context 'when file is under directory' do
let(:path) { 'a/b/c.md' }
2019-12-26 22:10:19 +05:30
subject { described_class.new(blob_path: path, project: project, ref: 'master') }
2019-09-30 21:07:59 +05:30
before do
allow(Gitlab::Git::Blob).to receive(:batch).and_return([
Gitlab::Git::Blob.new(path: path)
])
end
it { expect(subject.path).to eq('a/b/c.md') }
it { expect(subject.basename).to eq('a/b/c') }
context 'when filename has multiple extensions' do
let(:path) { 'a/b/c.whatever.md' }
it { expect(subject.basename).to eq('a/b/c.whatever') }
end
end
end
2020-03-13 15:44:24 +05:30
describe 'policy' do
let(:project) { build(:project, :repository) }
subject { described_class.new(project: project) }
it 'works with policy' do
expect(Ability.allowed?(project.creator, :read_blob, subject)).to be_truthy
end
end
2019-02-15 15:39:39 +05:30
end