test-cmd.rb 0.5.0 → 0.5.1
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/lib/test-cmd.rb +104 -103
- data/test-cmd.rb.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7dff5c9ec06745d82889cd41c9e6577984c0ed1c92b58b1d69db24abb63bbed8
|
4
|
+
data.tar.gz: 705f57c755ac6b5c9f586fd71b8af263dc36ed4b75cb36b86dc776c4612556f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9c84f22a646b7890dcd68e3c9b45e5cd7a0e33807dd1bc05991bfda9b9573d555af9e4060a2fab9edaf73d4822b7e2c4cc4d29aa4d8a1fc08221177b54237bc
|
7
|
+
data.tar.gz: f5b2359e1b359fa3e96d8fa558664d082a8226ec8d07758432ce337779f999094ac20cf61408867ea099aab32bff9eafd63766f1dc9c8887b93f062c81ed6512
|
data/README.md
CHANGED
data/lib/test-cmd.rb
CHANGED
@@ -1,121 +1,122 @@
|
|
1
1
|
module Test
|
2
|
-
|
3
|
-
# test-cmd.rb is a library for accessing the output streams
|
4
|
-
# (both stdout and stderr) of a spawned process. The library was
|
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.
|
9
|
-
class Test::Cmd
|
10
|
-
require "tempfile"
|
2
|
+
end unless defined?(Test)
|
11
3
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
@out = Tempfile.new("cmd-stdout").tap(&:unlink)
|
22
|
-
@err = Tempfile.new("cmd-stderr").tap(&:unlink)
|
23
|
-
@status = nil
|
24
|
-
@spawned = false
|
25
|
-
end
|
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"
|
26
13
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
14
|
+
##
|
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 = Tempfile.new("cmd-stdout").tap(&:unlink)
|
24
|
+
@err = Tempfile.new("cmd-stderr").tap(&:unlink)
|
25
|
+
@status = nil
|
26
|
+
@spawned = false
|
27
|
+
end
|
36
28
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
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)
|
45
36
|
end
|
37
|
+
end
|
46
38
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
@status = $?
|
55
|
-
end
|
56
|
-
ensure
|
57
|
-
[stdout,stderr]
|
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)
|
58
46
|
end
|
47
|
+
end
|
59
48
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
@
|
66
|
-
|
67
|
-
@
|
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 = $?
|
68
57
|
end
|
58
|
+
ensure
|
59
|
+
[stdout,stderr]
|
60
|
+
end
|
69
61
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
79
71
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
86
81
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
82
|
+
## @return [Process::Status]
|
83
|
+
# Returns the status of a process
|
84
|
+
def status
|
85
|
+
spawn unless @spawned
|
86
|
+
@status
|
87
|
+
end
|
93
88
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
# Returns an Enumerator when a block is not given.
|
100
|
-
def each_line
|
101
|
-
return enum_for(:each_line) unless block_given?
|
102
|
-
spawn unless @spawned
|
103
|
-
io = @status.success? ? @stdout : @stderr
|
104
|
-
io.each_line.each { yield(_1.chomp) }
|
105
|
-
end
|
89
|
+
##
|
90
|
+
# @return [Integer]
|
91
|
+
# Returns the exit status of a process
|
92
|
+
def exit_status
|
93
|
+
status.exitstatus
|
106
94
|
end
|
107
95
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
108
|
+
end
|
109
|
+
|
110
|
+
module Test::CmdMixin
|
111
|
+
##
|
112
|
+
# @param [String] cmd
|
113
|
+
# A command to execute
|
114
|
+
# @param [Array<String>] args
|
115
|
+
# An array of command-line arguments.
|
116
|
+
# @return [Test::Cmd]
|
117
|
+
# Returns an instance of {Test::Cmd Test::Cmd}
|
118
|
+
def cmd(cmd, args = [])
|
119
|
+
Test::Cmd.new(cmd, args)
|
119
120
|
end
|
120
121
|
end
|
121
122
|
|
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.1"
|
9
9
|
gem.required_ruby_version = ">= 3.0"
|
10
10
|
gem.licenses = ["0BSD"]
|
11
11
|
gem.files = `git ls-files`.split($/)
|