Permalink
Browse files

better cli runner as we start to test arg parsing

  • Loading branch information...
1 parent 019fa10 commit 33c52e3e5839c7dde08d68b97dd0b70772cbe9c4 @rcarver committed Feb 20, 2012
View
@@ -97,7 +97,7 @@ def option_parser
end
end
- def parse!(argv)
+ def get_command(argv)
option_parser.parse!(argv)
command_name = argv.shift or abort option_parser.to_s
@@ -113,9 +113,17 @@ def parse!(argv)
end
end
+ def parse!(argv)
+ command = nil
+ @status = catch(:quit) {
+ command = get_command(argv)
+ }
+ command
+ end
+
def run!(argv)
@status = catch(:quit) {
- command = parse!(argv)
+ command = get_command(argv)
begin
command.run
rescue MysqlInspector::Access::Error => e
@@ -167,10 +175,10 @@ class LoadCommand < Command
def parse!(argv)
@database = argv.shift or usage "load DATABASE [VERSION]"
@version = argv.shift || CURRENT
- @dump = get_dump(@version) # ensure it exists
end
def run
+ get_dump(@version) # ensure it exists
config.load_dump(@version, @database)
end
end
@@ -181,14 +189,15 @@ class GrepCommand < Command
def parse!(argv)
@version = CURRENT
@matchers = *argv.map { |a| Regexp.new(a) }
- @dump = get_dump(@version)
end
def run
- grep = Grep.new(@dump, @matchers)
+ dump = get_dump(@version)
+
+ grep = Grep.new(dump, @matchers)
grep.execute
- puts "#{@dump.db_name}@#{@version}"
+ puts "#{dump.db_name}@#{@version}"
puts
puts "grep #{@matchers.map { |m| m.inspect } * " AND "}"
@@ -210,15 +219,16 @@ class DiffCommand < Command
def parse!(argv)
@version1 = CURRENT
@version2 = TARGET
- @dump1 = get_dump(@version1)
- @dump2 = get_dump(@version2)
end
def run
- diff = Diff.new(@dump1, @dump2)
+ dump1 = get_dump(@version1)
+ dump2 = get_dump(@version2)
+
+ diff = Diff.new(dump1, dump2)
diff.execute
- puts "diff #{@dump1.db_name}@#{@version1} #{@dump2.db_name}@#{@version2}"
+ puts "diff #{dump1.db_name}@#{@version1} #{dump2.db_name}@#{@version2}"
tables = diff.added_tables + diff.missing_tables + diff.different_tables
View
@@ -83,10 +83,21 @@ class MysqlInspectorCliSpec < MysqlInspectorSpec
let(:config) { MysqlInspector::Config.new }
+ def parse_command(klass, argv)
+ command = klass.new(config, StringIO.new, StringIO.new)
+ catch(:quit) { command.parse!(argv) }
+ command
+ end
+
+ def parse_args(argv)
+ cli = MysqlInspector::CLI.new(config, StringIO.new, StringIO.new)
+ cli.parse!(argv)
+ end
+
def mysql_inspector(args)
cli = MysqlInspector::CLI.new(config, StringIO.new, StringIO.new)
argv = args.split(/\s+/).map { |x| x.gsub(/'/, '') }
- catch(:quit) { cli.run!(argv) }
+ cli.run!(argv)
cli
end
@@ -40,7 +40,7 @@
end
end
-describe "mysql-inspector unknown_command" do
+describe "mysql-inspector an unknown command" do
subject { mysql_inspector "unknown_command" }
@@ -51,3 +51,26 @@
end
end
+describe "mysql-inspector error cases" do
+
+ describe "when the database does not exist" do
+ subject { inspect_database "write #{database_name}" }
+ it "fails" do
+ stdout.must_equal ""
+ stderr.must_equal "The database #{database_name} does not exist"
+ status.must_equal 1
+ end
+ end
+
+ describe "when the dump does not exist" do
+ subject { inspect_database "load #{database_name}" }
+ before do
+ create_mysql_database ""
+ end
+ it "fails" do
+ stdout.must_equal ""
+ stderr.must_equal "Dump \"current\" does not exist"
+ status.must_equal 1
+ end
+ end
+end
@@ -1,56 +1,43 @@
describe "mysql-inspector load" do
- describe "when you don't specify a database" do
+ describe "parsing arguments" do
- subject { inspect_database "load" }
- it "fails" do
- stdout.must_equal ""
+ subject { parse_command(MysqlInspector::CLI::LoadCommand, args) }
+ let(:args) { [] }
+
+ specify "it fails when you don't specify a database" do
stderr.must_equal "Usage: mysql-inspector load DATABASE [VERSION]"
- status.must_equal 1
+ stdout.must_equal ""
end
- end
-
- describe "by default" do
-
- subject { inspect_database "load #{database_name}" }
- describe "when the dump does not exist" do
- it "fails" do
- stdout.must_equal ""
- stderr.must_equal "Dump \"current\" does not exist"
- status.must_equal 1
- end
+ specify "it loads from current" do
+ args.concat ["my_database"]
+ subject.ivar(:database).must_equal "my_database"
+ subject.ivar(:version).must_equal "current"
end
- describe "when the database does not exist" do
- before do
- create_mysql_database ""
- inspect_database "write #{database_name}"
- drop_mysql_database
- end
- it "fails" do
- stdout.must_equal ""
- stderr.must_equal "The database mysql_inspector_test does not exist"
- status.must_equal 1
- end
+ specify "it loads another version" do
+ args.concat ["my_database", "other"]
+ subject.ivar(:database).must_equal "my_database"
+ subject.ivar(:version).must_equal "other"
end
+ end
- describe "when the database and dump exist" do
- before do
- create_mysql_database schema_b
- inspect_database "write #{database_name}"
- create_mysql_database ideas_schema
- cli_access.table_names.size.must_equal 1
- end
- it "succeeds" do
- stdout.must_equal ""
- stderr.must_equal ""
- status.must_equal 0
+ describe "running" do
+ subject { inspect_database "load #{database_name}" }
+ before do
+ create_mysql_database schema_b
+ inspect_database "write #{database_name}"
+ create_mysql_database ideas_schema
+ cli_access.table_names.size.must_equal 1
+ end
+ it "succeeds" do
+ stdout.must_equal ""
+ stderr.must_equal ""
+ status.must_equal 0
- it "creates all tables"
- cli_access.table_names.size.must_equal 3
- end
+ it "creates all tables"
+ cli_access.table_names.size.must_equal 3
end
end
end
-
@@ -2,21 +2,32 @@
describe "mysql-inspector write" do
- describe "when you don't specify a database" do
+ describe "parsing arguments" do
- subject { inspect_database "write" }
+ subject { parse_command(MysqlInspector::CLI::WriteCommand, args) }
+ let(:args) { [] }
- it "fails" do
- stdout.must_equal ""
+ specify "it fails when you don't specify a database" do
stderr.must_equal "Usage: mysql-inspector write DATABASE [VERSION]"
- status.must_equal 1
+ stdout.must_equal ""
+ end
+
+ specify "it writes to current" do
+ args.concat ["my_database"]
+ subject.ivar(:database).must_equal "my_database"
+ subject.ivar(:version).must_equal "current"
+ end
+
+ specify "it writes to another version" do
+ args.concat ["my_database", "other"]
+ subject.ivar(:database).must_equal "my_database"
+ subject.ivar(:version).must_equal "other"
end
end
- describe "by default" do
+ describe "running" do
subject { inspect_database "write #{database_name}" }
-
let(:dirname) { "#{tmpdir}/current" }
specify "when the database does not exist" do
@@ -42,23 +53,4 @@
Dir.glob(dirname + "/*.table").size.must_equal 3
end
end
-
- describe "writing another version" do
-
- subject { inspect_database "write #{database_name} target" }
-
- let(:dirname) { "#{tmpdir}/target" }
-
- it "creates a directory and files" do
- create_mysql_database schema_a
-
- it "succeeds"
- stdout.must_equal ""
- stderr.must_equal ""
- status.must_equal 0
-
- it "creates a directory and files"
- File.directory?(dirname).must_equal true
- end
- end
end

0 comments on commit 33c52e3

Please sign in to comment.