test-cmd.rb 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/test-cmd.rb +104 -103
  4. data/test-cmd.rb.gemspec +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9c8ce3140a3961899e285245cbd7d6ceee4caf38508b268ba5500388bfa1aa8
4
- data.tar.gz: 907ef5b303a8edfbdcb79930a40274c6976c79a9f78f5754c456adcff745f1e8
3
+ metadata.gz: 7dff5c9ec06745d82889cd41c9e6577984c0ed1c92b58b1d69db24abb63bbed8
4
+ data.tar.gz: 705f57c755ac6b5c9f586fd71b8af263dc36ed4b75cb36b86dc776c4612556f0
5
5
  SHA512:
6
- metadata.gz: 03ab91c5888319ad5d6beb8ab45eac1d4749ba39607e09e5d7866af08027dced706b428c6dd80d077ac53ed4536c594068d87394ffdaaf3c0528d11c5fc690fb
7
- data.tar.gz: eba0d96c1277ab435d51a2a8ce21c6269d98527224d15982a9ed3c967df56e820285b964fe8a984b9ceb6ec1cb468098777c733a220564be0c25399e6e190211
6
+ metadata.gz: c9c84f22a646b7890dcd68e3c9b45e5cd7a0e33807dd1bc05991bfda9b9573d555af9e4060a2fab9edaf73d4822b7e2c4cc4d29aa4d8a1fc08221177b54237bc
7
+ data.tar.gz: f5b2359e1b359fa3e96d8fa558664d082a8226ec8d07758432ce337779f999094ac20cf61408867ea099aab32bff9eafd63766f1dc9c8887b93f062c81ed6512
data/README.md CHANGED
@@ -111,7 +111,7 @@ are available as sources.
111
111
 
112
112
  ``` ruby
113
113
  # Gemfile
114
- gem "test-cmd.rb", github: "0x1eef/test-cmd.rb", tag: "v0.5.0"
114
+ gem "test-cmd.rb", github: "0x1eef/test-cmd.rb", tag: "v0.5.1"
115
115
  ```
116
116
 
117
117
  **Rubygems.org**
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
- # @param [String] cmd
14
- # A command to spawn.
15
- # @param [Array<String>] args
16
- # An array of command-line arguments.
17
- # @return [Test::Cmd]
18
- def initialize(cmd, args = [])
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
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
- # @param [String, #to_s] arg
29
- # A command-line argument.
30
- # @return [Test::Cmd]
31
- def arg(arg)
32
- tap do
33
- @args.push(arg)
34
- end
35
- end
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
- # @param [Array<String, #to_s>] args
39
- # One or more command-line arguments.
40
- # @return [Test::Cmd]
41
- def args(*args)
42
- tap do
43
- @args.concat(args)
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
- # Spawns a command.
49
- # @return [Test::Cmd]
50
- def spawn
51
- tap do
52
- @spawned = true
53
- Process.wait Process.spawn(@cmd, *@args, {out: @out, err: @err})
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
- # @return [String]
62
- # Returns the contents of stdout.
63
- def stdout
64
- spawn unless @spawned
65
- @stdout ||= @out.tap(&:rewind).read
66
- ensure
67
- @out.close unless @out.closed?
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
- # @return [String]
72
- # Returns the contents of stderr.
73
- def stderr
74
- spawn unless @spawned
75
- @stderr ||= @err.tap(&:rewind).read
76
- ensure
77
- @err.close unless @err.closed?
78
- 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
79
71
 
80
- ## @return [Process::Status]
81
- # Returns the status of a process
82
- def status
83
- spawn unless @spawned
84
- @status
85
- end
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
- # @return [Integer]
89
- # Returns the exit status of a process
90
- def exit_status
91
- status.exitstatus
92
- end
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
- # Yields each line of stdout when the command
96
- # was successful, or each line of stderr when
97
- # the command was not successful.
98
- # @return [Enumerator]
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
- module CmdMixin
109
- ##
110
- # @param [String] cmd
111
- # A command to execute
112
- # @param [Array<String>] args
113
- # An array of command-line arguments.
114
- # @return [Test::Cmd]
115
- # Returns an instance of {Test::Cmd Test::Cmd}
116
- def cmd(cmd, args = [])
117
- Test::Cmd.new(cmd, args)
118
- end
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.0"
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($/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test-cmd.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'