test-cmd.rb 2.5.0 → 2.6.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/.github/workflows/specs.yml +1 -1
- data/README.md +27 -6
- data/lib/test/command/version.rb +1 -1
- data/lib/test/command.rb +26 -3
- data/test/cmd_test.rb +35 -0
- 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: 8f3880288c628fc25dc0a16dcee05222dc776ffb8cab098d2fad3610534ebded
|
|
4
|
+
data.tar.gz: 39573f323c733d4fef70b3e784c3b6c4f956f7da60e0c1bbe7e454d51487f003
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3b8e25c54804ac4cfa14f4356a2ea9f3e7b97f994792ae3f77b60e19151d3ee66d84959a40347b82adf1e0b5c277c3d28ef81b85992a5a496881db8e695ca867
|
|
7
|
+
data.tar.gz: face7b9c865c69666ff8eb29393af0ce856777bdf70d617889021f3a8496c0d331be88c527eec3f861b606773d93b1ca70e634c7e08e4a12f60320d57777e5bd
|
data/.github/workflows/specs.yml
CHANGED
data/README.md
CHANGED
|
@@ -34,7 +34,6 @@ test-cmd.rb can be installed via rubygems.org:
|
|
|
34
34
|
|
|
35
35
|
### Commands
|
|
36
36
|
|
|
37
|
-
|
|
38
37
|
```ruby
|
|
39
38
|
require "test-cmd"
|
|
40
39
|
|
|
@@ -63,7 +62,7 @@ p Test::Command
|
|
|
63
62
|
|
|
64
63
|
##
|
|
65
64
|
# Limit how much data is read from stdout and
|
|
66
|
-
# stderr. At most 20k
|
|
65
|
+
# stderr. At most 20k bytes is read from both:
|
|
67
66
|
p Test::Command
|
|
68
67
|
.new("git")
|
|
69
68
|
.limit(stdout: 20_000, stderr: 20_000)
|
|
@@ -75,13 +74,35 @@ p Test::Command
|
|
|
75
74
|
<summary>Environment</summary>
|
|
76
75
|
<br>
|
|
77
76
|
|
|
77
|
+
A command that is not given an environment inherits the parent's
|
|
78
|
+
environment. When `env` is called, only the variables that are
|
|
79
|
+
given to `env` are set on the spawned process, alongside `$PATH`
|
|
80
|
+
which is always inherited. `$PATH` is inherited so that the
|
|
81
|
+
command can still be found via path lookup.
|
|
82
|
+
|
|
78
83
|
```ruby
|
|
79
|
-
|
|
84
|
+
##
|
|
85
|
+
# Inherit the parent's environment. All variables
|
|
86
|
+
# from the parent process are available.
|
|
87
|
+
p Test::Command
|
|
88
|
+
.new("env")
|
|
89
|
+
.stdout
|
|
80
90
|
|
|
91
|
+
##
|
|
92
|
+
# Only $FOO and $BAR are set, plus the inherited
|
|
93
|
+
# $PATH. Everything else is left behind.
|
|
94
|
+
p Test::Command
|
|
95
|
+
.new("env")
|
|
96
|
+
.env("FOO" => "42", "BAR" => "7")
|
|
97
|
+
.stdout
|
|
98
|
+
|
|
99
|
+
##
|
|
100
|
+
# An empty hash yields only $PATH and the rest of
|
|
101
|
+
# environment is left empty.
|
|
81
102
|
p Test::Command
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
103
|
+
.new("env")
|
|
104
|
+
.env({})
|
|
105
|
+
.stdout
|
|
85
106
|
```
|
|
86
107
|
</details>
|
|
87
108
|
|
data/lib/test/command/version.rb
CHANGED
data/lib/test/command.rb
CHANGED
|
@@ -28,7 +28,7 @@ class Test::Command
|
|
|
28
28
|
def initialize(cmd, *argv)
|
|
29
29
|
@cmd = cmd
|
|
30
30
|
@argv = argv.dup
|
|
31
|
-
@env =
|
|
31
|
+
@env = nil
|
|
32
32
|
@status = nil
|
|
33
33
|
@spawned = false
|
|
34
34
|
@stdout = ""
|
|
@@ -50,7 +50,7 @@ class Test::Command
|
|
|
50
50
|
# Environment variables to set for the spawned command
|
|
51
51
|
# @return [Test::Command]
|
|
52
52
|
def env(env)
|
|
53
|
-
tap { @env.merge!(env) }
|
|
53
|
+
tap { (@env ||= {}).merge!(env) }
|
|
54
54
|
end
|
|
55
55
|
|
|
56
56
|
##
|
|
@@ -91,7 +91,7 @@ class Test::Command
|
|
|
91
91
|
@spawned = true
|
|
92
92
|
@out, @err = Pipe.pair, Pipe.pair
|
|
93
93
|
@pid = Process.spawn(
|
|
94
|
-
|
|
94
|
+
envp,
|
|
95
95
|
@cmd, *@argv.map(&:to_s),
|
|
96
96
|
{in: @in_r, out: @out.w, err: @err.w}
|
|
97
97
|
)
|
|
@@ -246,6 +246,29 @@ class Test::Command
|
|
|
246
246
|
|
|
247
247
|
private
|
|
248
248
|
|
|
249
|
+
##
|
|
250
|
+
# Builds the environment passed to Process.spawn.
|
|
251
|
+
# The spawned process receives only the variables
|
|
252
|
+
# set with #env: every inherited variable is
|
|
253
|
+
# cleared, so an empty @env spawns with an empty
|
|
254
|
+
# environment.
|
|
255
|
+
# @return [Hash{String => String}]
|
|
256
|
+
def envp
|
|
257
|
+
if @env.nil?
|
|
258
|
+
##
|
|
259
|
+
# Inherit the parent's environment
|
|
260
|
+
ENV.to_h
|
|
261
|
+
else
|
|
262
|
+
##
|
|
263
|
+
# Clear all environment variables except those
|
|
264
|
+
# that are set by `@env`. Always inherit ${PATH}.
|
|
265
|
+
envp = ENV.except("PATH")
|
|
266
|
+
.each_key
|
|
267
|
+
.to_h { |key| [key, nil] }
|
|
268
|
+
envp.merge!(@env)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
249
272
|
##
|
|
250
273
|
# Reads up to +max+ bytes from a stream, then drains
|
|
251
274
|
# the remainder so a child writing more than the limit
|
data/test/cmd_test.rb
CHANGED
|
@@ -38,6 +38,41 @@ class Test::Command
|
|
|
38
38
|
.env("FOO" => "42")
|
|
39
39
|
.stdout
|
|
40
40
|
end
|
|
41
|
+
|
|
42
|
+
def test_ruby_env_only_given_keys
|
|
43
|
+
assert_equal "BAR\nFOO\nPATH\n",
|
|
44
|
+
::Test::Command.new("ruby", "-e", "puts ENV.keys.sort")
|
|
45
|
+
.env("FOO" => "42", "BAR" => "7")
|
|
46
|
+
.stdout
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def test_ruby_env_empty_is_empty_env
|
|
50
|
+
assert_equal "PATH",
|
|
51
|
+
::Test::Command.new("ruby", "-e", "print ENV.keys.sort.join(\",\")")
|
|
52
|
+
.env({})
|
|
53
|
+
.stdout
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_ruby_env_does_not_leak_inherited
|
|
57
|
+
assert_equal "true",
|
|
58
|
+
::Test::Command.new("ruby", "-e", "print ENV.key?('PATH')")
|
|
59
|
+
.stdout
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_ruby_env_path_carries_over_inherited
|
|
63
|
+
parent_path = ENV["PATH"]
|
|
64
|
+
assert_equal parent_path,
|
|
65
|
+
::Test::Command.new("ruby", "-e", "print ENV['PATH']")
|
|
66
|
+
.stdout
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_ruby_env_path_carries_over_empty
|
|
70
|
+
parent_path = ENV["PATH"]
|
|
71
|
+
assert_equal parent_path,
|
|
72
|
+
::Test::Command.new("ruby", "-e", "print ENV['PATH']")
|
|
73
|
+
.env({})
|
|
74
|
+
.stdout
|
|
75
|
+
end
|
|
41
76
|
end
|
|
42
77
|
|
|
43
78
|
##
|