cmds 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,78 +1,118 @@
1
1
  require 'spec_helper'
2
2
 
3
- def expect_to_replace input, output
4
- [
5
- "#{ input }",
6
- "blah #{ input }",
7
- "#{ input } blah",
8
- "blah\n#{ input }\nblah",
9
- ].each do |str|
10
- expect( Cmds.replace_shortcuts input ).to eq output
11
- end
12
- end
13
-
14
3
  describe 'Cmds::replace_shortcuts' do
4
+ meth = Cmds.method(:replace_shortcuts)
5
+
15
6
  it "should replace %s with <%= arg %>" do
16
- expect_to_replace "%s", "<%= arg %>"
17
- end
18
-
7
+ expect_map meth, {
8
+ ["%s"] => "<%= arg %>",
9
+ ["blah %s"] => "blah <%= arg %>",
10
+ ["%s blah"] => "<%= arg %> blah",
11
+ ["blah\n%s\nblah"] => "blah\n<%= arg %>\nblah",
12
+ ["%s %s"] => "<%= arg %> <%= arg %>",
13
+ }
14
+ end
15
+
19
16
  it "should replace %%s with %s (escaping)" do
20
- expect_to_replace "%%s", "%s"
21
- end
22
-
23
- it "should replace %%%s with %%s (escaping)" do
24
- expect_to_replace "%%%s", "%%s"
25
- end
26
-
17
+ expect_map meth, {
18
+ ["%%s"] => "%s",
19
+ ["blah %%s"] => "blah %s",
20
+ ["%%s blah"] => "%s blah",
21
+ ["blah\n%%s\nblah"] => "blah\n%s\nblah",
22
+ ["%%s %%s"] => "%s %s",
23
+ ["%%%s"] => "%%s",
24
+ }
25
+ end
26
+
27
27
  it "should replace %{key} with <%= key %>" do
28
- expect_to_replace "%{key}", "<%= key %>"
29
- end
30
-
31
- it "should replace %%{key} with %{key} (escaping)" do
32
- expect_to_replace '%%{key}', '%{key}'
33
- end
34
-
35
- it "should replace %%%{key} with %%{key} (escaping)" do
36
- expect_to_replace '%%%{key}', '%%{key}'
37
- end
38
-
28
+ expect_map meth, {
29
+ ["%{key}"] => "<%= key %>",
30
+ ["blah %{key}"] => "blah <%= key %>",
31
+ ["%{key} blah"] => "<%= key %> blah",
32
+ ["blah\n%{key}\nblah"] => "blah\n<%= key %>\nblah",
33
+ ["%{x} %{y}"] => "<%= x %> <%= y %>",
34
+ }
35
+ end
36
+
39
37
  it "should replace %{key?} with <%= key? %>" do
40
- expect_to_replace "%{key?}", "<%= key? %>"
41
- end
42
-
38
+ expect_map meth, {
39
+ ["%{key?}"] => "<%= key? %>",
40
+ ["blah %{key?}"] => "blah <%= key? %>",
41
+ ["%{key?} blah"] => "<%= key? %> blah",
42
+ ["blah\n%{key?}\nblah"] => "blah\n<%= key? %>\nblah",
43
+ ["%{x?} %{y?}"] => "<%= x? %> <%= y? %>",
44
+ }
45
+ end
46
+
47
+ it "should replace %%{key} with %{key} (escaping)" do
48
+ expect_map meth, {
49
+ ["%%{key}"] => "%{key}",
50
+ ["blah %%{key}"] => "blah %{key}",
51
+ ["%%{key} blah"] => "%{key} blah",
52
+ ["blah\n%%{key}\nblah"] => "blah\n%{key}\nblah",
53
+ ["%%{x} %%{y}"] => "%{x} %{y}",
54
+ ["%%%{key}"] => "%%{key}",
55
+ }
56
+ end
57
+
43
58
  it "should replace %%{key?} with %{key?} (escaping)" do
