Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 5 commits
  • 9 files changed
  • 0 comments
  • 1 contributor
6  bin/mysql-inspector
@@ -6,9 +6,5 @@ require "mysql_inspector"
6 6
 
7 7
 config = MysqlInspector::Config.new
8 8
 cli = MysqlInspector::CLI.new(config, $stdout, $stderr)
9  
-
10  
-catch(:quit) {
11  
-  cli.run(ARGV)
12  
-}
13  
-
  9
+cli.run!(ARGV)
14 10
 exit cli.status
305  lib/mysql_inspector/cli.rb
... ...
@@ -1,5 +1,4 @@
1 1
 require 'optparse'
2  
-require 'stringio'
3 2
 
4 3
 module MysqlInspector
5 4
   class CLI
@@ -9,37 +8,61 @@ class CLI
9 8
     CURRENT = "current"
10 9
     TARGET  = "target"
11 10
 
12  
-    def initialize(config, stdout=nil, stderr=nil)
13  
-      @config = config
14  
-      @stdout = stdout || StringIO.new
15  
-      @stderr = stderr || StringIO.new
16  
-      @status = 0
17  
-    end
  11
+    module Helper
18 12
 
19  
-    attr_reader :stdout
20  
-    attr_reader :stderr
21  
-    attr_reader :status
  13
+      def exit(msg)
  14
+        @stdout.puts msg
  15
+        throw :quit, 0
  16
+      end
22 17
 
23  
-    def exit(msg)
24  
-      @stdout.puts msg
25  
-      @status = 0
26  
-      throw :quit
27  
-    end
  18
+      def abort(msg)
  19
+        @stderr.puts msg
  20
+        throw :quit, 1
  21
+      end
  22
+
  23
+      def usage(msg)
  24
+        abort "Usage: #{NAME} #{msg}"
  25
+      end
28 26
 
29  
-    def abort(msg)
30  
-      @stderr.puts msg
31  
-      @status = 1
32  
-      throw :quit
  27
+      def puts(*args)
  28
+        @stdout.puts(*args)
  29
+      end
33 30
     end
34 31
 
35  
-    def puts(*args)
36  
-      @stdout.puts(*args)
  32
+    module Formatting
  33
+      # Print table item details.
  34
+      #
  35
+      # Examples
  36
+      #
  37
+      #     LABEL    item1
  38
+      #              item2
  39
+      #
  40
+      def format_items(label, items, &formatter)
  41
+        pad = " " * 4
  42
+        formatter ||= proc { |item | item.to_sql }
  43
+        items.each.with_index { |item, i|
  44
+          if i == 0
  45
+            puts [label, pad, formatter.call(item)] * ""
  46
+          else
  47
+            puts [" " * label.size, pad, formatter.call(item)] * ""
  48
+          end
  49
+        }
  50
+      end
37 51
     end
38 52
 
39  
-    def usage(msg)
40  
-      abort "Usage: #{NAME} #{msg}"
  53
+    include Helper
  54
+
  55
+    def initialize(config, stdout, stderr)
  56
+      @config = config
  57
+      @stdout = stdout
  58
+      @stderr = stderr
  59
+      @status = 0
41 60
     end
42 61
 
  62
+    attr_reader :stdout
  63
+    attr_reader :stderr
  64
+    attr_reader :status
  65
+
43 66
     def option_parser
44 67
       @option_parser ||= OptionParser.new do |opts|
45 68
         opts.banner = "Usage: #{NAME} [options] command [command args]"
@@ -74,128 +97,190 @@ def option_parser
74 97
       end
75 98
     end
76 99
 
77  
-    def run(argv)
  100
+    def get_command(argv)
78 101
       option_parser.parse!(argv)
79 102
 
80  
-      command = argv.shift
  103
+      command_name = argv.shift or abort option_parser.to_s
  104
+      command_class = command_name.capitalize + "Command"
81 105
 
82  
-      if respond_to?("run_#{command}")
83  
-        send("run_#{command}", argv)
84  
-      else
85  
-        abort option_parser.to_s
  106
+      begin
  107
+        klass = self.class.const_get(command_class)
  108
+        command = klass.new(@config, @stdout, @stderr)
  109
+        command.parse!(argv)
  110
+        command
  111
+      rescue NameError
  112
+        abort "Unknown command #{command_name.inspect}"
86 113
       end
87 114
     end
88 115
 
