test-cmd.rb 0.6.0 → 0.7.0

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: 031f9708015f5e2e10d4b9afe09db3841c2a7c8ffa9ee83717d12ff4e078cd02
4
- data.tar.gz: e1afc91430c82bfb3e146ea3b3a8a58c9fdb047b4c25c2be7b62d2b1470fd694
3
+ metadata.gz: 4a40537381e81f677332c21cfbc95b997fe0f89a4cbf4c714e2eb857da496554
4
+ data.tar.gz: 88ceffc1c05ed2b90f79c39c73c6a95adc5ccd9bce97ff435dea3dbea07feb89
5
5
  SHA512:
6
- metadata.gz: 599e9bd0599c675f58b742e556b60c0d475b3a8fbe50012eddcc633eb1f09492791e88f0fbc7161a63172b96df1268a80304cd1b6dbb738ddd06d48ce9ad30ed
7
- data.tar.gz: acc8485e9f6fde4c25e80dd354933f5750804fe0f066054b2e4cea3c4093a2020df28d86ab7f055766466658d3a69d6a53d89699bb6f0d3652b3cdd8f46eec3d
6
+ metadata.gz: 362b5b2155218c63f01321c279ebb5a7cd204fbdb01af2e76ce3d767d30c4b3a13c2a2eec52de77f7bb80419d37f7fdadb44ce1c3fb97e42d708d916a0eedfa1
7
+ data.tar.gz: 055f87c0d765be7ec2612ba9ae3e0f49cdaf2458eac747c8399b8b57dda732dca20d4848d9cc6809393fb3dfbd295cf2d55aec50eee9fbec7f8a63d37ef8a466
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## About
2
2
 
3
- test-cmd.rb is a library for accessing the output streams
4
- (both stdout and stderr) of a spawned process.
3
+ test-cmd.rb provides an object-oriented interface for spawning
4
+ a process.
5
5
 
6
6
  ## Examples
7
7
 
@@ -10,7 +10,7 @@ test-cmd.rb is a library for accessing the output streams
10
10
  The following example demonstrates how tests might be written with
11
11
  test-unit from the standard library. The
