test-cmd.rb 0.7.0 → 0.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a40537381e81f677332c21cfbc95b997fe0f89a4cbf4c714e2eb857da496554
4
- data.tar.gz: 88ceffc1c05ed2b90f79c39c73c6a95adc5ccd9bce97ff435dea3dbea07feb89
3
+ metadata.gz: 6cce30514a659d318e574a7a8c13de222c8632a4ab944847821195fe512e133a
4
+ data.tar.gz: d5a12184b17454efa414a380006c6f240e6747e13220d3b28940c6cf79e7df33
5
5
  SHA512:
6
- metadata.gz: 362b5b2155218c63f01321c279ebb5a7cd204fbdb01af2e76ce3d767d30c4b3a13c2a2eec52de77f7bb80419d37f7fdadb44ce1c3fb97e42d708d916a0eedfa1
7
- data.tar.gz: 055f87c0d765be7ec2612ba9ae3e0f49cdaf2458eac747c8399b8b57dda732dca20d4848d9cc6809393fb3dfbd295cf2d55aec50eee9fbec7f8a63d37ef8a466
6
+ metadata.gz: e5d88ccede58e0d3f23d226481291aa7c97d64ca1a18ab3d620dee57fbf6590f2e0da1cb62962e8ac328a6a91d80b87d7a525136893e55f5ab9c583e6eda81dc
7
+ data.tar.gz: 6190123fe4136ca820e3c1241a32927ee72be094024abb49d0aaa15be9c6955bccdcc396029b2e035629712bd31a8bc5e221dd9db311422e459ea3a2a253da0e
data/.rubocop.yml CHANGED
@@ -17,3 +17,7 @@ AllCops:
17
17
  Style/MultilineIfModifier:
18
18
  Exclude:
19
19
  - 'lib/test-cmd.rb'
20
+
21
+ Layout/MultilineMethodCallIndentation:
22
+ Exclude:
23
+ - 'test/*.rb'
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
- .args("-s", "-h")
51
- .stdout
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,119 +1,11 @@
1
1
  module Test
2
2
  end unless defined?(Test)
3
3
 
4
- ##
5
- # test-cmd.rb is a library for accessing the output streams
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 [String, #to_s] arg
27
- # A command-line argument.
28
- # @return [Test::Cmd]
29
- def arg(arg)
30
- tap do
31
- @argv.push(arg)
32
- end
33
- end
34
-
35
- ##
36
- # @param [Array<String, #to_s>] argv
37
- # One or more command-line arguments.
38
- # @return [Test::Cmd]
39
- def args(*argv)
40
- tap do
41
- @argv.concat(argv)
42
- end
43
- end
44
-
45
- ##
46
- # Spawns a command.
47
- # @return [Test::Cmd]
48
- def spawn
49
- tap do
50
- @spawned = true
51
- Process.wait Process.spawn(@cmd, *@argv, {out: @out, err: @err})
52
- @status = $?
53
- end
54
- ensure
55
- [stdout,stderr]
56
- end
57
-
58
- ##
59
- # @return [String]
60
- # Returns the contents of stdout.
61
- def stdout
62
- spawn unless @spawned
63
- @stdout ||= @out.tap(&:rewind).read
64
- ensure
65
- @out.close unless @out.closed?
66
- end
67
-
68
- ##
69
- # @return [String]
70
- # Returns the contents of stderr.
71
- def stderr
72
- spawn unless @spawned
73
- @stderr ||= @err.tap(&:rewind).read
74
- ensure
75
- @err.close unless @err.closed?
76
- end
77
-
78
- ##
79
- # @return [Process::Status]
80
- # Returns the status of a process
81
- def status
82
- spawn unless @spawned
83
- @status
84
- end
85
-
86
- ##
87
- # @return [Integer]
88
- # Returns the exit status of a process
89
- def exit_status
90
- status.exitstatus
91
- end
92
-
93
- ##
94
- # @param [Symbol] io
95
- # The output stream as a Symbol (:stdout, :stderr).
96
- # @return [Enumerator]
97
- # Returns an Enumerator when a block is not given.
98
- def each_line(io = :stdout)
99
- return enum_for(:each_line, io) unless block_given?
100
- spawn unless @spawned
101
- public_send(io).each_line { yield(_1) }
102
- end
103
-
104
- private
105
-
106
- ##
107
- # @api private
108
- def unlink!(file)
109
- file.tap do
110
- File.chmod(0, file.path)
111
- file.unlink
112
- end
113
- end
4
+ module Test
5
+ require_relative "test/cmd"
114
6
  end
115
7
 
116
- module Test::Cmd::Mixin
8
+ module Kernel
117
9
  ##
118
10
  # @param (see Test::Cmd#initialize)
119
11
  # @return (see Test::Cmd#initialize)
@@ -121,7 +13,3 @@ module Test::Cmd::Mixin
121
13
  Test::Cmd.new(cmd, *argv)
122
14
  end
123
15
  end
124
-
125
- module Kernel
126
- include Test::Cmd::Mixin
127
- end
@@ -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,28 +34,9 @@ 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
37
+ def test_cmd_with_argv
38
+ assert_equal "42\n", cmd("ruby")
39
+ .argv("-e", "warn 42")
40
+ .stderr
56
41
  end
57
42
  end
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.7.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.7.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-04-01 00:00:00.000000000 Z
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.3
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.