From 6905a62867067f2f61feb6734be3972cb5b9f7a7 Mon Sep 17 00:00:00 2001 From: Markus Koller Date: Thu, 20 Jun 2019 14:59:58 +0200 Subject: [PATCH] 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 2f36efa0871 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. --- app/views/search/results/_wiki_blob.html.haml | 2 +- .../unreleased/search-blob-basenames.yml | 5 +++ lib/gitlab/search/found_blob.rb | 9 +++-- .../user_searches_for_wiki_pages_spec.rb | 6 +-- spec/lib/gitlab/search/found_blob_spec.rb | 39 +++++++++++++++---- 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 changelogs/unreleased/search-blob-basenames.yml diff --git a/app/views/search/results/_wiki_blob.html.haml b/app/views/search/results/_wiki_blob.html.haml index 5847751b268..b351ecd4edf 100644 --- a/app/views/search/results/_wiki_blob.html.haml +++ b/app/views/search/results/_wiki_blob.html.haml @@ -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 } diff --git a/changelogs/unreleased/search-blob-basenames.yml b/changelogs/unreleased/search-blob-basenames.yml new file mode 100644 index 00000000000..48ad1130e3f --- /dev/null +++ b/changelogs/unreleased/search-blob-basenames.yml @@ -0,0 +1,5 @@ +--- +title: Build correct basenames for title search results +merge_request: 29898 +author: +type: fixed diff --git a/lib/gitlab/search/found_blob.rb b/lib/gitlab/search/found_blob.rb index a62ab1521a7..01ce90c85f7 100644 --- a/lib/gitlab/search/found_blob.rb +++ b/lib/gitlab/search/found_blob.rb @@ -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, '') diff --git a/spec/features/search/user_searches_for_wiki_pages_spec.rb b/spec/features/search/user_searches_for_wiki_pages_spec.rb index 6d4facd0649..ee43755262e 100644 --- a/spec/features/search/user_searches_for_wiki_pages_spec.rb +++ b/spec/features/search/user_searches_for_wiki_pages_spec.rb @@ -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 diff --git a/spec/lib/gitlab/search/found_blob_spec.rb b/spec/lib/gitlab/search/found_blob_spec.rb index 74157e5c67c..da263bc7523 100644 --- a/spec/lib/gitlab/search/found_blob_spec.rb +++ b/spec/lib/gitlab/search/found_blob_spec.rb @@ -3,14 +3,15 @@ require 'spec_helper' 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(: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