test-cmd.rb 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9c8ce3140a3961899e285245cbd7d6ceee4caf38508b268ba5500388bfa1aa8
4
- data.tar.gz: 907ef5b303a8edfbdcb79930a40274c6976c79a9f78f5754c456adcff745f1e8
3
+ metadata.gz: c2396593840d674fad470e2e4fcb0f0fc74e55987fe5caec27128457b0608453
4
+ data.tar.gz: 3b6c43870258be5d3c0a1039e5e8436bff314ea05c03dab2866593dafbf43591
5
5
  SHA512:
6
- metadata.gz: 03ab91c5888319ad5d6beb8ab45eac1d4749ba39607e09e5d7866af08027dced706b428c6dd80d077ac53ed4536c594068d87394ffdaaf3c0528d11c5fc690fb
7
- data.tar.gz: eba0d96c1277ab435d51a2a8ce21c6269d98527224d15982a9ed3c967df56e820285b964fe8a984b9ceb6ec1cb468098777c733a220564be0c25399e6e190211
6
+ metadata.gz: edb9931271bfc62a1c22be0717659fa83a670652b97ef7ebe74fe98e2605b21fc4c87647c75aa20c4913a8a76e6bd1d3de7a3d28fe74f75d800293844c2b442e
7
+ data.tar.gz: 3f95d41e84bb4ddf323798dee95e33872e72ff32869f3c1d82f629c00e1b15e840fd93b504f5fd15c7cfa4aa4dd280da172f7edeffd017ec3b486ce7dd2d8ea3
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.2"
115
115
  ```
116
116
 
117
117
  **Rubygems.org**
data/Rakefile.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/setup"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList['test/*_test.rb']
6
+ t.verbose = true
7
+ t.warning = false
8
+ end
9
+ task default: :test
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
- # 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"
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
- # @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
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
- # @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
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
- # @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
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
- # 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]
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
- # @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?
68
- 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
69
81
 
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
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
- ## @return [Process::Status]
81
- # Returns the status of a process
82
- def status
83
- spawn unless @spawned
84
- @status
85
- end
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
- # @return [Integer]
89
- # Returns the exit status of a process
90
- def exit_status
91
- status.exitstatus
92
- 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
93
108
 
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) }
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
- 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
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.0"
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.0
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-30 00:00:00.000000000 Z
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