kommando 0.0.11 → 0.0.12

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 087165e6259c0a09ac7fb5eaf432338dd92fe9b6
4
- data.tar.gz: 06ff447d8ee0ebadce51fae705628cef8877d08d
3
+ metadata.gz: 8f31bfc50087990d25272c5bb0bd2d45967dfb70
4
+ data.tar.gz: 8ee8d780d34cc47e500bcb0479d7d9e80934c65b
5
5
  SHA512:
6
- metadata.gz: 378bdaa26cda65cd3cd154501578cddc3a6e34c19e573ac3a3bf92bb270d2cecdec64a91eb1ded589cd641b2520cac7d9f8e6d4521d52bd20f13b2eef2ad08c6
7
- data.tar.gz: 961ed2f546c6edf6b81091a0988128331dbc485cdb25d71d7f7fefd83ba22ecdc4352d9ce73f4dcb810343a45d2683e984b1831fde3a70499d02efff9138ca18
6
+ metadata.gz: a4b6b2dc55fcbfbe5c95e270d7ce48424eaf1e6a46b57c7f002c3cda1fc51e3858e93f9f866fcd6801f134f0247e395551ce9b0b7cfb34dbecc8872bd06eaee7
7
+ data.tar.gz: 526e8c68a2543d69422d22904ccb3561dcddfbeefa4a64a45f0b9566caf045183a1c80d41e4e84bc98f60ff6fac7c00fe21f72f322831407637b9d2be8edeccf
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
+ .byebug_history
@@ -1,5 +1,11 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.12
4
+ Support for matching out and running code on match.
5
+
6
+ - FEAT: Out matching with `out.on /regexp/, block`.
7
+ - EXAMPLES: `stderr_to_out.rb`, `in_shell.rb` and `nano_match.rb`
8
+
3
9
  ## 0.0.11
4
10
  STDIN support.
5
11
 
@@ -7,7 +7,7 @@ fi
7
7
 
8
8
  set -e
9
9
 
10
- rake
10
+ rake; rake; rake
11
11
  bin/e2e
12
12
  bin/stress 2 10
13
13
 
data/bin/stress CHANGED
@@ -35,6 +35,7 @@ puts "starting #{number_of_threads} threads for #{number_of_iterations} iteratio
35
35
  threads = []
36
36
  number_of_threads.times do |i|
37
37
  threads << Thread.new do
38
+ sleep(rand(0.9)) # rampup
38
39
  stress number_of_iterations
39
40
  end
40
41
  end
@@ -0,0 +1,16 @@
1
+ require "./lib/kommando"
2
+
3
+ k = Kommando.new "$ printf 'name: '; read N; printf \"lol, $N\n\"", {
4
+ output: true
5
+ }
6
+
7
+ k.out.on "name: " do
8
+ k.out.on /^lol, [^\n]+\r\n/ do
9
+ puts "Matched 'lol, USERINPUT\\r\\n' on stdout"
10
+ end
11
+
12
+ k.in << "hello\n"
13
+
14
+ end
15
+
16
+ k.run
@@ -22,3 +22,5 @@ Thread.new do
22
22
  end
23
23
 
24
24
  k.run
25
+
26
+ `reset` #TODO: how to reset in kommando?
@@ -0,0 +1,24 @@
1
+ require "./lib/kommando"
2
+ require "tempfile"
3
+
4
+ scratch = Tempfile.new
5
+
6
+ k = Kommando.new "nano scratch.path", {
7
+ output: true
8
+ }
9
+ k.out.on "GNU nano" do
10
+ k.in << "hello\r"
11
+ k.in << "\x1B\x1Bx"
12
+
13
+ k.out.on "Save modified buffer" do
14
+ k.in << "y"
15
+
16
+ k.out.on "File Name to Write" do
17
+ k.in << "\r"
18
+ end
19
+ end
20
+ end
21
+
22
+ k.run
23
+
24
+ `reset` #TODO: reset in kommando
@@ -0,0 +1,7 @@
1
+ require "./lib/kommando"
2
+
3
+ k = Kommando.new "$ (>&2 echo 'err'); echo 'out'"
4
+ k.run
5
+
6
+ raise "err" unless k.out == "err\r\nout"
7
+ puts k.out
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
25
  spec.add_development_dependency "rspec-autotest", "~> 1.0"
26
26
  spec.add_development_dependency "ZenTest", "~> 4.11"
27
+ spec.add_development_dependency "byebug"
27
28
  end
@@ -36,6 +36,9 @@ class Kommando
36
36
  @pid = nil
37
37
 
38
38
  @shell = false
39
+
40
+ @matchers = {}
41
+ @matcher_buffer = ""
39
42
  end
40
43
 
41
44
  def run_async
@@ -130,16 +133,30 @@ class Kommando
130
133
  @stdout.append c if c
131
134
  print c if @output_stdout
132
135
  stdout_file.write c if @output_file
136
+
137
+ if c
138
+ @matcher_buffer << c
139
+
140
+ matchers_copy = @matchers.clone # blocks can insert to @matchers while iteration is undergoing
141
+ matchers_copy.each_pair do |matcher,block|
142
+ if @matcher_buffer.match matcher
143
+ block.call
144
+ @matchers.delete matcher # do not match again TODO: is this safe?
145
+ end
146
+ end
147
+ end
133
148
  end
134
149
  end
135
150
 
136
151
  thread_stdin = Thread.new do
152
+ sleep 0.1 # allow program to start, do not write "in terminal"
137
153
  while true do
138
154
  c = @stdin.getc
139
155
  unless c
140
156
  sleep 0.01
141
157
  next
142
158
  end
159
+
143
160
  stdin.write c
144
161
  end
145
162
  end
@@ -188,11 +205,19 @@ class Kommando
188
205
  end
189
206
 
190
207
  def out
191
- if @shell
208
+ string = if @shell
192
209
  @stdout.to_s.strip
193
210
  else
194
211
  @stdout.to_s
195
212
  end
213
+
214
+ kommando = self
215
+ string.define_singleton_method(:on) do |matcher, &block|
216
+ matchers = kommando.instance_variable_get(:@matchers)
217
+ matchers[matcher] = block
218
+ end
219
+
220
+ string
196
221
  end
197
222
 
198
223
  def code
@@ -2,6 +2,8 @@ class Kommando::Buffer
2
2
 
3
3
  def initialize
4
4
  @buffer = []
5
+
6
+ @matchers = {}
5
7
  end
6
8
 
7
9
  def append(string)
@@ -19,4 +21,8 @@ class Kommando::Buffer
19
21
  def getc
20
22
  @buffer.shift
21
23
  end
24
+
25
+ def on(matcher, &block)
26
+ @matchers[matcher] = block
27
+ end
22
28
  end
@@ -1,3 +1,3 @@
1
1
  class Kommando
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kommando
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matti Paksula
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-11 00:00:00.000000000 Z
11
+ date: 2016-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '4.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: byebug
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: Great for integration testing.
84
98
  email:
85
99
  - matti.paksula@iki.fi
@@ -108,11 +122,14 @@ files:
108
122
  - examples/env.rb
109
123
  - examples/exit.rb
110
124
  - examples/in.rb
125
+ - examples/in_shell.rb
111
126
  - examples/kill.rb
112
127
  - examples/live_output.rb
113
128
  - examples/nano.rb
129
+ - examples/nano_match.rb
114
130
  - examples/ping.rb
115
131
  - examples/shell.rb
132
+ - examples/stderr_to_out.rb
116
133
  - examples/stdout_to_file.rb
117
134
  - examples/timeout.rb
118
135
  - examples/uptime.rb