Unverified Commit f612f8ad by Andrew W. Lee Committed by GitHub

Add tests for the CLI (#635)

Currently there's no test coverage on CLI. This adds tests for a new class Parser which will replace the command line options currently in bin/annotate. The technical direction I'm planning to go is to remove ENV variables completely and to have things passed into as arguments. Will be adding deprecation warnings in 3.0 and then deprecate ENV variables completely in 3.1. Deprecation warnings will be non-blocking in 3.0 when an ENV variable is set and an argument isn't passed in, and will becoming blocking in 3.1.
parent 46d2d63f
......@@ -14,203 +14,19 @@ end
here = File.expand_path(File.dirname __FILE__)
$LOAD_PATH << "#{here}/../lib"
require 'optparse'
require 'annotate'
Annotate.bootstrap_rake
has_set_position = {}
target_action = :do_annotations
positions = %w(before top after bottom)
OptionParser.new do |opts|
opts.banner = 'Usage: annotate [options] [model_file]*'
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
target_action = :remove_annotations
end
opts.on('-p', '--position [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
ENV['position'] = p
%w(position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer).each do |key|
ENV[key] = p unless has_set_position[key]
end
end
opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
ENV['position_in_class'] = p
has_set_position['position_in_class'] = true
end
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
ENV['position_in_factory'] = p
has_set_position['position_in_factory'] = true
end
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
ENV['position_in_fixture'] = p
has_set_position['position_in_fixture'] = true
end
opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
ENV['position_in_test'] = p
has_set_position['position_in_test'] = true
end
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
ENV['position_in_routes'] = p
has_set_position['position_in_routes'] = true
end
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
ENV['position_in_serializer'] = p
has_set_position['position_in_serializer'] = true
end
opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
'If --w option is used, the same text will be used as opening and closing') do |p|
ENV['wrapper'] = p
end
opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
ENV['wrapper_open'] = p
end
opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
ENV['wrapper_close'] = p
end
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
ENV['routes'] = 'true'
end
opts.on('-a', '--active-admin', 'Annotate active_admin models') do
ENV['active_admin'] = 'true'
end
opts.on('-v', '--version', 'Show the current version of this gem') do
puts "annotate v#{Annotate.version}"; exit
end
opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
ENV['include_version'] = 'yes'
end
opts.on('-k', '--show-foreign-keys',
"List the table's foreign key constraints in the annotation") do
ENV['show_foreign_keys'] = 'yes'
end
require 'annotate/parser'
opts.on('--ck',
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
ENV['show_foreign_keys'] = 'yes'
ENV['show_complete_foreign_keys'] = 'yes'
end
opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do
ENV['show_indexes'] = 'yes'
end
opts.on('-s', '--simple-indexes',
"Concat the column's related indexes in the annotation") do
ENV['simple_indexes'] = 'yes'
end
opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models, separate multiple dirs with commas") do |dir|
ENV['model_dir'] = dir
end
opts.on('--root-dir dir',
"Annotate files stored within root dir projects, separate multiple dirs with commas") do |dir|
ENV['root_dir'] = dir
end
opts.on('--ignore-model-subdirects',
"Ignore subdirectories of the models directory") do |dir|
ENV['ignore_model_sub_dir'] = 'yes'
end
opts.on('--sort',
"Sort columns alphabetically, rather than in creation order") do |dir|
ENV['sort'] = 'yes'
end
opts.on('--classified-sort',
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir|
ENV['classified_sort'] = 'yes'
end
opts.on('-R', '--require path',
"Additional file to require before loading models, may be used multiple times") do |path|
if !ENV['require'].blank?
ENV['require'] = ENV['require'] + ",#{path}"
else
ENV['require'] = path
end
end
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
exclusions ||= %w(tests fixtures factories)
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
end
opts.on('-f', '--format [bare|rdoc|markdown]', %w(bare rdoc markdown), 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
ENV["format_#{fmt}"] = 'yes'
end
opts.on('--force', 'Force new annotations even if there are no changes.') do |force|
ENV['force'] = 'yes'
end
opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
ENV['frozen'] = 'yes'
end
opts.on('--timestamp', 'Include timestamp in (routes) annotation') do
ENV['timestamp'] = 'true'
end
opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |value|
ENV['trace'] = 'yes'
end
opts.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
ENV['ignore_columns'] = regex
end
opts.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
ENV['ignore_routes'] = regex
end
opts.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
ENV['hide_limit_column_types'] = "#{values}"
end
opts.on('--hide-default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
ENV['hide_default_column_types'] = "#{values}"
end
Annotate.bootstrap_rake
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |values|
ENV['ignore_unknown_models'] = 'true'
end
options_result = Annotate::Parser.parse(ARGV)
opts.on('--with-comment', "include database comments in model annotations") do |values|
ENV['with_comment'] = 'true'
end
end.parse!
exit if options_result[:exit]
options = Annotate.setup_options(
is_rake: ENV['is_rake'] && !ENV['is_rake'].empty?
)
Annotate.eager_load(options) if Annotate.include_models?
AnnotateModels.send(target_action, options) if Annotate.include_models?
AnnotateRoutes.send(target_action, options) if Annotate.include_routes?
AnnotateModels.send(options_result[:target_action], options) if Annotate.include_models?
AnnotateRoutes.send(options_result[:target_action], options) if Annotate.include_routes?
require 'optparse'
module Annotate
# Class for handling command line arguments
class Parser # rubocop:disable Metrics/ClassLength
def self.parse(args)
new(args).parse
end
attr_reader :args
ANNOTATION_POSITIONS = %w[before top after bottom].freeze
FILE_TYPE_POSITIONS = %w[position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer].freeze
EXCLUSION_LIST = %w[tests fixtures factories serializers].freeze
FORMAT_TYPES = %w[bare rdoc markdown].freeze
def initialize(args)
@args = args
end
def parse
options = default_options
parser(options).parse!(args)
options
end
private
def parser(options) # rubocop:disable Metrics/MethodLength
has_set_position = {}
positions = ANNOTATION_POSITIONS
OptionParser.new do |opts|
opts.banner = 'Usage: annotate [options] [model_file]*'
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
options[:target_action] = :remove_annotations
end
opts.on('-p', '--position [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
ENV['position'] = p
FILE_TYPE_POSITIONS.each do |key|
ENV[key] = p unless has_set_position[key]
end
end
opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
ENV['position_in_class'] = p
has_set_position['position_in_class'] = true
end
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
ENV['position_in_factory'] = p
has_set_position['position_in_factory'] = true
end
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
ENV['position_in_fixture'] = p
has_set_position['position_in_fixture'] = true
end
opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
ENV['position_in_test'] = p
has_set_position['position_in_test'] = true
end
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
ENV['position_in_routes'] = p
has_set_position['position_in_routes'] = true
end
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
ENV['position_in_serializer'] = p
has_set_position['position_in_serializer'] = true
end
opts.on('--w', '--wrapper STR', 'Wrap annotation with the text passed as parameter.',
'If --w option is used, the same text will be used as opening and closing') do |p|
ENV['wrapper'] = p
end
opts.on('--wo', '--wrapper-open STR', 'Annotation wrapper opening.') do |p|
ENV['wrapper_open'] = p
end
opts.on('--wc', '--wrapper-close STR', 'Annotation wrapper closing') do |p|
ENV['wrapper_close'] = p
end
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
ENV['routes'] = 'true'
end
opts.on('-a', '--active-admin', 'Annotate active_admin models') do
ENV['active_admin'] = 'true'
end
opts.on('-v', '--version', 'Show the current version of this gem') do
puts "annotate v#{Annotate.version}"
options[:exit] = true
end
opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
ENV['include_version'] = 'yes'
end
opts.on('-k', '--show-foreign-keys',
"List the table's foreign key constraints in the annotation") do
ENV['show_foreign_keys'] = 'yes'
end
opts.on('--ck',
'--complete-foreign-keys', 'Complete foreign key names in the annotation') do
ENV['show_foreign_keys'] = 'yes'
ENV['show_complete_foreign_keys'] = 'yes'
end
opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do
ENV['show_indexes'] = 'yes'
end
opts.on('-s', '--simple-indexes',
"Concat the column's related indexes in the annotation") do
ENV['simple_indexes'] = 'yes'
end
opts.on('--model-dir dir',
"Annotate model files stored in dir rather than app/models, separate multiple dirs with commas") do |dir|
ENV['model_dir'] = dir
end
opts.on('--root-dir dir',
"Annotate files stored within root dir projects, separate multiple dirs with commas") do |dir|
ENV['root_dir'] = dir
end
opts.on('--ignore-model-subdirects',
"Ignore subdirectories of the models directory") do |_dir|
ENV['ignore_model_sub_dir'] = 'yes'
end
opts.on('--sort',
"Sort columns alphabetically, rather than in creation order") do |_dir|
ENV['sort'] = 'yes'
end
opts.on('--classified-sort',
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |_dir|
ENV['classified_sort'] = 'yes'
end
opts.on('-R', '--require path',
"Additional file to require before loading models, may be used multiple times") do |path|
ENV['require'] = if !ENV['require'].blank?
ENV['require'] + ",#{path}"
else
path
end
end
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
exclusions ||= EXCLUSION_LIST
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
end
opts.on('-f', '--format [bare|rdoc|markdown]', FORMAT_TYPES, 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
ENV["format_#{fmt}"] = 'yes'
end
opts.on('--force', 'Force new annotations even if there are no changes.') do |_force|
ENV['force'] = 'yes'
end
opts.on('--frozen', 'Do not allow to change annotations. Exits non-zero if there are going to be changes to files.') do
ENV['frozen'] = 'yes'
end
opts.on('--timestamp', 'Include timestamp in (routes) annotation') do
ENV['timestamp'] = 'true'
end
opts.on('--trace', 'If unable to annotate a file, print the full stack trace, not just the exception message.') do |_value|
ENV['trace'] = 'yes'
end
opts.on('-I', '--ignore-columns REGEX', "don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`") do |regex|
ENV['ignore_columns'] = regex
end
opts.on('--ignore-routes REGEX', "don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`") do |regex|
ENV['ignore_routes'] = regex
end
opts.on('--hide-limit-column-types VALUES', "don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)") do |values|
ENV['hide_limit_column_types'] = values.to_s
end
opts.on('--hide-default-column-types VALUES', "don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)") do |values|
ENV['hide_default_column_types'] = values.to_s
end
opts.on('--ignore-unknown-models', "don't display warnings for bad model files") do |_values|
ENV['ignore_unknown_models'] = 'true'
end
opts.on('--with-comment', "include database comments in model annotations") do |_values|
ENV['with_comment'] = 'true'
end
end
end
def default_options
{
target_action: :do_annotations,
exit: false
}
end
end
end
require File.dirname(__FILE__) + '/../spec_helper.rb'
module Annotate # rubocop:disable Metrics/ModuleLength
describe Parser do # rubocop:disable Metrics/BlockLength
before(:example) do
ENV.clear
end
context 'when given empty args' do
it 'returns an options hash with defaults' do
result = Parser.parse([])
expect(result).to be_a(Hash)
expect(result).to include(target_action: :do_annotations)
end
end
%w[-d --delete].each do |option|
describe option do
it 'sets target_action to :remove_annotations' do
result = Parser.parse([option])
expect(result).to include(target_action: :remove_annotations)
end
end
end
%w[-p --position].each do |option|
describe option do
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying #{position}" do
it "#{position} position is an option" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(Parser::ANNOTATION_POSITIONS).to include(position)
end
it "sets ENV['position'] to be position" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with('position', position)
end
it 'sets the value in ENV for the different file types' do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
Parser::FILE_TYPE_POSITIONS.each do |file_type|
expect(ENV).to have_received(:[]=).with(file_type, position)
end
end
end
end
end
end
context 'when position_in_class is set to top' do
context 'and when position is a different value' do
it 'does not override' do
other_commands = %w[--pc top]
position_command = %w[-p bottom]
options = other_commands + position_command
Parser.parse(options)
expect(ENV['position_in_class']).to eq('top')
expect(ENV['position']).to eq('bottom')
end
end
end
%w[--pc --position-in-class].each do |option|
describe option do
let(:env_key) { 'position_in_class' }
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying '#{position}'" do
it "sets the ENV variable to '#{position}'" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with(env_key, position)
end
end
end
end
end
%w[--pf --position-in-factory].each do |option|
describe option do
let(:env_key) { 'position_in_factory' }
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying #{position}" do
it "sets the ENV variable to #{position}" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with(env_key, position)
end
end
end
end
end
%w[--px --position-in-fixture].each do |option|
describe option do
let(:env_key) { 'position_in_fixture' }
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying #{position}" do
it "sets the ENV variable to #{position}" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with(env_key, position)
end
end
end
end
end
%w[--pt --position-in-test].each do |option|
describe option do
let(:env_key) { 'position_in_test' }
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying #{position}" do
it "sets the ENV variable to #{position}" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with(env_key, position)
end
end
end
end
end
%w[--pr --position-in-routes].each do |option|
describe option do
let(:env_key) { 'position_in_routes' }
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying #{position}" do
it "sets the ENV variable to #{position}" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with(env_key, position)
end
end
end
end
end
%w[--ps --position-in-serializer].each do |option|
describe option do
let(:env_key) { 'position_in_serializer' }
Parser::ANNOTATION_POSITIONS.each do |position|
context "when specifying #{position}" do
it "sets the ENV variable to #{position}" do
allow(ENV).to receive(:[]=)
Parser.parse([option, position])
expect(ENV).to have_received(:[]=).with(env_key, position)
end
end
end
end
end
%w[--w --wrapper].each do |option|
describe option do
let(:env_key) { 'wrapper' }
let(:set_value) { 'STR' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, set_value])
end
end
end
%w[--wo --wrapper-open].each do |option|
describe option do
let(:env_key) { 'wrapper_open' }
let(:set_value) { 'STR' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, set_value])
end
end
end
%w[--wc --wrapper-close].each do |option|
describe option do
let(:env_key) { 'wrapper_close' }
let(:set_value) { 'STR' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, set_value])
end
end
end
%w[-r --routes].each do |option|
describe option do
let(:env_key) { 'routes' }
let(:set_value) { 'true' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
%w[-a --active-admin].each do |option|
describe option do
let(:env_key) { 'active_admin' }
let(:set_value) { 'true' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
%w[-v --version].each do |option|
describe option do
it 'sets the ENV variable' do
expect { Parser.parse([option]) }.to output("annotate v#{Annotate.version}\n").to_stdout
expect(Parser.parse([option])).to include(exit: true)
end
end
end
%w[-m --show-migration].each do |option|
describe option do
let(:env_key) { 'include_version' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
%w[-k --show-foreign-keys].each do |option|
describe option do
let(:env_key) { 'show_foreign_keys' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
%w[--ck --complete-foreign-keys].each do |option|
describe option do
it 'sets the ENV variable' do
allow(ENV).to receive(:[]=)
Parser.parse([option])
expect(ENV).to have_received(:[]=).with('show_foreign_keys', 'yes')
expect(ENV).to have_received(:[]=).with('show_complete_foreign_keys', 'yes')
end
end
end
%w[-i --show-indexes].each do |option|
describe option do
let(:env_key) { 'show_indexes' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
%w[-s --simple-indexes].each do |option|
describe option do
let(:env_key) { 'simple_indexes' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
describe '--model-dir' do
let(:option) { '--model-dir' }
let(:env_key) { 'model_dir' }
let(:set_value) { 'some_dir/' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, set_value])
end
end
describe '--root-dir' do
let(:option) { '--root-dir' }
let(:env_key) { 'root_dir' }
let(:set_value) { 'some_dir/' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, set_value])
end
end
describe '--ignore-model-subdirects' do
let(:option) { '--ignore-model-subdirects' }
let(:env_key) { 'ignore_model_sub_dir' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
describe '--sort' do
let(:option) { '--sort' }
let(:env_key) { 'sort' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
describe '--classified-sort' do
let(:option) { '--classified-sort' }
let(:env_key) { 'classified_sort' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
%w[-R --require].each do |option|
describe option do
let(:env_key) { 'require' }
let(:set_value) { 'another_dir' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, set_value])
end
context "when ENV['require'] is already set" do
let(:preset_require_value) { 'some_dir/' }
it "appends the path to ENV['require']" do
allow(ENV).to receive(:[]).and_return(preset_require_value)
expect(ENV).to receive(:[]=).with(env_key, "#{preset_require_value},#{set_value}")
Parser.parse([option, set_value])
end
end
end
end
describe 'Parser::EXCLUSION_LIST' do
it "has 'tests'" do
expect(Parser::EXCLUSION_LIST).to include('tests')
end
it "has 'fixtures'" do
expect(Parser::EXCLUSION_LIST).to include('fixtures')
end
it "has 'factories'" do
expect(Parser::EXCLUSION_LIST).to include('factories')
end
it "has 'serializers'" do
expect(Parser::EXCLUSION_LIST).to include('serializers')
end
end
%w[-e --exclude].each do |option|
describe option do
let(:set_value) { 'yes' }
it "sets the exclusion ENV variables for 'tests', 'fixtures', 'factories', and 'serializers'" do
allow(ENV).to receive(:[]=)
Parser.parse([option])
expect(ENV).to have_received(:[]=).with('exclude_tests', set_value)
expect(ENV).to have_received(:[]=).with('exclude_fixtures', set_value)
expect(ENV).to have_received(:[]=).with('exclude_factories', set_value)
expect(ENV).to have_received(:[]=).with('exclude_serializers', set_value)
end
context 'when a type is passed in' do
let(:exclusions) { "tests" }
it "sets the exclusion ENV variable for 'tests' only" do
expect(ENV).to receive(:[]=).with('exclude_tests', set_value)
Parser.parse([option, exclusions])
end
end
context 'when two types are passed in' do
let(:exclusions) { "tests,fixtures" }
it "sets the exclusion ENV variable for 'tests' and 'fixtures'" do
allow(ENV).to receive(:[]=)
Parser.parse([option, exclusions])
expect(ENV).to have_received(:[]=).with('exclude_tests', set_value)
expect(ENV).to have_received(:[]=).with('exclude_fixtures', set_value)
end
end
end
end
%w[-f --format].each do |option|
describe option do
Parser::FORMAT_TYPES.each do |format_type|
context "when passing in format type '#{format_type}'" do
let(:env_key) { "format_#{format_type}" }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option, format_type])
end
end
end
end
end
describe '--force' do
let(:option) { '--force' }
let(:env_key) { 'force' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
describe '--frozen' do
let(:option) { '--frozen' }
let(:env_key) { 'frozen' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
describe '--timestamp' do
let(:option) { '--timestamp' }
let(:env_key) { 'timestamp' }
let(:set_value) { 'true' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
describe '--trace' do
let(:option) { '--trace' }
let(:env_key) { 'trace' }
let(:set_value) { 'yes' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
%w[-I --ignore-columns].each do |option|
describe option do
let(:env_key) { 'ignore_columns' }
let(:regex) { '^(id|updated_at|created_at)' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, regex)
Parser.parse([option, regex])
end
end
end
describe '--ignore-routes' do
let(:option) { '--ignore-routes' }
let(:env_key) { 'ignore_routes' }
let(:regex) { '(mobile|resque|pghero)' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, regex)
Parser.parse([option, regex])
end
end
describe '--hide-limit-column-types' do
let(:option) { '--hide-limit-column-types' }
let(:env_key) { 'hide_limit_column_types' }
let(:values) { 'integer,boolean,text' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, values)
Parser.parse([option, values])
end
end
describe '--hide-default-column-types' do
let(:option) { '--hide-default-column-types' }
let(:env_key) { 'hide_default_column_types' }
let(:values) { 'json,jsonb,hstore' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, values)
Parser.parse([option, values])
end
end
describe '--ignore-unknown-models' do
let(:option) { '--ignore-unknown-models' }
let(:env_key) { 'ignore_unknown_models' }
let(:set_value) { 'true' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
describe '--with-comment' do
let(:option) { '--with-comment' }
let(:env_key) { 'with_comment' }
let(:set_value) { 'true' }
it 'sets the ENV variable' do
expect(ENV).to receive(:[]=).with(env_key, set_value)
Parser.parse([option])
end
end
end
end
......@@ -28,6 +28,7 @@ require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/class/subclasses'
require 'active_support/core_ext/string/inflections'
require 'annotate'
require 'annotate/parser'
require 'byebug'
module Annotate
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment