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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0790a7e8e68709204aeb42611543db79e3575615d6ed77ee0b166fff7827825
4
- data.tar.gz: 7965cd9baf53560ff8648f0652876b3d31a969528b1419b5b975406ec37c13fc
3
+ metadata.gz: 8f3880288c628fc25dc0a16dcee05222dc776ffb8cab098d2fad3610534ebded
4
+ data.tar.gz: 39573f323c733d4fef70b3e784c3b6c4f956f7da60e0c1bbe7e454d51487f003
5
5
  SHA512:
6
- metadata.gz: f0f7c63d381cbbd8c603c8fc7a1c22e7c73509b0a51965107c2fe2c23adf3c2b8cf5f243712b4f297f90aeb0535c01477ddd4518c10be6afd6a29b248c30af18
7
- data.tar.gz: aee5d21c5948e17b29f0df43636bb18d396d6919efdc3a0fe6c03628ba85d5f3ae0e04cd03e61e51c33ad30aa88a2e7a840051cc704d3ac384957ea75d6f1cd1
6
+ metadata.gz: 3b8e25c54804ac4cfa14f4356a2ea9f3e7b97f994792ae3f77b60e19151d3ee66d84959a40347b82adf1e0b5c277c3d28ef81b85992a5a496881db8e695ca867
7
+ data.tar.gz: face7b9c865c69666ff8eb29393af0ce856777bdf70d617889021f3a8496c0d331be88c527eec3f861b606773d93b1ca70e634c7e08e4a12f60320d57777e5bd
@@ -12,7 +12,7 @@ jobs:
12
12
  fail-fast: false
13
13
  matrix:
14
14
  os: [ubuntu-latest, macos-latest]
15
- ruby: [3.1, 3.2, 3.3]
15
+ ruby: [3.1, 3.2, 3.3, 4.0]
16
16
  runs-on: ${{ matrix.os }}
17
17
  steps:
18
18
  - uses: actions/checkout@v2
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 chars is read from both:
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
- require "test-cmd"
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
- .new("ruby", "-e", "puts ENV['FOO']")
83
- .env("FOO" => "42")
84
- .stdout # => "42\n"
103
+ .new("env")
104
+ .env({})
105
+ .stdout
85
106
  ```
86
107
  </details>
87
108
 
@@ -4,5 +4,5 @@ end unless defined?(Test)
4
4
  class Test::Command
5
5
  ##
6
6
  # @string [String]
7
- VERSION = "2.5.0"
7
+ VERSION = "2.6.0"
8
8
  end
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
- @env,
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
  ##
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.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson