test-cmd.rb 0.9.0 → 0.9.3

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
  SHA256:
3
- metadata.gz: 6cce30514a659d318e574a7a8c13de222c8632a4ab944847821195fe512e133a
4
- data.tar.gz: d5a12184b17454efa414a380006c6f240e6747e13220d3b28940c6cf79e7df33
3
+ metadata.gz: 7e5bf49f40fdbefaea05fb598e7e9fa5a12faeb72e90e8794b0a6b9366dfa8a0
4
+ data.tar.gz: 1b19f3a1df91a354717cc6c7862762f5ddadf72392b3bd71018ee41673accd0d
5
5
  SHA512:
6
- metadata.gz: e5d88ccede58e0d3f23d226481291aa7c97d64ca1a18ab3d620dee57fbf6590f2e0da1cb62962e8ac328a6a91d80b87d7a525136893e55f5ab9c583e6eda81dc
7
- data.tar.gz: 6190123fe4136ca820e3c1241a32927ee72be094024abb49d0aaa15be9c6955bccdcc396029b2e035629712bd31a8bc5e221dd9db311422e459ea3a2a253da0e
6
+ metadata.gz: 28a8899a6f9f9bf941a171ced9ae694b57762eb4c24608f9243ec61dc2a17b367566baeaae9de60280af70f75ef9b0a75e08359895f6b69b37f3eda844ad30e1
7
+ data.tar.gz: 5d87dcebab3b423c19b4d00b90bf447aad83033b71e74e47aff5fd2c904a7289eb5890c42175df0ee35a4c29f6eb2746e9db8d7220b397b13591dc8f67d6e70b
data/README.md CHANGED
@@ -1,10 +1,24 @@
1
1
  ## About
2
2
 
3
- test-cmd.rb provides an object-oriented interface for spawning
3
+ test-cmd.rb provides an object-oriented interface for spawning
4
4
  a process.
5
5
 
6
6
  ## Examples
7
7
 
