Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lsif_data_service_spec.rb « projects « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4866f8481217aa3a81bfe31317a5b05c77b14ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true

require 'spec_helper'

describe Projects::LsifDataService do
  let(:artifact) { create(:ci_job_artifact, :lsif) }
  let(:project) { build_stubbed(:project) }
  let(:path) { 'main.go' }
  let(:commit_id) { Digest::SHA1.hexdigest(SecureRandom.hex) }

  let(:service) { described_class.new(artifact.file, project, commit_id) }

  describe '#execute' do
    def highlighted_value(value)
      [{ language: 'go', value: Gitlab::Highlight.highlight(nil, value, language: 'go') }]
    end

    context 'fetched lsif file', :use_clean_rails_memory_store_caching do
      it 'is cached' do
        service.execute(path)

        cached_data = Rails.cache.fetch("project:#{project.id}:lsif:#{commit_id}")

        expect(cached_data.keys).to eq(%w[def_refs doc_ranges docs hover_refs ranges])
      end
    end

    context 'for main.go' do
      let(:path_prefix) { "/#{project.full_path}/-/blob/#{commit_id}" }

      it 'returns lsif ranges for the file' do
        expect(service.execute(path)).to eq([
          {
            end_char: 9,
            end_line: 6,
            start_char: 5,
            start_line: 6,
            definition_url: "#{path_prefix}/main.go#L7",
            hover: highlighted_value('func main()')
          },
          {
            end_char: 36,
            end_line: 3,
            start_char: 1,
            start_line: 3,
            definition_url: "#{path_prefix}/main.go#L4",
            hover: highlighted_value('package "github.com/user/hello/morestrings" ("github.com/user/hello/morestrings")')
          },
          {
            end_char: 12,
            end_line: 7,
            start_char: 1,
            start_line: 7,
            definition_url: "#{path_prefix}/main.go#L4",
            hover: highlighted_value('package "github.com/user/hello/morestrings" ("github.com/user/hello/morestrings")')
          },
          {
            end_char: 20,
            end_line: 7,
            start_char: 13,
            start_line: 7,
            definition_url: "#{path_prefix}/morestrings/reverse.go#L11",
            hover: highlighted_value('func Reverse(s string) string') + [{ value: "This method reverses a string \n\n" }]
          },
          {
            end_char: 12,
            end_line: 8,
            start_char: 1,
            start_line: 8,
            definition_url: "#{path_prefix}/main.go#L4",
            hover: highlighted_value('package "github.com/user/hello/morestrings" ("github.com/user/hello/morestrings")')
          },
          {
            end_char: 18,
            end_line: 8,
            start_char: 13,
            start_line: 8,
            definition_url: "#{path_prefix}/morestrings/reverse.go#L5",
            hover: highlighted_value('func Func2(i int) string')
          }
        ])
      end
    end

    context 'for morestring/reverse.go' do
      let(:path) { 'morestrings/reverse.go' }

      it 'returns lsif ranges for the file' do
        expect(service.execute(path).first).to eq({
          end_char: 2,
          end_line: 11,
          start_char: 1,
          start_line: 11,
          definition_url: "/#{project.full_path}/-/blob/#{commit_id}/morestrings/reverse.go#L12",
          hover: highlighted_value('var a string')
        })
      end
    end

    context 'for an unknown file' do
      let(:path) { 'unknown.go' }

      it 'returns nil' do
        expect(service.execute(path)).to eq(nil)
      end
    end
  end

  describe '#doc_id' do
    context 'when the passed path matches multiple files' do
      let(:path) { 'check/main.go' }
      let(:docs) do
        {
          1 => 'cmd/check/main.go',
          2 => 'cmd/command.go',
          3 => 'check/main.go',
          4 => 'cmd/nested/check/main.go'
        }
      end

      it 'fetches the document with the shortest absolute path' do
        expect(service.__send__(:find_doc_id, docs, path)).to eq(3)
      end
    end
  end
end