89  
-    def run_write(argv)
90  
-      database = argv.shift or usage "write DATABASE [VERSION]"
91  
-      version  = argv.shift || CURRENT
  116
+    def run!(argv)
  117
+      @status = catch(:quit) {
  118
+        command = get_command(argv)
  119
+        command.run!
  120
+      }
  121
+    end
92 122
 
93  
-      begin
94  
-        @config.write_dump(version, database)
95  
-      rescue MysqlInspector::Access::Error => e
96  
-        abort e.message
  123
+    class Command
  124
+      include Helper
  125
+
  126
+      def initialize(config, stdout, stderr)
  127
+        @config = config
  128
+        @stdout = stdout
  129
+        @stderr = stderr
  130
+        @status = nil
97 131
       end
98  
-    end
99 132
 
100  
-    def run_load(argv)
101  
-      database = argv.shift or usage "load DATABASE [VERSION]"
102  
-      version  = argv.shift || CURRENT
  133
+      attr_reader :config
  134
+      attr_reader :stdout
  135
+      attr_reader :stderr
  136
+      attr_reader :status
103 137
 
104  
-      get_dump(version) # ensure it exists
  138
+      def ivar(name)
  139
+        instance_variable_get("@#{name}")
  140
+      end
105 141
 
106  
-      begin
107  
-        @config.load_dump(version, database)
108  
-      rescue MysqlInspector::Access::Error => e
109  
-        abort e.message
  142
+      def get_dump(version)
  143
+        dump = @config.create_dump(version)
  144
+        dump.exists? or abort "Dump #{version.inspect} does not exist"
  145
+        dump
  146
+      end
  147
+
  148
+      def parse!(argv)
  149
+        @status = catch(:quit) {
  150
+          parse(argv)
  151
+        }
  152
+      end
  153
+
  154
+      def run!
  155
+        @status = catch(:quit) {
  156
+          begin
  157
+            run
  158
+            throw :quit, 0
  159
+          rescue MysqlInspector::Access::Error => e
  160
+            abort e.message
  161
+          end
  162
+        }
110 163
       end
111 164
     end
112 165
 
113  
-    def run_grep(argv)
114  
-      dump = get_dump(CURRENT)
  166
+    class WriteCommand < Command
115 167
 
116  
-      matchers = *argv.map { |a| Regexp.new(a) }
117  
-      grep = Grep.new(dump, matchers)
118  
-      grep.execute
  168
+      def parse(argv)
  169
+        @database = argv.shift or usage "write DATABASE [VERSION]"
  170
+        @version = argv.shift || CURRENT
  171
+      end
119 172
 
120  
-      puts "#{dump.db_name}@#{CURRENT}"
121  
-      puts
122  
-      puts "grep #{matchers.map { |m| m.inspect } * " AND "}"
  173
+      def run
  174
+        config.write_dump(@version, @database)
  175
+      end
  176
+    end
123 177
 
124  
-      puts if grep.any_matches?
  178
+    class LoadCommand < Command
125 179
 
126  
-      grep.each_table { |table, subset|
127  
-        puts table.table_name
128  
-        format_items("COL", subset.columns)
129  
-        format_items("IDX", subset.indices)
130  
-        format_items("CST", subset.constraints)
131  
-        puts
132  
-      }
  180
+      def parse(argv)
  181
+        @database = argv.shift or usage "load DATABASE [VERSION]"
  182
+        @version  = argv.shift || CURRENT
  183
+      end
  184
+
  185
+      def run
  186
+        get_dump(@version) # ensure it exists
  187
+        config.load_dump(@version, @database)
  188
+      end
133 189
     end
134 190
 
135  
-    def run_diff(argv)
136  
-      dump1 = get_dump(CURRENT)
137  
-      dump2 = get_dump(TARGET)
  191
+    class GrepCommand < Command
  192
+      include Formatting
138 193
 
139  
-      diff = Diff.new(dump1, dump2)
140  
-      diff.execute
  194
+      def parse(argv)
  195
+        @version = CURRENT
  196
+        @matchers = *argv.map { |a| Regexp.new(a) }
  197
+      end
141 198
 
142  
-      puts "diff #{dump1.db_name}@#{CURRENT} #{dump2.db_name}@#{TARGET}"
  199
+      def run
  200
+        dump = get_dump(@version)
143 201
 
144  
-      tables = diff.added_tables + diff.missing_tables + diff.different_tables
  202
