Skip to content

Commit

Permalink
test the command line via the CLI object and not through the shell
Browse files Browse the repository at this point in the history
  • Loading branch information
rcarver committed Feb 19, 2012
1 parent d946229 commit fdf6805
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
11 changes: 4 additions & 7 deletions bin/mysql-inspector
Expand Up @@ -3,15 +3,12 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'


require "mysql_inspector" require "mysql_inspector"
require "mysql_inspector/cli"


config = MysqlInspector::Config.new config = MysqlInspector::Config.new
cli = MysqlInspector::CLI.new(config, $stdout, $stderr) cli = MysqlInspector::CLI.new(config, $stdout, $stderr)


begin catch(:quit) {
cli.run(ARGV) cli.run(ARGV)
rescue MysqlInspector::CLI::Exit }
exit 0
rescue MysqlInspector::CLI::Abort exit cli.status
exit 1
end
1 change: 1 addition & 0 deletions lib/mysql_inspector.rb
Expand Up @@ -11,6 +11,7 @@
require "mysql_inspector/index" require "mysql_inspector/index"


require "mysql_inspector/access" require "mysql_inspector/access"
require "mysql_inspector/cli"
require "mysql_inspector/config" require "mysql_inspector/config"
require "mysql_inspector/diff" require "mysql_inspector/diff"
require "mysql_inspector/dump" require "mysql_inspector/dump"
Expand Down
16 changes: 10 additions & 6 deletions lib/mysql_inspector/cli.rb
@@ -1,10 +1,10 @@
require 'optparse' require 'optparse'
require 'stringio'


module MysqlInspector module MysqlInspector
class CLI class CLI


Exit = Class.new(RuntimeError) NAME = "mysql-inspector"
Abort = Class.new(RuntimeError)


CURRENT = "current" CURRENT = "current"
TARGET = "target" TARGET = "target"
Expand All @@ -13,32 +13,36 @@ def initialize(config, stdout=nil, stderr=nil)
@config = config @config = config
@stdout = stdout || StringIO.new @stdout = stdout || StringIO.new
@stderr = stderr || StringIO.new @stderr = stderr || StringIO.new
@status = 0
end end


attr_reader :stdout attr_reader :stdout
attr_reader :stderr attr_reader :stderr
attr_reader :status


def exit(msg) def exit(msg)
@stdout.puts msg @stdout.puts msg
raise Exit @status = 0
throw :quit
end end


def abort(msg) def abort(msg)
@stderr.puts msg @stderr.puts msg
raise Abort @status = 1
throw :quit
end end


def puts(*args) def puts(*args)
@stdout.puts(*args) @stdout.puts(*args)
end end


def usage(msg) def usage(msg)
abort "Usage: #{option_parser.program_name} #{msg}" abort "Usage: #{NAME} #{msg}"
end end


def option_parser def option_parser
@option_parser ||= OptionParser.new do |opts| @option_parser ||= OptionParser.new do |opts|
opts.banner = "Usage: #{opts.program_name} [options] command [command args]" opts.banner = "Usage: #{NAME} [options] command [command args]"


opts.separator "" opts.separator ""
opts.separator "Options" opts.separator "Options"
Expand Down
18 changes: 12 additions & 6 deletions test/helper.rb
Expand Up @@ -2,6 +2,7 @@
require 'minitest/mock' require 'minitest/mock'
require 'open3' require 'open3'
require 'ostruct' require 'ostruct'
require 'stringio'


require 'mysql_inspector' require 'mysql_inspector'


Expand Down Expand Up @@ -76,25 +77,30 @@ def cli_access
end end
end end


class MysqlInspectorBinarySpec < MysqlInspectorSpec class MysqlInspectorCliSpec < MysqlInspectorSpec


register_spec_type(self) { |desc| desc =~ /mysql-inspector/ } register_spec_type(self) { |desc| desc =~ /mysql-inspector/ }


let(:config) { MysqlInspector::Config.new }

def mysql_inspector(args) def mysql_inspector(args)
stdout, stderr, status = Open3.capture3("mysql-inspector #{args}") cli = MysqlInspector::CLI.new(config, StringIO.new, StringIO.new)
OpenStruct.new(:stdout => stdout, :stderr => stderr, :status => status.exitstatus) argv = args.split(/\s+/).map { |x| x.gsub(/'/, '') }
catch(:quit) { cli.run(argv) }
cli
end end


def inspect_database(args) def inspect_database(args)
mysql_inspector "--out #{tmpdir} #{args}" config.dir = tmpdir
mysql_inspector args
end end


def stdout def stdout
subject.stdout.chomp subject.stdout.string.chomp
end end


def stderr def stderr
subject.stderr.chomp subject.stderr.string.chomp
end end


def status def status
Expand Down

0 comments on commit fdf6805

Please sign in to comment.