debian-mirror-gitlab/spec/tooling/danger/changelog_spec.rb

323 lines
9.7 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
2021-04-17 20:07:23 +05:30
require 'gitlab-dangerfiles'
require 'gitlab/dangerfiles/spec_helper'
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
require_relative '../../../tooling/danger/changelog'
2021-04-17 20:07:23 +05:30
require_relative '../../../tooling/danger/project_helper'
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
RSpec.describe Tooling::Danger::Changelog do
2021-04-17 20:07:23 +05:30
include_context "with dangerfile"
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
let(:fake_danger) { DangerSpecHelper.fake_danger.include(described_class) }
let(:fake_project_helper) { double('fake-project-helper', helper: fake_helper).tap { |h| h.class.include(Tooling::Danger::ProjectHelper) } }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
subject(:changelog) { fake_danger.new(helper: fake_helper) }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
before do
allow(changelog).to receive(:project_helper).and_return(fake_project_helper)
end
describe '#required_reasons' do
subject { changelog.required_reasons }
context "added files contain a migration" do
let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
it { is_expected.to include(:db_changes) }
end
context "removed files contains a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to include(:feature_flag_removed) }
end
context "added files do not contain a migration" do
let(:changes) { changes_class.new([change_class.new('foo', :added, :frontend)]) }
it { is_expected.to be_empty }
end
context "removed files do not contain a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :backend)]) }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to be_empty }
end
end
2020-01-01 13:55:28 +05:30
2020-11-24 15:15:51 +05:30
describe '#required?' do
subject { changelog.required? }
context 'added files contain a migration' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to be_truthy }
end
context "removed files contains a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
it { is_expected.to be_truthy }
2020-11-24 15:15:51 +05:30
end
context 'added files do not contain a migration' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :added, :frontend)]) }
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to be_falsey }
end
context "removed files do not contain a feature flag" do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :backend)]) }
it { is_expected.to be_falsey }
2020-11-24 15:15:51 +05:30
end
end
describe '#optional?' do
2020-07-28 23:09:34 +05:30
let(:category_with_changelog) { :backend }
let(:label_with_changelog) { 'frontend' }
2021-03-11 19:13:27 +05:30
let(:category_without_changelog) { Tooling::Danger::Changelog::NO_CHANGELOG_CATEGORIES.first }
let(:label_without_changelog) { Tooling::Danger::Changelog::NO_CHANGELOG_LABELS.first }
2020-01-01 13:55:28 +05:30
2020-11-24 15:15:51 +05:30
subject { changelog.optional? }
2020-01-01 13:55:28 +05:30
2020-07-28 23:09:34 +05:30
context 'when MR contains only categories requiring no changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :modified, category_without_changelog)]) }
2020-01-01 13:55:28 +05:30
2020-07-28 23:09:34 +05:30
it 'is falsey' do
2020-01-01 13:55:28 +05:30
is_expected.to be_falsy
end
end
2020-07-28 23:09:34 +05:30
context 'when MR contains a label that require no changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :modified, category_with_changelog)]) }
2020-07-28 23:09:34 +05:30
let(:mr_labels) { [label_with_changelog, label_without_changelog] }
it 'is falsey' do
is_expected.to be_falsy
end
2020-01-01 13:55:28 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when MR contains a category that require changelog and a category that require no changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :modified, category_with_changelog), change_class.new('foo', :modified, category_without_changelog)]) }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
context 'with no labels' do
it 'is truthy' do
is_expected.to be_truthy
end
2020-01-01 13:55:28 +05:30
end
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
context 'with changelog label' do
let(:mr_labels) { ['feature'] }
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
it 'is truthy' do
is_expected.to be_truthy
end
2020-10-24 23:57:45 +05:30
end
2021-04-17 20:07:23 +05:30
context 'with no changelog label' do
let(:mr_labels) { ['tooling'] }
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
it 'is truthy' do
is_expected.to be_falsey
end
2020-10-24 23:57:45 +05:30
end
end
2020-01-01 13:55:28 +05:30
end
describe '#found' do
subject { changelog.found }
context 'added files contain a changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :added, :changelog)]) }
2020-01-01 13:55:28 +05:30
2021-04-17 20:07:23 +05:30
it { is_expected.to be_truthy }
2020-01-01 13:55:28 +05:30
end
context 'added files do not contain a changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('foo', :added, :backend)]) }
it { is_expected.to eq(nil) }
2020-01-01 13:55:28 +05:30
end
end
describe '#ee_changelog?' do
2020-04-22 19:07:51 +05:30
subject { changelog.ee_changelog? }
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
context 'is ee changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('ee/changelogs/unreleased/entry.yml', :added, :changelog)]) }
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
it { is_expected.to be_truthy }
2020-01-01 13:55:28 +05:30
end
2020-04-22 19:07:51 +05:30
context 'is not ee changelog' do
2021-04-17 20:07:23 +05:30
let(:changes) { changes_class.new([change_class.new('changelogs/unreleased/entry.yml', :added, :changelog)]) }
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
it { is_expected.to be_falsy }
2020-01-01 13:55:28 +05:30
end
end
2020-11-24 15:15:51 +05:30
describe '#modified_text' do
subject { changelog.modified_text }
2021-04-29 21:17:54 +05:30
context 'when in CI context' do
shared_examples 'changelog modified text' do |key|
specify do
expect(subject).to include('CHANGELOG.md was edited')
expect(subject).to include('bin/changelog -m 1234 "Fake Title"')
expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"')
end
end
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
before do
allow(fake_helper).to receive(:ci?).and_return(true)
end
context "when title is not changed from sanitization", :aggregate_failures do
let(:mr_title) { 'Fake Title' }
it_behaves_like 'changelog modified text'
end
context "when title needs sanitization", :aggregate_failures do
let(:mr_title) { 'DRAFT: Fake Title' }
it_behaves_like 'changelog modified text'
2021-03-08 18:12:59 +05:30
end
end
2021-04-29 21:17:54 +05:30
context 'when in local context' do
let(:mr_title) { 'Fake Title' }
before do
allow(fake_helper).to receive(:ci?).and_return(false)
end
2021-03-08 18:12:59 +05:30
specify do
expect(subject).to include('CHANGELOG.md was edited')
2021-04-29 21:17:54 +05:30
expect(subject).not_to include('bin/changelog')
2021-03-08 18:12:59 +05:30
end
2020-11-24 15:15:51 +05:30
end
end
2021-04-17 20:07:23 +05:30
describe '#required_texts' do
let(:mr_title) { 'Fake Title' }
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
subject { changelog.required_texts }
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
context 'when in CI context' do
before do
allow(fake_helper).to receive(:ci?).and_return(true)
2021-03-08 18:12:59 +05:30
end
2021-04-29 21:17:54 +05:30
shared_examples 'changelog required text' do |key|
specify do
expect(subject).to have_key(key)
expect(subject[key]).to include('CHANGELOG missing')
expect(subject[key]).to include('bin/changelog -m 1234 "Fake Title"')
expect(subject[key]).not_to include('--ee')
end
end
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
context 'with a new migration file' do
let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
context "when title is not changed from sanitization", :aggregate_failures do
it_behaves_like 'changelog required text', :db_changes
end
context "when title needs sanitization", :aggregate_failures do
let(:mr_title) { 'DRAFT: Fake Title' }
it_behaves_like 'changelog required text', :db_changes
end
2021-04-17 20:07:23 +05:30
end
2021-04-29 21:17:54 +05:30
context 'with a removed feature flag file' do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
it_behaves_like 'changelog required text', :feature_flag_removed
2021-03-08 18:12:59 +05:30
end
2020-11-24 15:15:51 +05:30
end
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
context 'when in local context' do
before do
allow(fake_helper).to receive(:ci?).and_return(false)
end
shared_examples 'changelog required text' do |key|
specify do
expect(subject).to have_key(key)
expect(subject[key]).to include('CHANGELOG missing')
expect(subject[key]).not_to include('bin/changelog')
expect(subject[key]).not_to include('--ee')
end
end
context 'with a new migration file' do
let(:changes) { changes_class.new([change_class.new('foo', :added, :migration)]) }
context "when title is not changed from sanitization", :aggregate_failures do
it_behaves_like 'changelog required text', :db_changes
end
context "when title needs sanitization", :aggregate_failures do
let(:mr_title) { 'DRAFT: Fake Title' }
2021-04-17 20:07:23 +05:30
2021-04-29 21:17:54 +05:30
it_behaves_like 'changelog required text', :db_changes
end
end
context 'with a removed feature flag file' do
let(:changes) { changes_class.new([change_class.new('foo', :deleted, :feature_flag)]) }
it_behaves_like 'changelog required text', :feature_flag_removed
end
2021-04-17 20:07:23 +05:30
end
2020-11-24 15:15:51 +05:30
end
2021-03-08 18:12:59 +05:30
describe '#optional_text' do
2020-11-24 15:15:51 +05:30
subject { changelog.optional_text }
2021-04-29 21:17:54 +05:30
context 'when in CI context' do
shared_examples 'changelog optional text' do |key|
specify do
expect(subject).to include('CHANGELOG missing')
expect(subject).to include('bin/changelog -m 1234 "Fake Title"')
expect(subject).to include('bin/changelog --ee -m 1234 "Fake Title"')
end
end
2021-03-08 18:12:59 +05:30
2021-04-29 21:17:54 +05:30
before do
allow(fake_helper).to receive(:ci?).and_return(true)
end
context "when title is not changed from sanitization", :aggregate_failures do
let(:mr_title) { 'Fake Title' }
it_behaves_like 'changelog optional text'
end
context "when title needs sanitization", :aggregate_failures do
let(:mr_title) { 'DRAFT: Fake Title' }
it_behaves_like 'changelog optional text'
2021-03-08 18:12:59 +05:30
end
end
2021-04-29 21:17:54 +05:30
context 'when in local context' do
let(:mr_title) { 'Fake Title' }
before do
allow(fake_helper).to receive(:ci?).and_return(false)
end
2021-03-08 18:12:59 +05:30
specify do
expect(subject).to include('CHANGELOG missing')
2021-04-29 21:17:54 +05:30
expect(subject).not_to include('bin/changelog')
2021-03-08 18:12:59 +05:30
end
2020-11-24 15:15:51 +05:30
end
end
2020-01-01 13:55:28 +05:30
end