44
- expect_to_replace '%%{key?}', '%{key?}'
45
- end
46
-
47
- it "should replace %%%{key?} with %%{key?} (escaping)" do
48
- expect_to_replace '%%%{key?}', '%%{key?}'
49
- end
50
-
59
+ expect_map meth, {
60
+ ["%%{key?}"] => "%{key?}",
61
+ ["blah %%{key?}"] => "blah %{key?}",
62
+ ["%%{key?} blah"] => "%{key?} blah",
63
+ ["blah\n%%{key?}\nblah"] => "blah\n%{key?}\nblah",
64
+ ["%%{x?} %%{y?}"] => "%{x?} %{y?}",
65
+ ["%%%{key?}"] => "%%{key?}",
66
+ }
67
+ end
68
+
51
69
  it "should replace %<key>s with <%= key %>" do
52
- expect_to_replace "%<key>s", "<%= key %>"
53
- end
54
-
55
- it "should replace %%<key>s with %<key>s (escaping)" do
56
- expect_to_replace "%%<key>s", "%<key>s"
57
- end
58
-
59
- it "should replace %%%<key>s with %%<key>s (escaping)" do
60
- expect_to_replace "%%%<key>s", "%%<key>s"
61
- end
62
-
70
+ expect_map meth, {
71
+ ["%<key>s"] => "<%= key %>",
72
+ ["blah %<key>s"] => "blah <%= key %>",
73
+ ["%<key>s blah"] => "<%= key %> blah",
74
+ ["blah\n%<key>s\nblah"] => "blah\n<%= key %>\nblah",
75
+ ["%<x>s %<y>s"] => "<%= x %> <%= y %>",
76
+ }
77
+ end
78
+
63
79
  it "should replace %<key?>s with <%= key? %>" do
64
- expect_to_replace '%<key?>s', '<%= key? %>'
65
- end
66
-
80
+ expect_map meth, {
81
+ ["%<key?>s"] => "<%= key? %>",
82
+ ["blah %<key?>s"] => "blah <%= key? %>",
83
+ ["%<key?>s blah"] => "<%= key? %> blah",
84
+ ["blah\n%<key?>s\nblah"] => "blah\n<%= key? %>\nblah",
85
+ ["%<x?>s %<y?>s"] => "<%= x? %> <%= y? %>",
86
+ }
87
+ end
88
+
89
+ it "should replace %%<key>s with %<key>s (escaping)" do
90
+ expect_map meth, {
91
+ ["%%<key>s"] => "%<key>s",
92
+ ["blah %%<key>s"] => "blah %<key>s",
93
+ ["%%<key>s blah"] => "%<key>s blah",
94
+ ["blah\n%%<key>s\nblah"] => "blah\n%<key>s\nblah",
95
+ ["%%<x>s %%<y>s"] => "%<x>s %<y>s",
96
+ ["%%%<key>s"] => "%%<key>s",
97
+ }
98
+ end
99
+
67
100
  it "should replace %%<key?>s with %<key?>s (escaping)" do
68
- expect_to_replace '%%<key?>s', '%<key?>s'
69
- end
70
-
71
- it "should replace %%%<key?>s with %%<key?>s (escaping)" do
72
- expect_to_replace '%%%<key?>s', '%%<key?>s'
73
- end
74
-
101
+ expect_map meth, {
102
+ ["%%<key?>s"] => "%<key?>s",
103
+ ["blah %%<key?>s"] => "blah %<key?>s",
104
+ ["%%<key?>s blah"] => "%<key?>s blah",
105
+ ["blah\n%%<key?>s\nblah"] => "blah\n%<key?>s\nblah",
106
+ ["%%<x?>s %%<y?>s"] => "%<x?>s %<y?>s",
107
+ ["%%%<key?>s"] => "%%<key?>s",
108
+ }
109
+ end
110
+
111
+
75
112
  it "should not touch % that don't fit the shortcut sytax" do
76
- expect( Cmds.replace_shortcuts "50%" ).to eq "50%"
113
+ expect_map meth, {
114
+ ["50%"] => "50%",
115
+ ["50%savings!"] => "50%savings!",
116
+ }
77
117
  end