8
+ ### Callbacks
9
+
10
+ The success and failure callbacks provide hooks for when
11
+ a command exits successfully or unsuccessfully. The callback
12
+ is passed an instance of
13
+ [Test::Cmd](https://0x1eef.github.io/x/test-cmd.rb/Test/Cmd.html):
14
+
15
+ ``` ruby
16
+ require "test/cmd"
17
+ cmd("ruby", "-e", "exit 0")
18
+ .success { print "The command [#{_1.pid}] was successful", "\n" }
19
+ .failure { print "The command [#{_1.pid}] was unsuccessful", "\n" }
20
+ ```
21
+
8
22
  ### Test::Unit
9
23
 
10
24
  The following example demonstrates how tests might be written with
@@ -35,22 +49,6 @@ class CmdTest < Test::Unit::TestCase
35
49
  end
36
50
  ```
37
51
 
38
- ### Builder
39
-
40
- test-cmd.rb provides an API that is similar to Rust's
41
- [Command API](https://doc.rust-lang.org/std/process/struct.Command.html).
42
- <br>
43
- The
44
- [API reference](https://0x1eef.github.io/x/test-cmd.rb)
45
- covers it in more-depth:
46
-
47
- ``` ruby
48
- require "test/cmd"
49
- puts cmd("du")
50
- .argv("-s", "-h")
51
- .stdout
52
- ```
53
-
54
52
  ### IO#sync
55
53
 
56
54
  Sometimes it can be neccessary to bypass Ruby's internal buffer and flush
@@ -61,12 +59,12 @@ results. Consider the following example, where the output will be
61
59
  ``` ruby
62
60
  ##
63
61
  # test.rb
64
- pid = fork do
62
+ fork do
65
63
  sleep(1)
66
64
  puts "bar"
67
65
  end
68
66
  puts "foo"
69
- Process.wait(pid)
67
+ Process.wait
70
68
 
71
69
  ##
72
70
  # cmd.rb
@@ -79,12 +77,12 @@ And with output flushed to the operating system immediately:
79
77
  ##
80
78
  # test.rb
81
79
  $stdout.sync = true
82
- pid = fork do
80
+ fork do
83
81
  sleep(1)
84
82
  puts "bar"
85
83
  end
86
84
  puts "foo"
87
- Process.wait(pid)
85
+ Process.wait
88
86
 
89
87
  ##
90
88
  # cmd.rb
@@ -93,7 +91,7 @@ p cmd("ruby", "test.rb").stdout # => "foo\nbar\n"
93
91
 
94
92
  ## Documentation
95
93
 
96
- A complete API reference is available at
94
+ A complete API reference is available at
97
95
  [0x1eef.github.io/x/test-cmd.rb](https://0x1eef.github.io/x/test-cmd.rb).
98
96
 
99
97
  ## Install
data/lib/test/cmd.rb CHANGED
@@ -31,7 +31,7 @@ class Test::Cmd
31
31
  # Spawns a command
32
32
  # @return [Test::Cmd]
33
33
  def spawn
34
- return if @spawned
34
+ return self if @spawned
35
35
 
36
36
  tap do
37
37
  @spawned = true
@@ -70,6 +70,13 @@ class Test::Cmd
70
70
  @status
71
71
  end
72
72
 
73
+ ##
74
+ # @return [Integer]
75
+ # Returns the process ID of a spawned command
76
+ def pid
77
+ status.pid
78
+ end
79
+
73
80
  ##
74
81
  # @return [Integer]
75
82
  # Returns the exit status of a process
@@ -77,6 +84,45 @@ class Test::Cmd
77
84
  status.exitstatus
78
85
  end
79
86
 
87
+ ##
88
+ # @return [Boolean]
89
+ # Returns true when a command exited successfully
90
+ def success?
91
+ status.success?
92
+ end
93
+
94
+ ##
95
+ # Yields an instance of {Test::Cmd Test::Cmd}.
96
+ #
97
+ # @example
98
+ # cmd("ruby", "-e", "exit 0")
99
+ # .success { print "Command [#{_1.pid}] exited successfully", "\n" }
100
+ # .failure { }
101
+ #
102
+ # @return [Test::Cmd]
103
+ def success
104
+ tap do
105
+ spawn
106
+ status.success? ? yield(self) : nil
107
+ end
108
+ end
109
+
110
+ ##
111
+ # Yields an instance of {Test::Cmd Test::Cmd}.
112
+ #
113
+ # @example
114
+ # cmd("ruby", "-e", "exit 1")
115
+ # .success { }
116
+ # .failure { print "Command [#{_1.pid}] exited unsuccessfully", "\n" }
117
+ #
118
+ # @return [Test::Cmd]
119
+ def failure
120
+ tap do
121
+ spawn
122
+ status.success? ? nil : yield(self)
123
+ end
124
+ end
125
+
80
126
  private
81
127
 
82
128
  attr_reader :out_io, :err_io
@@ -19,17 +19,36 @@ class CmdTest < Test::Unit::TestCase
19
19
 
20
20
  def test_ruby_success_status
21
21
  assert_equal true, cmd("ruby", "-e", "exit 0").status.success?
22
+ assert_equal true, cmd("ruby", "-e", "exit 0").success?
23
+ end
24
+
25
+ def test_ruby_success_callback
26
+ call_ok, call_fail = [false, false]
27
+ cmd("ruby", "-e", "exit 0")
28
+ .success { call_ok = true }
29
+ .failure { call_fail = true }
30
+ assert_equal true, call_ok
31
+ assert_equal false, call_fail
32
+ end
33
+
34
+ def test_ruby_failure_callback
35
+ call_ok, call_fail = [false, false]
36
+ cmd("ruby", "-e", "exit 1")
37
+ .success { call_ok = true }
38
+ .failure { call_fail = true }
39
+ assert_equal true, call_fail
40
+ assert_equal false, call_ok
22
41
  end
23
42
 
24
43
  def test_stdout_with_fork
25
44
  code = <<-CODE.each_line.map { _1.chomp.strip }.join(";")
26
45
  $stdout.sync = true
27
- pid = fork do
46
+ fork do
28
47
  sleep(1)
29
48
  puts "bar"
30
49
  end
31
50
  puts "foo"
32
- Process.wait(pid)
51
+ Process.wait
33
52
  CODE
34
53
  assert_equal "foo\nbar\n", cmd("ruby", "-e", code).stdout
35
54
  end
data/test-cmd.rb.gemspec CHANGED
@@ -5,12 +5,12 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["0x1eef"]
6
6
  gem.email = ["0x1eef@protonmail.com"]
7
7
  gem.homepage = "https://github.com/0x1eef/test-cmd.rb#readme"
8
- gem.version = "0.9.0"
8
+ gem.version = "0.9.3"
9
9
  gem.required_ruby_version = ">= 3.0"
10
10
  gem.licenses = ["0BSD"]
11
11
  gem.files = `git ls-files`.split($/)
12
12
  gem.require_paths = ["lib"]
13
- gem.summary = "An object-oriented interface for spawning a process."
13
+ gem.summary = "An object-oriented interface for spawning a process"
14
14
  gem.metadata = { "documentation_uri" => "https://0x1eef.github.io/x/test-cmd.rb/" }
15
15
  gem.description = gem.summary
16
16
  gem.add_development_dependency "test-unit", "~> 3.5.7"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test-cmd.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-12 00:00:00.000000000 Z
11
+ date: 2024-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '13.1'
83
- description: An object-oriented interface for spawning a process.
83
+ description: An object-oriented interface for spawning a process
84
84
  email:
85
85
  - 0x1eef@protonmail.com
86
86
  executables: []
@@ -124,5 +124,5 @@ requirements: []
124
124
  rubygems_version: 3.5.9
125
125
  signing_key:
126
126
  specification_version: 4
127
- summary: An object-oriented interface for spawning a process.
127
+ summary: An object-oriented interface for spawning a process
128
128
  test_files: []