gitlab-org--gitlab-foss/spec/lib/gitlab/reference_extractor_spec.rb

157 lines
5.3 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe Gitlab::ReferenceExtractor do
2015-03-27 07:58:23 -04:00
let(:project) { create(:project) }
2015-04-03 12:03:34 -04:00
subject { Gitlab::ReferenceExtractor.new(project, project.creator) }
2015-03-27 07:58:23 -04:00
it 'extracts username references' do
2015-03-27 07:58:23 -04:00
subject.analyze('this contains a @user reference')
2015-04-03 12:03:34 -04:00
expect(subject.references[:user]).to eq([[project, 'user']])
end
it 'extracts issue references' do
2015-03-27 07:58:23 -04:00
subject.analyze('this one talks about issue #1234')
2015-04-03 12:03:34 -04:00
expect(subject.references[:issue]).to eq([[project, '1234']])
end
it 'extracts JIRA issue references' do
2015-03-27 07:58:23 -04:00
subject.analyze('this one talks about issue JIRA-1234')
2015-04-03 12:03:34 -04:00
expect(subject.references[:issue]).to eq([[project, 'JIRA-1234']])
end
it 'extracts merge request references' do
2015-03-27 07:58:23 -04:00
subject.analyze("and here's !43, a merge request")
2015-04-03 12:03:34 -04:00
expect(subject.references[:merge_request]).to eq([[project, '43']])
end
it 'extracts snippet ids' do
2015-03-27 07:58:23 -04:00
subject.analyze('snippets like $12 get extracted as well')
2015-04-03 12:03:34 -04:00
expect(subject.references[:snippet]).to eq([[project, '12']])
end
it 'extracts commit shas' do
2015-03-27 07:58:23 -04:00
subject.analyze('commit shas 98cf0ae3 are pulled out as Strings')
2015-04-03 12:03:34 -04:00
expect(subject.references[:commit]).to eq([[project, '98cf0ae3']])
end
it 'extracts commit ranges' do
2015-03-27 07:58:23 -04:00
subject.analyze('here you go, a commit range: 98cf0ae3...98cf0ae4')
2015-04-03 12:03:34 -04:00
expect(subject.references[:commit_range]).to eq([[project, '98cf0ae3...98cf0ae4']])
end
it 'extracts multiple references and preserves their order' do
2015-03-27 07:58:23 -04:00
subject.analyze('@me and @you both care about this')
2015-04-03 12:03:34 -04:00
expect(subject.references[:user]).to eq([
[project, 'me'],
[project, 'you']
])
end
it 'leaves the original note unmodified' do
text = 'issue #123 is just the worst, @user'
2015-03-27 07:58:23 -04:00
subject.analyze(text)
expect(text).to eq('issue #123 is just the worst, @user')
end
it 'extracts no references for <pre>..</pre> blocks' do
2015-03-27 07:58:23 -04:00
subject.analyze("<pre>def puts '#1 issue'\nend\n</pre>```")
expect(subject.issues).to be_blank
end
it 'extracts no references for <code>..</code> blocks' do
2015-03-27 07:58:23 -04:00
subject.analyze("<code>def puts '!1 request'\nend\n</code>```")
expect(subject.merge_requests).to be_blank
end
it 'extracts no references for code blocks with language' do
2015-03-27 07:58:23 -04:00
subject.analyze("this code:\n```ruby\ndef puts '#1 issue'\nend\n```")
expect(subject.issues).to be_blank
end
it 'extracts issue references for invalid code blocks' do
2015-03-27 07:58:23 -04:00
subject.analyze('test: ```this one talks about issue #1234```')
2015-04-03 12:03:34 -04:00
expect(subject.references[:issue]).to eq([[project, '1234']])
end
it 'handles all possible kinds of references' do
2015-04-08 12:52:37 -04:00
accessors = described_class::TYPES.map { |t| "#{t}s".to_sym }
expect(subject).to respond_to(*accessors)
end
2015-04-03 12:03:34 -04:00
it 'accesses valid user objects' do
2015-03-27 07:58:23 -04:00
@u_foo = create(:user, username: 'foo')
@u_bar = create(:user, username: 'bar')
2015-04-03 12:03:34 -04:00
@u_offteam = create(:user, username: 'offteam')
2015-03-27 07:58:23 -04:00
project.team << [@u_foo, :reporter]
project.team << [@u_bar, :guest]
2015-03-27 07:58:23 -04:00
subject.analyze('@foo, @baduser, @bar, and @offteam')
2015-04-03 12:03:34 -04:00
expect(subject.users).to eq([@u_foo, @u_bar, @u_offteam])
2015-03-27 07:58:23 -04:00
end
2015-03-27 07:58:23 -04:00
it 'accesses valid issue objects' do
@i0 = create(:issue, project: project)
@i1 = create(:issue, project: project)
2015-03-27 07:58:23 -04:00
subject.analyze("##{@i0.iid}, ##{@i1.iid}, and #999.")
expect(subject.issues).to eq([@i0, @i1])
end
2015-03-27 07:58:23 -04:00
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')
2015-03-27 07:58:23 -04:00
subject.analyze("!999, !#{@m1.iid}, and !#{@m0.iid}.")
expect(subject.merge_requests).to eq([@m1, @m0])
end
2015-03-27 07:58:23 -04:00
it 'accesses valid snippets' do
@s0 = create(:project_snippet, project: project)
@s1 = create(:project_snippet, project: project)
@s2 = create(:project_snippet)
2015-03-27 07:58:23 -04:00
subject.analyze("$#{@s0.id}, $999, $#{@s2.id}, $#{@s1.id}")
expect(subject.snippets).to eq([@s0, @s1])
end
2015-03-27 07:58:23 -04:00
it 'accesses valid commits' do
commit = project.repository.commit('master')
2015-03-27 07:58:23 -04:00
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
2015-03-27 07:58:23 -04:00
it 'accesses valid commit ranges' do
commit = project.repository.commit('master')
earlier_commit = project.repository.commit('master~2')
2015-03-27 07:58:23 -04:00
subject.analyze("this references commits #{earlier_commit.sha[0..6]}...#{commit.sha[0..6]}")
extracted = subject.commit_ranges
expect(extracted.size).to eq(1)
expect(extracted[0][0].sha).to eq(earlier_commit.sha)
expect(extracted[0][0].message).to eq(earlier_commit.message)
expect(extracted[0][1].sha).to eq(commit.sha)
expect(extracted[0][1].message).to eq(commit.message)
end
context 'with a project with an underscore' do
2015-04-03 12:03:34 -04:00
let(:other_project) { create(:project, path: 'test_project') }
let(:issue) { create(:issue, project: other_project) }
before do
other_project.team << [project.creator, :developer]
end
it 'handles project issue references' do
2015-04-03 12:03:34 -04:00
subject.analyze("this refers issue #{other_project.path_with_namespace}##{issue.iid}")
2015-03-27 07:58:23 -04:00
extracted = subject.issues
expect(extracted.size).to eq(1)
expect(extracted).to eq([issue])
end
end
end