test-cmd.rb 0.6.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -14
- data/lib/test-cmd.rb +19 -34
- data/test/test_cmd_test.rb +25 -9
- data/test-cmd.rb.gemspec +2 -3
- metadata +4 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3271a0fc385b5c2ec050a4295cfd9c67b662dca479a1e5c197610fb0f773fb8b
|
4
|
+
data.tar.gz: da1a24b436cfde2e63a6fccdaf6ac5d597f180f10b578319549713e64992a095
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf4e1638056aa03ce82db25547efe1ad9c2bbb2ac737a3ec13b9f8c23576c090101b59c19ce7adf554a2b47e9ae16d9fac6439e730c74423a8fe37e317be745d
|
7
|
+
data.tar.gz: 82c83edc676b854ffa3e96930a6d6b6d51e3ab484df093f1919529e5abc9914e6ff0bc65180733ed0434d48fdee15be93bbe5ff7526e422f2ff57e56c061a4c7
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
## About
|
2
2
|
|
3
|
-
test-cmd.rb
|
4
|
-
|
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
|
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 "
|
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 "
|
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(
|
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(
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
puts str
|
49
|
+
puts cmd("du")
|
50
|
+
.argv("-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>]
|
14
|
-
#
|
13
|
+
# @param [Array<String>] argv
|
14
|
+
# A variable number of command-line arguments.
|
15
15
|
# @return [Test::Cmd]
|
16
|
-
def initialize(cmd,
|
16
|
+
def initialize(cmd, *argv)
|
17
17
|
@cmd = cmd
|
18
|
-
@
|
18
|
+
@argv = argv.dup
|
19
19
|
@out = unlink!(Tempfile.new("cmd-stdout"))
|
20
20
|
@err = unlink!(Tempfile.new("cmd-stderr"))
|
21
21
|
@status = nil
|
@@ -23,22 +23,12 @@ class Test::Cmd
|
|
23
23
|
end
|
24
24
|
|
25
25
|
##
|
26
|
-
# @param [String, #to_s]
|
27
|
-
# A command-line argument.
|
28
|
-
# @return [Test::Cmd]
|
29
|
-
def arg(arg)
|
30
|
-
tap do
|
31
|
-
@args.push(arg)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# @param [Array<String, #to_s>] args
|
26
|
+
# @param [Array<String, #to_s>] argv
|
37
27
|
# One or more command-line arguments.
|
38
28
|
# @return [Test::Cmd]
|
39
|
-
def
|
29
|
+
def argv(*argv)
|
40
30
|
tap do
|
41
|
-
@
|
31
|
+
@argv.concat(argv)
|
42
32
|
end
|
43
33
|
end
|
44
34
|
|
@@ -48,7 +38,7 @@ class Test::Cmd
|
|
48
38
|
def spawn
|
49
39
|
tap do
|
50
40
|
@spawned = true
|
51
|
-
Process.wait Process.spawn(@cmd, *@
|
41
|
+
Process.wait Process.spawn(@cmd, *@argv, {out: @out, err: @err})
|
52
42
|
@status = $?
|
53
43
|
end
|
54
44
|
ensure
|
@@ -75,7 +65,8 @@ class Test::Cmd
|
|
75
65
|
@err.close unless @err.closed?
|
76
66
|
end
|
77
67
|
|
78
|
-
##
|
68
|
+
##
|
69
|
+
# @return [Process::Status]
|
79
70
|
# Returns the status of a process
|
80
71
|
def status
|
81
72
|
spawn unless @spawned
|
@@ -90,16 +81,14 @@ class Test::Cmd
|
|
90
81
|
end
|
91
82
|
|
92
83
|
##
|
93
|
-
#
|
94
|
-
#
|
95
|
-
# the command was not successful.
|
84
|
+
# @param [Symbol] io
|
85
|
+
# The output stream as a Symbol (:stdout, :stderr).
|
96
86
|
# @return [Enumerator]
|
97
87
|
# Returns an Enumerator when a block is not given.
|
98
|
-
def each_line
|
99
|
-
return enum_for(:each_line) unless block_given?
|
88
|
+
def each_line(io = :stdout)
|
89
|
+
return enum_for(:each_line, io) unless block_given?
|
100
90
|
spawn unless @spawned
|
101
|
-
io
|
102
|
-
io.each_line.each { yield(_1.chomp) }
|
91
|
+
public_send(io).each_line { yield(_1) }
|
103
92
|
end
|
104
93
|
|
105
94
|
private
|
@@ -116,14 +105,10 @@ end
|
|
116
105
|
|
117
106
|
module Test::Cmd::Mixin
|
118
107
|
##
|
119
|
-
# @param
|
120
|
-
#
|
121
|
-
|
122
|
-
|
123
|
-
# @return [Test::Cmd]
|
124
|
-
# Returns an instance of {Test::Cmd Test::Cmd}
|
125
|
-
def cmd(cmd, args = [])
|
126
|
-
Test::Cmd.new(cmd, args)
|
108
|
+
# @param (see Test::Cmd#initialize)
|
109
|
+
# @return (see Test::Cmd#initialize)
|
110
|
+
def cmd(cmd, *argv)
|
111
|
+
Test::Cmd.new(cmd, *argv)
|
127
112
|
end
|
128
113
|
end
|
129
114
|
|
data/test/test_cmd_test.rb
CHANGED
@@ -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 "
|
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 "
|
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(
|
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(
|
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,37 @@ class CmdTest < Test::Unit::TestCase
|
|
27
27
|
puts "foo"
|
28
28
|
Process.wait(pid)
|
29
29
|
CODE
|
30
|
-
assert_equal "foo\nbar\n", cmd(
|
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(
|
35
|
+
cmd("ruby", "-e", "puts 'FooBar'")
|
36
36
|
.each_line do
|
37
37
|
run = true
|
38
|
-
assert_equal
|
38
|
+
assert_equal "FooBar\n", _1
|
39
39
|
end
|
40
|
-
|
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(
|
55
|
+
cmd("ruby", "-e", "puts 'FooBar'").each_line
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_cmd_with_argv
|
59
|
+
assert_equal "42\n", cmd("ruby")
|
60
|
+
.argv("-e", "warn 42")
|
61
|
+
.stderr
|
46
62
|
end
|
47
63
|
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.
|
8
|
+
gem.version = "0.8.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 = "
|
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.
|
4
|
+
version: 0.8.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-
|
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:
|
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:
|
129
|
-
of a spawned process.
|
127
|
+
summary: An object-oriented interface for spawning a process.
|
130
128
|
test_files: []
|