78
- end
118
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Cmds::stream" do
4
+ let(:times) { 5 }
5
+
6
+ it "writes to $stdout and $stderr by default" do
7
+ out, err = temp_outs do
8
+ Cmds.new('./test/tick.rb <%= times %>').stream times: times
9
+ end
10
+
11
+ expect(out).to eq times.times.map{|_| "#{_}\n"}.join
12
+ expect(err).to eq ''
13
+ end
14
+
15
+ it "handles writes in blocks" do
16
+ out_count = 0
17
+ err_count = 0
18
+ Cmds.new('./test/tick.rb <%= times %>').stream(times: times) do |io|
19
+ io.on_out do |line|
20
+ out_count += 1
21
+ end
22
+
23
+ io.on_err do |line|
24
+ err_count += 1
25
+ end
26
+ end
27
+ expect(out_count).to eq times
28
+ expect(err_count).to eq 0
29
+ end
30
+
31
+ context "input" do
32
+ it "accepts string value input from a block" do
33
+
34
+ out, err = temp_outs do
35
+ Cmds.new("wc -l").stream do
36
+ <<-BLOCK
37
+ one
38
+ two
39
+ three
40
+ BLOCK
41
+ end
42
+ end
43
+
44
+ expect(out).to match /^\s+3\n$/
45
+ expect(err).to eq ''
46
+ end
47
+
48
+ it "accepts stream value input from a block" do
49
+ out, err = temp_outs do
50
+ Cmds.new("wc -l").stream do
51
+ File.open "./test/lines.txt"
52
+ end
53
+ end
54
+
55
+ expect(out).to match /^\s+3\n$/
56
+ end
57
+ end
58
+ end # Cmds::stream
@@ -0,0 +1,3 @@
1
+ require 'cmds/debug'
2
+
3
+ Cmds::Debug.on
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,72 @@
1
+ require 'pathname'
2
+ require 'json'
3
+
1
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
5
  require 'cmds'
3
6
 
4
- def argv result
5
- expect(result.ok?).to be true
6
- JSON.load(result.out)['ARGV']
7
+ ECHO_CMD = "./test/echo_cmd.rb"
8
+
9
+ def echo_cmd_data result
10
+ expect( result.cmd ).to start_with ECHO_CMD
11
+ expect( result ).to be_instance_of Cmds::Result
12
+ expect( result.ok? ).to be true
13
+ data = JSON.load result.out
14
+ expect( data ).to be_instance_of Hash
15
+ data
16
+ end
17
+
18
+ def echo_cmd_key result, key
19
+ data = echo_cmd_data result
20
+ expect( data.key? key ).to be true
21
+ data[key]
22
+ end
23
+
24
+ def echo_cmd_argv result
25
+ echo_cmd_key result, 'ARGV'
26
+ end
27
+
28
+ def echo_cmd_stdin result
29
+ echo_cmd_key result, 'stdin'
7
30
  end
8
31
 
9
- def expect_argv result
10
- expect(argv(result))
32
+ # rspec uses StringIO for stdout and stderr, which spawn doesn't like.
33
+ # this function swaps in tempfiles for $stdout and $stderr and returns
34
+ # there outputs as strings.
35
+ def temp_outs
36
+ # save ref to $stdout and $stderr
37
+ prev_stdout, prev_stderr = $stdout, $stderr
38
+
39
+ # create templfiles for out and err
40
+ out_f, err_f = ['out', 'err'].map {|s|
41
+ Tempfile.new "rspec_std#{ s }"
42
+ }
43
+
44
+ # assign those to $stdout and $stderr
45
+ $stdout, $stderr = out_f, err_f
46
+
47
+ # run the provided block
48
+ yield
49
+
50
+ # get the out and err strings and clean up
51
+ out, err = [out_f, err_f].map {|f|
52
+ f.rewind
53
+ str = f.read
54
+ f.close
55
+ f.unlink
56
+ str
57
+ }
58
+
59
+ # swap back to the old $stdout and $stderr
60
+ $stdout, $stderr = prev_stdout, prev_stderr
61
+
62
+ # return the output strings
63
+ [out, err]
64
+ end # temp_out
65
+
66
+ def expect_map method, map
67
+ map.each do |input, output|
68
+ expect( method.call *input ).to eq output
69
+ end
11
70
  end
