2015-04-21 18:04:20 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe CommitRange, models: true do
|
2015-05-12 17:58:29 -04:00
|
|
|
describe 'modules' do
|
|
|
|
subject { described_class }
|
|
|
|
|
|
|
|
it { is_expected.to include_module(Referable) }
|
|
|
|
end
|
|
|
|
|
2017-01-26 17:44:58 -05:00
|
|
|
let!(:project) { create(:project, :public, :repository) }
|
2015-11-30 15:10:52 -05:00
|
|
|
let!(:commit1) { project.commit("HEAD~2") }
|
|
|
|
let!(:commit2) { project.commit }
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
let(:sha_from) { commit1.short_id }
|
|
|
|
let(:sha_to) { commit2.short_id }
|
|
|
|
|
|
|
|
let(:full_sha_from) { commit1.id }
|
|
|
|
let(:full_sha_to) { commit2.id }
|
|
|
|
|
|
|
|
let(:range) { described_class.new("#{sha_from}...#{sha_to}", project) }
|
|
|
|
let(:range2) { described_class.new("#{sha_from}..#{sha_to}", project) }
|
2015-04-21 18:04:20 -04:00
|
|
|
|
|
|
|
it 'raises ArgumentError when given an invalid range string' do
|
2015-11-30 15:10:52 -05:00
|
|
|
expect { described_class.new("Foo", project) }.to raise_error(ArgumentError)
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2016-05-02 12:15:25 -04:00
|
|
|
describe '#initialize' do
|
|
|
|
it 'does not modify strings in-place' do
|
|
|
|
input = "#{sha_from}...#{sha_to} "
|
|
|
|
|
|
|
|
described_class.new(input, project)
|
|
|
|
|
|
|
|
expect(input).to eq("#{sha_from}...#{sha_to} ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-12 17:58:29 -04:00
|
|
|
describe '#to_s' do
|
|
|
|
it 'is correct for three-dot syntax' do
|
2015-11-30 15:10:52 -05:00
|
|
|
expect(range.to_s).to eq "#{full_sha_from}...#{full_sha_to}"
|
2015-05-12 17:58:29 -04:00
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
|
2015-05-12 17:58:29 -04:00
|
|
|
it 'is correct for two-dot syntax' do
|
2015-11-30 15:10:52 -05:00
|
|
|
expect(range2.to_s).to eq "#{full_sha_from}..#{full_sha_to}"
|
2015-05-12 17:58:29 -04:00
|
|
|
end
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#to_reference' do
|
2016-11-02 19:49:13 -04:00
|
|
|
let(:cross) { create(:empty_project, namespace: project.namespace) }
|
2015-05-02 23:11:21 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
it 'returns a String reference to the object' do
|
2015-12-01 06:58:45 -05:00
|
|
|
expect(range.to_reference).to eq "#{full_sha_from}...#{full_sha_to}"
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
2015-12-01 06:58:45 -05:00
|
|
|
expect(range2.to_reference).to eq "#{full_sha_from}..#{full_sha_to}"
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a cross-project reference' do
|
2016-11-02 19:49:13 -04:00
|
|
|
expect(range.to_reference(cross)).to eq "#{project.path}@#{full_sha_from}...#{full_sha_to}"
|
2015-12-01 06:58:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#reference_link_text' do
|
2016-11-02 19:49:13 -04:00
|
|
|
let(:cross) { create(:empty_project, namespace: project.namespace) }
|
2015-12-01 06:58:45 -05:00
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(range.reference_link_text).to eq "#{sha_from}...#{sha_to}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a String reference to the object' do
|
|
|
|
expect(range2.reference_link_text).to eq "#{sha_from}..#{sha_to}"
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a cross-project reference' do
|
2016-11-02 19:49:13 -04:00
|
|
|
expect(range.reference_link_text(cross)).to eq "#{project.path}@#{sha_from}...#{sha_to}"
|
2015-05-02 23:11:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-21 18:04:20 -04:00
|
|
|
describe '#to_param' do
|
|
|
|
it 'includes the correct keys' do
|
|
|
|
expect(range.to_param.keys).to eq %i(from to)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the correct values for a three-dot range' do
|
2015-11-30 15:10:52 -05:00
|
|
|
expect(range.to_param).to eq({ from: full_sha_from, to: full_sha_to })
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the correct values for a two-dot range' do
|
2015-11-30 15:10:52 -05:00
|
|
|
expect(range2.to_param).to eq({ from: full_sha_from + '^', to: full_sha_to })
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-25 14:58:49 -04:00
|
|
|
describe '#exclude_start?' do
|
2015-04-21 18:04:20 -04:00
|
|
|
it 'is false for three-dot ranges' do
|
2015-04-25 14:58:49 -04:00
|
|
|
expect(range.exclude_start?).to eq false
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true for two-dot ranges' do
|
2015-04-25 14:58:49 -04:00
|
|
|
expect(range2.exclude_start?).to eq true
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#valid_commits?' do
|
2015-11-30 15:10:52 -05:00
|
|
|
context 'with a valid repo' do
|
|
|
|
before do
|
|
|
|
expect(project).to receive(:valid_repo?).and_return(true)
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
it 'is false when `sha_from` is invalid' do
|
|
|
|
expect(project).to receive(:commit).with(sha_from).and_return(nil)
|
|
|
|
expect(project).to receive(:commit).with(sha_to).and_call_original
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
expect(range).not_to be_valid_commits
|
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
it 'is false when `sha_to` is invalid' do
|
|
|
|
expect(project).to receive(:commit).with(sha_from).and_call_original
|
|
|
|
expect(project).to receive(:commit).with(sha_to).and_return(nil)
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
expect(range).not_to be_valid_commits
|
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
it 'is true when both `sha_from` and `sha_to` are valid' do
|
|
|
|
expect(range).to be_valid_commits
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
2015-11-30 15:10:52 -05:00
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
context 'without a valid repo' do
|
|
|
|
before do
|
|
|
|
expect(project).to receive(:valid_repo?).and_return(false)
|
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
|
2015-11-30 15:10:52 -05:00
|
|
|
it 'returns false' do
|
|
|
|
expect(range).not_to be_valid_commits
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-26 07:38:28 -04:00
|
|
|
|
|
|
|
describe '#has_been_reverted?' do
|
2016-11-29 08:47:43 -05:00
|
|
|
let(:issue) { create(:issue) }
|
|
|
|
let(:user) { issue.author }
|
2016-05-26 07:38:28 -04:00
|
|
|
|
2016-11-29 08:47:43 -05:00
|
|
|
it 'returns true if the commit has been reverted' do
|
2016-05-26 07:38:28 -04:00
|
|
|
create(:note_on_issue,
|
2016-06-01 12:56:06 -04:00
|
|
|
noteable: issue,
|
2016-05-26 07:38:28 -04:00
|
|
|
system: true,
|
2016-11-29 08:47:43 -05:00
|
|
|
note: commit1.revert_description(user),
|
2016-06-01 12:56:06 -04:00
|
|
|
project: issue.project)
|
2016-05-26 07:38:28 -04:00
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
expect_any_instance_of(Commit).to receive(:reverts_commit?)
|
|
|
|
.with(commit1, user)
|
|
|
|
.and_return(true)
|
2016-05-26 07:38:28 -04:00
|
|
|
|
2016-11-29 08:47:43 -05:00
|
|
|
expect(commit1.has_been_reverted?(user, issue)).to eq(true)
|
2016-05-26 07:38:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false a commit has not been reverted' do
|
2016-11-29 08:47:43 -05:00
|
|
|
expect(commit1.has_been_reverted?(user, issue)).to eq(false)
|
2016-05-26 07:38:28 -04:00
|
|
|
end
|
|
|
|
end
|
2015-04-21 18:04:20 -04:00
|
|
|
end
|