Skip to content
Merged
19 changes: 7 additions & 12 deletions lib/next_rails/bundle_report/cli.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# frozen_string_literal: true

require 'optparse'
require 'next_rails'
require 'next_rails/bundle_report'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
require 'next_rails/bundle_report'
require 'next_rails/bundle_report'

class NextRails::BundleReport::CLI
def initialize(argv)
validate_arguments(argv)
@argv = argv
end

def validate_arguments(argv)
Expand All @@ -27,28 +31,19 @@ def validate_arguments(argv)
end

def run
at_exit do
setup_dependencies
options = parse_options
execute_report(ARGV.first, options)
end
options = parse_options
execute_report(@argv.first, options)
end

private

def setup_dependencies
require 'optparse'
require 'next_rails'
require 'next_rails/bundle_report'
end

def parse_options
options = {}
option_parser = OptionParser.new do |opts|
opts.banner = <<-EOS
Usage: #{$0} [report-type] [options]

report-type There are two report types available: `outdated` and `compatibility`
report-type There are three report types available: `outdated`, `compatibility` and `ruby_check`.

Examples:
#{$0} compatibility --rails-version 5.0
Expand Down
32 changes: 32 additions & 0 deletions spec/next_rails/bundle_report/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require 'byebug'

RSpec.describe NextRails::BundleReport::CLI do
describe '#initialize' do
it 'raises if called with invalid arguments' do
expect { described_class.new(['invalid_report_type']) }
.to raise_error(ArgumentError,
/Invalid report type 'invalid_report_type'. Valid types are: outdated, compatibility, ruby_check./)
end

it 'calls outdated if called with outdated' do
expect(NextRails::BundleReport).to receive(:outdated)
described_class.new(['outdated']).run
end

it 'calls compatible_ruby_version if called with ruby_check' do
expect(NextRails::BundleReport).to receive(:compatible_ruby_version)
described_class.new(['ruby_check', '--rails-version=7.0.0']).run
end

it 'calls rails_compatibility if called with compatibility with rails-version option' do
expect(NextRails::BundleReport).to receive(:rails_compatibility)
described_class.new(['compatibility', '--rails-version=7.0.0']).run
end

it 'calls ruby_compatibility if called with compatibility with ruby-version option' do
expect(NextRails::BundleReport).to receive(:ruby_compatibility)
described_class.new(['compatibility', '--ruby-version=3.2.0']).run
end
end
end
Loading