+        grep = Grep.new(dump, @matchers)
  203
+        grep.execute
145 204
 
146  
-      if tables.any?
147  
-        puts
148  
-        tables.sort.each do |t|
149  
-          prefix = diff_prefix_for_table(t, diff)
150  
-          puts "#{prefix} #{t.table_name}"
151  
-          if t.is_a?(Diff::TableDiff)
152  
-            diff_format_items("  COL", t.added_columns, t.missing_columns)
153  
-            diff_format_items("  IDX", t.added_indices, t.missing_indices)
154  
-            diff_format_items("  CST", t.added_constraints, t.missing_constraints)
155  
-          end
156  
-        end
  205
+        puts "#{dump.db_name}@#{@version}"
157 206
         puts
158  
-      end
159  
-    end
  207
+        puts "grep #{@matchers.map { |m| m.inspect } * " AND "}"
160 208
 
161  
-  protected
  209
+        puts if grep.any_matches?
162 210
 
163  
-    def get_dump(version)
164  
-      dump = @config.create_dump(version)
165  
-      dump.exists? or abort "Dump #{version.inspect} does not exist"
166  
-      dump
  211
+        grep.each_table { |table, subset|
  212
+          puts table.table_name
  213
+          format_items("COL", subset.columns)
  214
+          format_items("IDX", subset.indices)
  215
+          format_items("CST", subset.constraints)
  216
+          puts
  217
+        }
  218
+      end
167 219
     end
168 220
 
169  
-    # Print table details
170  
-    def format_items(label, items, &formatter)
171  
-      pad = " " * 4
172  
-      formatter ||= proc { |item | item.to_sql }
173  
-      items.each.with_index { |item, i|
174  
-        if i == 0
175  
-          puts [label, pad, formatter.call(item)] * ""
  221
+    class DiffCommand < Command
  222
+      include Formatting
  223
+
  224
+      def parse(argv)
  225
+        case argv.size
  226
+        when 0
  227
+          @version1 = CURRENT
  228
+          @version2 = TARGET
  229
+        when 1
  230
+          @version1 = CURRENT
  231
+          @version2 = argv.shift
176 232
         else
177  
-          puts [" " * label.size, pad, formatter.call(item)] * ""
  233
+          @version1 = argv.shift
  234
+          @version2 = argv.shift
178 235
         end
179  
-      }
180  
-    end
  236
+      end
  237
+
  238
+      def run
  239
+        dump1 = get_dump(@version1)
  240
+        dump2 = get_dump(@version2)
  241
+
  242
+        diff = Diff.new(dump1, dump2)
  243
+        diff.execute
  244
+
  245
+        puts "diff #{dump1.db_name}@#{@version1} #{dump2.db_name}@#{@version2}"
181 246
 
182  
-    def diff_prefix_for_table(table, diff)
183  
-      case
184  
-      when diff.added_tables.include?(table) then "+"
185  
-      when diff.missing_tables.include?(table) then "-"
186  
-      else "="
  247
+        tables = diff.added_tables + diff.missing_tables + diff.different_tables
  248
+
  249
+        if tables.any?
  250
+          puts
  251
+          tables.sort.each do |t|
  252
+            prefix = prefix_for_table(t, diff)
  253
+            puts "#{prefix} #{t.table_name}"
  254
+            if t.is_a?(Diff::TableDiff)
  255
+              format_diff_items("  COL", t.added_columns, t.missing_columns)
  256
+              format_diff_items("  IDX", t.added_indices, t.missing_indices)
  257
+              format_diff_items("  CST", t.added_constraints, t.missing_constraints)
  258
+            end
  259
+          end
  260
+          puts
  261
+        end
187 262
       end
188  
-    end
189 263
 
190  
-    def diff_prefix_for_item(item, added, removed)
191  
-      added.include?(item) ? "+" : "-"
192  
-    end
  264
+    protected
193 265
 
194  
-    def diff_format_items(label, added, removed)
195  
-      format_items(label, (added + removed).sort) { |item|
196  
-        prefix = diff_prefix_for_item(item, added, removed)
197  
-        "#{prefix} #{item.to_sql}"
198  
-      }
  266
+      def prefix_for_table(table, diff)
  267
+        case
  268
+        when diff.added_tables.include?(table) then "+"
  269
+        when diff.missing_tables.include?(table) then "-"
  270
+        else "="
  271
+        end
  272
+      end
  273
+
  274
