test-cmd.rb 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +3 -3
- data/lib/test/cmd.rb +93 -0
- data/lib/test-cmd.rb +3 -105
- data/test/test_cmd_test.rb +4 -25
- data/test-cmd.rb.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cce30514a659d318e574a7a8c13de222c8632a4ab944847821195fe512e133a
|
4
|
+
data.tar.gz: d5a12184b17454efa414a380006c6f240e6747e13220d3b28940c6cf79e7df33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5d88ccede58e0d3f23d226481291aa7c97d64ca1a18ab3d620dee57fbf6590f2e0da1cb62962e8ac328a6a91d80b87d7a525136893e55f5ab9c583e6eda81dc
|
7
|
+
data.tar.gz: 6190123fe4136ca820e3c1241a32927ee72be094024abb49d0aaa15be9c6955bccdcc396029b2e035629712bd31a8bc5e221dd9db311422e459ea3a2a253da0e
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -47,8 +47,8 @@ covers it in more-depth:
|
|
47
47
|
``` ruby
|
48
48
|
require "test/cmd"
|
49
49
|
puts cmd("du")
|
50
|
-
|
51
|
-
|
50
|
+
.argv("-s", "-h")
|
51
|
+
.stdout
|
52
52
|
```
|
53
53
|
|
54
54
|
### IO#sync
|
@@ -100,7 +100,7 @@ A complete API reference is available at
|
|
100
100
|
|
101
101
|
**Rubygems.org**
|
102
102
|
|
103
|
-
test-cmd.rb can be installed via rubygems.org
|
103
|
+
test-cmd.rb can be installed via rubygems.org:
|
104
104
|
|
105
105
|
gem install test-cmd.rb
|
106
106
|
|
data/lib/test/cmd.rb
CHANGED
@@ -1 +1,94 @@
|
|
1
1
|
require_relative "../test-cmd"
|
2
|
+
|
3
|
+
##
|
4
|
+
# test-cmd.rb provides an object oriented interface
|
5
|
+
# for spawning a command.
|
6
|
+
class Test::Cmd
|
7
|
+
require "tempfile"
|
8
|
+
|
9
|
+
##
|
10
|
+
# @param [String] cmd
|
11
|
+
# A command to spawn
|
12
|
+
# @param [Array<String>] argv
|
13
|
+
# Zero or more command-line arguments
|
14
|
+
# @return [Test::Cmd]
|
15
|
+
def initialize(cmd, *argv)
|
16
|
+
@cmd = cmd
|
17
|
+
@argv = argv.dup
|
18
|
+
@status = nil
|
19
|
+
@spawned = false
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# @param [Array<String, #to_s>] argv
|
24
|
+
# Command-line arguments
|
25
|
+
# @return [Test::Cmd]
|
26
|
+
def argv(*argv)
|
27
|
+
tap { @argv.concat(argv) }
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
# Spawns a command
|
32
|
+
# @return [Test::Cmd]
|
33
|
+
def spawn
|
34
|
+
return if @spawned
|
35
|
+
|
36
|
+
tap do
|
37
|
+
@spawned = true
|
38
|
+
@out_io, @err_io = spawn_io
|
39
|
+
Process.spawn(@cmd, *@argv, {out: @out_io, err: @err_io})
|
40
|
+
Process.wait
|
41
|
+
@status = $?
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# @return [String]
|
47
|
+
# Returns the contents of stdout
|
48
|
+
def stdout
|
49
|
+
@stdout ||= begin
|
50
|
+
spawn
|
51
|
+
out_io.tap(&:rewind).read.tap { out_io.close }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# @return [String]
|
57
|
+
# Returns the contents of stderr
|
58
|
+
def stderr
|
59
|
+
@stderr ||= begin
|
60
|
+
spawn
|
61
|
+
err_io.tap(&:rewind).read.tap { err_io.close }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# @return [Process::Status]
|
67
|
+
# Returns the status of a process
|
68
|
+
def status
|
69
|
+
spawn
|
70
|
+
@status
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# @return [Integer]
|
75
|
+
# Returns the exit status of a process
|
76
|
+
def exit_status
|
77
|
+
status.exitstatus
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
attr_reader :out_io, :err_io
|
83
|
+
|
84
|
+
def spawn_io
|
85
|
+
[
|
86
|
+
%W[#{object_id} testcmd.out],
|
87
|
+
%W[#{object_id} testcmd.err]
|
88
|
+
].map {
|
89
|
+
file = Tempfile.new(_1)
|
90
|
+
File.chmod(0, file.path)
|
91
|
+
file.tap(&:unlink)
|
92
|
+
}
|
93
|
+
end
|
94
|
+
end
|
data/lib/test-cmd.rb
CHANGED
@@ -1,109 +1,11 @@
|
|
1
1
|
module Test
|
2
2
|
end unless defined?(Test)
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
# (both stdout and stderr) of a spawned process.
|
7
|
-
class Test::Cmd
|
8
|
-
require "tempfile"
|
9
|
-
|
10
|
-
##
|
11
|
-
# @param [String] cmd
|
12
|
-
# A command to spawn.
|
13
|
-
# @param [Array<String>] argv
|
14
|
-
# A variable number of command-line arguments.
|
15
|
-
# @return [Test::Cmd]
|
16
|
-
def initialize(cmd, *argv)
|
17
|
-
@cmd = cmd
|
18
|
-
@argv = argv.dup
|
19
|
-
@out = unlink!(Tempfile.new("cmd-stdout"))
|
20
|
-
@err = unlink!(Tempfile.new("cmd-stderr"))
|
21
|
-
@status = nil
|
22
|
-
@spawned = false
|
23
|
-
end
|
24
|
-
|
25
|
-
##
|
26
|
-
# @param [Array<String, #to_s>] argv
|
27
|
-
# One or more command-line arguments.
|
28
|
-
# @return [Test::Cmd]
|
29
|
-
def argv(*argv)
|
30
|
-
tap do
|
31
|
-
@argv.concat(argv)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
##
|
36
|
-
# Spawns a command.
|
37
|
-
# @return [Test::Cmd]
|
38
|
-
def spawn
|
39
|
-
tap do
|
40
|
-
@spawned = true
|
41
|
-
Process.wait Process.spawn(@cmd, *@argv, {out: @out, err: @err})
|
42
|
-
@status = $?
|
43
|
-
end
|
44
|
-
ensure
|
45
|
-
[stdout,stderr]
|
46
|
-
end
|
47
|
-
|
48
|
-
##
|
49
|
-
# @return [String]
|
50
|
-
# Returns the contents of stdout.
|
51
|
-
def stdout
|
52
|
-
spawn unless @spawned
|
53
|
-
@stdout ||= @out.tap(&:rewind).read
|
54
|
-
ensure
|
55
|
-
@out.close unless @out.closed?
|
56
|
-
end
|
57
|
-
|
58
|
-
##
|
59
|
-
# @return [String]
|
60
|
-
# Returns the contents of stderr.
|
61
|
-
def stderr
|
62
|
-
spawn unless @spawned
|
63
|
-
@stderr ||= @err.tap(&:rewind).read
|
64
|
-
ensure
|
65
|
-
@err.close unless @err.closed?
|
66
|
-
end
|
67
|
-
|
68
|
-
##
|
69
|
-
# @return [Process::Status]
|
70
|
-
# Returns the status of a process
|
71
|
-
def status
|
72
|
-
spawn unless @spawned
|
73
|
-
@status
|
74
|
-
end
|
75
|
-
|
76
|
-
##
|
77
|
-
# @return [Integer]
|
78
|
-
# Returns the exit status of a process
|
79
|
-
def exit_status
|
80
|
-
status.exitstatus
|
81
|
-
end
|
82
|
-
|
83
|
-
##
|
84
|
-
# @param [Symbol] io
|
85
|
-
# The output stream as a Symbol (:stdout, :stderr).
|
86
|
-
# @return [Enumerator]
|
87
|
-
# Returns an Enumerator when a block is not given.
|
88
|
-
def each_line(io = :stdout)
|
89
|
-
return enum_for(:each_line, io) unless block_given?
|
90
|
-
spawn unless @spawned
|
91
|
-
public_send(io).each_line { yield(_1) }
|
92
|
-
end
|
93
|
-
|
94
|
-
private
|
95
|
-
|
96
|
-
##
|
97
|
-
# @api private
|
98
|
-
def unlink!(file)
|
99
|
-
file.tap do
|
100
|
-
File.chmod(0, file.path)
|
101
|
-
file.unlink
|
102
|
-
end
|
103
|
-
end
|
4
|
+
module Test
|
5
|
+
require_relative "test/cmd"
|
104
6
|
end
|
105
7
|
|
106
|
-
module
|
8
|
+
module Kernel
|
107
9
|
##
|
108
10
|
# @param (see Test::Cmd#initialize)
|
109
11
|
# @return (see Test::Cmd#initialize)
|
@@ -111,7 +13,3 @@ module Test::Cmd::Mixin
|
|
111
13
|
Test::Cmd.new(cmd, *argv)
|
112
14
|
end
|
113
15
|
end
|
114
|
-
|
115
|
-
module Kernel
|
116
|
-
include Test::Cmd::Mixin
|
117
|
-
end
|
data/test/test_cmd_test.rb
CHANGED
@@ -17,6 +17,10 @@ class CmdTest < Test::Unit::TestCase
|
|
17
17
|
assert_equal 1, cmd("ruby", "-e", "exit 1").exit_status
|
18
18
|
end
|
19
19
|
|
20
|
+
def test_ruby_success_status
|
21
|
+
assert_equal true, cmd("ruby", "-e", "exit 0").status.success?
|
22
|
+
end
|
23
|
+
|
20
24
|
def test_stdout_with_fork
|
21
25
|
code = <<-CODE.each_line.map { _1.chomp.strip }.join(";")
|
22
26
|
$stdout.sync = true
|
@@ -30,31 +34,6 @@ class CmdTest < Test::Unit::TestCase
|
|
30
34
|
assert_equal "foo\nbar\n", cmd("ruby", "-e", code).stdout
|
31
35
|
end
|
32
36
|
|
33
|
-
def test_each_line_stdout
|
34
|
-
run = false
|
35
|
-
cmd("ruby", "-e", "puts 'FooBar'")
|
36
|
-
.each_line do
|
37
|
-
run = true
|
38
|
-
assert_equal "FooBar\n", _1
|
39
|
-
end
|
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
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_each_line_returns_enum
|
54
|
-
assert_instance_of Enumerator,
|
55
|
-
cmd("ruby", "-e", "puts 'FooBar'").each_line
|
56
|
-
end
|
57
|
-
|
58
37
|
def test_cmd_with_argv
|
59
38
|
assert_equal "42\n", cmd("ruby")
|
60
39
|
.argv("-e", "warn 42")
|
data/test-cmd.rb.gemspec
CHANGED
@@ -5,7 +5,7 @@ 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.9.0"
|
9
9
|
gem.required_ruby_version = ">= 3.0"
|
10
10
|
gem.licenses = ["0BSD"]
|
11
11
|
gem.files = `git ls-files`.split($/)
|
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.9.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-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: test-unit
|
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
|
-
rubygems_version: 3.5.
|
124
|
+
rubygems_version: 3.5.9
|
125
125
|
signing_key:
|
126
126
|
specification_version: 4
|
127
127
|
summary: An object-oriented interface for spawning a process.
|