From 3f0b6b327a5d83efbc347c2ae2f9ead0766e7af8 Mon Sep 17 00:00:00 2001 From: "Andrew W. Lee" Date: Sun, 29 Dec 2019 19:36:40 -0800 Subject: [PATCH] Refactor lib/annotate.rb (#707) This change converts .all_options into a constant and moves it into Annotate::Constants. It also changes usages of .all_options. --- .rubocop_todo.yml | 26 ++-- lib/annotate.rb | 77 ++---------- lib/annotate/constants.rb | 33 +++++ lib/annotate/helpers.rb | 33 +++++ lib/tasks/annotate_models.rake | 70 +++++------ lib/tasks/annotate_models_migrate.rake | 6 +- lib/tasks/annotate_routes.rake | 10 +- spec/lib/annotate/annotate_models_spec.rb | 12 +- spec/lib/annotate/helpers_spec.rb | 141 ++++++++++++++++++++++ spec/lib/annotate_spec.rb | 21 ---- spec/spec_helper.rb | 2 + 11 files changed, 281 insertions(+), 150 deletions(-) create mode 100644 lib/annotate/helpers.rb create mode 100644 spec/lib/annotate/helpers_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b44bc5a11..0238dc4b4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2019-09-02 16:23:56 -0700 using RuboCop version 0.68.1. +# on 2019-12-24 20:33:53 -0800 using RuboCop version 0.68.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -51,7 +51,7 @@ Layout/AlignArray: Exclude: - 'spec/lib/annotate/annotate_models_spec.rb' -# Offense count: 107 +# Offense count: 111 # Cop supports --auto-correct. # Configuration parameters: EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle. # SupportedHashRocketStyles: key, separator, table @@ -74,7 +74,7 @@ Layout/BlockAlignment: Exclude: - 'lib/annotate/annotate_models.rb' -# Offense count: 45 +# Offense count: 47 # Cop supports --auto-correct. Layout/ClosingHeredocIndentation: Exclude: @@ -87,7 +87,7 @@ Layout/ClosingHeredocIndentation: - 'spec/integration/standalone.rb' - 'spec/lib/annotate/annotate_models_spec.rb' -# Offense count: 13 +# Offense count: 14 # Cop supports --auto-correct. Layout/EmptyLineAfterGuardClause: Exclude: @@ -173,7 +173,7 @@ Layout/ExtraSpacing: Layout/IndentFirstHashElement: EnforcedStyle: consistent -# Offense count: 54 +# Offense count: 55 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: auto_detection, squiggly, active_support, powerpack, unindent @@ -410,11 +410,11 @@ Lint/ShadowingOuterLocalVariable: Metrics/AbcSize: Max: 141 -# Offense count: 28 +# Offense count: 31 # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: - Max: 259 + Max: 268 # Offense count: 1 # Configuration parameters: CountBlocks. @@ -439,7 +439,7 @@ Naming/AccessorMethodName: Exclude: - 'lib/annotate.rb' -# Offense count: 76 +# Offense count: 79 # Configuration parameters: Blacklist. # Blacklist: (?-mix:(^|\s)(EO[A-Z]{1}|END)(\s|$)) Naming/HeredocDelimiterNaming: @@ -572,7 +572,7 @@ Style/FormatStringToken: Exclude: - 'lib/annotate/annotate_models.rb' -# Offense count: 186 +# Offense count: 189 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: when_needed, always, never @@ -633,14 +633,12 @@ Style/MultilineBlockChain: - 'lib/annotate/annotate_models.rb' - 'spec/spec_helper.rb' -# Offense count: 5 +# Offense count: 3 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle. # SupportedStyles: literals, strict Style/MutableConstant: Exclude: - - 'lib/annotate.rb' - - 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_routes.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' - 'spec/integration/rails_2.3_with_bundler/config/environment.rb' @@ -778,7 +776,7 @@ Style/StderrPuts: - 'lib/annotate/annotate_models.rb' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' -# Offense count: 247 +# Offense count: 249 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. # SupportedStyles: single_quotes, double_quotes @@ -830,7 +828,7 @@ Style/UnneededPercentQ: - 'annotate.gemspec' - 'spec/integration/rails_2.3_with_bundler/config/boot.rb' -# Offense count: 465 +# Offense count: 477 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https diff --git a/lib/annotate.rb b/lib/annotate.rb index 4abe74088..34e41768c 100644 --- a/lib/annotate.rb +++ b/lib/annotate.rb @@ -5,6 +5,7 @@ require 'annotate/annotate_models' require 'annotate/annotate_routes' require 'annotate/constants' +require 'annotate/helpers' begin # ActiveSupport 3.x... @@ -17,36 +18,6 @@ end module Annotate - ## - # The set of available options to customize the behavior of Annotate. - # - POSITION_OPTIONS = [ - :position_in_routes, :position_in_class, :position_in_test, - :position_in_fixture, :position_in_factory, :position, - :position_in_serializer - ].freeze - FLAG_OPTIONS = [ - :show_indexes, :simple_indexes, :include_version, :exclude_tests, - :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, - :format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen, - :trace, :timestamp, :exclude_serializers, :classified_sort, - :show_foreign_keys, :show_complete_foreign_keys, - :exclude_scaffolds, :exclude_controllers, :exclude_helpers, - :exclude_sti_subclasses, :ignore_unknown_models, :with_comment - ].freeze - OTHER_OPTIONS = [ - :additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, - :wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types, - :ignore_routes, :active_admin - ].freeze - PATH_OPTIONS = [ - :require, :model_dir, :root_dir - ].freeze - - def self.all_options - [POSITION_OPTIONS, FLAG_OPTIONS, PATH_OPTIONS, OTHER_OPTIONS] - end - ## # Set default values that can be overridden via environment variables. # @@ -56,7 +27,7 @@ def self.set_defaults(options = {}) options = ActiveSupport::HashWithIndifferentAccess.new(options) - all_options.flatten.each do |key| + Constants::ALL_ANNOTATE_OPTIONS.flatten.each do |key| if options.key?(key) default_value = if options[key].is_a?(Array) options[key].join(',') @@ -74,16 +45,16 @@ def self.set_defaults(options = {}) # TODO: what is the difference between this and set_defaults? # def self.setup_options(options = {}) - POSITION_OPTIONS.each do |key| - options[key] = fallback(ENV[key.to_s], ENV['position'], 'before') + Constants::POSITION_OPTIONS.each do |key| + options[key] = Annotate::Helpers.fallback(ENV[key.to_s], ENV['position'], 'before') end - FLAG_OPTIONS.each do |key| - options[key] = true?(ENV[key.to_s]) + Constants::FLAG_OPTIONS.each do |key| + options[key] = Annotate::Helpers.true?(ENV[key.to_s]) end - OTHER_OPTIONS.each do |key| + Constants::OTHER_OPTIONS.each do |key| options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s] : nil end - PATH_OPTIONS.each do |key| + Constants::PATH_OPTIONS.each do |key| options[key] = !ENV[key.to_s].blank? ? ENV[key.to_s].split(',') : [] end @@ -94,29 +65,13 @@ def self.setup_options(options = {}) options[:wrapper_close] ||= options[:wrapper] # These were added in 2.7.0 but so this is to revert to old behavior by default - options[:exclude_scaffolds] = Annotate.true?(ENV.fetch('exclude_scaffolds', 'true')) - options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true')) - options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true')) + options[:exclude_scaffolds] = Annotate::Helpers.true?(ENV.fetch('exclude_scaffolds', 'true')) + options[:exclude_controllers] = Annotate::Helpers.true?(ENV.fetch('exclude_controllers', 'true')) + options[:exclude_helpers] = Annotate::Helpers.true?(ENV.fetch('exclude_helpers', 'true')) options end - def self.reset_options - all_options.flatten.each { |key| ENV[key.to_s] = nil } - end - - def self.skip_on_migration? - ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ Constants::TRUE_RE || ENV['skip_on_db_migrate'] =~ Constants::TRUE_RE - end - - def self.include_routes? - ENV['routes'] =~ Constants::TRUE_RE - end - - def self.include_models? - ENV['models'] =~ Constants::TRUE_RE - end - def self.loaded_tasks=(val) @loaded_tasks = val end @@ -191,14 +146,4 @@ def self.bootstrap_rake load_tasks Rake::Task[:set_annotation_options].invoke end - - def self.fallback(*args) - args.detect { |arg| !arg.blank? } - end - - def self.true?(val) - return false if val.blank? - return false unless val =~ Constants::TRUE_RE - true - end end diff --git a/lib/annotate/constants.rb b/lib/annotate/constants.rb index 5c76d8c8c..bc7f6a01a 100644 --- a/lib/annotate/constants.rb +++ b/lib/annotate/constants.rb @@ -1,5 +1,38 @@ module Annotate module Constants TRUE_RE = /^(true|t|yes|y|1)$/i.freeze + + ## + # The set of available options to customize the behavior of Annotate. + # + POSITION_OPTIONS = [ + :position_in_routes, :position_in_class, :position_in_test, + :position_in_fixture, :position_in_factory, :position, + :position_in_serializer + ].freeze + + FLAG_OPTIONS = [ + :show_indexes, :simple_indexes, :include_version, :exclude_tests, + :exclude_fixtures, :exclude_factories, :ignore_model_sub_dir, + :format_bare, :format_rdoc, :format_markdown, :sort, :force, :frozen, + :trace, :timestamp, :exclude_serializers, :classified_sort, + :show_foreign_keys, :show_complete_foreign_keys, + :exclude_scaffolds, :exclude_controllers, :exclude_helpers, + :exclude_sti_subclasses, :ignore_unknown_models, :with_comment + ].freeze + + OTHER_OPTIONS = [ + :additional_file_patterns, :ignore_columns, :skip_on_db_migrate, :wrapper_open, :wrapper_close, + :wrapper, :routes, :models, :hide_limit_column_types, :hide_default_column_types, + :ignore_routes, :active_admin + ].freeze + + PATH_OPTIONS = [ + :require, :model_dir, :root_dir + ].freeze + + ALL_ANNOTATE_OPTIONS = [ + POSITION_OPTIONS, FLAG_OPTIONS, OTHER_OPTIONS, PATH_OPTIONS + ].freeze end end diff --git a/lib/annotate/helpers.rb b/lib/annotate/helpers.rb new file mode 100644 index 000000000..fb44924e3 --- /dev/null +++ b/lib/annotate/helpers.rb @@ -0,0 +1,33 @@ +module Annotate + # Class for holding helper methods. Done to make lib/annotate.rb less bloated. + class Helpers + class << self + def skip_on_migration? + ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ Constants::TRUE_RE || ENV['skip_on_db_migrate'] =~ Constants::TRUE_RE + end + + def include_routes? + ENV['routes'] =~ Constants::TRUE_RE + end + + def include_models? + ENV['models'] =~ Constants::TRUE_RE + end + + def true?(val) + return false if val.blank? + return false unless val =~ Constants::TRUE_RE + + true + end + + def fallback(*args) + args.detect { |arg| !arg.blank? } + end + + def reset_options(options) + options.flatten.each { |key| ENV[key.to_s] = nil } + end + end + end +end diff --git a/lib/tasks/annotate_models.rake b/lib/tasks/annotate_models.rake index 2e0e38593..dcb10892a 100644 --- a/lib/tasks/annotate_models.rake +++ b/lib/tasks/annotate_models.rake @@ -11,46 +11,46 @@ task annotate_models: :environment do require "#{annotate_lib}/annotate/active_record_patch" options = {is_rake: true} - ENV['position'] = options[:position] = Annotate.fallback(ENV['position'], 'before') + ENV['position'] = options[:position] = Annotate::Helpers.fallback(ENV['position'], 'before') options[:additional_file_patterns] = ENV['additional_file_patterns'] ? ENV['additional_file_patterns'].split(',') : [] - options[:position_in_class] = Annotate.fallback(ENV['position_in_class'], ENV['position']) - options[:position_in_fixture] = Annotate.fallback(ENV['position_in_fixture'], ENV['position']) - options[:position_in_factory] = Annotate.fallback(ENV['position_in_factory'], ENV['position']) - options[:position_in_test] = Annotate.fallback(ENV['position_in_test'], ENV['position']) - options[:position_in_serializer] = Annotate.fallback(ENV['position_in_serializer'], ENV['position']) - options[:show_foreign_keys] = Annotate.true?(ENV['show_foreign_keys']) - options[:show_complete_foreign_keys] = Annotate.true?(ENV['show_complete_foreign_keys']) - options[:show_indexes] = Annotate.true?(ENV['show_indexes']) - options[:simple_indexes] = Annotate.true?(ENV['simple_indexes']) + options[:position_in_class] = Annotate::Helpers.fallback(ENV['position_in_class'], ENV['position']) + options[:position_in_fixture] = Annotate::Helpers.fallback(ENV['position_in_fixture'], ENV['position']) + options[:position_in_factory] = Annotate::Helpers.fallback(ENV['position_in_factory'], ENV['position']) + options[:position_in_test] = Annotate::Helpers.fallback(ENV['position_in_test'], ENV['position']) + options[:position_in_serializer] = Annotate::Helpers.fallback(ENV['position_in_serializer'], ENV['position']) + options[:show_foreign_keys] = Annotate::Helpers.true?(ENV['show_foreign_keys']) + options[:show_complete_foreign_keys] = Annotate::Helpers.true?(ENV['show_complete_foreign_keys']) + options[:show_indexes] = Annotate::Helpers.true?(ENV['show_indexes']) + options[:simple_indexes] = Annotate::Helpers.true?(ENV['simple_indexes']) options[:model_dir] = ENV['model_dir'] ? ENV['model_dir'].split(',') : ['app/models'] options[:root_dir] = ENV['root_dir'] - options[:include_version] = Annotate.true?(ENV['include_version']) + options[:include_version] = Annotate::Helpers.true?(ENV['include_version']) options[:require] = ENV['require'] ? ENV['require'].split(',') : [] - options[:exclude_tests] = Annotate.true?(ENV['exclude_tests']) - options[:exclude_factories] = Annotate.true?(ENV['exclude_factories']) - options[:exclude_fixtures] = Annotate.true?(ENV['exclude_fixtures']) - options[:exclude_serializers] = Annotate.true?(ENV['exclude_serializers']) - options[:exclude_scaffolds] = Annotate.true?(ENV['exclude_scaffolds']) - options[:exclude_controllers] = Annotate.true?(ENV.fetch('exclude_controllers', 'true')) - options[:exclude_helpers] = Annotate.true?(ENV.fetch('exclude_helpers', 'true')) - options[:exclude_sti_subclasses] = Annotate.true?(ENV['exclude_sti_subclasses']) - options[:ignore_model_sub_dir] = Annotate.true?(ENV['ignore_model_sub_dir']) - options[:format_bare] = Annotate.true?(ENV['format_bare']) - options[:format_rdoc] = Annotate.true?(ENV['format_rdoc']) - options[:format_markdown] = Annotate.true?(ENV['format_markdown']) - options[:sort] = Annotate.true?(ENV['sort']) - options[:force] = Annotate.true?(ENV['force']) - options[:frozen] = Annotate.true?(ENV['frozen']) - options[:classified_sort] = Annotate.true?(ENV['classified_sort']) - options[:trace] = Annotate.true?(ENV['trace']) - options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper']) - options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper']) + options[:exclude_tests] = Annotate::Helpers.true?(ENV['exclude_tests']) + options[:exclude_factories] = Annotate::Helpers.true?(ENV['exclude_factories']) + options[:exclude_fixtures] = Annotate::Helpers.true?(ENV['exclude_fixtures']) + options[:exclude_serializers] = Annotate::Helpers.true?(ENV['exclude_serializers']) + options[:exclude_scaffolds] = Annotate::Helpers.true?(ENV['exclude_scaffolds']) + options[:exclude_controllers] = Annotate::Helpers.true?(ENV.fetch('exclude_controllers', 'true')) + options[:exclude_helpers] = Annotate::Helpers.true?(ENV.fetch('exclude_helpers', 'true')) + options[:exclude_sti_subclasses] = Annotate::Helpers.true?(ENV['exclude_sti_subclasses']) + options[:ignore_model_sub_dir] = Annotate::Helpers.true?(ENV['ignore_model_sub_dir']) + options[:format_bare] = Annotate::Helpers.true?(ENV['format_bare']) + options[:format_rdoc] = Annotate::Helpers.true?(ENV['format_rdoc']) + options[:format_markdown] = Annotate::Helpers.true?(ENV['format_markdown']) + options[:sort] = Annotate::Helpers.true?(ENV['sort']) + options[:force] = Annotate::Helpers.true?(ENV['force']) + options[:frozen] = Annotate::Helpers.true?(ENV['frozen']) + options[:classified_sort] = Annotate::Helpers.true?(ENV['classified_sort']) + options[:trace] = Annotate::Helpers.true?(ENV['trace']) + options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper']) + options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper']) options[:ignore_columns] = ENV.fetch('ignore_columns', nil) options[:ignore_routes] = ENV.fetch('ignore_routes', nil) - options[:hide_limit_column_types] = Annotate.fallback(ENV['hide_limit_column_types'], '') - options[:hide_default_column_types] = Annotate.fallback(ENV['hide_default_column_types'], '') - options[:with_comment] = Annotate.true?(ENV['with_comment']) - options[:ignore_unknown_models] = Annotate.true?(ENV.fetch('ignore_unknown_models', 'false')) + options[:hide_limit_column_types] = Annotate::Helpers.fallback(ENV['hide_limit_column_types'], '') + options[:hide_default_column_types] = Annotate::Helpers.fallback(ENV['hide_default_column_types'], '') + options[:with_comment] = Annotate::Helpers.true?(ENV['with_comment']) + options[:ignore_unknown_models] = Annotate::Helpers.true?(ENV.fetch('ignore_unknown_models', 'false')) AnnotateModels.do_annotations(options) end @@ -64,6 +64,6 @@ task remove_annotation: :environment do options[:model_dir] = ENV['model_dir'] options[:root_dir] = ENV['root_dir'] options[:require] = ENV['require'] ? ENV['require'].split(',') : [] - options[:trace] = Annotate.true?(ENV['trace']) + options[:trace] = Annotate::Helpers.true?(ENV['trace']) AnnotateModels.remove_annotations(options) end diff --git a/lib/tasks/annotate_models_migrate.rake b/lib/tasks/annotate_models_migrate.rake index c8dbc2f0c..fa1d653ef 100644 --- a/lib/tasks/annotate_models_migrate.rake +++ b/lib/tasks/annotate_models_migrate.rake @@ -23,11 +23,11 @@ module Annotate @@working = false def self.update_annotations - unless @@working || Annotate.skip_on_migration? + unless @@working || Annotate::Helpers.skip_on_migration? @@working = true - self.update_models if Annotate.include_models? - self.update_routes if Annotate.include_routes? + self.update_models if Annotate::Helpers.include_models? + self.update_routes if Annotate::Helpers.include_routes? end end diff --git a/lib/tasks/annotate_routes.rake b/lib/tasks/annotate_routes.rake index 208505d24..3f3c7b143 100644 --- a/lib/tasks/annotate_routes.rake +++ b/lib/tasks/annotate_routes.rake @@ -4,12 +4,12 @@ task :annotate_routes => :environment do require "#{annotate_lib}/annotate/annotate_routes" options={} - ENV['position'] = options[:position] = Annotate.fallback(ENV['position'], 'before') - options[:position_in_routes] = Annotate.fallback(ENV['position_in_routes'], ENV['position']) - options[:ignore_routes] = Annotate.fallback(ENV['ignore_routes'], nil) + ENV['position'] = options[:position] = Annotate::Helpers.fallback(ENV['position'], 'before') + options[:position_in_routes] = Annotate::Helpers.fallback(ENV['position_in_routes'], ENV['position']) + options[:ignore_routes] = Annotate::Helpers.fallback(ENV['ignore_routes'], nil) options[:require] = ENV['require'] ? ENV['require'].split(',') : [] - options[:wrapper_open] = Annotate.fallback(ENV['wrapper_open'], ENV['wrapper']) - options[:wrapper_close] = Annotate.fallback(ENV['wrapper_close'], ENV['wrapper']) + options[:wrapper_open] = Annotate::Helpers.fallback(ENV['wrapper_open'], ENV['wrapper']) + options[:wrapper_close] = Annotate::Helpers.fallback(ENV['wrapper_close'], ENV['wrapper']) AnnotateRoutes.do_annotations(options) end diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 650a62a99..56195e352 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -790,12 +790,12 @@ def mock_column(name, type, options = {}) describe '#set_defaults' do it 'should default show_complete_foreign_keys to false' do - expect(Annotate.true?(ENV['show_complete_foreign_keys'])).to be(false) + expect(Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])).to be(false) end it 'should be able to set show_complete_foreign_keys to true' do Annotate.set_defaults('show_complete_foreign_keys' => 'true') - expect(Annotate.true?(ENV['show_complete_foreign_keys'])).to be(true) + expect(Annotate::Helpers.true?(ENV['show_complete_foreign_keys'])).to be(true) end after :each do @@ -1697,7 +1697,7 @@ class User < ActiveRecord::Base mock_column(:name, :string, limit: 50) ]) @schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info') - Annotate.reset_options + Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS) end def write_model(file_name, file_content) @@ -1715,9 +1715,9 @@ def annotate_one_file(options = {}) # Wipe settings so the next call will pick up new values... Annotate.instance_variable_set('@has_set_defaults', false) - Annotate::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' } - Annotate::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' } - Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' } + Annotate::Constants::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' } + Annotate::Constants::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' } + Annotate::Constants::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' } end def magic_comments_list_each diff --git a/spec/lib/annotate/helpers_spec.rb b/spec/lib/annotate/helpers_spec.rb new file mode 100644 index 000000000..b8de5df52 --- /dev/null +++ b/spec/lib/annotate/helpers_spec.rb @@ -0,0 +1,141 @@ +require_relative '../../spec_helper' + +RSpec.describe Annotate::Helpers do + describe '.skip_on_migration?' do + subject { described_class.skip_on_migration? } + + before do + allow(ENV).to receive(:[]).and_return(nil) + end + + it { is_expected.to be_falsy } + + context "when ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] is set" do + let(:key) { 'ANNOTATE_SKIP_ON_DB_MIGRATE' } + let(:env_value) { '1' } + + before do + allow(ENV).to receive(:[]).with(key).and_return(env_value) + end + + it { is_expected.to be_truthy } + end + + context "when ENV['skip_on_db_migrate'] is set" do + let(:key) { 'skip_on_db_migrate' } + let(:env_value) { '1' } + + before do + allow(ENV).to receive(:[]).with(key).and_return(env_value) + end + + it { is_expected.to be_truthy } + end + end + + describe '.include_routes?' do + subject { described_class.include_routes? } + + before do + allow(ENV).to receive(:[]).and_return(nil) + end + + it { is_expected.to be_falsy } + + context "when ENV['routes'] is set" do + let(:key) { 'routes' } + let(:env_value) { '1' } + + before do + allow(ENV).to receive(:[]).with(key).and_return(env_value) + end + + it { is_expected.to be_truthy } + end + end + + describe '.include_models?' do + subject { described_class.include_models? } + + before do + allow(ENV).to receive(:[]).and_return(nil) + end + + it { is_expected.to be_falsy } + + context "when ENV['models'] is set" do + let(:key) { 'models' } + let(:env_value) { '1' } + + before do + allow(ENV).to receive(:[]).with(key).and_return(env_value) + end + + it { is_expected.to be_truthy } + end + end + + describe '.true?' do + subject { described_class.true?(val) } + + let(:val) { nil } + it { is_expected.to be_falsy } + + context 'when val is blank' do + let(:val) { '' } + + it { is_expected.to be_falsy } + end + + context 'when it matches the regex' do + valid_truthy_values = %w[true t yes y 1] + + valid_truthy_values.each do |truthy_value| + let(:val) { truthy_value } + + it "returns truthy for '#{truthy_value}'" do + is_expected.to be_truthy + end + end + end + end + + describe '.fallback' do + subject { described_class.fallback(*args) } + let(:args) { [arg_1, arg_2] } + + let(:arg_1) { '' } # is considered blank + let(:arg_2) { 'yes' } + + it 'returns the first non-blank argument' do + is_expected.to eq(arg_2) + end + + context 'when the first argument is non-blank' do + let(:arg_1) { 'yes' } + let(:arg_2) { 'no' } + + it { is_expected.to eq(arg_1) } + end + end + + describe '.reset_options' do + subject { described_class.reset_options(options) } + + let(:options) { [included_option] } + let(:included_option) { :some_key } + let(:excluded_option) { :yet_another_key } + let(:reset_value) { nil } + + before do + allow(ENV).to receive(:[]=) + end + + it 'resets ENV value' do + expect(ENV).to receive(:[]=).with(included_option.to_s, reset_value) + expect(ENV).to_not receive(:[]=).with(excluded_option.to_s, reset_value) + + subject + end + end +end diff --git a/spec/lib/annotate_spec.rb b/spec/lib/annotate_spec.rb index 3b7c30ce8..d2c2748df 100644 --- a/spec/lib/annotate_spec.rb +++ b/spec/lib/annotate_spec.rb @@ -6,25 +6,4 @@ expect(Annotate.version).to be_instance_of(String) end end - - describe '.skip_on_migration?' do - it "checks ENV for 'ANNOTATE_SKIP_ON_DB_MIGRATE' or 'skip_on_db_migrate'" do - expect(ENV).to receive(:[]).twice - described_class.skip_on_migration? - end - end - - describe '.include_routes?' do - it "checks ENV with 'routes'" do - expect(ENV).to receive(:[]).with('routes') - described_class.include_routes? - end - end - - describe '.include_models?' do - it "checks ENV with 'models'" do - expect(ENV).to receive(:[]).with('models') - described_class.include_models? - end - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2763a91be..41ff7a5b7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,6 +29,8 @@ require 'active_support/core_ext/string/inflections' require 'annotate' require 'annotate/parser' +require 'annotate/helpers' +require 'annotate/constants' require 'byebug' module Annotate