kommando 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28bb48c41521e414b25f99f504388d7f487540e7
4
- data.tar.gz: c5a621a4d4e123a1e384662d8beac09eafd3c289
3
+ metadata.gz: 5d04e223cee14a391a08470955f57eaa63009148
4
+ data.tar.gz: 58629080acfb3bc56c0f071283a652ea90ebacd5
5
5
  SHA512:
6
- metadata.gz: dac7252e7d4af6dc8ceeeff7c53a2b087e193ca2f9a87c2a5b44d9287bf3a65a883495845e87122ce9860e574e72a90722017264c5885af520087457a124ff85
7
- data.tar.gz: 36802a54834ad364b2addabf3ea8679c801c82b3f68090c129cd5b9a0b32ddad8d198b35b229143619df6072e91995b1625601a2cee997886195afeab5d39d02
6
+ metadata.gz: bebe0fa396b5e2be603e7454550e11a1098135df56c0e9529bd7bc6a0a2213789146147d36043eff7877a0ff8080fb4d0ec2c2b7dacff2202862d57b45f4d348
7
+ data.tar.gz: 5b6f6f11dae84848a166d9b9ee76150659d1d29feda6f91263b4e3c394e9e92d174a395c94f3ccd8cb3ef66479b8dde9c4a41c5797904b98ee58be66781443f5
@@ -1 +1 @@
1
- 2.3.0
1
+ 2.3.3
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.22
4
+ - FEAT: Global whens with `Kommando.when :timeout block` and clearing with `Kommando.when = nil`
5
+ - FEAT: Context available with `k.when :timeout do |kontextual_k|` and `Kommando.when :timeout |kontextual_k|`
6
+ - FEAT: Global timeout with `Kommando.timeout = 1` and `puts Kommando.timeout`
7
+ - FEAT: Callback `k.when :success` if command was run and exited with zero status
8
+ - FEAT: Callback `k.when :failed` if command did not run or exited with non-zero status
9
+ - FIX: `Kommando.puts` shorthand did not return an instance
10
+
3
11
  ## 0.0.21
4
12
  - FEAT: `k.in.write` as an alias for `k.in <<`
5
13
  - FEAT: `k.in.writeln` as an shorthand for `k.in.write "lol\r"`
@@ -11,3 +11,47 @@ k.wait
11
11
  raise "err" unless k.out == "hello"
12
12
 
13
13
  Kommando.puts "$ echo ok"
14
+
15
+ k = Kommando.puts "$ sleep 10", {
16
+ timeout: 0.00001
17
+ }
18
+
19
+ k.when :timeout do
20
+ puts "did timeout as expected."
21
+ end
22
+
23
+ Kommando.timeout = 0.0001
24
+ k = Kommando.new "$ sleep 10"
25
+ k.when :timeout do
26
+ puts "did timeout as expected with global timeout #{Kommando.timeout}"
27
+ end
28
+ k.run
29
+
30
+
31
+ Kommando.timeout = 0.0002
32
+ Kommando.when :timeout do
33
+ puts "Global when timeout 1"
34
+ end
35
+ Kommando.when :timeout do
36
+ puts "Global when timeout 2"
37
+ end
38
+
39
+ Kommando.run "$ sleep 10"
40
+
41
+ Kommando.timeout = nil
42
+ Kommando.when = nil
43
+
44
+ Kommando.when :success do
45
+ puts "succes callback"
46
+ end
47
+
48
+ Kommando.when :failed do
49
+ puts "failed callback"
50
+ end
51
+
52
+ Kommando.when :exit do
53
+ puts "exit callback"
54
+ end
55
+
56
+ Kommando.run "$ exit 0"
57
+ Kommando.run "$ exit 1"
@@ -1,22 +1,29 @@
1
1
  require "./lib/kommando"
2
2
 
3
- last_k = nil
3
+ Kommando.timeout = 0.1
4
+ Kommando.when :timeout do |k|
5
+ print "t"
6
+ puts k.inspect
7
+ exit 1
8
+ end
9
+
4
10
  100.times do
5
- last_k = Kommando.run "uptime", {
6
- timeout: 0.1
7
- }
8
- last_k.when :timeout do
9
- print "t"
10
- puts last_k.out
11
- end
11
+ Kommando.run "uptime"
12
12
  print "."
13
13
  end
14
14
 
15
- puts ""
15
+ 3.times do
16
+ unless Thread.list.count == 1
17
+ print "w(#{Thread.list.count})"
18
+ sleep 0.01
19
+ else
20
+ break
21
+ end
22
+ end
23
+
16
24
  unless Thread.list.count == 1
17
25
  puts Thread.list.map(&:inspect).join("\n")
18
- puts last_k.out
19
- raise "Thread leak"
20
-
26
+ raise "Thread leak: #{Thread.list.count}"
21
27
  end
