Build correct basenames for title search results

The "basename" here needs to be the full path without the trailing
extension, instead of stripping the leading path as well.

This was previously fixed in 2f36efa087 inside the view, but the
problematic code was still present in FoundBlob, and the corresponding
spec didn't actually use a child wiki page to properly verify the fix.
This commit is contained in:
Markus Koller 2019-06-20 14:59:58 +02:00
parent f4605ad880
commit 6905a62867
No known key found for this signature in database
GPG key ID: A2B74A05A7A2B7B7
5 changed files with 47 additions and 14 deletions

View file

@ -1,5 +1,5 @@
- project = find_project_for_result_blob(projects, wiki_blob)
- wiki_blob = parse_search_result(wiki_blob)
- wiki_blob_link = project_wiki_path(project, Pathname.new(wiki_blob.filename).sub_ext(''))
- wiki_blob_link = project_wiki_path(project, wiki_blob.basename)
= render partial: 'search/results/blob_data', locals: { blob: wiki_blob, project: project, file_name: wiki_blob.filename, blob_link: wiki_blob_link }

View file

@ -0,0 +1,5 @@
---
title: Build correct basenames for title search results
merge_request: 29898
author:
type: fixed

View file

@ -93,7 +93,7 @@ module Gitlab
data = {
id: blob.id,
binary_filename: blob.path,
binary_basename: File.basename(blob.path, File.extname(blob.path)),
binary_basename: path_without_extension(blob.path),
ref: ref,
startline: 1,
binary_data: blob.data,
@ -111,6 +111,10 @@ module Gitlab
content_match.match(FILENAME_REGEXP) { |matches| matches[:filename] }
end
def path_without_extension(path)
Pathname.new(path).sub_ext('').to_s
end
def parsed_content
strong_memoize(:parsed_content) do
if content_match
@ -137,8 +141,7 @@ module Gitlab
filename = matches[:filename]
startline = matches[:startline]
startline = startline.to_i - index
extname = Regexp.escape(File.extname(filename))
basename = filename.sub(/#{extname}$/, '')
basename = path_without_extension(filename)
end
data << line.sub(prefix.to_s, '')

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe 'User searches for wiki pages', :js do
let(:user) { create(:user) }
let(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'test_wiki', content: 'Some Wiki content' }) }
let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'directory/title', content: 'Some Wiki content' }) }
before do
project.add_maintainer(user)
@ -22,7 +22,7 @@ describe 'User searches for wiki pages', :js do
click_link(project.full_name)
end
fill_in('dashboard_search', with: 'content')
fill_in('dashboard_search', with: search_term)
find('.btn-search').click
page.within('.search-filter') do
@ -43,7 +43,7 @@ describe 'User searches for wiki pages', :js do
context 'when searching by title' do
it_behaves_like 'search wiki blobs' do
let(:search_term) { 'test_wiki' }
let(:search_term) { 'title' }
end
end
end

View file

@ -3,14 +3,15 @@
require 'spec_helper'
describe Gitlab::Search::FoundBlob do
describe 'parsing results' do
let(:project) { create(:project, :public, :repository) }
describe 'parsing content results' do
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) }
it "returns a valid FoundBlob" do
it 'returns a valid FoundBlob' do
is_expected.to be_an described_class
expect(subject.id).to be_nil
expect(subject.path).to eq('CHANGELOG')
@ -21,13 +22,13 @@ describe Gitlab::Search::FoundBlob do
expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n")
end
it "doesn't parses content if not needed" do
it 'does not parse content if not needed' do
expect(subject).not_to receive(:parse_search_result)
expect(subject.project_id).to eq(project.id)
expect(subject.binary_filename).to eq('CHANGELOG')
end
it "parses content only once when needed" do
it 'parses content only once when needed' do
expect(subject).to receive(:parse_search_result).once.and_call_original
expect(subject.filename).to eq('CHANGELOG')
expect(subject.startline).to eq(188)
@ -119,7 +120,7 @@ describe Gitlab::Search::FoundBlob do
end
end
context "when filename has extension" do
context 'when filename has extension' do
let(:search_result) { "master:CONTRIBUTE.md\x005\x00- [Contribute to GitLab](#contribute-to-gitlab)\n" }
it { expect(subject.path).to eq('CONTRIBUTE.md') }
@ -127,7 +128,7 @@ describe Gitlab::Search::FoundBlob do
it { expect(subject.basename).to eq('CONTRIBUTE') }
end
context "when file under directory" do
context 'when file is under directory' do
let(:search_result) { "master:a/b/c.md\x005\x00a b c\n" }
it { expect(subject.path).to eq('a/b/c.md') }
@ -135,4 +136,28 @@ describe Gitlab::Search::FoundBlob do
it { expect(subject.basename).to eq('a/b/c') }
end
end
describe 'parsing title results' do
context 'when file is under directory' do
let(:path) { 'a/b/c.md' }
subject { described_class.new(blob_filename: path, project: project, ref: 'master') }
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.filename).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
end