12
71
 
13
72
  shared_examples "ok" do
data/test/answers.txt ADDED
@@ -0,0 +1,3 @@
1
+ nrser
2
+ blah
3
+ blue
data/test/bin/dspec ADDED
@@ -0,0 +1 @@
1
+ rspec -r ./spec/debug_helper.rb "$@"
data/test/echo_cmd.rb CHANGED
@@ -10,6 +10,7 @@ argv = ARGV.clone
10
10
  data = {
11
11
  '$0' => $0,
12
12
  'ARGV' => argv,
13
+ 'stdin' => $stdin.read,
13
14
  }
14
15
 
15
16
  puts JSON.pretty_generate(data)
data/test/lines.txt ADDED
@@ -0,0 +1,4 @@
1
+ one
2
+ two
3
+ three
4
+ four!
data/test/questions.rb ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # ask some questions
4
+
5
+ qs = [
6
+ "what is your name?",
7
+ "what is your quest?",
8
+ "what is you favorite color?",
9
+ ]
10
+
11
+ qs.each do |q|
12
+ puts q
13
+ response = gets
14
+ $stderr.puts "response: #{ response.inspect }"
15
+ end
data/test/tick.rb ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ARGV[0].to_i.times do |i|
4
+ puts i
5
+ sleep 0.1
6
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-09 00:00:00.000000000 Z
11
+ date: 2015-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nrser
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pastel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - neil@ztkae.com
@@ -97,22 +111,39 @@ files:
97
111
  - ansible/hosts
98
112
  - cmds.gemspec
99
113
  - lib/cmds.rb
114
+ - lib/cmds/capture.rb
115
+ - lib/cmds/debug.rb
116
+ - lib/cmds/erb_context.rb
117
+ - lib/cmds/io_handler.rb
118
+ - lib/cmds/pipe.rb
119
+ - lib/cmds/result.rb
120
+ - lib/cmds/shell_eruby.rb
121
+ - lib/cmds/stream.rb
122
+ - lib/cmds/sugar.rb
123
+ - lib/cmds/util.rb
100
124
  - lib/cmds/version.rb
101
- - scratch/blah.rb
102
125
  - scratch/erb.rb
103
- - spec/cmds/call_spec.rb
126
+ - scratch/popen3.rb
127
+ - scratch/proxy.rb
128
+ - spec/cmds/assert_spec.rb
129
+ - spec/cmds/capture_spec.rb
104
130
  - spec/cmds/curry_spec.rb
105
131
  - spec/cmds/erb_context_spec.rb
106
132
  - spec/cmds/error_spec.rb
107
133
  - spec/cmds/expand_option_hash_spec.rb
108
134
  - spec/cmds/ok_spec.rb
109
- - spec/cmds/raise_on_error_spec.rb
110
135
  - spec/cmds/replace_shortcuts_spec.rb
111
- - spec/cmds/run_spec.rb
136
+ - spec/cmds/stream_spec.rb
112
137
  - spec/cmds/sub_spec.rb
113
138
  - spec/cmds_spec.rb
139
+ - spec/debug_helper.rb
114
140
  - spec/spec_helper.rb
141
+ - test/answers.txt
142
+ - test/bin/dspec
115
143
  - test/echo_cmd.rb
144
+ - test/lines.txt
145
+ - test/questions.rb
146
+ - test/tick.rb
116
147
  homepage: ''
117
148
  licenses:
118
149
  - MIT
@@ -138,16 +169,22 @@ signing_key:
138
169
  specification_version: 4
139
170
  summary: helps read, write and remember commands.
140
171
  test_files:
141
- - spec/cmds/call_spec.rb
172
+ - spec/cmds/assert_spec.rb
173
+ - spec/cmds/capture_spec.rb
142
174
  - spec/cmds/curry_spec.rb
