reference_extractor_spec.rb 3.54 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::ReferenceExtractor do
Douwe Maan's avatar
Douwe Maan committed
4
  let(:project) { create(:project) }
5
  subject { Gitlab::ReferenceExtractor.new(project, project.creator) }
Douwe Maan's avatar
Douwe Maan committed
6

Douwe Maan's avatar
Douwe Maan committed
7
  it 'accesses valid user objects' do
Douwe Maan's avatar
Douwe Maan committed
8 9
    @u_foo = create(:user, username: 'foo')
    @u_bar = create(:user, username: 'bar')
Douwe Maan's avatar
Douwe Maan committed
10
    @u_offteam = create(:user, username: 'offteam')
11

Douwe Maan's avatar
Douwe Maan committed
12 13
    project.team << [@u_foo, :reporter]
    project.team << [@u_bar, :guest]
14

Douwe Maan's avatar
Douwe Maan committed
15
    subject.analyze('@foo, @baduser, @bar, and @offteam')
16
    expect(subject.users).to match_array([@u_foo, @u_bar, @u_offteam])
Douwe Maan's avatar
Douwe Maan committed
17
  end
18

19 20 21 22 23 24 25 26 27
  it 'ignores user mentions inside specific elements' do
    @u_foo = create(:user, username: 'foo')
    @u_bar = create(:user, username: 'bar')
    @u_offteam = create(:user, username: 'offteam')

    project.team << [@u_foo, :reporter]
    project.team << [@u_bar, :guest]

    subject.analyze(%Q{
28
      Inline code: `@foo`
29 30 31 32 33 34

      Code block:

      ```
      @bar
      ```
35

36
      Quote:
37 38

      > @offteam
39
    })
40
    expect(subject.users).to match_array([])
41 42
  end

Douwe Maan's avatar
Douwe Maan committed
43 44 45
  it 'accesses valid issue objects' do
    @i0 = create(:issue, project: project)
    @i1 = create(:issue, project: project)
46

47
    subject.analyze("#{@i0.to_reference}, #{@i1.to_reference}, and #{Issue.reference_prefix}999.")
48
    expect(subject.issues).to match_array([@i0, @i1])
Douwe Maan's avatar
Douwe Maan committed
49
  end
50

Douwe Maan's avatar
Douwe Maan committed
51
  it 'accesses valid merge requests' do
52 53
    @m0 = create(:merge_request, source_project: project, target_project: project, source_branch: 'markdown')
    @m1 = create(:merge_request, source_project: project, target_project: project, source_branch: 'feature_conflict')
54

Douwe Maan's avatar
Douwe Maan committed
55
    subject.analyze("!999, !#{@m1.iid}, and !#{@m0.iid}.")
56
    expect(subject.merge_requests).to match_array([@m1, @m0])
Douwe Maan's avatar
Douwe Maan committed
57
  end
58

59 60 61 62 63 64
  it 'accesses valid labels' do
    @l0 = create(:label, title: 'one', project: project)
    @l1 = create(:label, title: 'two', project: project)
    @l2 = create(:label)

    subject.analyze("~#{@l0.id}, ~999, ~#{@l2.id}, ~#{@l1.id}")
65
    expect(subject.labels).to match_array([@l0, @l1])
66 67
  end

Douwe Maan's avatar
Douwe Maan committed
68 69 70 71
  it 'accesses valid snippets' do
    @s0 = create(:project_snippet, project: project)
    @s1 = create(:project_snippet, project: project)
    @s2 = create(:project_snippet)
72

Douwe Maan's avatar
Douwe Maan committed
73
    subject.analyze("$#{@s0.id}, $999, $#{@s2.id}, $#{@s1.id}")
74
    expect(subject.snippets).to match_array([@s0, @s1])
Douwe Maan's avatar
Douwe Maan committed
75
  end
76

Douwe Maan's avatar
Douwe Maan committed
77
  it 'accesses valid commits' do
78
    commit = project.commit('master')
79

Douwe Maan's avatar
Douwe Maan committed
80 81 82 83 84 85
    subject.analyze("this references commits #{commit.sha[0..6]} and 012345")
    extracted = subject.commits
    expect(extracted.size).to eq(1)
    expect(extracted[0].sha).to eq(commit.sha)
    expect(extracted[0].message).to eq(commit.message)
  end
86

Douwe Maan's avatar
Douwe Maan committed
87
  it 'accesses valid commit ranges' do
88 89
    commit = project.commit('master')
    earlier_commit = project.commit('master~2')
90

Douwe Maan's avatar
Douwe Maan committed
91
    subject.analyze("this references commits #{earlier_commit.sha[0..6]}...#{commit.sha[0..6]}")
92

Douwe Maan's avatar
Douwe Maan committed
93 94
    extracted = subject.commit_ranges
    expect(extracted.size).to eq(1)
95 96 97
    expect(extracted.first).to be_kind_of(CommitRange)
    expect(extracted.first.commit_from).to eq earlier_commit
    expect(extracted.first.commit_to).to eq commit
98
  end
99 100

  context 'with a project with an underscore' do
Douwe Maan's avatar
Douwe Maan committed
101 102 103 104
    let(:other_project) { create(:project, path: 'test_project') }
    let(:issue) { create(:issue, project: other_project) }

    before do
105
      other_project.team << [project.creator, :developer]
Douwe Maan's avatar
Douwe Maan committed
106
    end
107 108

    it 'handles project issue references' do
109
      subject.analyze("this refers issue #{issue.to_reference(project)}")
Douwe Maan's avatar
Douwe Maan committed
110
      extracted = subject.issues
111
      expect(extracted.size).to eq(1)
112
      expect(extracted).to match_array([issue])
113 114
    end
  end
115
end