Commit 6905a628 authored by Markus Koller's avatar Markus Koller

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 2f36efa0 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.
parent f4605ad8
- project = find_project_for_result_blob(projects, wiki_blob) - project = find_project_for_result_blob(projects, wiki_blob)
- wiki_blob = parse_search_result(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 } = render partial: 'search/results/blob_data', locals: { blob: wiki_blob, project: project, file_name: wiki_blob.filename, blob_link: wiki_blob_link }
---
title: Build correct basenames for title search results
merge_request: 29898
author:
type: fixed
...@@ -93,7 +93,7 @@ module Gitlab ...@@ -93,7 +93,7 @@ module Gitlab
data = { data = {
id: blob.id, id: blob.id,
binary_filename: blob.path, binary_filename: blob.path,
binary_basename: File.basename(blob.path, File.extname(blob.path)), binary_basename: path_without_extension(blob.path),
ref: ref, ref: ref,
startline: 1, startline: 1,
binary_data: blob.data, binary_data: blob.data,
...@@ -111,6 +111,10 @@ module Gitlab ...@@ -111,6 +111,10 @@ module Gitlab
content_match.match(FILENAME_REGEXP) { |matches| matches[:filename] } content_match.match(FILENAME_REGEXP) { |matches| matches[:filename] }
end end
def path_without_extension(path)
Pathname.new(path).sub_ext('').to_s
end
def parsed_content def parsed_content
strong_memoize(:parsed_content) do strong_memoize(:parsed_content) do
if content_match if content_match
...@@ -137,8 +141,7 @@ module Gitlab ...@@ -137,8 +141,7 @@ module Gitlab
filename = matches[:filename] filename = matches[:filename]
startline = matches[:startline] startline = matches[:startline]
startline = startline.to_i - index startline = startline.to_i - index
extname = Regexp.escape(File.extname(filename)) basename = path_without_extension(filename)
basename = filename.sub(/#{extname}$/, '')
end end
data << line.sub(prefix.to_s, '') data << line.sub(prefix.to_s, '')
......
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
describe 'User searches for wiki pages', :js do describe 'User searches for wiki pages', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) } 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 before do
project.add_maintainer(user) project.add_maintainer(user)
...@@ -22,7 +22,7 @@ describe 'User searches for wiki pages', :js do ...@@ -22,7 +22,7 @@ describe 'User searches for wiki pages', :js do
click_link(project.full_name) click_link(project.full_name)
end end
fill_in('dashboard_search', with: 'content') fill_in('dashboard_search', with: search_term)
find('.btn-search').click find('.btn-search').click
page.within('.search-filter') do page.within('.search-filter') do
...@@ -43,7 +43,7 @@ describe 'User searches for wiki pages', :js do ...@@ -43,7 +43,7 @@ describe 'User searches for wiki pages', :js do
context 'when searching by title' do context 'when searching by title' do
it_behaves_like 'search wiki blobs' do it_behaves_like 'search wiki blobs' do
let(:search_term) { 'test_wiki' } let(:search_term) { 'title' }
end end
end end
end end
...@@ -3,14 +3,15 @@ ...@@ -3,14 +3,15 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Search::FoundBlob do describe Gitlab::Search::FoundBlob do
describe 'parsing results' do
let(:project) { create(:project, :public, :repository) } let(:project) { create(:project, :public, :repository) }
describe 'parsing content results' do
let(:results) { project.repository.search_files_by_content('feature', 'master') } let(:results) { project.repository.search_files_by_content('feature', 'master') }
let(:search_result) { results.first } let(:search_result) { results.first }
subject { described_class.new(content_match: search_result, project: project) } 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 is_expected.to be_an described_class
expect(subject.id).to be_nil expect(subject.id).to be_nil
expect(subject.path).to eq('CHANGELOG') expect(subject.path).to eq('CHANGELOG')
...@@ -21,13 +22,13 @@ describe Gitlab::Search::FoundBlob do ...@@ -21,13 +22,13 @@ describe Gitlab::Search::FoundBlob do
expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n") expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n")
end 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).not_to receive(:parse_search_result)
expect(subject.project_id).to eq(project.id) expect(subject.project_id).to eq(project.id)
expect(subject.binary_filename).to eq('CHANGELOG') expect(subject.binary_filename).to eq('CHANGELOG')
end 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).to receive(:parse_search_result).once.and_call_original
expect(subject.filename).to eq('CHANGELOG') expect(subject.filename).to eq('CHANGELOG')
expect(subject.startline).to eq(188) expect(subject.startline).to eq(188)
...@@ -119,7 +120,7 @@ describe Gitlab::Search::FoundBlob do ...@@ -119,7 +120,7 @@ describe Gitlab::Search::FoundBlob do
end end
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" } 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.path).to eq('CONTRIBUTE.md') }
...@@ -127,7 +128,7 @@ describe Gitlab::Search::FoundBlob do ...@@ -127,7 +128,7 @@ describe Gitlab::Search::FoundBlob do
it { expect(subject.basename).to eq('CONTRIBUTE') } it { expect(subject.basename).to eq('CONTRIBUTE') }
end 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" } 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.path).to eq('a/b/c.md') }
...@@ -135,4 +136,28 @@ describe Gitlab::Search::FoundBlob do ...@@ -135,4 +136,28 @@ describe Gitlab::Search::FoundBlob do
it { expect(subject.basename).to eq('a/b/c') } it { expect(subject.basename).to eq('a/b/c') }
end end
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 end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment