test-cmd.rb 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/Rakefile.rb +9 -0
- data/lib/test-cmd.rb +113 -101
- data/test-cmd.rb.gemspec +2 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2396593840d674fad470e2e4fcb0f0fc74e55987fe5caec27128457b0608453
|
4
|
+
data.tar.gz: 3b6c43870258be5d3c0a1039e5e8436bff314ea05c03dab2866593dafbf43591
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edb9931271bfc62a1c22be0717659fa83a670652b97ef7ebe74fe98e2605b21fc4c87647c75aa20c4913a8a76e6bd1d3de7a3d28fe74f75d800293844c2b442e
|
7
|
+
data.tar.gz: 3f95d41e84bb4ddf323798dee95e33872e72ff32869f3c1d82f629c00e1b15e840fd93b504f5fd15c7cfa4aa4dd280da172f7edeffd017ec3b486ce7dd2d8ea3
|
data/README.md
CHANGED
data/Rakefile.rb
ADDED
data/lib/test-cmd.rb
CHANGED
@@ -1,121 +1,133 @@
|
|
1
1
|
module Test
|
2
|
+
end unless defined?(Test)
|
3
|
+
|
4
|
+
##
|
5
|
+
# test-cmd.rb is a library for accessing the output streams
|
6
|
+
# (both stdout and stderr) of a spawned process. The library was
|
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.
|
11
|
+
class Test::Cmd
|
12
|
+
require "tempfile"
|
13
|
+
|
2
14
|
##
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
|
15
|
+
# @param [String] cmd
|
16
|
+
# A command to spawn.
|
17
|
+
# @param [Array<String>] args
|
18
|
+
# An array of command-line arguments.
|
19
|
+
# @return [Test::Cmd]
|
20
|
+
def initialize(cmd, args = [])
|
21
|
+
@cmd = cmd
|
22
|
+
@args = args.dup
|
23
|
+
@out = unlink!(Tempfile.new("cmd-stdout"))
|
24
|
+
@err = unlink!(Tempfile.new("cmd-stderr"))
|
25
|
+
@status = nil
|
26
|
+
@spawned = false
|
27
|
+
end
|
11
28
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
@cmd = cmd
|
20
|
-
@args = args.dup
|
21
|
-
@out = Tempfile.new("cmd-stdout").tap(&:unlink)
|
22
|
-
@err = Tempfile.new("cmd-stderr").tap(&:unlink)
|
23
|
-
@status = nil
|
24
|
-
@spawned = false
|
29
|
+
##
|
30
|
+
# @param [String, #to_s] arg
|
31
|
+
# A command-line argument.
|
32
|
+
# @return [Test::Cmd]
|
33
|
+
def arg(arg)
|
34
|
+
tap do
|
35
|
+
@args.push(arg)
|
25
36
|
end
|
37
|
+
end
|
26
38
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
39
|
+
##
|
40
|
+
# @param [Array<String, #to_s>] args
|
41
|
+
# One or more command-line arguments.
|
42
|
+
# @return [Test::Cmd]
|
43
|
+
def args(*args)
|
44
|
+
tap do
|
45
|
+
@args.concat(args)
|
35
46
|
end
|
47
|
+
end
|
36
48
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
49
|
+
##
|
50
|
+
# Spawns a command.
|
51
|
+
# @return [Test::Cmd]
|
52
|
+
def spawn
|
53
|
+
tap do
|
54
|
+
@spawned = true
|
55
|
+
Process.wait Process.spawn(@cmd, *@args, {out: @out, err: @err})
|
56
|
+
@status = $?
|
45
57
|
end
|
58
|
+
ensure
|
59
|
+
[stdout,stderr]
|
60
|
+
end
|
46
61
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
ensure
|
57
|
-
[stdout,stderr]
|
58
|
-
end
|
62
|
+
##
|
63
|
+
# @return [String]
|
64
|
+
# Returns the contents of stdout.
|
65
|
+
def stdout
|
66
|
+
spawn unless @spawned
|
67
|
+
@stdout ||= @out.tap(&:rewind).read
|
68
|
+
ensure
|
69
|
+
@out.close unless @out.closed?
|
70
|
+
end
|
59
71
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
72
|
+
##
|
73
|
+
# @return [String]
|
74
|
+
# Returns the contents of stderr.
|
75
|
+
def stderr
|
76
|
+
spawn unless @spawned
|
77
|
+
@stderr ||= @err.tap(&:rewind).read
|
78
|
+
ensure
|
79
|
+
@err.close unless @err.closed?
|
80
|
+
end
|
69
81
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
ensure
|
77
|
-
@err.close unless @err.closed?
|
78
|
-
end
|
82
|
+
## @return [Process::Status]
|
83
|
+
# Returns the status of a process
|
84
|
+
def status
|
85
|
+
spawn unless @spawned
|
86
|
+
@status
|
87
|
+
end
|
79
88
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
89
|
+
##
|
90
|
+
# @return [Integer]
|
91
|
+
# Returns the exit status of a process
|
92
|
+
def exit_status
|
93
|
+
status.exitstatus
|
94
|
+
end
|
86
95
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
96
|
+
##
|
97
|
+
# Yields each line of stdout when the command
|
98
|
+
# was successful, or each line of stderr when
|
99
|
+
# the command was not successful.
|
100
|
+
# @return [Enumerator]
|
101
|
+
# Returns an Enumerator when a block is not given.
|
102
|
+
def each_line
|
103
|
+
return enum_for(:each_line) unless block_given?
|
104
|
+
spawn unless @spawned
|
105
|
+
io = @status.success? ? @stdout : @stderr
|
106
|
+
io.each_line.each { yield(_1.chomp) }
|
107
|
+
end
|
93
108
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
spawn unless @spawned
|
103
|
-
io = @status.success? ? @stdout : @stderr
|
104
|
-
io.each_line.each { yield(_1.chomp) }
|
109
|
+
private
|
110
|
+
|
111
|
+
##
|
112
|
+
# @api private
|
113
|
+
def unlink!(file)
|
114
|
+
file.tap do
|
115
|
+
File.chmod(0000, file.path)
|
116
|
+
file.unlink
|
105
117
|
end
|
106
118
|
end
|
119
|
+
end
|
107
120
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
end
|
121
|
+
module Test::CmdMixin
|
122
|
+
##
|
123
|
+
# @param [String] cmd
|
124
|
+
# A command to execute
|
125
|
+
# @param [Array<String>] args
|
126
|
+
# An array of command-line arguments.
|
127
|
+
# @return [Test::Cmd]
|
128
|
+
# Returns an instance of {Test::Cmd Test::Cmd}
|
129
|
+
def cmd(cmd, args = [])
|
130
|
+
Test::Cmd.new(cmd, args)
|
119
131
|
end
|
120
132
|
end
|
121
133
|
|
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.5.
|
8
|
+
gem.version = "0.5.2"
|
9
9
|
gem.required_ruby_version = ">= 3.0"
|
10
10
|
gem.licenses = ["0BSD"]
|
11
11
|
gem.files = `git ls-files`.split($/)
|
@@ -18,4 +18,5 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.add_development_dependency "yard", "~> 0.9"
|
19
19
|
gem.add_development_dependency "redcarpet", "~> 3.5"
|
20
20
|
gem.add_development_dependency "standard", "~> 1.24"
|
21
|
+
gem.add_development_dependency "rake", "~> 13.1"
|
21
22
|
end
|
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.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- '0x1eef'
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-01
|
11
|
+
date: 2024-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: test-unit
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.24'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '13.1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '13.1'
|
69
83
|
description: test-cmd.rb provides access to the output streams (both stdout and stderr)
|
70
84
|
of a spawned process.
|
71
85
|
email:
|
@@ -82,6 +96,7 @@ files:
|
|
82
96
|
- Gemfile
|
83
97
|
- LICENSE
|
84
98
|
- README.md
|
99
|
+
- Rakefile.rb
|
85
100
|
- lib/test-cmd.rb
|
86
101
|
- lib/test/cmd.rb
|
87
102
|
- test-cmd.rb.gemspec
|