+      def prefix_for_item(item, added, removed)
  275
+        added.include?(item) ? "+" : "-"
  276
+      end
  277
+
  278
+      def format_diff_items(label, added, removed)
  279
+        format_items(label, (added + removed).sort) { |item|
  280
+          prefix = prefix_for_item(item, added, removed)
  281
+          "#{prefix} #{item.to_sql}"
  282
+        }
  283
+      end
199 284
     end
200 285
 
201 286
   end
1  lib/mysql_inspector/config.rb
@@ -46,6 +46,7 @@ def access(database_name)
46 46
     end
47 47
 
48 48
     def create_dump(version)
  49
+      raise [dir, version].inspect if dir.nil? or version.nil?
49 50
       Dump.new(File.join(dir, version))
50 51
     end
51 52
 
20  test/helper.rb
@@ -83,15 +83,31 @@ class MysqlInspectorCliSpec < MysqlInspectorSpec
83 83
 
84 84
   let(:config) { MysqlInspector::Config.new }
85 85
 
  86
+  before do
  87
+    config.dir = tmpdir
  88
+  end
  89
+
  90
+  def parse_command(klass, argv)
  91
+    command = klass.new(config, StringIO.new, StringIO.new)
  92
+    command.parse!(argv)
  93
+    command
  94
+  end
  95
+
  96
+  def run_command(klass, argv)
  97
+    command = klass.new(config, StringIO.new, StringIO.new)
  98
+    command.parse!(argv)
  99
+    command.run!
  100
+    command
  101
+  end
  102
+
86 103
   def mysql_inspector(args)
87 104
     cli = MysqlInspector::CLI.new(config, StringIO.new, StringIO.new)
