Skip to content

Commit

Permalink
Merge branch 'io'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Medina committed Feb 9, 2017
2 parents 73153be + 6aca3ab commit 4bdde3e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/gistory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require 'gistory/cli/main'
require 'gistory/cli/arg_parser'
require 'gistory/cli/io'
require 'gistory/configuration'

require 'gistory/errors'
Expand Down
9 changes: 6 additions & 3 deletions lib/gistory/cli/arg_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/gistory/cli/io.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 11 additions & 11 deletions lib/gistory/cli/main.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions test/cli/arg_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4bdde3e

Please sign in to comment.