2019-12-12 10:08:41 -05:00
# frozen_string_literal: true
2021-03-12 07:09:33 -05:00
require 'gitlab-dangerfiles'
require 'gitlab/dangerfiles/spec_helper'
2019-12-12 10:08:41 -05:00
2021-01-20 07:11:06 -05:00
require_relative '../../../tooling/danger/changelog'
2021-03-12 07:09:33 -05:00
require_relative '../../../tooling/danger/project_helper'
2019-12-12 10:08:41 -05:00
2021-01-20 07:11:06 -05:00
RSpec . describe Tooling :: Danger :: Changelog do
2021-03-12 07:09:33 -05:00
include_context " with dangerfile "
2019-12-12 10:08:41 -05:00
2021-03-12 07:09:33 -05:00
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 ) } }
2019-12-12 10:08:41 -05:00
2021-02-24 07:10:54 -05:00
subject ( :changelog ) { fake_danger . new ( helper : fake_helper ) }
2021-03-12 07:09:33 -05:00
before do
allow ( changelog ) . to receive ( :project_helper ) . and_return ( fake_project_helper )
end
2021-05-21 11:10:51 -04:00
describe '#check_changelog_commit_categories' do
context 'when all changelog commits are correct' do
it 'does not produce any messages' do
commit = double ( :commit , message : " foo \n Changelog: fixed " )
allow ( changelog ) . to receive ( :changelog_commits ) . and_return ( [ commit ] )
2021-05-14 05:10:24 -04:00
2021-05-21 11:10:51 -04:00
expect ( changelog ) . not_to receive ( :fail )
2021-05-14 05:10:24 -04:00
2021-05-21 11:10:51 -04:00
changelog . check_changelog_commit_categories
end
2021-05-14 05:10:24 -04:00
end
2021-05-21 11:10:51 -04:00
context 'when a commit has an incorrect trailer' do
it 'adds a message' do
commit = double ( :commit , message : " foo \n Changelog: foo " , sha : '123' )
allow ( changelog ) . to receive ( :changelog_commits ) . and_return ( [ commit ] )
2021-05-14 05:10:24 -04:00
2021-05-21 11:10:51 -04:00
expect ( changelog ) . to receive ( :fail )
changelog . check_changelog_commit_categories
end
2021-05-14 05:10:24 -04:00
end
2021-05-21 11:10:51 -04:00
end
describe '#check_changelog_trailer' do
subject { changelog . check_changelog_trailer ( commit ) }
2021-05-14 05:10:24 -04:00
context " when commit include a changelog trailer with an unknown category " do
let ( :commit ) { double ( 'commit' , message : " Hello world \n \n Changelog: foo " , sha : " abc123 " ) }
it { is_expected . to have_attributes ( errors : [ " Commit #{ commit . sha } uses an invalid changelog category: foo " ] ) }
end
2021-06-02 08:10:05 -04:00
context 'when a commit uses the wrong casing for a trailer' do
let ( :commit ) { double ( 'commit' , message : " Hello world \n \n changelog: foo " , sha : " abc123 " ) }
it { is_expected . to have_attributes ( errors : [ " The changelog trailer for commit #{ commit . sha } must be `Changelog` (starting with a capital C), not `changelog` " ] ) }
end
2021-05-14 05:10:24 -04:00
described_class :: CATEGORIES . each do | category |
context " when commit include a changelog trailer with category set to ' #{ category } ' " do
let ( :commit ) { double ( 'commit' , message : " Hello world \n \n Changelog: #{ category } " , sha : " abc123 " ) }
it { is_expected . to have_attributes ( errors : [ ] ) }
end
end
end
describe '#check_changelog_path' do
let ( :changelog_path ) { 'changelog-path.yml' }
let ( :foss_change ) { nil }
let ( :ee_change ) { nil }
let ( :changelog_change ) { nil }
let ( :changes ) { changes_class . new ( [ foss_change , ee_change , changelog_change ] . compact ) }
before do
allow ( changelog ) . to receive ( :present? ) . and_return ( true )
end
subject { changelog . check_changelog_path }
context " when changelog is not present " do
before do
allow ( changelog ) . to receive ( :present? ) . and_return ( false )
end
it { is_expected . to have_attributes ( errors : [ ] , warnings : [ ] , markdowns : [ ] , messages : [ ] ) }
end
context " with EE changes " do
let ( :ee_change ) { change_class . new ( 'ee/app/models/foo.rb' , :added , :backend ) }
context " and a non-EE changelog, and changelog not required " do
before do
allow ( changelog ) . to receive ( :required? ) . and_return ( false )
2021-05-21 11:10:51 -04:00
allow ( changelog ) . to receive ( :ee_changelog? ) . and_return ( false )
2021-05-14 05:10:24 -04:00
end
2021-05-27 08:10:56 -04:00
it { is_expected . to have_attributes ( warnings : [ " This MR changes code in `ee/`, but its Changelog commit is missing the [`EE: true` trailer](https://docs.gitlab.com/ee/development/changelog.html # gitlab-enterprise-changes). Consider adding it to your Changelog commits. " ] ) }
2021-05-14 05:10:24 -04:00
end
context " and a EE changelog " do
2021-05-21 11:10:51 -04:00
before do
allow ( changelog ) . to receive ( :ee_changelog? ) . and_return ( true )
end
2021-05-14 05:10:24 -04:00
it { is_expected . to have_attributes ( errors : [ ] , warnings : [ ] , markdowns : [ ] , messages : [ ] ) }
context " and there are DB changes " do
let ( :foss_change ) { change_class . new ( 'db/migrate/foo.rb' , :added , :migration ) }
2021-05-27 08:10:56 -04:00
it { is_expected . to have_attributes ( warnings : [ " This MR has a Changelog commit with the `EE: true` trailer, but there are database changes which [requires](https://docs.gitlab.com/ee/development/changelog.html # what-warrants-a-changelog-entry) the Changelog commit to not have the `EE: true` trailer. Consider removing the `EE: true` trailer from your commits. " ] ) }
2021-05-14 05:10:24 -04:00
end
end
end
context " with no EE changes " do
let ( :foss_change ) { change_class . new ( 'app/models/foo.rb' , :added , :backend ) }
context " and a non-EE changelog " do
2021-05-21 11:10:51 -04:00
before do
allow ( changelog ) . to receive ( :ee_changelog? ) . and_return ( false )
end
2021-05-14 05:10:24 -04:00
it { is_expected . to have_attributes ( errors : [ ] , warnings : [ ] , markdowns : [ ] , messages : [ ] ) }
end
context " and a EE changelog " do
2021-05-21 11:10:51 -04:00
before do
allow ( changelog ) . to receive ( :ee_changelog? ) . and_return ( true )
end
2021-05-14 05:10:24 -04:00
2021-05-27 08:10:56 -04:00
it { is_expected . to have_attributes ( warnings : [ " This MR has a Changelog commit for EE, but no code changes in `ee/`. Consider removing the `EE: true` trailer from your commits. " ] ) }
2021-05-14 05:10:24 -04:00
end
end
end
2021-02-24 07:10:54 -05:00
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 ) ] ) }
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 ) ] ) }
it { is_expected . to be_empty }
end
end
2019-12-12 10:08:41 -05:00
2020-08-20 23:10:16 -04:00
describe '#required?' do
subject { changelog . required? }
context 'added files contain a migration' do
2021-02-24 07:10:54 -05:00
let ( :changes ) { changes_class . new ( [ change_class . new ( 'foo' , :added , :migration ) ] ) }
2020-08-20 23:10:16 -04:00
2021-02-24 07:10:54 -05:00
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-08-20 23:10:16 -04:00
end
context 'added files do not contain a migration' do
2021-02-24 07:10:54 -05:00
let ( :changes ) { changes_class . new ( [ change_class . new ( 'foo' , :added , :frontend ) ] ) }
2020-08-20 23:10:16 -04:00
2021-02-24 07:10:54 -05:00
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-08-20 23:10:16 -04:00
end
end
describe '#optional?' do
2020-07-15 14:09:09 -04:00
let ( :category_with_changelog ) { :backend }
let ( :label_with_changelog ) { 'frontend' }
2021-01-20 07:11:06 -05:00
let ( :category_without_changelog ) { Tooling :: Danger :: Changelog :: NO_CHANGELOG_CATEGORIES . first }
let ( :label_without_changelog ) { Tooling :: Danger :: Changelog :: NO_CHANGELOG_LABELS . first }
2019-12-12 10:08:41 -05:00
2020-08-20 23:10:16 -04:00
subject { changelog . optional? }
2019-12-12 10:08:41 -05:00
2020-07-15 14:09:09 -04:00
context 'when MR contains only categories requiring no changelog' do
2021-02-24 07:10:54 -05:00
let ( :changes ) { changes_class . new ( [ change_class . new ( 'foo' , :modified , category_without_changelog ) ] ) }
2019-12-12 10:08:41 -05:00
2020-07-15 14:09:09 -04:00
it 'is falsey' do
2019-12-12 10:08:41 -05:00
is_expected . to be_falsy
end
end
2020-07-15 14:09:09 -04:00
context 'when MR contains a label that require no changelog' do
2021-02-24 07:10:54 -05:00
let ( :changes ) { changes_class . new ( [ change_class . new ( 'foo' , :modified , category_with_changelog ) ] ) }
2020-07-15 14:09:09 -04:00
let ( :mr_labels ) { [ label_with_changelog , label_without_changelog ] }
it 'is falsey' do
is_expected . to be_falsy
end
2019-12-12 10:08:41 -05:00
end
2020-07-15 14:09:09 -04:00
context 'when MR contains a category that require changelog and a category that require no changelog' do
2021-02-24 07:10:54 -05:00
let ( :changes ) { changes_class . new ( [ change_class . new ( 'foo' , :modified , category_with_changelog ) , change_class . new ( 'foo' , :modified , category_without_changelog ) ] ) }
2019-12-12 10:08:41 -05:00
2021-02-24 07:10:54 -05:00
context 'with no labels' do
it 'is truthy' do
is_expected . to be_truthy
end
2019-12-12 10:08:41 -05:00
end
2020-07-28 14:09:36 -04:00
2021-02-24 07:10:54 -05:00
context 'with changelog label' do
2021-10-27 08:09:41 -04:00
let ( :mr_labels ) { [ 'type::feature' ] }
2020-07-28 14:09:36 -04:00
2021-02-24 07:10:54 -05:00
it 'is truthy' do
is_expected . to be_truthy
end
2020-07-28 14:09:36 -04:00
end
2021-02-24 07:10:54 -05:00
context 'with no changelog label' do
2021-10-27 08:09:41 -04:00
let ( :mr_labels ) { [ 'type::tooling' ] }
2020-07-28 14:09:36 -04:00
2021-02-24 07:10:54 -05:00
it 'is truthy' do
is_expected . to be_falsey
end
2020-07-28 14:09:36 -04:00
end
end
2019-12-12 10:08:41 -05:00
end
2021-05-14 05:10:24 -04:00
describe '#present?' do
2021-05-21 11:10:51 -04:00
it 'returns true when a Changelog commit is present' do
allow ( changelog )
. to receive ( :valid_changelog_commits )
. and_return ( [ double ( :commit ) ] )
2019-12-12 10:08:41 -05:00
2021-05-21 11:10:51 -04:00
expect ( changelog ) . to be_present
2019-12-12 10:08:41 -05:00
end
2021-05-21 11:10:51 -04:00
it 'returns false when a Changelog commit is missing' do
allow ( changelog ) . to receive ( :valid_changelog_commits ) . and_return ( [ ] )
2021-02-24 07:10:54 -05:00
2021-05-21 11:10:51 -04:00
expect ( changelog ) . not_to be_present
2019-12-12 10:08:41 -05:00
end
end
2021-05-21 11:10:51 -04:00
describe '#changelog_commits' do
it 'returns the commits that include a Changelog trailer' do
commit1 = double ( :commit , message : " foo \n Changelog: fixed " )
commit2 = double ( :commit , message : " bar \n Changelog: kittens " )
commit3 = double ( :commit , message : 'testing' )
git = double ( :git )
2019-12-12 10:08:41 -05:00
2021-05-21 11:10:51 -04:00
allow ( changelog ) . to receive ( :git ) . and_return ( git )
allow ( git ) . to receive ( :commits ) . and_return ( [ commit1 , commit2 , commit3 ] )
2019-12-12 10:08:41 -05:00
2021-05-21 11:10:51 -04:00
expect ( changelog . changelog_commits ) . to eq ( [ commit1 , commit2 ] )
2019-12-12 10:08:41 -05:00
end
2021-05-21 11:10:51 -04:00
end
describe '#valid_changelog_commits' do
it 'returns the commits with a valid Changelog trailer' do
commit1 = double ( :commit , message : " foo \n Changelog: fixed " )
commit2 = double ( :commit , message : " bar \n Changelog: kittens " )
2019-12-12 10:08:41 -05:00
2021-05-21 11:10:51 -04:00
allow ( changelog )
. to receive ( :changelog_commits )
. and_return ( [ commit1 , commit2 ] )
2019-12-12 10:08:41 -05:00
2021-05-21 11:10:51 -04:00
expect ( changelog . valid_changelog_commits ) . to eq ( [ commit1 ] )
2019-12-12 10:08:41 -05:00
end
end
2020-08-20 23:10:16 -04:00
2021-05-21 11:10:51 -04:00
describe '#ee_changelog?' do
it 'returns true when an EE changelog commit is present' do
commit = double ( :commit , message : " foo \n EE: true " )
2021-05-14 05:10:24 -04:00
2021-05-21 11:10:51 -04:00
allow ( changelog ) . to receive ( :changelog_commits ) . and_return ( [ commit ] )
2021-05-14 05:10:24 -04:00
2021-05-21 11:10:51 -04:00
expect ( changelog . ee_changelog? ) . to eq ( true )
2021-05-14 05:10:24 -04:00
end
2021-05-21 11:10:51 -04:00
it 'returns false when an EE changelog commit is missing' do
commit = double ( :commit , message : 'foo' )
allow ( changelog ) . to receive ( :changelog_commits ) . and_return ( [ commit ] )
2021-05-14 05:10:24 -04:00
2021-05-21 11:10:51 -04:00
expect ( changelog . ee_changelog? ) . to eq ( false )
2021-05-14 05:10:24 -04:00
end
end
2020-08-20 23:10:16 -04:00
describe '#modified_text' do
subject { changelog . modified_text }
2021-03-23 20:09:26 -04:00
context 'when in CI context' do
shared_examples 'changelog modified text' do | key |
specify do
expect ( subject ) . to include ( 'CHANGELOG.md was edited' )
2021-05-21 11:10:51 -04:00
expect ( subject ) . to include ( '`Changelog` trailer' )
expect ( subject ) . to include ( '`EE: true`' )
2021-03-23 20:09:26 -04:00
end
end
2021-01-04 13:10:11 -05:00
2021-03-23 20:09:26 -04:00
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-01-04 13:10:11 -05:00
end
end
2021-03-23 20:09:26 -04:00
context 'when in local context' do
let ( :mr_title ) { 'Fake Title' }
before do
allow ( fake_helper ) . to receive ( :ci? ) . and_return ( false )
end
2021-01-04 13:10:11 -05:00
specify do
expect ( subject ) . to include ( 'CHANGELOG.md was edited' )
2021-05-21 11:10:51 -04:00
expect ( subject ) . not_to include ( '`Changelog` trailer' )
2021-01-04 13:10:11 -05:00
end
2020-08-20 23:10:16 -04:00
end
end
2021-02-24 07:10:54 -05:00
describe '#required_texts' do
2021-03-12 07:09:33 -05:00
let ( :mr_title ) { 'Fake Title' }
2020-08-20 23:10:16 -04:00
2021-02-24 07:10:54 -05:00
subject { changelog . required_texts }
2021-01-04 13:10:11 -05:00
2021-03-23 20:09:26 -04:00
context 'when in CI context' do
before do
allow ( fake_helper ) . to receive ( :ci? ) . and_return ( true )
2021-01-04 13:10:11 -05:00
end
2021-03-23 20:09:26 -04:00
shared_examples 'changelog required text' do | key |
specify do
expect ( subject ) . to have_key ( key )
expect ( subject [ key ] ) . to include ( 'CHANGELOG missing' )
2021-05-21 11:10:51 -04:00
expect ( subject [ key ] ) . to include ( '`Changelog` trailer' )
2021-03-23 20:09:26 -04:00
end
end
2021-01-04 13:10:11 -05:00
2021-03-23 20:09:26 -04:00
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-02-24 07:10:54 -05:00
end
2021-03-23 20:09:26 -04:00
context 'with a removed feature flag file' do
let ( :changes ) { changes_class . new ( [ change_class . new ( 'foo' , :deleted , :feature_flag ) ] ) }
2021-02-24 07:10:54 -05:00
2021-03-23 20:09:26 -04:00
it_behaves_like 'changelog required text' , :feature_flag_removed
2021-01-04 13:10:11 -05:00
end
2020-08-20 23:10:16 -04:00
end
2021-02-24 07:10:54 -05:00
2021-03-23 20:09:26 -04:00
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' )
2021-05-21 11:10:51 -04:00
expect ( subject [ key ] ) . not_to include ( '`Changelog` trailer' )
2021-03-23 20:09:26 -04:00
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-02-24 07:10:54 -05:00
2021-03-23 20:09:26 -04:00
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-02-24 07:10:54 -05:00
end
2020-08-20 23:10:16 -04:00
end
2021-01-04 13:10:11 -05:00
describe '#optional_text' do
2020-08-20 23:10:16 -04:00
subject { changelog . optional_text }
2021-03-23 20:09:26 -04:00
context 'when in CI context' do
shared_examples 'changelog optional text' do | key |
specify do
expect ( subject ) . to include ( 'CHANGELOG missing' )
2021-05-21 11:10:51 -04:00
expect ( subject ) . to include ( '`Changelog` trailer' )
expect ( subject ) . to include ( 'EE: true' )
2021-03-23 20:09:26 -04:00
end
end
2021-01-04 13:10:11 -05:00
2021-03-23 20:09:26 -04:00
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-01-04 13:10:11 -05:00
end
end
2021-03-23 20:09:26 -04:00
context 'when in local context' do
let ( :mr_title ) { 'Fake Title' }
before do
allow ( fake_helper ) . to receive ( :ci? ) . and_return ( false )
end
2021-01-04 13:10:11 -05:00
specify do
expect ( subject ) . to include ( 'CHANGELOG missing' )
end
2020-08-20 23:10:16 -04:00
end
end
2019-12-12 10:08:41 -05:00
end