28
+
22
29
  puts "ok"
@@ -92,3 +92,28 @@ end
92
92
  delta = (Time.now-started).round(1)
93
93
  raise "sleep does not work (delta is: #{delta})" unless delta == 0.5
94
94
  puts "end"
95
+
96
+
97
+ k = Kommando.new "$ sleep 2", {
98
+ timeout: 0.01
99
+ }
100
+ k.when :timeout do |kk|
101
+ puts kk.inspect
102
+ end
103
+
104
+ k.run
105
+
106
+ puts "-- Global --"
107
+
108
+ Kommando.timeout = 0.01
109
+ Kommando.when :timeout do |ks|
110
+ puts ks.inspect
111
+ end
112
+ Kommando.run "$ sleep 2"
113
+
114
+ puts "--"
115
+ Kommando.when :success do |ks|
116
+ puts "out: #{ks.out}"
117
+ end
118
+
119
+ Kommando.run "uptime"
@@ -8,6 +8,9 @@ require_relative "kommando/when"
8
8
 
9
9
  class Kommando
10
10
  class << self
11
+ @@timeout = nil
12
+ @@whens = nil
13
+
11
14
  def run(cmd, opts={})
12
15
  k = Kommando.new cmd, opts
13
16
  k.run
@@ -24,6 +27,25 @@ class Kommando
24
27
  k = Kommando.new cmd, opts
25
28
  k.run
26
29
  Kernel.puts k.out
30
+ k
31
+ end
32
+
33
+ def timeout
34
+ @@timeout
35
+ end
36
+
37
+ def timeout=(value)
38
+ @@timeout=value
39
+ end
40
+
41
+
42
+ def when(event_name, &block)
43
+ @@whens ||= Kommando::When.new
44
+ @@whens.register event_name, block
45
+ end
46
+
47
+ def when=(w)
48
+ @@whens = w
27
49
  end
28
50
  end
29
51
 
@@ -43,7 +65,10 @@ class Kommando
43
65
  opts[:timeout]
44
66
  elsif opts[:timeout].class == Fixnum
45
67
  opts[:timeout].to_f
68
+ else
69
+ @timeout = @@timeout
46
70
  end
71
+
47
72
  @timeout_happened = false
48
73
  @kill_happened = false
49
74
  @rescue_happened = false
@@ -75,7 +100,15 @@ class Kommando
75
100
  @matcher_buffer = ""
76
101
 
77
102
  @whens = {}
78
- @when = When.new
103
+ @when = When.new(self)
104
+
105
+ if @@whens
106
+ @@whens.instance_variable_get("@whens").each_pair do |event_name, blocks|
107
+ blocks.each do |block|
108
+ @when.register event_name, block
109
+ end
110
+ end
111
+ end
79
112
  end
80
113
 
81
114
  def run_async
@@ -254,6 +287,11 @@ class Kommando
254
287
 
255
288
  @when.fire :timeout if @timeout_happened
256
289
  @when.fire :exit
290
+ if @code == 0
291
+ @when.fire :success
292
+ else
293
+ @when.fire :failed
294
+ end
257
295
 
258
296
  debug "run returning true"
259
297
  true
@@ -307,6 +345,7 @@ class Kommando
307
345
  def raise_after_callbacks(exception)
308
346
  @when.fire :error
309
347
  @when.fire :exit
348
+ @when.fire :failed
310
349
  raise exception
311
350
  end
312
351
 
@@ -1,3 +1,3 @@
1
1
  class Kommando
2
- VERSION = "0.0.21"
2
+ VERSION = "0.0.22"
3
3
  end
@@ -1,7 +1,8 @@
1
1
  class Kommando::When
2
- VALID_EVENTS = :start, :retry, :timeout, :error, :exit
2
+ VALID_EVENTS = :start, :retry, :timeout, :error, :exit, :success, :failed
3
3
 
4
- def initialize
4
+ def initialize(parent=nil)
5
+ @parent = parent
5
6
  @whens = {}
6
7
  @fired = []
7
8
  end
@@ -36,7 +37,11 @@ class Kommando::When
36
37
  debug "firing cbs for #{event_name_as_sym}"
37
38
  blocks.each do |block|
38
39
  debug "firing cb for #{event_name_as_sym}"
39
- block.call
40
+ if @parent
41
+ block.call(@parent)
42
+ else
43
+ block.call
44
+ end
40
45
  debug "fired cb for #{event_name_as_sym}"
41
46
  end
42
47
  else
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.21
4
+ version: 0.0.22
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-10-16 00:00:00.000000000 Z
11
+ date: 2016-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -197,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  requirements: []
199
199
  rubyforge_project:
200
- rubygems_version: 2.5.1
200
+ rubygems_version: 2.5.2
201
201
  signing_key:
202
202
  specification_version: 4
203
203
  summary: Command runner with expect-like features