debian-mirror-gitlab/spec/lib/gitlab/danger/teammate_spec.rb

213 lines
6.6 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2020-07-28 23:09:34 +05:30
require 'timecop'
2019-12-21 20:55:43 +05:30
require 'rspec-parameterized'
2019-09-04 21:01:54 +05:30
require 'gitlab/danger/teammate'
2021-01-03 14:25:43 +05:30
require 'active_support/testing/time_helpers'
2019-09-04 21:01:54 +05:30
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Danger::Teammate do
using RSpec::Parameterized::TableSyntax
2020-10-24 23:57:45 +05:30
subject { described_class.new(options) }
2019-12-21 20:55:43 +05:30
2020-07-28 23:09:34 +05:30
let(:tz_offset_hours) { 2.0 }
let(:options) do
{
2020-10-24 23:57:45 +05:30
'username' => 'luigi',
'projects' => projects,
'role' => role,
'markdown_name' => '[Luigi](https://gitlab.com/luigi) (`@luigi`)',
'tz_offset_hours' => tz_offset_hours
2020-07-28 23:09:34 +05:30
}
end
2020-10-24 23:57:45 +05:30
2020-07-28 23:09:34 +05:30
let(:capabilities) { ['reviewer backend'] }
2019-07-07 11:18:12 +05:30
let(:projects) { { project => capabilities } }
2019-09-04 21:01:54 +05:30
let(:role) { 'Engineer, Manage' }
let(:labels) { [] }
2019-07-07 11:18:12 +05:30
let(:project) { double }
2020-10-24 23:57:45 +05:30
describe '#==' do
it 'compares Teammate username' do
joe1 = described_class.new('username' => 'joe', 'projects' => projects)
joe2 = described_class.new('username' => 'joe', 'projects' => [])
jane1 = described_class.new('username' => 'jane', 'projects' => projects)
jane2 = described_class.new('username' => 'jane', 'projects' => [])
expect(joe1).to eq(joe2)
expect(jane1).to eq(jane2)
expect(jane1).not_to eq(nil)
expect(described_class.new('username' => nil)).not_to eq(nil)
end
end
describe '#to_h' do
it 'returns the given options' do
expect(subject.to_h).to eq(options)
end
end
2019-09-04 21:01:54 +05:30
context 'when having multiple capabilities' do
let(:capabilities) { ['reviewer backend', 'maintainer frontend', 'trainee_maintainer qa'] }
2019-07-07 11:18:12 +05:30
it '#reviewer? supports multiple roles per project' do
2019-09-04 21:01:54 +05:30
expect(subject.reviewer?(project, :backend, labels)).to be_truthy
2019-07-07 11:18:12 +05:30
end
it '#traintainer? supports multiple roles per project' do
2019-09-04 21:01:54 +05:30
expect(subject.traintainer?(project, :qa, labels)).to be_truthy
2019-07-07 11:18:12 +05:30
end
it '#maintainer? supports multiple roles per project' do
2019-09-04 21:01:54 +05:30
expect(subject.maintainer?(project, :frontend, labels)).to be_truthy
end
2019-12-26 22:10:19 +05:30
context 'when labels contain devops::create and the category is test' do
2019-12-04 20:38:33 +05:30
let(:labels) { ['devops::create'] }
2019-09-04 21:01:54 +05:30
2020-01-01 13:55:28 +05:30
context 'when role is Software Engineer in Test, Create' do
let(:role) { 'Software Engineer in Test, Create' }
2019-09-04 21:01:54 +05:30
it '#reviewer? returns true' do
expect(subject.reviewer?(project, :test, labels)).to be_truthy
end
it '#maintainer? returns false' do
expect(subject.maintainer?(project, :test, labels)).to be_falsey
end
2019-10-12 21:52:04 +05:30
context 'when hyperlink is mangled in the role' do
2020-01-01 13:55:28 +05:30
let(:role) { '<a href="#">Software Engineer in Test</a>, Create' }
2019-10-12 21:52:04 +05:30
it '#reviewer? returns true' do
expect(subject.reviewer?(project, :test, labels)).to be_truthy
end
end
2019-09-04 21:01:54 +05:30
end
2020-01-01 13:55:28 +05:30
context 'when role is Software Engineer in Test' do
let(:role) { 'Software Engineer in Test' }
2019-12-04 20:38:33 +05:30
it '#reviewer? returns false' do
expect(subject.reviewer?(project, :test, labels)).to be_falsey
end
end
2020-01-01 13:55:28 +05:30
context 'when role is Software Engineer in Test, Manage' do
let(:role) { 'Software Engineer in Test, Manage' }
2019-09-04 21:01:54 +05:30
it '#reviewer? returns false' do
expect(subject.reviewer?(project, :test, labels)).to be_falsey
end
end
2019-12-04 20:38:33 +05:30
context 'when role is Backend Engineer, Engineering Productivity' do
let(:role) { 'Backend Engineer, Engineering Productivity' }
it '#reviewer? returns true' do
expect(subject.reviewer?(project, :engineering_productivity, labels)).to be_truthy
end
it '#maintainer? returns false' do
expect(subject.maintainer?(project, :engineering_productivity, labels)).to be_falsey
end
2019-12-26 22:10:19 +05:30
context 'when capabilities include maintainer backend' do
let(:capabilities) { ['maintainer backend'] }
it '#maintainer? returns true' do
expect(subject.maintainer?(project, :engineering_productivity, labels)).to be_truthy
end
end
context 'when capabilities include trainee_maintainer backend' do
let(:capabilities) { ['trainee_maintainer backend'] }
it '#traintainer? returns true' do
expect(subject.traintainer?(project, :engineering_productivity, labels)).to be_truthy
end
end
2019-12-04 20:38:33 +05:30
end
2019-07-07 11:18:12 +05:30
end
end
2019-09-04 21:01:54 +05:30
context 'when having single capability' do
2019-07-07 11:18:12 +05:30
let(:capabilities) { 'reviewer backend' }
it '#reviewer? supports one role per project' do
2019-09-04 21:01:54 +05:30
expect(subject.reviewer?(project, :backend, labels)).to be_truthy
2019-07-07 11:18:12 +05:30
end
it '#traintainer? supports one role per project' do
2019-09-04 21:01:54 +05:30
expect(subject.traintainer?(project, :database, labels)).to be_falsey
2019-07-07 11:18:12 +05:30
end
it '#maintainer? supports one role per project' do
2019-09-04 21:01:54 +05:30
expect(subject.maintainer?(project, :frontend, labels)).to be_falsey
2019-07-07 11:18:12 +05:30
end
end
2019-12-21 20:55:43 +05:30
2020-07-28 23:09:34 +05:30
describe '#local_hour' do
2021-01-03 14:25:43 +05:30
include ActiveSupport::Testing::TimeHelpers
2020-07-28 23:09:34 +05:30
around do |example|
2021-01-03 14:25:43 +05:30
travel_to(Time.utc(2020, 6, 23, 10)) { example.run }
2020-07-28 23:09:34 +05:30
end
2019-12-21 20:55:43 +05:30
2020-07-28 23:09:34 +05:30
context 'when author is given' do
where(:tz_offset_hours, :expected_local_hour) do
-12 | 22
-10 | 0
2 | 12
4 | 14
12 | 22
2019-12-21 20:55:43 +05:30
end
2020-07-28 23:09:34 +05:30
with_them do
it 'returns the correct local_hour' do
expect(subject.local_hour).to eq(expected_local_hour)
2019-12-21 20:55:43 +05:30
end
end
end
2020-07-28 23:09:34 +05:30
end
2019-12-21 20:55:43 +05:30
2020-07-28 23:09:34 +05:30
describe '#markdown_name' do
2020-11-24 15:15:51 +05:30
it 'returns markdown name with timezone info' do
expect(subject.markdown_name).to eq("#{options['markdown_name']} (UTC+2)")
2019-12-21 20:55:43 +05:30
end
2020-11-24 15:15:51 +05:30
context 'when offset is 1.5' do
let(:tz_offset_hours) { 1.5 }
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
it 'returns markdown name with timezone info, not truncated' do
expect(subject.markdown_name).to eq("#{options['markdown_name']} (UTC+1.5)")
2019-12-21 20:55:43 +05:30
end
2020-11-24 15:15:51 +05:30
end
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
context 'when author is given' do
where(:tz_offset_hours, :author_offset, :diff_text) do
-12 | -10 | "2 hours behind `@mario`"
-10 | -12 | "2 hours ahead of `@mario`"
-10 | 2 | "12 hours behind `@mario`"
2 | 4 | "2 hours behind `@mario`"
4 | 2 | "2 hours ahead of `@mario`"
2 | 3 | "1 hour behind `@mario`"
3 | 2 | "1 hour ahead of `@mario`"
2 | 2 | "same timezone as `@mario`"
end
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
with_them do
it 'returns markdown name with timezone info' do
author = described_class.new(options.merge('username' => 'mario', 'tz_offset_hours' => author_offset))
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
floored_offset_hours = subject.__send__(:floored_offset_hours)
utc_offset = floored_offset_hours >= 0 ? "+#{floored_offset_hours}" : floored_offset_hours
2020-07-28 23:09:34 +05:30
2020-11-24 15:15:51 +05:30
expect(subject.markdown_name(author: author)).to eq("#{options['markdown_name']} (UTC#{utc_offset}, #{diff_text})")
2020-07-28 23:09:34 +05:30
end
end
2019-12-21 20:55:43 +05:30
end
end
2019-07-07 11:18:12 +05:30
end