cmds 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,159 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Cmds::sub" do
4
+ it "should work with a keyword substitutions" do
5
+ expect(
6
+ Cmds.sub "psql <%= opts %> <%= database %> < <%= filepath %>",
7
+ [],
8
+ database: "blah",
9
+ filepath: "/where ever/it/is.psql",
10
+ opts: {
11
+ username: "bingo bob",
12
+ host: "localhost",
13
+ port: 12345,
14
+ }
15
+ ).to eq 'psql --host=localhost --port=12345 --username=bingo\ bob blah < /where\ ever/it/is.psql'
16
+ end
17
+
18
+ it "should work with positional substitutions" do
19
+ expect(
20
+ Cmds.sub "psql <%= arg %> <%= arg %> < <%= arg %>", [
21
+ {
22
+ username: "bingo bob",
23
+ host: "localhost",
24
+ port: 12345,
25
+ },
26
+ "blah",
27
+ "/where ever/it/is.psql",
28
+ ]
29
+ ).to eq 'psql --host=localhost --port=12345 --username=bingo\ bob blah < /where\ ever/it/is.psql'
30
+ end
31
+
32
+ it "should work with no arguments" do
33
+ expect(
34
+ Cmds.sub "blah <% if true %>blow<% end %>"
35
+ ).to eq "blah blow"
36
+ end
37
+
38
+ it "should work with positional and keyword substitutions" do
39
+ expect(
40
+ Cmds.sub "blah <%= arg %> <%= y %>", ["ex"], y: "why"
41
+ ).to eq "blah ex why"
42
+ end
43
+
44
+ it "should work with direct reference to args" do
45
+ expect(
46
+ Cmds.sub "psql <%= @args[2] %> <%= @args[0] %> < <%= @args[1] %>", [
47
+ "blah",
48
+ "/where ever/it/is.psql",
49
+ {
50
+ username: "bingo bob",
51
+ host: "localhost",
52
+ port: 12345,
53
+ },
54
+ ]
55
+ ).to eq 'psql --host=localhost --port=12345 --username=bingo\ bob blah < /where\ ever/it/is.psql'
56
+ end
57
+
58
+ context "if statement" do
59
+ let(:tpl) {
60
+ <<-BLOCK
61
+ defaults
62
+ <% if current_host? %>
63
+ -currentHost <%= current_host %>
64
+ <% end %>
65
+ export <%= domain %> <%= filepath %>
66
+ BLOCK
67
+ }
68
+
69
+ it "should work when value is present" do
70
+ expect(
71
+ Cmds.sub tpl, [], current_host: 'xyz',
72
+ domain: 'com.nrser.blah',
73
+ filepath: '/tmp/export.plist'
74
+ ).to eq "defaults -currentHost xyz export com.nrser.blah /tmp/export.plist"
75
+ end
76
+
77
+ it "should work when value is missing" do
78
+ expect(
79
+ Cmds.sub tpl, [], domain: 'com.nrser.blah',
80
+ filepath: '/tmp/export.plist'
81
+ ).to eq "defaults export com.nrser.blah /tmp/export.plist"
82
+ end
83
+ end
84
+
85
+ context "each statement" do
86
+ let(:tpl) {
87
+ <<-BLOCK
88
+ defaults write <%= domain %> <%= key %> -dict
89
+ <% values.each do |key, value| %>
90
+ <%= key %> <%= value %>
91
+ <% end %>
92
+ BLOCK
93
+ }
94
+
95
+ it "should loop correctly and escape values" do
96
+ expect(
97
+ Cmds.sub tpl, [], domain: "com.nrser.blah",
98
+ key: 'k',
99
+ values: {x: '<ex>', y: 'why'}
100
+ ).to eq "defaults write com.nrser.blah k -dict x \\<ex\\> y why"
101
+ end
102
+ end
103
+
104
+ context "optional subs" do
105
+ let(:tpl) {
106
+ <<-BLOCK
107
+ blah <%= x? %> <%= y? %> <%= z? %>
108
+ BLOCK
109
+ }
110
+
111
+ it "should omit the missing subs" do
112
+ expect(Cmds.sub tpl, [], x: "ex", z: "zee").to eq "blah ex zee"
113
+ end
114
+ end
115
+
116
+ context "errors" do
117
+ it "should raise TypeError if subs in not an array or a hash" do
118
+ expect{Cmds.sub "a <%= b %> <%= c %>", "dee!"}.to raise_error TypeError
119
+ end
120
+
121
+ it "should error when a kwarg is missing" do
122
+ expect {
123
+ Cmds.sub "a <%= b %> <%= c %>", [], b: 'bee!'
124
+ }.to raise_error KeyError
125
+ end
126
+
127
+ it "should error when an arg is missing" do
128
+ expect {
129
+ Cmds.sub "a <%= arg %> <%= arg %>", ['bee!']
130
+ }.to raise_error IndexError
131
+ end
132
+
133
+ it "should error when more than two args are passed" do
134
+ expect {
135
+ Cmds.sub "blah", 1, 2, 3
136
+ }.to raise_error ArgumentError
137
+ end
138
+ end # errors
139
+
140
+ context "shortcuts" do
141
+ it "should replace %s" do
142
+ expect(
143
+ Cmds.sub "./test/echo_cmd.rb %s", ["hello world!"]
144
+ ).to eq './test/echo_cmd.rb hello\ world\!'
145
+ end
146
+
147
+ it "should replace %{key}" do
148
+ expect(
149
+ Cmds.sub "./test/echo_cmd.rb %{key}", [], key: "hello world!"
150
+ ).to eq './test/echo_cmd.rb hello\ world\!'
151
+ end
152
+
153
+ it "should replace %<key>s" do
154
+ expect(
155
+ Cmds.sub "./test/echo_cmd.rb %<key>s", [], key: "hello world!"
156
+ ).to eq './test/echo_cmd.rb hello\ world\!'
157
+ end
158
+ end # shortcuts
159
+ end # ::sub
data/spec/cmds_spec.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cmds do
4
+ it 'has a version number' do
5
+ expect(Cmds::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'cmds'
3
+
4
+ def argv result
5
+ expect(result.ok?).to be true
6
+ JSON.load(result.out)['ARGV']
7
+ end
8
+
9
+ def expect_argv result
10
+ expect(argv(result))
11
+ end
12
+
13
+ shared_examples "ok" do
14
+ it "should be ok" do
15
+ expect( result.ok? ).to be true
16
+ end
17
+
18
+ it "should have empty err" do
19
+ expect( result.err ).to eq ""
20
+ end
21
+ end # ok
data/test/echo_cmd.rb ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json'
4
+
5
+ # this script echos the command that invoked it back as a
6
+ # JSON structure for testing
7
+
8
+ argv = ARGV.clone
9
+
10
+ data = {
11
+ '$0' => $0,
12
+ 'ARGV' => argv,
13
+ }
14
+
15
+ puts JSON.pretty_generate(data)
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmds
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nrser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nrser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.11
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.11
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - neil@ztkae.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - ansible/dev.yml
97
+ - ansible/hosts
98
+ - cmds.gemspec
99
+ - lib/cmds.rb
100
+ - lib/cmds/version.rb
101
+ - scratch/blah.rb
102
+ - scratch/erb.rb
103
+ - spec/cmds/call_spec.rb
104
+ - spec/cmds/curry_spec.rb
105
+ - spec/cmds/erb_context_spec.rb
106
+ - spec/cmds/expand_option_hash_spec.rb
107
+ - spec/cmds/replace_shortcuts_spec.rb
108
+ - spec/cmds/run_spec.rb
109
+ - spec/cmds/sub_spec.rb
110
+ - spec/cmds_spec.rb
111
+ - spec/spec_helper.rb
112
+ - test/echo_cmd.rb
113
+ homepage: ''
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.2.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: helps read, write and remember commands.
137
+ test_files:
138
+ - spec/cmds/call_spec.rb
139
+ - spec/cmds/curry_spec.rb
140
+ - spec/cmds/erb_context_spec.rb
141
+ - spec/cmds/expand_option_hash_spec.rb
142
+ - spec/cmds/replace_shortcuts_spec.rb
143
+ - spec/cmds/run_spec.rb
144
+ - spec/cmds/sub_spec.rb
145
+ - spec/cmds_spec.rb
146
+ - spec/spec_helper.rb
147
+ - test/echo_cmd.rb