diff --git a/lib/gistory.rb b/lib/gistory.rb index ec2a40c..93b45b7 100644 --- a/lib/gistory.rb +++ b/lib/gistory.rb @@ -3,6 +3,7 @@ require 'gistory/cli/main' require 'gistory/cli/arg_parser' +require 'gistory/cli/io' require 'gistory/configuration' require 'gistory/errors' diff --git a/lib/gistory/cli/arg_parser.rb b/lib/gistory/cli/arg_parser.rb index a0a17ea..ec31da1 100644 --- a/lib/gistory/cli/arg_parser.rb +++ b/lib/gistory/cli/arg_parser.rb @@ -4,16 +4,18 @@ module Gistory module Cli class ArgParser - def initialize(args:) + def initialize(args:, io: Gistory::Cli::Io.new) @args = args @config = Gistory.config @parser = create_parser(@config) + @io = io end def parse @parser.parse!(@args) parse_gem_name + @io.error("extra parameters ignored: #{@args}") unless @args.count.zero? @config rescue OptionParser::InvalidOption => err raise(Gistory::ParserError, err.message) @@ -60,20 +62,21 @@ def add_max_lockfile_changes(parser, config) default = config.max_lockfile_changes description = "max number of changes to the lock file (default #{default})" parser.on('-m', '--max-lockfile-changes [INTEGER]', Integer, description) do |m| + raise(Gistory::ParserError, 'argument --max-lockfile-changes must be an integer') if m.nil? config.max_lockfile_changes = m end end def add_help(parser) parser.on_tail('-h', '--help', 'Show this message') do - puts parser + @io.puts parser exit end end def add_version(parser) parser.on_tail('--version', 'Show version') do - puts Gistory::VERSION + @io.puts "gistory version #{Gistory::VERSION}" exit end end diff --git a/lib/gistory/cli/io.rb b/lib/gistory/cli/io.rb new file mode 100644 index 0000000..6c2653a --- /dev/null +++ b/lib/gistory/cli/io.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true +require 'colorize' + +module Gistory + module Cli + class Io + def initialize(out: $stdout, err: $stderr) + @out = out + @err = err + end + + def puts(msg) + @out.puts(msg) + end + + def error(msg) + @err.puts(msg.red) + end + end + end +end diff --git a/lib/gistory/cli/main.rb b/lib/gistory/cli/main.rb index ced0070..e1d5ab3 100644 --- a/lib/gistory/cli/main.rb +++ b/lib/gistory/cli/main.rb @@ -1,24 +1,24 @@ # frozen_string_literal: true -require 'colorize' module Gistory module Cli class Main - def initialize(repo_path:, args:) + def initialize(repo_path:, args:, io: Gistory::Cli::Io.new) @repo_path = repo_path @args = args + @io = io end def run repo = GitRepo.new(path: @repo_path) - parser = Cli::ArgParser.new(args: @args) + parser = Cli::ArgParser.new(args: @args, io: @io) config = parser.parse history(repo, config.gem_name) rescue Gistory::ParserError => error - puts error.message.red - puts parser + @io.error error.message + @io.puts parser rescue Gistory::Error => error - puts error.message.red + @io.error error.message end private @@ -30,13 +30,13 @@ def history(repo, gem_name) raise(Gistory::Error, "Gem '#{gem_name}' not found in lock file, maybe a typo?") end - puts "Gem: #{gem_name}" - puts "Current version: #{changes.first.version}" - puts '' + @io.puts "Gem: #{gem_name}" + @io.puts "Current version: #{changes.first.version}" + @io.puts '' - puts 'Change history:' + @io.puts 'Change history:' changes.each do |change| - puts "#{change.version} on #{change.date.strftime('%a, %e %b %Y %H:%M %Z')} (commit #{change.short_hash})" + @io.puts "#{change.version} on #{change.date.strftime('%a, %e %b %Y %H:%M %Z')} (commit #{change.short_hash})" end end end diff --git a/test/cli/arg_parser_test.rb b/test/cli/arg_parser_test.rb index cdacf55..8414664 100644 --- a/test/cli/arg_parser_test.rb +++ b/test/cli/arg_parser_test.rb @@ -25,14 +25,21 @@ def test_raises_no_args end def test_raises_no_gem - parser = ArgParser.new(args: ['-max-lockfile-changes=10']) + parser = ArgParser.new(args: ['--max-lockfile-changes', '10']) assert_raises do parser.parse end end def test_raises_invalid_args - parser = ArgParser.new(args: ['mygem', '-max-lockfile-changes=10', '-z1']) + parser = ArgParser.new(args: ['mygem', '-m10', '-z1']) + assert_raises Gistory::ParserError do + parser.parse + end + end + + def test_raises_max_lockfile_changes_non_integer + parser = ArgParser.new(args: ['mygem', '-mA']) assert_raises Gistory::ParserError do parser.parse end