BigW Consortium Gitlab

reference_extractor_spec.rb 4.57 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::ReferenceExtractor, lib: true do
Douwe Maan committed
4
  let(:project) { create(:project) }
5

6 7
  before { project.team << [project.creator, :developer] }

8
  subject { Gitlab::ReferenceExtractor.new(project, project.creator) }
Douwe Maan committed
9

Douwe Maan committed
10
  it 'accesses valid user objects' do
Douwe Maan committed
11 12
    @u_foo = create(:user, username: 'foo')
    @u_bar = create(:user, username: 'bar')
Douwe Maan committed
13
    @u_offteam = create(:user, username: 'offteam')
14

Douwe Maan committed
15 16
    project.team << [@u_foo, :reporter]
    project.team << [@u_bar, :guest]
17

Douwe Maan committed
18
    subject.analyze('@foo, @baduser, @bar, and @offteam')
19
    expect(subject.users).to match_array([@u_foo, @u_bar, @u_offteam])
Douwe Maan committed
20
  end
21

22 23 24 25 26 27 28 29 30
  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{
31
      Inline code: `@foo`
32 33 34 35 36 37

      Code block:

      ```
      @bar
      ```
38

39
      Quote:
40 41

      > @offteam
42
    })
43
    expect(subject.users).to match_array([])
44 45
  end

Douwe Maan committed
46 47 48
  it 'accesses valid issue objects' do
    @i0 = create(:issue, project: project)
    @i1 = create(:issue, project: project)
49

50
    subject.analyze("#{@i0.to_reference}, #{@i1.to_reference}, and #{Issue.reference_prefix}999.")
51
    expect(subject.issues).to match_array([@i0, @i1])
Douwe Maan committed
52
  end
53

Douwe Maan committed
54
  it 'accesses valid merge requests' do
55 56
    @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')
57

Douwe Maan committed
58
    subject.analyze("!999, !#{@m1.iid}, and !#{@m0.iid}.")
59
    expect(subject.merge_requests).to match_array([@m1, @m0])
Douwe Maan committed
60
  end
61

62 63 64 65 66 67
  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}")
68
    expect(subject.labels).to match_array([@l0, @l1])
69 70
  end

Douwe Maan committed
71 72 73 74
  it 'accesses valid snippets' do
    @s0 = create(:project_snippet, project: project)
    @s1 = create(:project_snippet, project: project)
    @s2 = create(:project_snippet)
75

Douwe Maan committed
76
    subject.analyze("$#{@s0.id}, $999, $#{@s2.id}, $#{@s1.id}")
77
    expect(subject.snippets).to match_array([@s0, @s1])
Douwe Maan committed
78
  end
79

Douwe Maan committed
80
  it 'accesses valid commits' do
81
    commit = project.commit('master')
82

Douwe Maan committed
83 84 85 86 87 88
    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
89

Douwe Maan committed
90
  it 'accesses valid commit ranges' do
91 92
    commit = project.commit('master')
    earlier_commit = project.commit('master~2')
93

Douwe Maan committed
94
    subject.analyze("this references commits #{earlier_commit.sha[0..6]}...#{commit.sha[0..6]}")
95

Douwe Maan committed
96 97
    extracted = subject.commit_ranges
    expect(extracted.size).to eq(1)
98 99 100
    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
101
  end
102

Drew Blessing committed
103 104 105 106 107 108
  context 'with an external issue tracker' do
    let(:project) { create(:jira_project) }
    subject { described_class.new(project, project.creator) }

    it 'returns JIRA issues for a JIRA-integrated project' do
      subject.analyze('JIRA-123 and FOOBAR-4567')
109 110
      expect(subject.issues).to eq [ExternalIssue.new('JIRA-123', project),
                                    ExternalIssue.new('FOOBAR-4567', project)]
Drew Blessing committed
111 112 113
    end
  end

114
  context 'with a project with an underscore' do
Douwe Maan committed
115 116 117 118
    let(:other_project) { create(:project, path: 'test_project') }
    let(:issue) { create(:issue, project: other_project) }

    before do
119
      other_project.team << [project.creator, :developer]
Douwe Maan committed
120
    end
121 122

    it 'handles project issue references' do
123
      subject.analyze("this refers issue #{issue.to_reference(project)}")
Douwe Maan committed
124
      extracted = subject.issues
125
      expect(extracted.size).to eq(1)
126
      expect(extracted).to match_array([issue])
127 128
    end
  end
129 130 131 132 133 134

  describe '#all' do
    let(:issue) { create(:issue, project: project) }
    let(:label) { create(:label, project: project) }
    let(:text) { "Ref. #{issue.to_reference} and #{label.to_reference}" }

135 136 137 138
    before do
      project.team << [project.creator, :developer]
      subject.analyze(text)
    end
139 140 141 142 143

    it 'returns all referables' do
      expect(subject.all).to match_array([issue, label])
    end
  end
144 145 146 147 148

  describe '.references_pattern' do
    subject { described_class.references_pattern }
    it { is_expected.to be_kind_of Regexp }
  end
149
end