BigW Consortium Gitlab

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

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

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

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

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

Douwe Maan committed
19 20 21
  it 'accesses valid issue objects' do
    @i0 = create(:issue, project: project)
    @i1 = create(:issue, project: project)
22

Douwe Maan committed
23 24 25
    subject.analyze("##{@i0.iid}, ##{@i1.iid}, and #999.")
    expect(subject.issues).to eq([@i0, @i1])
  end
26

Douwe Maan committed
27 28 29
  it 'accesses valid merge requests' do
    @m0 = create(:merge_request, source_project: project, target_project: project, source_branch: 'aaa')
    @m1 = create(:merge_request, source_project: project, target_project: project, source_branch: 'bbb')
30

Douwe Maan committed
31 32 33
    subject.analyze("!999, !#{@m1.iid}, and !#{@m0.iid}.")
    expect(subject.merge_requests).to eq([@m1, @m0])
  end
34

35 36 37 38 39 40 41 42 43
  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}")
    expect(subject.labels).to eq([@l0, @l1])
  end

Douwe Maan committed
44 45 46 47
  it 'accesses valid snippets' do
    @s0 = create(:project_snippet, project: project)
    @s1 = create(:project_snippet, project: project)
    @s2 = create(:project_snippet)
48

Douwe Maan committed
49 50 51
    subject.analyze("$#{@s0.id}, $999, $#{@s2.id}, $#{@s1.id}")
    expect(subject.snippets).to eq([@s0, @s1])
  end
52

Douwe Maan committed
53
  it 'accesses valid commits' do
54
    commit = project.commit('master')
55

Douwe Maan committed
56 57 58 59 60 61
    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
62

Douwe Maan committed
63
  it 'accesses valid commit ranges' do
64 65
    commit = project.commit('master')
    earlier_commit = project.commit('master~2')
66

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

Douwe Maan committed
69 70
    extracted = subject.commit_ranges
    expect(extracted.size).to eq(1)
71 72 73
    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
74
  end
75 76

  context 'with a project with an underscore' do
Douwe Maan committed
77 78 79 80 81 82
    let(:other_project) { create(:project, path: 'test_project') }
    let(:issue) { create(:issue, project: other_project) }

    before do
      other_project.team << [project.creator, :developer]
    end
83 84

    it 'handles project issue references' do
Douwe Maan committed
85
      subject.analyze("this refers issue #{other_project.path_with_namespace}##{issue.iid}")
Douwe Maan committed
86
      extracted = subject.issues
87 88 89 90
      expect(extracted.size).to eq(1)
      expect(extracted).to eq([issue])
    end
  end
91
end