test-cmd.rb 0.9.0 → 0.9.2

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
  SHA256:
3
- metadata.gz: 6cce30514a659d318e574a7a8c13de222c8632a4ab944847821195fe512e133a
4
- data.tar.gz: d5a12184b17454efa414a380006c6f240e6747e13220d3b28940c6cf79e7df33
3
+ metadata.gz: 3d07ddf04e4a028f4c745b1583b0fb54a8d0a66c6545be5ae06a36fe2bed5d44
4
+ data.tar.gz: fc37608b53d4648fcc580c8845af31a8d423087816fdc862662e6e9dda99d29f
5
5
  SHA512:
6
- metadata.gz: e5d88ccede58e0d3f23d226481291aa7c97d64ca1a18ab3d620dee57fbf6590f2e0da1cb62962e8ac328a6a91d80b87d7a525136893e55f5ab9c583e6eda81dc
7
- data.tar.gz: 6190123fe4136ca820e3c1241a32927ee72be094024abb49d0aaa15be9c6955bccdcc396029b2e035629712bd31a8bc5e221dd9db311422e459ea3a2a253da0e
6
+ metadata.gz: b170b84a449b49991d94f902b4d60aa9030f257e3123335e2eb42718b8116ff27200d396738c83cfe38c03eeb762bf07f47887e0508f0a5f88cb4185b4ea7e82
7
+ data.tar.gz: 07e946aa6b365bfc17e6337eb3c0c7fd773783a8e3b7cdb80dbdf88e91cdfb1550a086d727ea5b14ab350d259c2ad46038ea7683ada3146066dd34cd6d792dc9
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
@@ -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,38 @@ class Test::Cmd
77
84
  status.exitstatus
78
85
  end
79
86
 
87
+ ##
88
+ # Yields an instance of {Test::Cmd Test::Cmd}.
89
+ #
90
+ # @example
91
+ # cmd("ruby", "-e", "exit 0")
92
+ # .success { print "Command exited successfully: #{_1.exit_status}", "\n" }
93
+ # .failure { }
94
+ #
95
+ # @return [Test::Cmd]
96
+ def success
97
+ tap do
98
+ spawn
99
+ status.success? ? yield(self) : nil
100
+ end
101
+ end
102
+
103
+ ##
104
+ # Yields an instance of {Test::Cmd Test::Cmd}.
105
+ #
106
+ # @example
107
+ # cmd("ruby", "-e", "exit 1")
108
+ # .success { }
109
+ # .failure { print "Command exited unsuccessfully: #{_1.exit_status}", "\n" }
110
+ #
111
+ # @return [Test::Cmd]
112
+ def failure
113
+ tap do
114
+ spawn
115
+ status.success? ? nil : yield(self)
116
+ end
117
+ end
118
+
80
119
  private
81
120
 
82
121
  attr_reader :out_io, :err_io
@@ -21,6 +21,24 @@ class CmdTest < Test::Unit::TestCase
21
21
  assert_equal true, cmd("ruby", "-e", "exit 0").status.success?
22
22
  end
23
23
 
24
+ def test_ruby_success_callback
25
+ call_ok, call_fail = [false, false]
26
+ cmd("ruby", "-e", "exit 0")
27
+ .success { call_ok = true }
28
+ .failure { call_fail = true }
29
+ assert_equal true, call_ok
30
+ assert_equal false, call_fail
31
+ end
32
+
33
+ def test_ruby_failure_callback
34
+ call_ok, call_fail = [false, false]
35
+ cmd("ruby", "-e", "exit 1")
36
+ .success { call_ok = true }
37
+ .failure { call_fail = true }
38
+ assert_equal true, call_fail
39
+ assert_equal false, call_ok
40
+ end
41
+
24
42
  def test_stdout_with_fork
25
43
  code = <<-CODE.each_line.map { _1.chomp.strip }.join(";")
26
44
  $stdout.sync = true
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.2"
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,7 +1,7 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'
@@ -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: []