test-cmd.rb 0.10.0 → 0.11.1

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: 809adac1c621abbe0f30ba697594c4fe2bf931169b5f5bebdc833c5153e840cd
4
- data.tar.gz: 6882e65bd6fa276500285b2f735158506458c19509fa0278fc42410fb1ade2da
3
+ metadata.gz: 82a43ee0666fd9baea96c1974997092b1a739f730327e0a30ffa5d2e5b48cdaa
4
+ data.tar.gz: 2ca5079265f236de839baa8c6731433f99a9b0650c60e7c122db03c2b1a5e7bc
5
5
  SHA512:
6
- metadata.gz: d7f8f2757c6eb96e0b27fae6d59ce8e39b2c7d34e865b25a07e02871a8a4116b08ce78513757fc561a8cd356e6b3e25c04b60ec7a54de347de2dbad251a402e6
7
- data.tar.gz: 827b850156e8b11cc38b625c93c3760037dcee47514a0a21761c5d4d9a64f89d2a2b834f2c001f24a7a46e8282cce1a862ed6d336e90736dad722a41ed3c0d56
6
+ metadata.gz: 4a6ec40cdce6d791ca2e4880cd6bc31af6151706bb92dd467c0d2b99ce3551e2dd13efd2ecea46d32c2e2feb30995466ade1b16529e93d52e2321b2b29233788
7
+ data.tar.gz: 3b286a1f7e87e1066837b4a6c5f61421409637becb71792ab06919b5b7620759bd0f6b036e6694cc1f18e14144733bdefb117bd98a61f2c66a4b9f540661f52d
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## About
2
2
 
3
3
  test-cmd.rb provides an object-oriented interface for spawning
4
- a process.
4
+ a command.
5
5
 
6
6
  ## Examples
7
7
 
