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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6c0bc0c0b5e20d51b2455762362ee54301e95c6171a6427dbdf6ea85acdc834
4
- data.tar.gz: 9bcbbe35149e54547b7e01a678de58a068e4a35a0728d454fa613f26d2d9c3ee
3
+ metadata.gz: 92d0cf20a1d63472f4ac178bcb7b74b05d2e5875fe05bccb56f9decbcccd691a
4
+ data.tar.gz: '0318fb0281b874accf7f44c6f4a3c2320d3aa4a3405829a36430347f8d65a9c4'
5
5
  SHA512:
6
- metadata.gz: ec35ba65fefe5c978fe9d467d90f1c9719a984078f2bafb22f1a9e1757ca322533ee99c7f77d4268bbb051986230685058da3da88ca2674567b264d915b5f97b
7
- data.tar.gz: 517746b21cb1edd44a3d96216ae3b842b88acec38c6b3fad389d3748ef8cacc241f4baf205aa1fe285a2aea92c5775e359a04d6fd89f5f9f2e205a7e13784bf8
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.0.0"
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($/)
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: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - robert