2019-10-16 09:07:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-07 16:19:21 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 18:09:01 +00:00
|
|
|
RSpec.describe BranchesFinder do
|
2016-07-07 16:19:21 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-01-25 21:49:52 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-07-07 16:19:21 +00:00
|
|
|
let(:repository) { project.repository }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
context 'sort only' do
|
|
|
|
it 'sorts by name' do
|
|
|
|
branches_finder = described_class.new(repository, {})
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq("'test'")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sorts by recently_updated' do
|
2016-07-27 03:56:17 +00:00
|
|
|
branches_finder = described_class.new(repository, { sort: 'updated_desc' })
|
2016-07-12 18:00:43 +00:00
|
|
|
|
2016-07-07 16:19:21 +00:00
|
|
|
result = branches_finder.execute
|
|
|
|
|
2016-07-31 02:09:19 +00:00
|
|
|
recently_updated_branch = repository.branches.max do |a, b|
|
2016-09-27 03:07:31 +00:00
|
|
|
repository.commit(a.dereferenced_target).committed_date <=> repository.commit(b.dereferenced_target).committed_date
|
2016-07-31 02:09:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(result.first.name).to eq(recently_updated_branch.name)
|
2016-07-07 16:19:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sorts by last_updated' do
|
2016-07-27 03:56:17 +00:00
|
|
|
branches_finder = described_class.new(repository, { sort: 'updated_asc' })
|
2016-07-07 16:19:21 +00:00
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('feature')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filter only' do
|
|
|
|
it 'filters branches by name' do
|
|
|
|
branches_finder = described_class.new(repository, { search: 'fix' })
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('fix')
|
2017-10-22 14:50:58 +00:00
|
|
|
expect(result.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters branches by name ignoring letter case' do
|
|
|
|
branches_finder = described_class.new(repository, { search: 'FiX' })
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('fix')
|
2016-07-07 16:19:21 +00:00
|
|
|
expect(result.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not find any branch with that name' do
|
|
|
|
branches_finder = described_class.new(repository, { search: 'random' })
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.count).to eq(0)
|
|
|
|
end
|
2019-06-18 14:20:11 +00:00
|
|
|
|
|
|
|
it 'filters branches by provided names' do
|
2019-12-20 09:24:38 +00:00
|
|
|
branches_finder = described_class.new(repository, { names: %w[fix csv lfs does-not-exist] })
|
2019-06-18 14:20:11 +00:00
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.count).to eq(3)
|
|
|
|
expect(result.map(&:name)).to eq(%w{csv fix lfs})
|
|
|
|
end
|
2016-07-07 16:19:21 +00:00
|
|
|
|
2019-11-05 12:06:20 +00:00
|
|
|
it 'filters branches by name that begins with' do
|
|
|
|
params = { search: '^feature_' }
|
2016-07-07 16:19:21 +00:00
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('feature_conflict')
|
2019-11-05 12:06:20 +00:00
|
|
|
expect(result.count).to eq(1)
|
2016-07-07 16:19:21 +00:00
|
|
|
end
|
|
|
|
|
2019-11-05 12:06:20 +00:00
|
|
|
it 'filters branches by name that ends with' do
|
|
|
|
params = { search: 'feature$' }
|
2018-10-11 14:25:30 +00:00
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('feature')
|
2019-11-05 12:06:20 +00:00
|
|
|
expect(result.count).to eq(1)
|
2018-10-11 14:25:30 +00:00
|
|
|
end
|
|
|
|
|
2019-11-05 12:06:20 +00:00
|
|
|
it 'filters branches by nonexistent name that begins with' do
|
|
|
|
params = { search: '^nope' }
|
2016-07-07 16:19:21 +00:00
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
2019-11-05 12:06:20 +00:00
|
|
|
expect(result.count).to eq(0)
|
2016-07-07 16:19:21 +00:00
|
|
|
end
|
2018-10-11 14:25:30 +00:00
|
|
|
|
2019-11-05 12:06:20 +00:00
|
|
|
it 'filters branches by nonexistent name that ends with' do
|
|
|
|
params = { search: 'nope$' }
|
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filter and sort' do
|
|
|
|
it 'filters branches by name and sorts by recently_updated' do
|
|
|
|
params = { sort: 'updated_desc', search: 'feat' }
|
2018-10-11 14:25:30 +00:00
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('feature_conflict')
|
2019-11-05 12:06:20 +00:00
|
|
|
expect(result.count).to eq(2)
|
2018-10-11 14:25:30 +00:00
|
|
|
end
|
|
|
|
|
2019-11-05 12:06:20 +00:00
|
|
|
it 'filters branches by name and sorts by recently_updated, with exact matches first' do
|
|
|
|
params = { sort: 'updated_desc', search: 'feature' }
|
2018-10-11 14:25:30 +00:00
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('feature')
|
2019-11-05 12:06:20 +00:00
|
|
|
expect(result.second.name).to eq('feature_conflict')
|
|
|
|
expect(result.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters branches by name and sorts by last_updated' do
|
|
|
|
params = { sort: 'updated_asc', search: 'feature' }
|
|
|
|
branches_finder = described_class.new(repository, params)
|
|
|
|
|
|
|
|
result = branches_finder.execute
|
|
|
|
|
|
|
|
expect(result.first.name).to eq('feature')
|
|
|
|
expect(result.count).to eq(2)
|
2018-10-11 14:25:30 +00:00
|
|
|
end
|
2016-07-07 16:19:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|