From 7f32ad7097b8badcad98a52b9373d67ce7f33184 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Mon, 3 Apr 2017 18:31:52 +0100 Subject: Fix blob highlighting in search --- .../30400-fix-blob-highlighting-in-search.yml | 4 ++++ lib/gitlab/project_search_results.rb | 2 +- lib/gitlab/search_results.rb | 21 +++++++++++++++++++++ spec/features/search_spec.rb | 4 +++- spec/lib/gitlab/project_search_results_spec.rb | 8 ++++++-- 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 changelogs/unreleased/30400-fix-blob-highlighting-in-search.yml diff --git a/changelogs/unreleased/30400-fix-blob-highlighting-in-search.yml b/changelogs/unreleased/30400-fix-blob-highlighting-in-search.yml new file mode 100644 index 00000000000..942258450c0 --- /dev/null +++ b/changelogs/unreleased/30400-fix-blob-highlighting-in-search.yml @@ -0,0 +1,4 @@ +--- +title: Fix blob highlighting in search +merge_request: 10420 +author: diff --git a/lib/gitlab/project_search_results.rb b/lib/gitlab/project_search_results.rb index db325c00705..0b8959f2fb9 100644 --- a/lib/gitlab/project_search_results.rb +++ b/lib/gitlab/project_search_results.rb @@ -62,7 +62,7 @@ module Gitlab data << line.sub(ref, '').sub(filename, '').sub(/^:-\d+-/, '').sub(/^::\d+:/, '') end - OpenStruct.new( + FoundBlob.new( filename: filename, basename: basename, ref: ref, diff --git a/lib/gitlab/search_results.rb b/lib/gitlab/search_results.rb index ccfa517e04b..efe8095beea 100644 --- a/lib/gitlab/search_results.rb +++ b/lib/gitlab/search_results.rb @@ -1,5 +1,26 @@ module Gitlab class SearchResults + class FoundBlob + attr_reader :id, :filename, :basename, :ref, :startline, :data + + def initialize(opts = {}) + @id = opts.fetch(:id, nil) + @filename = opts.fetch(:filename, nil) + @basename = opts.fetch(:basename, nil) + @ref = opts.fetch(:ref, nil) + @startline = opts.fetch(:startline, nil) + @data = opts.fetch(:data, nil) + end + + def path + filename + end + + def no_highlighting? + false + end + end + attr_reader :current_user, :query # Limit search results by passed projects diff --git a/spec/features/search_spec.rb b/spec/features/search_spec.rb index a6560a81096..40ef4c098b9 100644 --- a/spec/features/search_spec.rb +++ b/spec/features/search_spec.rb @@ -119,13 +119,15 @@ describe "Search", feature: true do visit namespace_project_path(project.namespace, project) page.within '.search' do - fill_in 'search', with: 'def' + fill_in 'search', with: 'application.js' click_button 'Go' end click_link "Code" expect(page).to have_selector('.file-content .code') + + expect(page).to have_selector("span.line[lang='javascript']") end end diff --git a/spec/lib/gitlab/project_search_results_spec.rb b/spec/lib/gitlab/project_search_results_spec.rb index 9a8096208db..e0ebea63eb4 100644 --- a/spec/lib/gitlab/project_search_results_spec.rb +++ b/spec/lib/gitlab/project_search_results_spec.rb @@ -41,8 +41,10 @@ describe Gitlab::ProjectSearchResults, lib: true do subject { described_class.parse_search_result(search_result) } - it "returns a valid OpenStruct object" do - is_expected.to be_an OpenStruct + it "returns a valid FoundBlob" do + is_expected.to be_an Gitlab::SearchResults::FoundBlob + expect(subject.id).to be_nil + expect(subject.path).to eq('CHANGELOG') expect(subject.filename).to eq('CHANGELOG') expect(subject.basename).to eq('CHANGELOG') expect(subject.ref).to eq('master') @@ -53,6 +55,7 @@ describe Gitlab::ProjectSearchResults, lib: true do context "when filename has extension" do let(:search_result) { "master:CONTRIBUTE.md:5:- [Contribute to GitLab](#contribute-to-gitlab)\n" } + it { expect(subject.path).to eq('CONTRIBUTE.md') } it { expect(subject.filename).to eq('CONTRIBUTE.md') } it { expect(subject.basename).to eq('CONTRIBUTE') } end @@ -60,6 +63,7 @@ describe Gitlab::ProjectSearchResults, lib: true do context "when file under directory" do let(:search_result) { "master:a/b/c.md:5:a b c\n" } + 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') } end -- cgit v1.2.1