143
175
  - spec/cmds/erb_context_spec.rb
144
176
  - spec/cmds/error_spec.rb
145
177
  - spec/cmds/expand_option_hash_spec.rb
146
178
  - spec/cmds/ok_spec.rb
147
- - spec/cmds/raise_on_error_spec.rb
148
179
  - spec/cmds/replace_shortcuts_spec.rb
149
- - spec/cmds/run_spec.rb
180
+ - spec/cmds/stream_spec.rb
150
181
  - spec/cmds/sub_spec.rb
151
182
  - spec/cmds_spec.rb
183
+ - spec/debug_helper.rb
152
184
  - spec/spec_helper.rb
185
+ - test/answers.txt
186
+ - test/bin/dspec
153
187
  - test/echo_cmd.rb
188
+ - test/lines.txt
189
+ - test/questions.rb
190
+ - test/tick.rb
data/scratch/blah.rb DELETED
@@ -1,6 +0,0 @@
1
- def f *args
2
- p args
3
- p kwargs
4
- end
5
-
6
- f 1, 2, 3
@@ -1,27 +0,0 @@
1
- require 'json'
2
-
3
- require 'spec_helper'
4
-
5
- describe "Cmds::call" do
6
- it "is reusable" do
7
- args_cmd = Cmds.new "./test/echo_cmd.rb <%= arg %>"
8
- kwds_cmd = Cmds.new "./test/echo_cmd.rb <%= s %>"
9
-
10
- ["arg one", "arg two", "arg three"].each do |arg|
11
- [args_cmd.call([arg]), kwds_cmd.call(s: arg)].each do |result|
12
- expect_argv( result ).to eq [arg]
13
- end
14
- end
15
- end # is reusable
16
-
17
- it "accepts input" do
18
- input = <<-BLOCK
19
- one
20
- two
21
- three
22
- four!
23
- BLOCK
24
-
25
- expect( Cmds.new("wc -l", input: input).call.out ).to match /^\s+4$/
26
- end
27
- end # Cmds::call
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Cmds::raise_on_error" do
4
- it "should raise an error when the command fails" do
5
- expect{ Cmds.raise_on_error "exit 1" }.to raise_error Errno::EPERM
6
- end
7
-
8
- it "should do the same for Cmds!" do
9
- expect{ Cmds! "exit 1" }.to raise_error Errno::EPERM
10
- end
11
- end # Cmds::run
@@ -1,49 +0,0 @@
1
- require 'json'
2
-
3
- require 'spec_helper'
4
-
5
- describe "Cmds::run" do
6
- context "echo_cmd.rb 'hello world!'" do
7
-
8
- shared_examples "executes correctly" do
9
- it_behaves_like "ok"
10
-
11
- it "should have 'hello world!' as ARGV[0]" do
12
- expect( JSON.load(result.out)['ARGV'][0] ).to eq "hello world!"
13
- end
14
- end # executes correctly
15
-
16
- context "positional args" do
17
- let(:result) {
18
- Cmds "./test/echo_cmd.rb <%= arg %>", ["hello world!"]
19
- }
20
-
21
- it_behaves_like "executes correctly"
22
- end
23
-
24
- context "keyword args" do
25
- let(:result) {
26
- Cmds "./test/echo_cmd.rb <%= s %>", s: "hello world!"
27
- }
28
-
29
- it_behaves_like "executes correctly"
30
- end
31
-
32
- end # context echo_cmd.rb 'hello world!'
33
-
34
- # context "feeding kwargs to args cmd" do
35
- # let(:result) {
36
- # Cmds "./test/echo_cmd.rb %s", s: "sup y'all"
37
- # }
38
-
39
- # it "" do
40
- # expect( result.cmd ).to eq nil
41
- # end
42
- # end
43
-
44
- it "should error when second (subs) arg is not a hash or array" do
45
- expect {
46
- Cmds "./test/echo_cmd.rb <%= arg %>", "hello world!"
47
- }.to raise_error TypeError
48
- end
49
- end # Cmds::run