12
12
  [`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/CmdMixin.html#cmd-instance_method)
13
- method is given the name of a command, along with any arguments:
13
+ method takes the name or path of a command, alongside any arguments:
14
14
 
15
15
  ```ruby
16
16
  require "test/unit"
@@ -18,19 +18,19 @@ require "test/cmd"
18
18
 
19
19
  class CmdTest < Test::Unit::TestCase
20
20
  def test_ruby_stdout
21
- assert_equal "foo\n", cmd(%q(ruby -e '$stdout.puts "foo"')).stdout
21
+ assert_equal "42\n", cmd("ruby", "-e", "puts 42").stdout
22
22
  end
23
23
 
24
24
  def test_ruby_stderr
25
- assert_equal "bar\n", cmd(%q(ruby -e '$stderr.puts "bar"')).stderr
25
+ assert_equal "42\n", cmd("ruby", "-e", "warn 42").stderr
26
26
  end
27
27
 
28
28
  def test_ruby_success_exit_status
29
- assert_equal 0, cmd(%q(ruby -e 'exit 0')).exit_status
29
+ assert_equal 0, cmd("ruby", "-e", "exit 0").exit_status
30
30
  end
31
31
 
32
32
  def test_ruby_failure_exit_status
33
- assert_equal 1, cmd(%q(ruby -e 'exit 1')).exit_status
33
+ assert_equal 1, cmd("ruby", "-e", "exit 1").exit_status
34
34
  end
35
35
  end
36
36
  ```
@@ -46,10 +46,9 @@ covers it in more-depth:
46
46
 
47
47
  ``` ruby
48
48
  require "test/cmd"
49
- str = cmd("du")
50
- .arg("-s").arg("-h")
51
- .spawn.stdout
52
- puts str
49
+ puts cmd("du")
50
+ .args("-s", "-h")
51
+ .stdout
53
52
  ```
54
53
 
55
54
  ### IO#sync
@@ -71,7 +70,7 @@ Process.wait(pid)
71
70
 
72
71
  ##
73
72
  # cmd.rb
74
- p cmd("ruby test.rb").stdout # => "bar\nfoo\n"
73
+ p cmd("ruby", "test.rb").stdout # => "bar\nfoo\n"
75
74
  ```
76
75
 
77
76
  And with output flushed to the operating system immediately:
@@ -89,7 +88,7 @@ Process.wait(pid)
89
88
 
90
89
  ##
91
90
  # cmd.rb
92
- p cmd("ruby test.rb").stdout # => "foo\nbar\n"
91
+ p cmd("ruby", "test.rb").stdout # => "foo\nbar\n"
93
92
  ```
94
93
 
95
94
  ## Documentation
@@ -115,4 +114,3 @@ test-cmd.rb can be installed via rubygems.org.
115
114
  [BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
116
115
  <br>
117
116
  See [LICENSE](./LICENSE).
118
-
data/lib/test-cmd.rb CHANGED
@@ -10,12 +10,12 @@ class Test::Cmd
10
10
  ##
11
11
  # @param [String] cmd
12
12
  # A command to spawn.
13
- # @param [Array<String>] args
14
- # An array of command-line arguments.
13
+ # @param [Array<String>] argv
14
+ # A variable number of command-line arguments.
15
15
  # @return [Test::Cmd]
16
- def initialize(cmd, args = [])
16
+ def initialize(cmd, *argv)
17
17
  @cmd = cmd
18
- @args = args.dup
18
+ @argv = argv.dup
19
19
  @out = unlink!(Tempfile.new("cmd-stdout"))
20
20
  @err = unlink!(Tempfile.new("cmd-stderr"))
21
21
  @status = nil
@@ -28,17 +28,17 @@ class Test::Cmd
28
28
  # @return [Test::Cmd]
29
29
  def arg(arg)
30
30
  tap do
31
- @args.push(arg)
31
+ @argv.push(arg)
32
32
  end
33
33
  end
34
34
 
35
35
  ##
36
- # @param [Array<String, #to_s>] args
36
+ # @param [Array<String, #to_s>] argv
37
37
  # One or more command-line arguments.
38
38
  # @return [Test::Cmd]
39
- def args(*args)
39
+ def args(*argv)
40
40
  tap do
41
- @args.concat(args)
41
+ @argv.concat(argv)
42
42
  end
43
43
  end
44
44
 
@@ -48,7 +48,7 @@ class Test::Cmd
48
48
  def spawn
49
49
  tap do
50
50
  @spawned = true
51
- Process.wait Process.spawn(@cmd, *@args, {out: @out, err: @err})
51
+ Process.wait Process.spawn(@cmd, *@argv, {out: @out, err: @err})
52
52
  @status = $?
53
53
  end
54
54
  ensure
@@ -75,7 +75,8 @@ class Test::Cmd
75
75
  @err.close unless @err.closed?
76
76
  end
77
77
 
78
- ## @return [Process::Status]
78
+ ##
79
+ # @return [Process::Status]
79
80
  # Returns the status of a process
80
81
  def status
81
82
  spawn unless @spawned
@@ -90,16 +91,14 @@ class Test::Cmd
90
91
  end
91
92
 
92
93
  ##
93
- # Yields each line of stdout when the command
94
- # was successful, or each line of stderr when
95
- # the command was not successful.
94
+ # @param [Symbol] io
95
+ # The output stream as a Symbol (:stdout, :stderr).
96
96
  # @return [Enumerator]
97
97
  # Returns an Enumerator when a block is not given.
98
- def each_line
99
- return enum_for(:each_line) unless block_given?
98
+ def each_line(io = :stdout)
99
+ return enum_for(:each_line, io) unless block_given?
100
100
  spawn unless @spawned
101
- io = @status.success? ? @stdout : @stderr
102
- io.each_line.each { yield(_1.chomp) }
101
+ public_send(io).each_line { yield(_1) }
103
102
  end
104
103
 
105
104
  private
@@ -116,14 +115,10 @@ end
116
115
 
117
116
  module Test::Cmd::Mixin
118
117
  ##
119
- # @param [String] cmd
120
- # A command to execute
121
- # @param [Array<String>] args
122
- # An array of command-line arguments.
123
- # @return [Test::Cmd]
124
- # Returns an instance of {Test::Cmd Test::Cmd}
125
- def cmd(cmd, args = [])
126
- Test::Cmd.new(cmd, args)
118
+ # @param (see Test::Cmd#initialize)
119
+ # @return (see Test::Cmd#initialize)
120
+ def cmd(cmd, *argv)
121
+ Test::Cmd.new(cmd, *argv)
127
122
  end
128
123
  end
129
124
 
@@ -2,19 +2,19 @@ require_relative "setup"
2
2
 
3
3
  class CmdTest < Test::Unit::TestCase
4
4
  def test_ruby_stdout
5
- assert_equal "foo\n", cmd(%q(ruby -e '$stdout.puts "foo"')).stdout
5
+ assert_equal "42\n", cmd("ruby", "-e", "puts 42").stdout
6
6
  end
7
7
 
8
8
  def test_ruby_stderr
9
- assert_equal "bar\n", cmd(%q(ruby -e '$stderr.puts "bar"')).stderr
9
+ assert_equal "42\n", cmd("ruby", "-e", "warn 42").stderr
10
10
  end
11
11
 
12
12
  def test_ruby_success_exit_status
13
- assert_equal 0, cmd(%q(ruby -e 'exit 0')).exit_status
13
+ assert_equal 0, cmd("ruby", "-e", "exit 0").exit_status
14
14
  end
15
15
 
16
16
  def test_ruby_failure_exit_status
17
- assert_equal 1, cmd(%q(ruby -e 'exit 1')).exit_status
17
+ assert_equal 1, cmd("ruby", "-e", "exit 1").exit_status
18
18
  end
19
19
 
20
20
  def test_stdout_with_fork
@@ -27,21 +27,31 @@ class CmdTest < Test::Unit::TestCase
27
27
  puts "foo"
28
28
  Process.wait(pid)
29
29
  CODE
30
- assert_equal "foo\nbar\n", cmd(%Q(ruby -e '#{code}')).stdout
30
+ assert_equal "foo\nbar\n", cmd("ruby", "-e", code).stdout
31
31
  end
32
32
 
33
33
  def test_each_line_stdout
34
34
  run = false
35
- cmd(%q(ruby -e '$stdout.puts "FooBar"'))
35
+ cmd("ruby", "-e", "puts 'FooBar'")
36
36
  .each_line do
37
37
  run = true
38
- assert_equal _1, "FooBar"
38
+ assert_equal "FooBar\n", _1
39
39
  end
40
- assert_equal true, run
40
+ assert run
41
+ end
42
+
43
+ def test_each_line_stderr
44
+ run = false
45
+ cmd("ruby", "-e", "warn 'BarFoo'")
46
+ .each_line(:stderr) do
47
+ run = true
48
+ assert_equal "BarFoo\n", _1
49
+ end
50
+ assert run
41
51
  end
42
52
 
43
53
  def test_each_line_returns_enum
44
54
  assert_instance_of Enumerator,
45
- cmd(%q(ruby -e '$stdout.puts "FooBar"')).each_line
55
+ cmd("ruby", "-e", "puts 'FooBar'").each_line
46
56
  end
47
57
  end
data/test-cmd.rb.gemspec CHANGED
@@ -5,13 +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.6.0"
8
+ gem.version = "0.7.0"
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 = "test-cmd.rb provides access to the output streams " \
14
- "(both stdout and stderr) of a spawned process."
13
+ gem.summary = "An object-oriented interface for spawning a process."
15
14
  gem.metadata = { "documentation_uri" => "https://0x1eef.github.io/x/test-cmd.rb/" }
16
15
  gem.description = gem.summary
17
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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-17 00:00:00.000000000 Z
11
+ date: 2024-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -80,8 +80,7 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '13.1'
83
- description: test-cmd.rb provides access to the output streams (both stdout and stderr)
84
- of a spawned process.
83
+ description: An object-oriented interface for spawning a process.
85
84
  email:
86
85
  - 0x1eef@protonmail.com
87
86
  executables: []
@@ -125,6 +124,5 @@ requirements: []
125
124
  rubygems_version: 3.5.3
126
125
  signing_key:
127
126
  specification_version: 4
128
- summary: test-cmd.rb provides access to the output streams (both stdout and stderr)
129
- of a spawned process.
127
+ summary: An object-oriented interface for spawning a process.
130
128
  test_files: []