Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

test the command line via the CLI object and not through the shell

  • Loading branch information...
commit fdf6805b8f604eb870f270e0290d50e0a6570c79 1 parent d946229
@rcarver authored
View
11 bin/mysql-inspector
@@ -3,15 +3,12 @@
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require "mysql_inspector"
-require "mysql_inspector/cli"
config = MysqlInspector::Config.new
cli = MysqlInspector::CLI.new(config, $stdout, $stderr)
-begin
+catch(:quit) {
cli.run(ARGV)
-rescue MysqlInspector::CLI::Exit
- exit 0
-rescue MysqlInspector::CLI::Abort
- exit 1
-end
+}
+
+exit cli.status
View
1  lib/mysql_inspector.rb
@@ -11,6 +11,7 @@
require "mysql_inspector/index"
require "mysql_inspector/access"
+require "mysql_inspector/cli"
require "mysql_inspector/config"
require "mysql_inspector/diff"
require "mysql_inspector/dump"
View
16 lib/mysql_inspector/cli.rb
@@ -1,10 +1,10 @@
require 'optparse'
+require 'stringio'
module MysqlInspector
class CLI
- Exit = Class.new(RuntimeError)
- Abort = Class.new(RuntimeError)
+ NAME = "mysql-inspector"
CURRENT = "current"
TARGET = "target"
@@ -13,19 +13,23 @@ def initialize(config, stdout=nil, stderr=nil)
@config = config
@stdout = stdout || StringIO.new
@stderr = stderr || StringIO.new
+ @status = 0
end
attr_reader :stdout
attr_reader :stderr
+ attr_reader :status
def exit(msg)
@stdout.puts msg
- raise Exit
+ @status = 0
+ throw :quit
end
def abort(msg)
@stderr.puts msg
- raise Abort
+ @status = 1
+ throw :quit
end
def puts(*args)
@@ -33,12 +37,12 @@ def puts(*args)
end
def usage(msg)
- abort "Usage: #{option_parser.program_name} #{msg}"
+ abort "Usage: #{NAME} #{msg}"
end
def option_parser
@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 "Options"
View
18 test/helper.rb
@@ -2,6 +2,7 @@
require 'minitest/mock'
require 'open3'
require 'ostruct'
+require 'stringio'
require 'mysql_inspector'
@@ -76,25 +77,30 @@ def cli_access
end
end
-class MysqlInspectorBinarySpec < MysqlInspectorSpec
+class MysqlInspectorCliSpec < MysqlInspectorSpec
register_spec_type(self) { |desc| desc =~ /mysql-inspector/ }
+ let(:config) { MysqlInspector::Config.new }
+
def mysql_inspector(args)
- stdout, stderr, status = Open3.capture3("mysql-inspector #{args}")
- OpenStruct.new(:stdout => stdout, :stderr => stderr, :status => status.exitstatus)
+ cli = MysqlInspector::CLI.new(config, StringIO.new, StringIO.new)
+ argv = args.split(/\s+/).map { |x| x.gsub(/'/, '') }
+ catch(:quit) { cli.run(argv) }
+ cli
end
def inspect_database(args)
- mysql_inspector "--out #{tmpdir} #{args}"
+ config.dir = tmpdir
+ mysql_inspector args
end
def stdout
- subject.stdout.chomp
+ subject.stdout.string.chomp
end
def stderr
- subject.stderr.chomp
+ subject.stderr.string.chomp
end
def status
Please sign in to comment.
Something went wrong with that request. Please try again.