test-cmd.rb 0.5.2 → 0.7.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 +21 -33
- data/lib/test-cmd.rb +24 -33
- data/test/test_cmd_test.rb +19 -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: 4a40537381e81f677332c21cfbc95b997fe0f89a4cbf4c714e2eb857da496554
|
4
|
+
data.tar.gz: 88ceffc1c05ed2b90f79c39c73c6a95adc5ccd9bce97ff435dea3dbea07feb89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 362b5b2155218c63f01321c279ebb5a7cd204fbdb01af2e76ce3d767d30c4b3a13c2a2eec52de77f7bb80419d37f7fdadb44ce1c3fb97e42d708d916a0eedfa1
|
7
|
+
data.tar.gz: 055f87c0d765be7ec2612ba9ae3e0f49cdaf2458eac747c8399b8b57dda732dca20d4848d9cc6809393fb3dfbd295cf2d55aec50eee9fbec7f8a63d37ef8a466
|
data/README.md
CHANGED
@@ -1,11 +1,7 @@
|
|
1
1
|
## About
|
2
2
|
|
3
|
-
test-cmd.rb
|
4
|
-
|
5
|
-
first realized in a test environment, where it provided a path
|
6
|
-
for verifying that when code examples are run they produce the
|
7
|
-
expected output. The library can be generally useful outside a
|
8
|
-
test environment, too.
|
3
|
+
test-cmd.rb provides an object-oriented interface for spawning
|
4
|
+
a process.
|
9
5
|
|
10
6
|
## Examples
|
11
7
|
|
@@ -14,7 +10,7 @@ test environment, too.
|
|
14
10
|
The following example demonstrates how tests might be written with
|
15
11
|
test-unit from the standard library. The
|
16
12
|
[`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/CmdMixin.html#cmd-instance_method)
|
17
|
-
method
|
13
|
+
method takes the name or path of a command, alongside any arguments:
|
18
14
|
|
19
15
|
```ruby
|
20
16
|
require "test/unit"
|
@@ -22,19 +18,19 @@ require "test/cmd"
|
|
22
18
|
|
23
19
|
class CmdTest < Test::Unit::TestCase
|
24
20
|
def test_ruby_stdout
|
25
|
-
assert_equal "
|
21
|
+
assert_equal "42\n", cmd("ruby", "-e", "puts 42").stdout
|
26
22
|
end
|
27
23
|
|
28
24
|
def test_ruby_stderr
|
29
|
-
assert_equal "
|
25
|
+
assert_equal "42\n", cmd("ruby", "-e", "warn 42").stderr
|
30
26
|
end
|
31
27
|
|
32
28
|
def test_ruby_success_exit_status
|
33
|
-
assert_equal 0, cmd(
|
29
|
+
assert_equal 0, cmd("ruby", "-e", "exit 0").exit_status
|
34
30
|
end
|
35
31
|
|
36
32
|
def test_ruby_failure_exit_status
|
37
|
-
assert_equal 1, cmd(
|
33
|
+
assert_equal 1, cmd("ruby", "-e", "exit 1").exit_status
|
38
34
|
end
|
39
35
|
end
|
40
36
|
```
|
@@ -50,10 +46,9 @@ covers it in more-depth:
|
|
50
46
|
|
51
47
|
``` ruby
|
52
48
|
require "test/cmd"
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
puts str
|
49
|
+
puts cmd("du")
|
50
|
+
.args("-s", "-h")
|
51
|
+
.stdout
|
57
52
|
```
|
58
53
|
|
59
54
|
### IO#sync
|
@@ -75,7 +70,7 @@ Process.wait(pid)
|
|
75
70
|
|
76
71
|
##
|
77
72
|
# cmd.rb
|
78
|
-
p cmd("ruby test.rb").stdout # => "bar\nfoo\n"
|
73
|
+
p cmd("ruby", "test.rb").stdout # => "bar\nfoo\n"
|
79
74
|
```
|
80
75
|
|
81
76
|
And with output flushed to the operating system immediately:
|
@@ -93,36 +88,29 @@ Process.wait(pid)
|
|
93
88
|
|
94
89
|
##
|
95
90
|
# cmd.rb
|
96
|
-
p cmd("ruby test.rb").stdout # => "foo\nbar\n"
|
91
|
+
p cmd("ruby", "test.rb").stdout # => "foo\nbar\n"
|
97
92
|
```
|
98
93
|
|
99
|
-
##
|
94
|
+
## Documentation
|
100
95
|
|
101
|
-
|
102
|
-
|
96
|
+
A complete API reference is available at
|
97
|
+
[0x1eef.github.io/x/test-cmd.rb](https://0x1eef.github.io/x/test-cmd.rb).
|
103
98
|
|
104
99
|
## Install
|
105
100
|
|
106
|
-
test-cmd.rb is distributed as a RubyGem through its git repositories. <br>
|
107
|
-
[GitHub](https://github.com/0x1eef/test-cmd.rb),
|
108
|
-
and
|
109
|
-
[GitLab](https://gitlab.com/0x1eef/test-cmd.rb)
|
110
|
-
are available as sources.
|
111
|
-
|
112
|
-
``` ruby
|
113
|
-
# Gemfile
|
114
|
-
gem "test-cmd.rb", github: "0x1eef/test-cmd.rb", tag: "v0.5.2"
|
115
|
-
```
|
116
|
-
|
117
101
|
**Rubygems.org**
|
118
102
|
|
119
|
-
test-cmd.rb can
|
103
|
+
test-cmd.rb can be installed via rubygems.org.
|
120
104
|
|
121
105
|
gem install test-cmd.rb
|
122
106
|
|
107
|
+
## Sources
|
108
|
+
|
109
|
+
* [GitHub](https://github.com/0x1eef/test-cmd.rb#readme)
|
110
|
+
* [GitLab](https://gitlab.com/0x1eef/test-cmd.rb#about)
|
111
|
+
|
123
112
|
## License
|
124
113
|
|
125
114
|
[BSD Zero Clause](https://choosealicense.com/licenses/0bsd/).
|
126
115
|
<br>
|
127
116
|
See [LICENSE](./LICENSE).
|
128
|
-
|
data/lib/test-cmd.rb
CHANGED
@@ -3,23 +3,19 @@ end unless defined?(Test)
|
|
3
3
|
|
4
4
|
##
|
5
5
|
# test-cmd.rb is a library for accessing the output streams
|
6
|
-
# (both stdout and stderr) of a spawned process.
|
7
|
-
# first realized in a test environment, where it provided a path
|
8
|
-
# for verifying that when code examples are run they produce the
|
9
|
-
# expected output. The library can be generally useful outside a
|
10
|
-
# test environment, too.
|
6
|
+
# (both stdout and stderr) of a spawned process.
|
11
7
|
class Test::Cmd
|
12
8
|
require "tempfile"
|
13
9
|
|
14
10
|
##
|
15
11
|
# @param [String] cmd
|
16
12
|
# A command to spawn.
|
17
|
-
# @param [Array<String>]
|
18
|
-
#
|
13
|
+
# @param [Array<String>] argv
|
14
|
+
# A variable number of command-line arguments.
|
19
15
|
# @return [Test::Cmd]
|
20
|
-
def initialize(cmd,
|
16
|
+
def initialize(cmd, *argv)
|
21
17
|
@cmd = cmd
|
22
|
-
@
|
18
|
+
@argv = argv.dup
|
23
19
|
@out = unlink!(Tempfile.new("cmd-stdout"))
|
24
20
|
@err = unlink!(Tempfile.new("cmd-stderr"))
|
25
21
|
@status = nil
|
@@ -32,17 +28,17 @@ class Test::Cmd
|
|
32
28
|
# @return [Test::Cmd]
|
33
29
|
def arg(arg)
|
34
30
|
tap do
|
35
|
-
@
|
31
|
+
@argv.push(arg)
|
36
32
|
end
|
37
33
|
end
|
38
34
|
|
39
35
|
##
|
40
|
-
# @param [Array<String, #to_s>]
|
36
|
+
# @param [Array<String, #to_s>] argv
|
41
37
|
# One or more command-line arguments.
|
42
38
|
# @return [Test::Cmd]
|
43
|
-
def args(*
|
39
|
+
def args(*argv)
|
44
40
|
tap do
|
45
|
-
@
|
41
|
+
@argv.concat(argv)
|
46
42
|
end
|
47
43
|
end
|
48
44
|
|
@@ -52,7 +48,7 @@ class Test::Cmd
|
|
52
48
|
def spawn
|
53
49
|
tap do
|
54
50
|
@spawned = true
|
55
|
-
Process.wait Process.spawn(@cmd, *@
|
51
|
+
Process.wait Process.spawn(@cmd, *@argv, {out: @out, err: @err})
|
56
52
|
@status = $?
|
57
53
|
end
|
58
54
|
ensure
|
@@ -79,7 +75,8 @@ class Test::Cmd
|
|
79
75
|
@err.close unless @err.closed?
|
80
76
|
end
|
81
77
|
|
82
|
-
##
|
78
|
+
##
|
79
|
+
# @return [Process::Status]
|
83
80
|
# Returns the status of a process
|
84
81
|
def status
|
85
82
|
spawn unless @spawned
|
@@ -94,16 +91,14 @@ class Test::Cmd
|
|
94
91
|
end
|
95
92
|
|
96
93
|
##
|
97
|
-
#
|
98
|
-
#
|
99
|
-
# the command was not successful.
|
94
|
+
# @param [Symbol] io
|
95
|
+
# The output stream as a Symbol (:stdout, :stderr).
|
100
96
|
# @return [Enumerator]
|
101
97
|
# Returns an Enumerator when a block is not given.
|
102
|
-
def each_line
|
103
|
-
return enum_for(:each_line) unless block_given?
|
98
|
+
def each_line(io = :stdout)
|
99
|
+
return enum_for(:each_line, io) unless block_given?
|
104
100
|
spawn unless @spawned
|
105
|
-
io
|
106
|
-
io.each_line.each { yield(_1.chomp) }
|
101
|
+
public_send(io).each_line { yield(_1) }
|
107
102
|
end
|
108
103
|
|
109
104
|
private
|
@@ -112,25 +107,21 @@ class Test::Cmd
|
|
112
107
|
# @api private
|
113
108
|
def unlink!(file)
|
114
109
|
file.tap do
|
115
|
-
File.chmod(
|
110
|
+
File.chmod(0, file.path)
|
116
111
|
file.unlink
|
117
112
|
end
|
118
113
|
end
|
119
114
|
end
|
120
115
|
|
121
|
-
module Test::
|
116
|
+
module Test::Cmd::Mixin
|
122
117
|
##
|
123
|
-
# @param
|
124
|
-
#
|
125
|
-
|
126
|
-
|
127
|
-
# @return [Test::Cmd]
|
128
|
-
# Returns an instance of {Test::Cmd Test::Cmd}
|
129
|
-
def cmd(cmd, args = [])
|
130
|
-
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)
|
131
122
|
end
|
132
123
|
end
|
133
124
|
|
134
125
|
module Kernel
|
135
|
-
include Test::
|
126
|
+
include Test::Cmd::Mixin
|
136
127
|
end
|
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,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(
|
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
|
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.
|
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 = "
|
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.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-
|
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: []
|