test-cmd.rb 2.0.0 → 2.1.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 +4 -4
- data/README.md +22 -1
- data/lib/test/cmd.rb +10 -0
- data/test/cmd_test.rb +11 -0
- 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: 92d0cf20a1d63472f4ac178bcb7b74b05d2e5875fe05bccb56f9decbcccd691a
|
|
4
|
+
data.tar.gz: '0318fb0281b874accf7f44c6f4a3c2320d3aa4a3405829a36430347f8d65a9c4'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4daa335326b92120051a40666c07d8b731f447b45e01a73bc5e1cbb2eac65d53e4e4bb87d3ded462b6375b86e85bbe15419d1b8e64642c5f1a60659427456401
|
|
7
|
+
data.tar.gz: '0859457b884abaafc8ea53ecc2fd238c4c111b2f6175f177d85ab48b1378de7bc81556160a06433ba900e59d0513b7e910e060944f99fb7d8db6b57182e5e15f'
|
data/README.md
CHANGED
|
@@ -36,14 +36,35 @@ A command is created with
|
|
|
36
36
|
which takes the name or path of a command, and given additional
|
|
37
37
|
arguments with
|
|
38
38
|
[`Test::Command#argv`](https://r.uby.dev/api-docs/test-cmd.rb/Test/Command.html#argv-instance_method).
|
|
39
|
+
Environment variables can be set for the spawned process with
|
|
40
|
+
[`Test::Command#env`](https://r.uby.dev/api-docs/test-cmd.rb/Test/Command.html#env-instance_method).
|
|
39
41
|
The instance has access to the command's process ID, exit status,
|
|
40
42
|
standard output stream, and standard error stream.
|
|
41
43
|
|
|
42
44
|
```ruby
|
|
43
45
|
require "test-cmd"
|
|
44
|
-
Test::Command.new("ls").argv("-l").stdout
|
|
46
|
+
puts Test::Command.new("ls").argv("-l").stdout
|
|
47
|
+
puts Test::Command.new("env").env("FOO" => "bar").stdout
|
|
45
48
|
```
|
|
46
49
|
|
|
50
|
+
<details>
|
|
51
|
+
<summary>Environment</summary>
|
|
52
|
+
<br>
|
|
53
|
+
|
|
54
|
+
Environment variables for the spawned process are set with
|
|
55
|
+
[`Test::Command#env`](https://r.uby.dev/api-docs/test-cmd.rb/Test/Command.html#env-instance_method),
|
|
56
|
+
which merges the given variables into the child's environment and
|
|
57
|
+
returns the command for chaining.
|
|
58
|
+
|
|
59
|
+
```ruby
|
|
60
|
+
require "test-cmd"
|
|
61
|
+
puts Test::Command
|
|
62
|
+
.new("ruby", "-e", "puts ENV['FOO']")
|
|
63
|
+
.env("FOO" => "42")
|
|
64
|
+
.stdout # => "42\n"
|
|
65
|
+
```
|
|
66
|
+
</details>
|
|
67
|
+
|
|
47
68
|
<details>
|
|
48
69
|
<summary>Callbacks</summary>
|
|
49
70
|
<br>
|
data/lib/test/cmd.rb
CHANGED
|
@@ -26,6 +26,7 @@ class Test::Command
|
|
|
26
26
|
def initialize(cmd, *argv)
|
|
27
27
|
@cmd = cmd
|
|
28
28
|
@argv = argv.dup
|
|
29
|
+
@env = {}
|
|
29
30
|
@status = nil
|
|
30
31
|
@spawned = false
|
|
31
32
|
@stdout = ""
|
|
@@ -41,6 +42,14 @@ class Test::Command
|
|
|
41
42
|
tap { @argv.concat(argv) }
|
|
42
43
|
end
|
|
43
44
|
|
|
45
|
+
##
|
|
46
|
+
# @param [Hash{String => String}] env
|
|
47
|
+
# Environment variables to set for the spawned command
|
|
48
|
+
# @return [Test::Command]
|
|
49
|
+
def env(env)
|
|
50
|
+
tap { @env.merge!(env) }
|
|
51
|
+
end
|
|
52
|
+
|
|
44
53
|
##
|
|
45
54
|
# Spawns a command
|
|
46
55
|
# @return [Test::Command]
|
|
@@ -57,6 +66,7 @@ class Test::Command
|
|
|
57
66
|
# streams with whole-buffer reads rather than a byte at
|
|
58
67
|
# a time.
|
|
59
68
|
@pid = Process.spawn(
|
|
69
|
+
@env,
|
|
60
70
|
@cmd, *@argv,
|
|
61
71
|
{out: @out.w, err: @err.w, in: IO::NULL}
|
|
62
72
|
)
|
data/test/cmd_test.rb
CHANGED
|
@@ -21,6 +21,17 @@ class Test::Command
|
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
+
##
|
|
25
|
+
# Test::Command#env
|
|
26
|
+
class EnvTest < Test
|
|
27
|
+
def test_ruby_env
|
|
28
|
+
assert_equal "42\n",
|
|
29
|
+
::Test::Command.new("ruby", "-e", "puts ENV['FOO']")
|
|
30
|
+
.env("FOO" => "42")
|
|
31
|
+
.stdout
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
24
35
|
##
|
|
25
36
|
# Test::Command#{exit_status, status, success?}
|
|
26
37
|
class ExitStatusTest < Test
|
data/test-cmd.rb.gemspec
CHANGED
|
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
|
5
5
|
gem.authors = ["robert"]
|
|
6
6
|
gem.email = ["robert@r.uby.dev"]
|
|
7
7
|
gem.homepage = "https://github.com/0x1eef/test-cmd.rb#readme"
|
|
8
|
-
gem.version = "2.
|
|
8
|
+
gem.version = "2.1.0"
|
|
9
9
|
gem.required_ruby_version = ">= 3.0"
|
|
10
10
|
gem.licenses = ["0BSD"]
|
|
11
11
|
gem.files = `git ls-files`.split($/)
|