88 105
     argv = args.split(/\s+/).map { |x| x.gsub(/'/, '') }
89  
-    catch(:quit) { cli.run(argv) }
  106
+    cli.run!(argv)
90 107
     cli
91 108
   end
92 109
 
93 110
   def inspect_database(args)
94  
-    config.dir = tmpdir
95 111
     mysql_inspector args
96 112
   end
97 113
 
34  test/mysql_inspector/cli_basics_test.rb
@@ -40,3 +40,37 @@
40 40
   end
41 41
 end
42 42
 
  43
+describe "mysql-inspector an unknown command" do
  44
+
  45
+  subject { mysql_inspector "unknown_command" }
  46
+
  47
+  it "fails" do
  48
+    stderr.must_equal "Unknown command \"unknown_command\""
  49
+    stdout.must_equal ""
  50
+    status.must_equal 1
  51
+  end
  52
+end
  53
+
  54
+describe "mysql-inspector error cases" do
  55
+
  56
+  describe "when the database does not exist" do
  57
+    subject { inspect_database "write #{database_name}" }
  58
+    it "fails" do
  59
+      stdout.must_equal ""
  60
+      stderr.must_equal "The database #{database_name} does not exist"
  61
+      status.must_equal 1
  62
+    end
  63
+  end
  64
+
  65
+  describe "when the dump does not exist" do
  66
+    subject { inspect_database "load #{database_name}" }
  67
+    before do
  68
+      create_mysql_database ""
  69
+    end
  70
+    it "fails" do
  71
+      stdout.must_equal ""
  72
+      stderr.must_equal "Dump \"current\" does not exist"
  73
+      status.must_equal 1
  74
+    end
  75
+  end
  76
+end
42  test/mysql_inspector/cli_diff_test.rb
@@ -2,33 +2,33 @@
2 2
 
3 3
 describe "mysql-inspector diff" do
4 4
 
5  
-  describe "in general" do
  5
+  describe "parsing arguments" do
6 6
 
7  
-    before do
8  
-      create_mysql_database schema_a
9  
-    end
  7
+    subject { parse_command(MysqlInspector::CLI::DiffCommand, args) }
  8
+    let(:args) { [] }
10 9
 
11  
-    subject { inspect_database "diff" }
  10
+    specify "it compares current to target" do
  11
+      subject.ivar(:version1).must_equal "current"
  12
+      subject.ivar(:version2).must_equal "target"
  13
+    end
12 14
 
13  
-    describe "when no current dump exists" do
14  
-      it "tells you" do
15  
-        stderr.must_equal %(Dump "current" does not exist)
16  
-        stdout.must_equal ""
17  
-        status.must_equal 1
18  
-      end
  15
+    specify "it compares current to something else" do
  16
+      args << "other"
  17
+      subject.ivar(:version1).must_equal "current"
  18
+      subject.ivar(:version2).must_equal "other"
19 19
     end
20 20
 
21  
-    describe "when no target dump exists" do
22  
-      it "tells you" do
23  
-        inspect_database "write #{database_name}"
24  
-        stderr.must_equal %(Dump "target" does not exist)
25  
-        stdout.must_equal ""
26  
-        status.must_equal 1
27  
-      end
  21
+    specify "it compares two arbitrary versions" do
  22
+      args << "other1"
  23
+      args << "other2"
  24
+      subject.ivar(:version1).must_equal "other1"
  25
+      subject.ivar(:version2).must_equal "other2"
28 26
     end
29 27
   end
30 28
 
31  
-  describe "when two dumps exist to compare" do
  29
+  describe "running" do
  30
+
  31
+    subject { inspect_database "diff" }
32 32
 
33 33
     before do
34 34
       create_mysql_database schema_a
@@ -37,9 +37,7 @@
37 37
       inspect_database "write #{database_name} target"
38 38
     end
39 39
 
40  
-    subject { inspect_database "diff" }
41  
-
42  
-    it "shows the differences" do
  40
+    specify do
43 41
       stderr.must_equal ""
44 42
       stdout.must_equal <<-EOL.unindented
45 43
         diff mysql_inspector_test@current mysql_inspector_test@target
121  test/mysql_inspector/cli_grep_test.rb
@@ -2,102 +2,77 @@
2 2
 
3 3
 describe "mysql-inspector grep" do
4 4
 
5  
-  before do
6  
-    create_mysql_database [users_schema, things_schema] * ";"
7  
-  end
  5
+  describe "parsing arguments" do
8 6
 
9  
-  describe "in general" do
  7
+    subject { parse_command(MysqlInspector::CLI::GrepCommand, args) }
  8
+    let(:args) { [] }
10 9
 
11  
-    subject { inspect_database "grep name" }
  10
+    specify "it searches current" do
  11
+      args.concat ["a", "^b"]
  12
+      subject.ivar(:version).must_equal "current"
  13
+      subject.ivar(:matchers).must_equal [/a/, /^b/]
  14
+    end
12 15
 
13  
-    describe "when no dump exists" do
14  
-      it "tells you" do
15  
-        stderr.must_equal %(Dump "current" does not exist)
16  
-        stdout.must_equal ""
17  
-        status.must_equal 1
18  
-      end
  16
+    specify "it searches another version" do
  17
+      skip "not supported. how would the args work?"
19 18
     end
20 19
   end
21 20
 
22  
-  describe "when a dump exists" do
  21
+  describe "running" do
23 22
     before do
  23
+      create_mysql_database [users_schema, things_schema] * ";"
24 24
       inspect_database "write #{database_name}"
25 25
     end
26 26
 
27  
-    describe "searching for a single term" do
  27
+    subject { inspect_database "grep #{matchers * ' '}" }
  28
+    let(:matchers) { [] }
28 29
 
29  
-      subject { inspect_database "grep name" }
  30
+    specify "searching for a single term" do
  31
+      matchers << "name"
30 32
 
31  
-      it "finds stuff" do
32  
-        stderr.must_equal ""
33  
-        stdout.must_equal <<-EOL.unindented
34  
-          mysql_inspector_test@current
  33
+      stderr.must_equal ""
  34
+      stdout.must_equal <<-EOL.unindented
  35
+        mysql_inspector_test@current
35 36
 
36  
-          grep /name/
  37
+        grep /name/
37 38
 
38  
-          things
39  
-          COL    `first_name` varchar(255) NOT NULL
40  
-                 `last_name` varchar(255) NOT NULL
41  
-                 `name` varchar(255) NOT NULL DEFAULT 'toy'
42  
-          IDX    KEY `name` (`first_name`,`last_name`)
43  
-          CST    CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
  39
+        things
  40
+        COL    `first_name` varchar(255) NOT NULL
  41
+               `last_name` varchar(255) NOT NULL
  42
+               `name` varchar(255) NOT NULL DEFAULT 'toy'
  43
+        IDX    KEY `name` (`first_name`,`last_name`)
  44
+        CST    CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
44 45
 
45  
-          users
46  
-          COL    `first_name` varchar(255) NOT NULL
47  
-                 `last_name` varchar(255) NOT NULL
48  
-          IDX    KEY `name` (`first_name`,`last_name`)
  46
+        users
  47
+        COL    `first_name` varchar(255) NOT NULL
  48
+               `last_name` varchar(255) NOT NULL
  49
+        IDX    KEY `name` (`first_name`,`last_name`)
49 50
 
50  
-        EOL
51  
-        status.must_equal 0
52  
-      end
  51
+      EOL
  52
+      status.must_equal 0
53 53
     end
54 54
 
55  
-    describe "anchoring the seach term" do
56  
-
57  
-      subject { inspect_database "grep '^name'" }
58  
-
59  
-      it "finds stuff" do
60  
-        stderr.must_equal ""
61  
-        stdout.must_equal <<-EOL.unindented
62  
-          mysql_inspector_test@current
63  
-
64  
-          grep /^name/
65  
-
66  
-          things
67  
-          COL    `name` varchar(255) NOT NULL DEFAULT 'toy'
68  
-          IDX    KEY `name` (`first_name`,`last_name`)
69  
-
70  
-          users
71  
-          IDX    KEY `name` (`first_name`,`last_name`)
72  
-
73  
-        EOL
74  
-        status.must_equal 0
75  
-      end
76  
-    end
77  
-
78  
-    describe "searching for multiple terms" do
79  
-
80  
-      subject { inspect_database "grep name first" }
  55
+    specify "searching for multiple terms" do
  56
+      matchers << "name"
  57
+      matchers << "first"
81 58
 
82  
-      it "finds stuff" do
83  
-        stderr.must_equal ""
84  
-        stdout.must_equal <<-EOL.unindented
85  
-          mysql_inspector_test@current
  59
+      stderr.must_equal ""
  60
+      stdout.must_equal <<-EOL.unindented
  61
+        mysql_inspector_test@current
86 62
 
87  
-          grep /name/ AND /first/
  63
+        grep /name/ AND /first/
88 64
 
89  
-          things
90  
-          COL    `first_name` varchar(255) NOT NULL
91  
-          IDX    KEY `name` (`first_name`,`last_name`)
92  
-          CST    CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
  65
+        things
  66
+        COL    `first_name` varchar(255) NOT NULL
  67
+        IDX    KEY `name` (`first_name`,`last_name`)
  68
+        CST    CONSTRAINT `belongs_to_user` FOREIGN KEY (`first_name`,`last_name`) REFERENCES `users` (`first_name`,`last_name`) ON DELETE NO ACTION ON UPDATE CASCADE
93 69
 
94  
-          users
95  
-          COL    `first_name` varchar(255) NOT NULL
96  
-          IDX    KEY `name` (`first_name`,`last_name`)
  70
+        users
  71
+        COL    `first_name` varchar(255) NOT NULL
  72
+        IDX    KEY `name` (`first_name`,`last_name`)
97 73
 
98  
-        EOL
99  
-        status.must_equal 0
100  
-      end
  74
+      EOL
  75
+      status.must_equal 0
101 76
     end
102 77
   end
103 78
 end
73  test/mysql_inspector/cli_load_test.rb
... ...
@@ -1,56 +1,43 @@
1 1
 describe "mysql-inspector load" do
2 2
 
3  
-  describe "when you don't specify a database" do
  3
+  describe "parsing arguments" do
4 4
 
5  
-    subject { inspect_database "load" }
6  
-    it "fails" do
7  
-      stdout.must_equal ""
  5
+    subject { parse_command(MysqlInspector::CLI::LoadCommand, args) }
  6
+    let(:args) { [] }
  7
+
  8
+    specify "it fails when you don't specify a database" do
8 9
       stderr.must_equal "Usage: mysql-inspector load DATABASE [VERSION]"
9  
-      status.must_equal 1
  10
+      stdout.must_equal ""
10 11
     end
11  
-  end
12  
-
13  
-  describe "by default" do
14  
-
15  
-    subject { inspect_database "load #{database_name}" }
16 12
 
17  
-    describe "when the dump does not exist" do
18  
-      it "fails" do
19  
-        stdout.must_equal ""
20  
-        stderr.must_equal "Dump \"current\" does not exist"
21  
-        status.must_equal 1
22  
-      end
  13
+    specify "it loads from current" do
  14
+      args.concat ["my_database"]
  15
+      subject.ivar(:database).must_equal "my_database"
  16
+      subject.ivar(:version).must_equal "current"
23 17
     end
24 18
 
25  
-    describe "when the database does not exist" do
26  
-      before do
27  
-        create_mysql_database ""
28  
-        inspect_database "write #{database_name}"
29  
-        drop_mysql_database
30  
-      end
31  
-      it "fails" do
32  
-        stdout.must_equal ""
33  
-        stderr.must_equal "The database mysql_inspector_test does not exist"
34  
-        status.must_equal 1
35  
-      end
  19
+    specify "it loads another version" do
  20
+      args.concat ["my_database", "other"]
  21
+      subject.ivar(:database).must_equal "my_database"
  22
+      subject.ivar(:version).must_equal "other"
36 23
     end
  24
+  end
  25
+
  26
+  describe "running" do
  27
+    subject { inspect_database "load #{database_name}" }
  28
+    specify do
  29
+      create_mysql_database schema_b
  30
+      inspect_database "write #{database_name}"
  31
+      create_mysql_database ideas_schema
  32
+      cli_access.table_names.size.must_equal 1
37 33
 
38  
-    describe "when the database and dump exist" do
39  
-      before do
40  
-        create_mysql_database schema_b
41  
-        inspect_database "write #{database_name}"
42  
-        create_mysql_database ideas_schema
43  
-        cli_access.table_names.size.must_equal 1
44  
-      end
45  
-      it "succeeds" do
46  
-        stdout.must_equal ""
47  
-        stderr.must_equal ""
48  
-        status.must_equal 0
49  
-
50  
-        it "creates all tables"
51  
-        cli_access.table_names.size.must_equal 3
52  
-      end
  34
+      it "outputs nothing"
  35
+      stdout.must_equal ""
  36
+      stderr.must_equal ""
  37
+      status.must_equal 0
  38
+
  39
+      it "creates all tables"
  40
+      cli_access.table_names.size.must_equal 3
53 41
     end
54 42
   end
55 43
 end
56  
-
47  test/mysql_inspector/cli_write_test.rb
@@ -2,19 +2,33 @@
2 2
 
3 3
 describe "mysql-inspector write" do
4 4
 
5  
-  describe "when you don't specify a database" do
  5
+  describe "parsing arguments" do
6 6
 
7  
-    subject { inspect_database "write" }
8  
-    it "fails" do
9  
-      stdout.must_equal ""
  7
+    subject { parse_command(MysqlInspector::CLI::WriteCommand, args) }
  8
+    let(:args) { [] }
  9
+
  10
+    specify "it fails when you don't specify a database" do
10 11
       stderr.must_equal "Usage: mysql-inspector write DATABASE [VERSION]"
11  
-      status.must_equal 1
  12
+      stdout.must_equal ""
  13
+    end
  14
+
  15
+    specify "it writes to current" do
  16
+      args.concat ["my_database"]
  17
+      subject.ivar(:database).must_equal "my_database"
  18
+      subject.ivar(:version).must_equal "current"
  19
+    end
  20
+
  21
+    specify "it writes to another version" do
  22
+      args.concat ["my_database", "other"]
  23
+      subject.ivar(:database).must_equal "my_database"
  24
+      subject.ivar(:version).must_equal "other"
12 25
     end
13 26
   end
14 27
 
15  
-  describe "by default" do
  28
+  describe "running" do
16 29
 
17  
-    subject { inspect_database "write #{database_name}" }
  30
+    subject { run_command(MysqlInspector::CLI::WriteCommand, args) }
  31
+    let(:args) { [database_name] }
18 32
 
19 33
     let(:dirname) { "#{tmpdir}/current" }
20 34
 
@@ -41,23 +55,4 @@
41 55
       Dir.glob(dirname + "/*.table").size.must_equal 3
42 56
     end
43 57
   end
44  
-
45  
-  describe "writing another version" do
46  
-
47  
-    subject { inspect_database "write #{database_name} target" }
48  
-
49  
-    let(:dirname) { "#{tmpdir}/target" }
50  
-
51  
-    it "creates a directory and files" do
52  
-      create_mysql_database schema_a
53  
-
54  
-      it "succeeds"
55  
-      stdout.must_equal ""
56  
-      stderr.must_equal ""
57  
-      status.must_equal 0
58  
-
59  
-      it "creates a directory and files"
60  
-      File.directory?(dirname).must_equal true
61  
-    end
62  
-  end
63 58
 end

No commit comments for this range

Something went wrong with that request. Please try again.