@@ -13,7 +13,7 @@ is passed an instance of
13
13
  [Test::Cmd](https://0x1eef.github.io/x/test-cmd.rb/Test/Cmd.html):
14
14
 
15
15
  ``` ruby
16
- require "test/cmd"
16
+ require "test-cmd"
17
17
  cmd("ruby", "-e", "exit 0")
18
18
  .success { print "The command [#{_1.pid}] was successful", "\n" }
19
19
  .failure { print "The command [#{_1.pid}] was unsuccessful", "\n" }
@@ -23,7 +23,7 @@ cmd("ruby", "-e", "exit 0")
23
23
 
24
24
  The following example demonstrates how tests might be written with
25
25
  test-unit from the standard library. The
26
- [`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/CmdMixin.html#cmd-instance_method)
26
+ [`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Kernel.html#cmd-instance_method)
27
27
  method takes the name or path of a command, alongside any arguments:
28
28
 
29
29
  ```ruby
@@ -32,19 +32,25 @@ require "test/cmd"
32
32
 
33
33
  class CmdTest < Test::Unit::TestCase
34
34
  def test_ruby_stdout
35
- assert_equal "42\n", cmd("ruby", "-e", "puts 42").stdout
35
+ assert_equal "42\n", ruby("puts 42").stdout
36
36
  end
37
37
 
38
38
  def test_ruby_stderr
39
- assert_equal "42\n", cmd("ruby", "-e", "warn 42").stderr
39
+ assert_equal "42\n", ruby("warn 42").stderr
40
40
  end
41
41
 
42
42
  def test_ruby_success_exit_status
43
- assert_equal 0, cmd("ruby", "-e", "exit 0").exit_status
43
+ assert_equal 0, ruby("exit 0").exit_status
44
44
  end
45
45
 
46
46
  def test_ruby_failure_exit_status
47
- assert_equal 1, cmd("ruby", "-e", "exit 1").exit_status
47
+ assert_equal 1, ruby("exit 1").exit_status
48
+ end
49
+
50
+ private
51
+
52
+ def ruby(code)
53
+ cmd("ruby", "-e", code)
48
54
  end
49
55
  end
50
56
  ```
@@ -92,7 +98,7 @@ p cmd("ruby", "test.rb").stdout # => "foo\nbar\n"
92
98
  ## Documentation
93
99
 
94
100
  A complete API reference is available at
95
- [0x1eef.github.io/x/test-cmd.rb](https://0x1eef.github.io/x/test-cmd.rb).
101
+ [0x1eef.github.io/x/test-cmd.rb](https://0x1eef.github.io/x/test-cmd.rb)
96
102
 
97
103
  ## Install
98
104
 
@@ -109,6 +115,6 @@ test-cmd.rb can be installed via rubygems.org:
109
115
 
110
116
  ## License
111
117
 
112
- [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
118
+ [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/)
113
119
  <br>
114
- See [LICENSE](./LICENSE).
120
+ See [LICENSE](./LICENSE)
data/lib/test/cmd.rb CHANGED
@@ -6,6 +6,7 @@ end unless defined?(Test)
6
6
  # for spawning a command.
7
7
  class Test::Cmd
8
8
  require "tempfile"
9
+ require "securerandom"
9
10
 
10
11
  ##
11
12
  # @param [String] cmd
@@ -40,6 +41,8 @@ class Test::Cmd
40
41
  Process.spawn(@cmd, *@argv, {out: @out_io, err: @err_io})
41
42
  Process.wait
42
43
  @status = $?
44
+ ensure
45
+ [stdout, stderr]
43
46
  end
44
47
  end
45
48
 
@@ -50,6 +53,8 @@ class Test::Cmd
50
53
  @stdout ||= begin
51
54
  spawn
52
55
  out_io.tap(&:rewind).read.tap { out_io.close }
56
+ rescue IOError
57
+ @stdout
53
58
  end
54
59
  end
55
60
 
@@ -60,6 +65,8 @@ class Test::Cmd
60
65
  @stderr ||= begin
61
66
  spawn
62
67
  err_io.tap(&:rewind).read.tap { err_io.close }
68
+ rescue IOError
69
+ @stderr
63
70
  end
64
71
  end
65
72
 
@@ -124,19 +131,32 @@ class Test::Cmd
124
131
  end
125
132
  end
126
133
 
134
+ ##
135
+ # @return [Boolean]
136
+ # Returns true when a command has been spawned
137
+ def spawned?
138
+ @spawned
139
+ end
140
+
127
141
  private
128
142
 
129
143
  attr_reader :out_io, :err_io
130
144
 
131
145
  def spawn_io
132
146
  [
133
- %W[#{object_id} testcmd.out],
134
- %W[#{object_id} testcmd.err]
135
- ].map {
136
- file = Tempfile.new(_1)
137
- File.chmod(0, file.path)
138
- file.tap(&:unlink)
139
- }
147
+ [".testcmd.stdout.#{ns}.", SecureRandom.alphanumeric(3)],
148
+ [".testcmd.stderr.#{ns}.", SecureRandom.alphanumeric(3)]
149
+ ].map do
150
+ Tempfile.new(_1).tap do |file|
151
+ File.chmod(0, file.path)
152
+ ensure
153
+ file.unlink
154
+ end
155
+ end
156
+ end
157
+
158
+ def ns
159
+ [Process.pid, object_id].join(".")
140
160
  end
141
161
  end
142
162
 
@@ -1,47 +1,55 @@
1
1
  require_relative "setup"
2
2
 
3
- class CmdTest < Test::Unit::TestCase
4
- def test_ruby_stdout
5
- assert_equal "42\n", cmd("ruby", "-e", "puts 42").stdout
3
+ class Test::Cmd
4
+ class Test < Test::Unit::TestCase
5
+ private
6
+ def ruby(str)
7
+ cmd "ruby", "-e", str
8
+ end
6
9
  end
10
+ end
7
11
 
8
- def test_ruby_stderr
9
- assert_equal "42\n", cmd("ruby", "-e", "warn 42").stderr
12
+ class Test::Cmd
13
+ ##
14
+ # Test::Cmd#argv
15
+ class ARGVTest < Test
16
+ def test_ruby_argv
17
+ assert_equal "42\n", cmd("ruby")
18
+ .argv("-e", "warn 42")
19
+ .stderr
20
+ end
10
21
  end
11
22
 
12
- def test_ruby_success_exit_status
13
- assert_equal 0, cmd("ruby", "-e", "exit 0").exit_status
14
- end
23
+ ##
24
+ # Test::Cmd#{exit_status, status, success?}
25
+ class ExitStatusTest < Test
26
+ def test_ruby_exit_status_success
27
+ assert_equal 0, ruby("exit 0").exit_status
28
+ end
15
29
 
16
- def test_ruby_failure_exit_status
17
- assert_equal 1, cmd("ruby", "-e", "exit 1").exit_status
18
- end
30
+ def test_ruby_exit_status_failure
31
+ assert_equal 1, ruby("exit 1").exit_status
32
+ end
19
33
 
20
- def test_ruby_success_status
21
- assert_equal true, cmd("ruby", "-e", "exit 0").status.success?
22
- assert_equal true, cmd("ruby", "-e", "exit 0").success?
34
+ def test_ruby_exit_status_predicates
35
+ assert_equal true, ruby("exit 0").status.success?
36
+ assert_equal true, ruby("exit 0").success?
37
+ end
23
38
  end
24
39
 
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
40
+ ##
41
+ # Test::Cmd#{stdout,stderr}
42
+ class OutputTest < Test
43
+ def test_ruby_stdout
44
+ assert_equal "42\n", ruby("puts 42").stdout
45
+ end
33
46
 
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
41
- end
47
+ def test_ruby_stderr
48
+ assert_equal "42\n", ruby("warn 42").stderr
49
+ end
42
50
 
43
- def test_stdout_with_fork
44
- code = <<-CODE.each_line.map { _1.chomp.strip }.join(";")
51
+ def test_ruby_stdout_fork
52
+ code = <<-CODE.each_line.map { _1.chomp.strip }.join(";")
45
53
  $stdout.sync = true
46
54
  fork do
47
55
  sleep(1)
@@ -50,12 +58,64 @@ class CmdTest < Test::Unit::TestCase
50
58
  puts "foo"
51
59
  Process.wait
52
60
  CODE
53
- assert_equal "foo\nbar\n", cmd("ruby", "-e", code).stdout
61
+ assert_equal "foo\nbar\n", ruby(code).stdout
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Test::Cmd#{success, failure}
67
+ class CallbackTest < Test
68
+ def test_ruby_success_callback
69
+ call_ok, call_fail = [false, false]
70
+ ruby("exit 0")
71
+ .success { call_ok = true }
72
+ .failure { call_fail = true }
73
+ assert_equal true, call_ok
74
+ assert_equal false, call_fail
75
+ end
76
+
77
+ def test_ruby_failure_callback
78
+ call_ok, call_fail = [false, false]
79
+ ruby("exit 1")
80
+ .success { call_ok = true }
81
+ .failure { call_fail = true }
82
+ assert_equal true, call_fail
83
+ assert_equal false, call_ok
84
+ end
54
85
  end
55
86
 
56
- def test_cmd_with_argv
57
- assert_equal "42\n", cmd("ruby")
58
- .argv("-e", "warn 42")
59
- .stderr
87
+ ##
88
+ # Test::Cmd#spawn
89
+ class SpawnTest < Test
90
+ def test_io_closed_after_spawn
91
+ %i[out_io err_io].each do |io|
92
+ assert_equal true, spawned_command.send(io).closed?
93
+ end
94
+ end
95
+
96
+ def test_io_unlink_after_spawn
97
+ %i[out_io err_io].each do |io|
98
+ path = spawned_command.send(io).__getobj__.path
99
+ assert_equal false, File.exist?(path)
100
+ end
101
+ end
102
+
103
+ private
104
+
105
+ def spawned_command
106
+ ruby("puts 42").spawn
107
+ end
108
+ end
109
+
110
+ ##
111
+ # Test::Cmd#spawned?
112
+ class SpawnedTest < Test
113
+ def test_spawned_before_spawn
114
+ assert_equal false, ruby("puts 42").spawned?
115
+ end
116
+
117
+ def test_spawned_after_spawn
118
+ assert_equal true, ruby("puts 42").tap(&:spawn).spawned?
119
+ end
60
120
  end
61
121
  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.10.0"
8
+ gem.version = "0.11.1"
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 command"
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.10.0
4
+ version: 0.11.1
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-15 00:00:00.000000000 Z
11
+ date: 2024-05-17 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 command
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 command
128
128
  test_files: []