From 7753e5f715ba105a8ec3dd335e8af4a9448aee63 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 1 Dec 2023 07:00:34 -0500 Subject: [PATCH] Add shared example for CLI command specs (#28165) --- spec/lib/mastodon/cli/accounts_spec.rb | 8 ++------ spec/lib/mastodon/cli/cache_spec.rb | 6 +----- spec/lib/mastodon/cli/canonical_email_blocks_spec.rb | 6 +----- spec/lib/mastodon/cli/domains_spec.rb | 6 +----- spec/lib/mastodon/cli/email_domain_blocks_spec.rb | 6 +----- spec/lib/mastodon/cli/emoji_spec.rb | 6 +----- spec/lib/mastodon/cli/feeds_spec.rb | 6 +----- spec/lib/mastodon/cli/ip_blocks_spec.rb | 6 +----- spec/lib/mastodon/cli/main_spec.rb | 6 +----- spec/lib/mastodon/cli/maintenance_spec.rb | 6 +----- spec/lib/mastodon/cli/media_spec.rb | 6 +----- spec/lib/mastodon/cli/preview_cards_spec.rb | 6 +----- spec/lib/mastodon/cli/search_spec.rb | 6 +----- spec/lib/mastodon/cli/settings_spec.rb | 6 +----- spec/lib/mastodon/cli/statuses_spec.rb | 6 +----- spec/lib/mastodon/cli/upgrade_spec.rb | 6 +----- spec/support/examples/cli.rb | 11 +++++++++++ 17 files changed, 28 insertions(+), 81 deletions(-) create mode 100644 spec/support/examples/cli.rb diff --git a/spec/lib/mastodon/cli/accounts_spec.rb b/spec/lib/mastodon/cli/accounts_spec.rb index 626cf4778..3216d0d1b 100644 --- a/spec/lib/mastodon/cli/accounts_spec.rb +++ b/spec/lib/mastodon/cli/accounts_spec.rb @@ -6,6 +6,8 @@ require 'mastodon/cli/accounts' describe Mastodon::CLI::Accounts do let(:cli) { described_class.new } + it_behaves_like 'CLI Command' + # `parallelize_with_progress` cannot run in transactions, so instead, # stub it with an alternative implementation that runs sequentially # and can run in transactions. @@ -24,12 +26,6 @@ describe Mastodon::CLI::Accounts do end end - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end - describe '#create' do shared_examples 'a new user with given email address and username' do it 'creates a new user with the specified email address' do diff --git a/spec/lib/mastodon/cli/cache_spec.rb b/spec/lib/mastodon/cli/cache_spec.rb index 3ab42dc8c..c1ce04710 100644 --- a/spec/lib/mastodon/cli/cache_spec.rb +++ b/spec/lib/mastodon/cli/cache_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/cache' describe Mastodon::CLI::Cache do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#clear' do before { allow(Rails.cache).to receive(:clear) } diff --git a/spec/lib/mastodon/cli/canonical_email_blocks_spec.rb b/spec/lib/mastodon/cli/canonical_email_blocks_spec.rb index eb57a3cd1..6e4675748 100644 --- a/spec/lib/mastodon/cli/canonical_email_blocks_spec.rb +++ b/spec/lib/mastodon/cli/canonical_email_blocks_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/canonical_email_blocks' describe Mastodon::CLI::CanonicalEmailBlocks do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#find' do let(:arguments) { ['user@example.com'] } diff --git a/spec/lib/mastodon/cli/domains_spec.rb b/spec/lib/mastodon/cli/domains_spec.rb index ea58845c0..765b63e2a 100644 --- a/spec/lib/mastodon/cli/domains_spec.rb +++ b/spec/lib/mastodon/cli/domains_spec.rb @@ -4,9 +4,5 @@ require 'rails_helper' require 'mastodon/cli/domains' describe Mastodon::CLI::Domains do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' end diff --git a/spec/lib/mastodon/cli/email_domain_blocks_spec.rb b/spec/lib/mastodon/cli/email_domain_blocks_spec.rb index 333ae3f2b..060943b18 100644 --- a/spec/lib/mastodon/cli/email_domain_blocks_spec.rb +++ b/spec/lib/mastodon/cli/email_domain_blocks_spec.rb @@ -4,9 +4,5 @@ require 'rails_helper' require 'mastodon/cli/email_domain_blocks' describe Mastodon::CLI::EmailDomainBlocks do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' end diff --git a/spec/lib/mastodon/cli/emoji_spec.rb b/spec/lib/mastodon/cli/emoji_spec.rb index 9b5865372..5d109eb52 100644 --- a/spec/lib/mastodon/cli/emoji_spec.rb +++ b/spec/lib/mastodon/cli/emoji_spec.rb @@ -4,9 +4,5 @@ require 'rails_helper' require 'mastodon/cli/emoji' describe Mastodon::CLI::Emoji do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' end diff --git a/spec/lib/mastodon/cli/feeds_spec.rb b/spec/lib/mastodon/cli/feeds_spec.rb index 030f08721..e16113c85 100644 --- a/spec/lib/mastodon/cli/feeds_spec.rb +++ b/spec/lib/mastodon/cli/feeds_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/feeds' describe Mastodon::CLI::Feeds do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#build' do before { Fabricate(:account) } diff --git a/spec/lib/mastodon/cli/ip_blocks_spec.rb b/spec/lib/mastodon/cli/ip_blocks_spec.rb index 030d9fcb1..684314dc7 100644 --- a/spec/lib/mastodon/cli/ip_blocks_spec.rb +++ b/spec/lib/mastodon/cli/ip_blocks_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/ip_blocks' describe Mastodon::CLI::IpBlocks do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#add' do let(:ip_list) do diff --git a/spec/lib/mastodon/cli/main_spec.rb b/spec/lib/mastodon/cli/main_spec.rb index e3709afe3..b5b5d6906 100644 --- a/spec/lib/mastodon/cli/main_spec.rb +++ b/spec/lib/mastodon/cli/main_spec.rb @@ -4,11 +4,7 @@ require 'rails_helper' require 'mastodon/cli/main' describe Mastodon::CLI::Main do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe 'version' do it 'returns the Mastodon version' do diff --git a/spec/lib/mastodon/cli/maintenance_spec.rb b/spec/lib/mastodon/cli/maintenance_spec.rb index a6789ea5a..95e695ab5 100644 --- a/spec/lib/mastodon/cli/maintenance_spec.rb +++ b/spec/lib/mastodon/cli/maintenance_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/maintenance' describe Mastodon::CLI::Maintenance do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#fix_duplicates' do context 'when the database version is too old' do diff --git a/spec/lib/mastodon/cli/media_spec.rb b/spec/lib/mastodon/cli/media_spec.rb index 305d67d28..15f84c14b 100644 --- a/spec/lib/mastodon/cli/media_spec.rb +++ b/spec/lib/mastodon/cli/media_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/media' describe Mastodon::CLI::Media do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#remove' do context 'with --prune-profiles and --remove-headers' do diff --git a/spec/lib/mastodon/cli/preview_cards_spec.rb b/spec/lib/mastodon/cli/preview_cards_spec.rb index 1e064ed58..a766d250e 100644 --- a/spec/lib/mastodon/cli/preview_cards_spec.rb +++ b/spec/lib/mastodon/cli/preview_cards_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/preview_cards' describe Mastodon::CLI::PreviewCards do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#remove' do context 'with relevant preview cards' do diff --git a/spec/lib/mastodon/cli/search_spec.rb b/spec/lib/mastodon/cli/search_spec.rb index d5cae5bf4..785dc2bd6 100644 --- a/spec/lib/mastodon/cli/search_spec.rb +++ b/spec/lib/mastodon/cli/search_spec.rb @@ -4,9 +4,5 @@ require 'rails_helper' require 'mastodon/cli/search' describe Mastodon::CLI::Search do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' end diff --git a/spec/lib/mastodon/cli/settings_spec.rb b/spec/lib/mastodon/cli/settings_spec.rb index ae58e74e5..7dcd1110b 100644 --- a/spec/lib/mastodon/cli/settings_spec.rb +++ b/spec/lib/mastodon/cli/settings_spec.rb @@ -4,11 +4,7 @@ require 'rails_helper' require 'mastodon/cli/settings' describe Mastodon::CLI::Settings do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe 'subcommand "registrations"' do let(:cli) { Mastodon::CLI::Registrations.new } diff --git a/spec/lib/mastodon/cli/statuses_spec.rb b/spec/lib/mastodon/cli/statuses_spec.rb index 38ebcd993..70e4e2c08 100644 --- a/spec/lib/mastodon/cli/statuses_spec.rb +++ b/spec/lib/mastodon/cli/statuses_spec.rb @@ -6,11 +6,7 @@ require 'mastodon/cli/statuses' describe Mastodon::CLI::Statuses do let(:cli) { described_class.new } - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' describe '#remove', use_transactional_tests: false do context 'with small batch size' do diff --git a/spec/lib/mastodon/cli/upgrade_spec.rb b/spec/lib/mastodon/cli/upgrade_spec.rb index 9e0ab9d06..817044f7e 100644 --- a/spec/lib/mastodon/cli/upgrade_spec.rb +++ b/spec/lib/mastodon/cli/upgrade_spec.rb @@ -4,9 +4,5 @@ require 'rails_helper' require 'mastodon/cli/upgrade' describe Mastodon::CLI::Upgrade do - describe '.exit_on_failure?' do - it 'returns true' do - expect(described_class.exit_on_failure?).to be true - end - end + it_behaves_like 'CLI Command' end diff --git a/spec/support/examples/cli.rb b/spec/support/examples/cli.rb new file mode 100644 index 000000000..091c842bd --- /dev/null +++ b/spec/support/examples/cli.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +shared_examples 'CLI Command' do + it 'configures Thor to exit on failure' do + expect(described_class.exit_on_failure?).to be true + end + + it 'descends from the CLI base class' do + expect(described_class.new).to be_a(Mastodon::CLI::Base) + end +end