excavator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+
3
+ context "Namespace" do
4
+ include Excavator
5
+
6
+ setup do
7
+ @namespace = Namespace.new
8
+ end
9
+
10
+ test "add command to namespace" do
11
+ cmd = MiniTest::Mock.new
12
+ cmd.expect(:name, :cmd)
13
+ @namespace << cmd
14
+ assert_equal cmd.object_id, @namespace.command(:cmd).object_id
15
+ end
16
+
17
+ test "add namespace to namespace" do
18
+ ns = Namespace.new("child")
19
+ @namespace << ns
20
+ assert_equal ns, @namespace.namespace(:child)
21
+ end
22
+
23
+ test "add namespace to namespace creates a connection" do
24
+ ns = Namespace.new("child")
25
+ @namespace << ns
26
+ assert_equal ns.parent, @namespace
27
+ end
28
+
29
+ test "#fullname" do
30
+ assert_equal "", @namespace.full_name
31
+
32
+ ns1 = Namespace.new("first")
33
+ @namespace << ns1
34
+ assert_equal "first", ns1.full_name
35
+
36
+ ns2 = Namespace.new("second")
37
+ ns1 << ns2
38
+ assert_equal "first:second", ns2.full_name
39
+ end
40
+
41
+ test "#commands_and_descriptions returns names and description" do
42
+ cmd = MiniTest::Mock.new
43
+ cmd.expect(:name, "command")
44
+ cmd.expect(:desc, "command description")
45
+ @namespace << cmd
46
+
47
+ ns1 = Namespace.new("first")
48
+ @namespace << ns1
49
+
50
+ inner_cmd = MiniTest::Mock.new
51
+ inner_cmd.expect(:name, "inner_command")
52
+ inner_cmd.expect(:desc, "inner command description")
53
+ ns1 << inner_cmd
54
+
55
+ expected = [
56
+ ["command", "command description"],
57
+ ["first:inner_command", "inner command description"]
58
+ ]
59
+ assert_equal expected, @namespace.commands_and_descriptions
60
+ end
61
+ end
@@ -0,0 +1,128 @@
1
+ require 'test_helper'
2
+
3
+ context "ParamParser" do
4
+ include Excavator
5
+
6
+ setup do
7
+ @parser = ParamParser.new
8
+ @parser.name = "test-command"
9
+ end
10
+
11
+ test "parses params into a hash using long switches" do
12
+ @parser.build(
13
+ :params => [ Param.new(:region), Param.new(:size) ]
14
+ )
15
+
16
+ hash = @parser.parse!(["--region", "us-east-1", "--size", "large"])
17
+ assert_equal({:region => "us-east-1", :size => "large"}, hash)
18
+ end
19
+
20
+ test "automatically assigns short switches via param name's first char" do
21
+ param = Param.new(:region)
22
+ @parser.build(:params => [param])
23
+ hash = @parser.parse!(["-r", "us-east-1"])
24
+ assert_equal({:region => "us-east-1"}, hash)
25
+ end
26
+
27
+ test "automatically detects short switch collisions" do
28
+ param1 = Param.new(:region)
29
+ param2 = Param.new(:rate)
30
+ @parser.build(:params => [param1, param2])
31
+
32
+ hash = @parser.parse!(["-r", "region", "-a", "rate"])
33
+ assert_equal({:region => "region", :rate => "rate"}, hash)
34
+ end
35
+
36
+ test "use given short switch if specified" do
37
+ param = Param.new(:region, :short => "n")
38
+ @parser.build(:params => [param])
39
+ hash = @parser.parse!(["-n", "us-east-1"])
40
+ assert_equal({:region => "us-east-1"}, hash)
41
+ end
42
+
43
+ test "no switch is given if all possible switches are used" do
44
+ param1 = Param.new(:abc)
45
+ param2 = Param.new(:bcd)
46
+ param3 = Param.new(:aabb) # No switch available
47
+ @parser.build(:params => [param1, param2, param3])
48
+
49
+ assert_nil param3.short
50
+ end
51
+
52
+ test "sets up defaults" do
53
+ param = Param.new(:region, :default => "us-east-1")
54
+ @parser.build(:params => [param])
55
+ hash = @parser.parse!([])
56
+ assert_equal({:region => "us-east-1"}, hash)
57
+ end
58
+
59
+ test "params can be optional" do
60
+ param = Param.new(:region, :optional => true)
61
+ @parser.build(:params => [param])
62
+ hash = @parser.parse!([])
63
+ assert_equal({}, hash)
64
+ end
65
+
66
+ test "pass parsed params in" do
67
+ param1 = Param.new(:region)
68
+ param2 = Param.new(:server)
69
+ @parser.build(:params => [param1, param2])
70
+ hash = @parser.parse!([{:region => 'us-west', :server => '111'}])
71
+
72
+ assert_equal({:region => 'us-west', :server => '111'}, hash)
73
+ end
74
+
75
+ test "#usage" do
76
+ required_param = Param.new(
77
+ :server_id, :desc => "Server instance id"
78
+ )
79
+ param_with_default = Param.new(
80
+ :image, :desc => "Image to use", :default => "ami-test"
81
+ )
82
+ optional_param = Param.new(
83
+ :region, :optional => true, :desc => "A region to use"
84
+ )
85
+ @parser.build(
86
+ :params => [ optional_param, required_param, param_with_default ],
87
+ :name => "test-command",
88
+ :desc => "test description"
89
+ )
90
+
91
+ assert_equal <<-USAGE, @parser.usage
92
+ test description
93
+
94
+ USAGE: test-command [options]
95
+
96
+ REQUIRED:
97
+ -s, --server_id=SERVER_ID Server instance id
98
+
99
+ OPTIONAL:
100
+ -r, --region=REGION A region to use
101
+ -i, --image=IMAGE Image to use
102
+ Defaults to: ami-test
103
+
104
+ -h, --help This message.
105
+ USAGE
106
+ end
107
+
108
+ test "raises error when required params are missing" do
109
+ param = Param.new(:region)
110
+ @parser.build(:params => [param])
111
+
112
+ assert_raises MissingParamsError do
113
+ @parser.parse!([])
114
+ end
115
+ end
116
+
117
+ test "MissingParam error has all the missing attributes" do
118
+ param1 = Param.new(:arg1)
119
+ param2 = Param.new(:arg2)
120
+ @parser.build(:params => [param1, param2])
121
+
122
+ exception = assert_raises MissingParamsError do
123
+ @parser.parse!([])
124
+ end
125
+
126
+ assert_equal [:arg1, :arg2], exception.params
127
+ end
128
+ end
@@ -0,0 +1,107 @@
1
+ require 'test_helper'
2
+
3
+ context "Runner" do
4
+ setup do
5
+ @runner = Excavator::Runner.new
6
+ end
7
+
8
+ test "default namespace" do
9
+ assert_equal :default, @runner.namespace.name
10
+ end
11
+
12
+ test "default namespace has no parent" do
13
+ assert_nil @runner.namespace.parent
14
+ end
15
+
16
+ test "#current_namespace returns namespaced command scope" do
17
+ first_namespace = nil
18
+ second_namespace = nil
19
+ @runner.in_namespace(:first) do
20
+ first_namespace = @runner.current_namespace
21
+ @runner.in_namespace(:second) do
22
+ second_namespace = @runner.current_namespace
23
+ end
24
+ end
25
+
26
+ assert_equal :first, first_namespace.name
27
+ assert_equal :second, second_namespace.name
28
+ end
29
+
30
+ test "#last_command keeps track of the last command" do
31
+ cmd1 = @runner.last_command
32
+ cmd2 = @runner.last_command
33
+ assert_equal cmd1.object_id, cmd2.object_id
34
+ end
35
+
36
+ test "#clear_last_command! does what it's name says" do
37
+ cmd1 = @runner.last_command
38
+ @runner.clear_last_command!
39
+ cmd2 = @runner.last_command
40
+
41
+ refute_equal cmd1.object_id, cmd2.object_id
42
+ end
43
+
44
+ test "add namespace" do
45
+ ns = Excavator::Namespace.new(:servers)
46
+ @runner.current_namespace << ns
47
+ assert_equal ns, @runner.current_namespace.namespace(:servers)
48
+ end
49
+
50
+ test "add a command to the runner" do
51
+ cmd = MiniTest::Mock.new
52
+ cmd.expect(:name, :server)
53
+ @runner.current_namespace << cmd
54
+ assert_equal cmd.object_id, @runner.find_command(:server).object_id
55
+ end
56
+
57
+ test "add command and namespace with the same name" do
58
+ ns = Excavator::Namespace.new(:server)
59
+ cmd = MiniTest::Mock.new
60
+ cmd.expect(:name, :server)
61
+
62
+ @runner.current_namespace << ns
63
+ @runner.current_namespace << cmd
64
+
65
+ assert_equal cmd.object_id, @runner.find_command("server").object_id
66
+ assert ns, @runner.current_namespace.namespace(:server)
67
+ end
68
+ end
69
+
70
+
71
+ context "Loading excavator commands from files (Runner#run)" do
72
+ setup do
73
+ Excavator.reset!
74
+ Excavator.runner.command_paths = [
75
+ basedir.join("test", "fixtures", "commands")
76
+ ]
77
+ end
78
+
79
+ test "execute a command" do
80
+ out, error = capture_io do
81
+ Excavator.runner.run("test")
82
+ end
83
+ assert_match /test command/, out
84
+ end
85
+
86
+ test "2-level namespace command" do
87
+ out, error = capture_io do
88
+ Excavator.runner.run("first:second:third")
89
+ end
90
+ assert_match /third/, out
91
+ end
92
+
93
+ test "execute command with default argument" do
94
+ out, error = capture_io do
95
+ Excavator.runner.run("command_with_arg")
96
+ end
97
+ assert_match /west/, out
98
+ end
99
+
100
+ test "execute command overriding default argument" do
101
+ out, error = capture_io do
102
+ Excavator.runner.run("command_with_arg", "-r", "east")
103
+ end
104
+ assert_match /east/, out
105
+ end
106
+ end
107
+
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ context "TableView" do
4
+ include Excavator
5
+
6
+ test "prints table title" do
7
+ table_view = TableView.new do |t|
8
+ t.title "Table View"
9
+ end
10
+
11
+ out, error = capture_io { puts table_view }
12
+ assert_match /^Table View$/, out
13
+ end
14
+
15
+ test "prints out headers" do
16
+ table_view = TableView.new do |t|
17
+ t.header "header1"
18
+ t.header "header2"
19
+ end
20
+
21
+ out, error = capture_io do
22
+ puts table_view
23
+ end
24
+
25
+ assert_match /^header1 \| header2$/, out
26
+ end
27
+
28
+ test "prints data aligned to headers" do
29
+ table_view = TableView.new do |t|
30
+ t.header :name
31
+ t.header :url
32
+
33
+ t.record "Google", "http://www.google.com"
34
+ end
35
+
36
+ out, error = capture_io { puts table_view }
37
+ lines = out.split("\n")
38
+
39
+ assert_match /^name\s\s \| url\s{18}$/, lines[0]
40
+ assert_match %r{Google \| http://www\.google\.com}, lines[1]
41
+ end
42
+
43
+ test "changing divider" do
44
+ table_view = TableView.new do |t|
45
+ t.header :name
46
+ t.header :url
47
+ t.divider "\t"
48
+ end
49
+
50
+ out, error = capture_io { puts table_view }
51
+ assert_match /^name\turl$/, out
52
+ end
53
+
54
+ test "complains when adding a record with not enough values" do
55
+ assert_raises TableView::InvalidDataForHeaders do
56
+ table_view = TableView.new do |t|
57
+ t.header :name
58
+ t.record 1, 2
59
+ end
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excavator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peter Bui
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &70321668148280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.10.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70321668148280
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby-debug19
27
+ requirement: &70321668147020 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70321668147020
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70321668145740 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70321668145740
47
+ description: ! 'Excavator is a scripting framework for writing multi-command executables
48
+ for the
49
+
50
+ unix environment.
51
+
52
+ '
53
+ email:
54
+ - peter@paydrotalks.com
55
+ executables:
56
+ - excavator
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - .gitignore
61
+ - Gemfile
62
+ - Rakefile
63
+ - bin/excavator
64
+ - excavator.gemspec
65
+ - lib/excavator.rb
66
+ - lib/excavator/command.rb
67
+ - lib/excavator/dsl.rb
68
+ - lib/excavator/environment.rb
69
+ - lib/excavator/namespace.rb
70
+ - lib/excavator/param.rb
71
+ - lib/excavator/param_parser.rb
72
+ - lib/excavator/runner.rb
73
+ - lib/excavator/table_view.rb
74
+ - lib/excavator/version.rb
75
+ - test/fixtures/commands/test.rb
76
+ - test/test_helper.rb
77
+ - test/unit/command_test.rb
78
+ - test/unit/dsl_test.rb
79
+ - test/unit/environment_test.rb
80
+ - test/unit/namespace_test.rb
81
+ - test/unit/param_parser_test.rb
82
+ - test/unit/runner_test.rb
83
+ - test/unit/table_view_test.rb
84
+ homepage: ''
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.10
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: A scripting framework for *nix systems.
108
+ test_files:
109
+ - test/fixtures/commands/test.rb
110
+ - test/test_helper.rb
111
+ - test/unit/command_test.rb
112
+ - test/unit/dsl_test.rb
113
+ - test/unit/environment_test.rb
114
+ - test/unit/namespace_test.rb
115
+ - test/unit/param_parser_test.rb
116
+ - test/unit/runner_test.rb
117
+ - test/unit/table_view_test.rb
118
+ has_rdoc: