test-cmd.rb 0.4.2 → 0.5.1

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: d42f6af0807d07f2fe0aef5e9ca2143e2ae28165c4786393eba628fe0eb11626
4
- data.tar.gz: 834df6cb9fe0c5688e1c60fedbd4643a5114001eb25e51e3ceb074691ac68d07
3
+ metadata.gz: 7dff5c9ec06745d82889cd41c9e6577984c0ed1c92b58b1d69db24abb63bbed8
4
+ data.tar.gz: 705f57c755ac6b5c9f586fd71b8af263dc36ed4b75cb36b86dc776c4612556f0
5
5
  SHA512:
6
- metadata.gz: 4219aa993d546ff5c09081b0f72e5f9a680aa810d1b760e9ed2fabbb951a881c28ea3a92a985c966ca9fdf98ed356760c40ce99acd014f3c8c576a03f0d9ce93
7
- data.tar.gz: c2893ffc88a9682c00fe911771006aa3641e7ec5cbd11fda1d8598f57d091fa74eb69f9046ea67890267b05a5df2cc1f517a9ad967cefe190ed981ec1c65a344
6
+ metadata.gz: c9c84f22a646b7890dcd68e3c9b45e5cd7a0e33807dd1bc05991bfda9b9573d555af9e4060a2fab9edaf73d4822b7e2c4cc4d29aa4d8a1fc08221177b54237bc
7
+ data.tar.gz: f5b2359e1b359fa3e96d8fa558664d082a8226ec8d07758432ce337779f999094ac20cf61408867ea099aab32bff9eafd63766f1dc9c8887b93f062c81ed6512
data/README.md CHANGED
@@ -13,21 +13,14 @@ test environment, too.
13
13
 
14
14
  The following example demonstrates how tests might be written with
15
15
  test-unit from the standard library. The
16
- [`Test::Cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/Cmd.html)
17
- module implements a
18
- [`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/Cmd.html#cmd-class_method)
19
- method that can be included into a
20
- testcase. The
21
- [`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/Cmd.html#cmd-class_method)
22
- method takes the command to run as its first and only argument:
16
+ [`cmd`](https://0x1eef.github.io/x/test-cmd.rb/Test/CmdMixin.html#cmd-instance_method)
17
+ method is given the name of a command, along with any arguments:
23
18
 
24
19
  ```ruby
25
20
  require "test/unit"
26
21
  require "test/cmd"
27
22
 
28
23
  class CmdTest < Test::Unit::TestCase
29
- include Test::Cmd
30
-
31
24
  def test_ruby_stdout
32
25
  assert_equal "foo\n", cmd(%q(ruby -e '$stdout.puts "foo"')).stdout
33
26
  end
@@ -46,6 +39,23 @@ class CmdTest < Test::Unit::TestCase
46
39
  end
47
40
  ```
48
41
 
42
+ ### Builder
43
+
44
+ test-cmd.rb provides an API that is similar to Rust's
45
+ [Command API](https://doc.rust-lang.org/std/process/struct.Command.html).
46
+ <br>
47
+ The
48
+ [API reference](https://0x1eef.github.io/x/test-cmd.rb)
49
+ covers it in more-depth:
50
+
51
+ ``` ruby
52
+ require "test/cmd"
53
+ str = cmd("du")
54
+ .arg("-s").arg("-h")
55
+ .spawn.stdout
56
+ puts str
57
+ ```
58
+
49
59
  ### IO#sync
50
60
 
51
61
  Sometimes it can be neccessary to bypass Ruby's internal buffer and flush
@@ -101,7 +111,7 @@ are available as sources.
101
111
 
102
112
  ``` ruby
103
113
  # Gemfile
104
- gem "test-cmd.rb", github: "0x1eef/test-cmd.rb", tag: "v0.4.2"
114
+ gem "test-cmd.rb", github: "0x1eef/test-cmd.rb", tag: "v0.5.1"
105
115
  ```
106
116
 
107
117
  **Rubygems.org**
data/lib/test-cmd.rb CHANGED
@@ -8,69 +8,118 @@ end unless defined?(Test)
8
8
  # for verifying that when code examples are run they produce the
9
9
  # expected output. The library can be generally useful outside a
10
10
  # test environment, too.
11
- module Test::Cmd
12
- class Result
13
- require "tempfile"
14
- ##
15
- # @return [String]
16
- # Returns the contents of stdout
17
- attr_reader :stdout
11
+ class Test::Cmd
12
+ require "tempfile"
18
13
 
19
- ##
20
- # @return [String]
21
- # Returns the contents of stderr
22
- attr_reader :stderr
23
-
24
- ## @return [Process::Status]
25
- # Returns the status of a process
26
- attr_reader :status
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
27
28
 
28
- ##
29
- # @param [Tempfile] stdout
30
- # @param [Tempfile] stderr
31
- # @param [Process::Status] pstatus
32
- # @return [Test::Cmd::Result]
33
- def initialize(stdout, stderr, pstatus)
34
- @stdout = stdout.tap(&:rewind).read
35
- @stderr = stderr.tap(&:rewind).read
36
- @status = pstatus
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)
37
36
  end
37
+ end
38
38
 
39
- ##
40
- # @return [Integer]
41
- # Returns the exit status of a process
42
- def exit_status
43
- @status.exitstatus
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)
44
46
  end
47
+ end
45
48
 
46
- ##
47
- # Yields each line of stdout when the command
48
- # was successful, or each line of stderr when
49
- # the command was not successful.
50
- #
51
- # @return [Enumerator]
52
- # Returns an Enumerator when a block is not given.
53
- def each_line
54
- return enum_for(:each_line) unless block_given?
55
- io = @status.success? ? @stdout : @stderr
56
- io.each_line.each { yield(_1.chomp) }
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 = $?
57
57
  end
58
+ ensure
59
+ [stdout,stderr]
60
+ end
61
+
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
71
+
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
81
+
82
+ ## @return [Process::Status]
83
+ # Returns the status of a process
84
+ def status
85
+ spawn unless @spawned
86
+ @status
87
+ end
88
+
89
+ ##
90
+ # @return [Integer]
91
+ # Returns the exit status of a process
92
+ def exit_status
93
+ status.exitstatus
58
94
  end
59
95
 
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
60
111
  ##
61
112
  # @param [String] cmd
62
113
  # A command to execute
63
- #
64
- # @return [Test::Cmd::Result]
65
- # Returns an instance of {Test::Cmd::Result Test::Cmd::Result}
66
- def cmd(cmd)
67
- out = Tempfile.new("cmd-stdout").tap(&:unlink)
68
- err = Tempfile.new("cmd-stderr").tap(&:unlink)
69
- Process.wait spawn(cmd, {err:, out:})
70
- Result.new(out, err, $?)
71
- ensure
72
- out.close
73
- err.close
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)
74
120
  end
75
- module_function :cmd
121
+ end
122
+
123
+ module Kernel
124
+ include Test::CmdMixin
76
125
  end
@@ -1,8 +1,6 @@
1
1
  require_relative "setup"
2
2
 
3
3
  class CmdTest < Test::Unit::TestCase
4
- include Test::Cmd
5
-
6
4
  def test_ruby_stdout
7
5
  assert_equal "foo\n", cmd(%q(ruby -e '$stdout.puts "foo"')).stdout
8
6
  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.4.2"
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,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.4.2
4
+ version: 0.5.1
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-07 00:00:00.000000000 Z
11
+ date: 2024-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit