File: test_spawn.rb

package info (click to toggle)
ruby-posix-spawn 0.3.13-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 268 kB
  • sloc: ruby: 974; ansic: 312; makefile: 8
file content (398 lines) | stat: -rw-r--r-- 11,284 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
require 'test_helper'

module SpawnImplementationTests
  def test_spawn_simple
    pid = _spawn('true')
    assert_process_exit_ok pid
  end

  def test_spawn_with_args
    pid = _spawn('true', 'with', 'some stuff')
    assert_process_exit_ok pid
  end

  def test_spawn_with_shell
    pid = _spawn('true && exit 13')
    assert_process_exit_status pid, 13
  end

  def test_spawn_with_cmdname_and_argv0_tuple
    pid = _spawn(['true', 'not-true'], 'some', 'args', 'toooo')
    assert_process_exit_ok pid
  end

  def test_spawn_with_invalid_argv
    assert_raises ArgumentError do
      _spawn(['echo','b','c','d'])
    end
  end

  ##
  # Environ

  def test_spawn_inherit_env
    ENV['PSPAWN'] = 'parent'
    pid = _spawn('test "$PSPAWN" = "parent"')
    assert_process_exit_ok pid
  ensure
    ENV.delete('PSPAWN')
  end

  def test_spawn_clean_env
    ENV['PSPAWN'] = 'parent'
    pid = _spawn({'TEMP'=>'child'}, 'test -z "$PSPAWN" && test "$TEMP" = "child"', :unsetenv_others => true)
    assert_process_exit_ok pid
  ensure
    ENV.delete('PSPAWN')
  end

  def test_spawn_set_env
    ENV['PSPAWN'] = 'parent'
    pid = _spawn({'PSPAWN'=>'child'}, 'test "$PSPAWN" = "child"')
    assert_process_exit_ok pid
  ensure
    ENV.delete('PSPAWN')
  end

  def test_spawn_unset_env
    ENV['PSPAWN'] = 'parent'
    pid = _spawn({'PSPAWN'=>nil}, 'test -z "$PSPAWN"')
    assert_process_exit_ok pid
  ensure
    ENV.delete('PSPAWN')
  end

  ##
  # FD => :close options

  def test_sanity_of_checking_clone_with_sh
    rd, wr = IO.pipe
    pid = _spawn("exec 2>/dev/null 9<&#{rd.posix_fileno} || exit 1", rd => rd)
    assert_process_exit_status pid, 0
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  def test_spawn_close_option_with_symbolic_standard_stream_names
    pid = _spawn('true 2>/dev/null 9<&0 || exit 1', :in => :close)
    assert_process_exit_status pid, 1

    pid = _spawn('true 2>/dev/null 9>&1 8>&2 || exit 1',
                 :out => :close, :err => :close)
    assert_process_exit_status pid, 1
  end

  def test_spawn_close_on_standard_stream_io_object
    pid = _spawn('true 2>/dev/null 9<&0 || exit 1', STDIN => :close)
    assert_process_exit_status pid, 1

    pid = _spawn('true 2>/dev/null 9>&1 8>&2 || exit 1',
                 STDOUT => :close, STDOUT => :close)
    assert_process_exit_status pid, 1
  end

  def test_spawn_close_option_with_fd_number
    rd, wr = IO.pipe
    pid = _spawn("true 2>/dev/null 9<&#{rd.posix_fileno} || exit 1", rd.posix_fileno => :close)
    assert_process_exit_status pid, 1

    assert !rd.closed?
    assert !wr.closed?
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  def test_spawn_close_option_with_io_object
    rd, wr = IO.pipe
    pid = _spawn("true 2>/dev/null 9<&#{rd.posix_fileno} || exit 1", rd => :close)
    assert_process_exit_status pid, 1

    assert !rd.closed?
    assert !wr.closed?
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  def test_spawn_close_invalid_fd_raises_exception
    pid = _spawn("echo", "hiya", 250 => :close)
    assert_process_exit_status pid, 127
  rescue Errno::EBADF
    # this happens on darwin only. GNU does spawn and exits 127.
  end

  def test_spawn_invalid_chdir_raises_exception
    pid = _spawn("echo", "hiya", :chdir => "/this/does/not/exist")
    # fspawn does chdir in child, so it exits with 127
    assert_process_exit_status pid, 127
  rescue Errno::ENOENT
    # pspawn and native spawn do chdir in parent, so they throw an exception
  end

  def test_spawn_closing_multiple_fds_with_array_keys
    rd, wr = IO.pipe
    pid = _spawn("true 2>/dev/null 9>&#{wr.posix_fileno} || exit 1", [rd, wr, :out] => :close)
    assert_process_exit_status pid, 1
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  ##
  # FD => FD options

  def test_spawn_redirect_fds_with_symbolic_names_and_io_objects
    rd, wr = IO.pipe
    pid = _spawn("echo", "hello world", :out => wr, rd => :close)
    wr.close
    output = rd.read
    assert_process_exit_ok pid
    assert_equal "hello world\n", output
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  def test_spawn_redirect_fds_with_fd_numbers
    rd, wr = IO.pipe
    pid = _spawn("echo", "hello world", 1 => wr.posix_fileno, rd.posix_fileno => :close)
    wr.close
    output = rd.read
    assert_process_exit_ok pid
    assert_equal "hello world\n", output
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  def test_spawn_redirect_invalid_fds_raises_exception
    pid = _spawn("echo", "hiya", 1 => 250)
    assert_process_exit_status pid, 127
  rescue Errno::EBADF
    # this happens on darwin only. GNU does spawn and exits 127.
  end

  def test_spawn_redirect_stderr_and_stdout_to_same_fd
    rd, wr = IO.pipe
    pid = _spawn("echo hello world 1>&2", :err => wr, :out => wr, rd => :close)
    wr.close
    output = rd.read
    assert_process_exit_ok pid
    assert_equal "hello world\n", output
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  def test_spawn_does_not_close_fd_when_redirecting
    pid = _spawn("exec 2>&1", :err => :out)
    assert_process_exit_ok pid
  end

  # Ruby 1.9 Process::spawn closes all fds by default. To keep an fd open, you
  # have to pass it explicitly as fd => fd.
  def test_explicitly_passing_an_fd_as_open
    rd, wr = IO.pipe
    pid = _spawn("exec 9>&#{wr.posix_fileno} || exit 1", wr => wr)
    assert_process_exit_ok pid
  ensure
    [rd, wr].each { |fd| fd.close rescue nil }
  end

  ##
  # FD => file options

  def test_spawn_redirect_fd_to_file_with_symbolic_name
    file = File.expand_path('../test-output', __FILE__)
    text = 'redirect_fd_to_file_with_symbolic_name'
    pid = _spawn('echo', text, :out => file)
    assert_process_exit_ok pid
    assert File.exist?(file)
    assert_equal "#{text}\n", File.read(file)
  ensure
    File.unlink(file) rescue nil
  end

  def test_spawn_redirect_fd_to_file_with_fd_number
    file = File.expand_path('../test-output', __FILE__)
    text = 'redirect_fd_to_file_with_fd_number'
    pid = _spawn('echo', text, 1 => file)
    assert_process_exit_ok pid
    assert File.exist?(file)
    assert_equal "#{text}\n", File.read(file)
  ensure
    File.unlink(file) rescue nil
  end

  def test_spawn_redirect_fd_to_file_with_io_object
    file = File.expand_path('../test-output', __FILE__)
    text = 'redirect_fd_to_file_with_io_object'
    pid = _spawn('echo', text, STDOUT => file)
    assert_process_exit_ok pid
    assert File.exist?(file)
    assert_equal "#{text}\n", File.read(file)
  ensure
    File.unlink(file) rescue nil
  end

  def test_spawn_redirect_fd_from_file_with_symbolic_name
    file = File.expand_path('../test-input', __FILE__)
    text = 'redirect_fd_from_file_with_symbolic_name'
    File.open(file, 'w') { |fd| fd.write(text) }

    pid = _spawn(%Q{test "$(cat)" = "#{text}"}, :in => file)
    assert_process_exit_ok pid
  ensure
    File.unlink(file) rescue nil
  end

  def test_spawn_redirect_fd_from_file_with_fd_number
    file = File.expand_path('../test-input', __FILE__)
    text = 'redirect_fd_from_file_with_fd_number'
    File.open(file, 'w') { |fd| fd.write(text) }

    pid = _spawn(%Q{test "$(cat)" = "#{text}"}, 0 => file)
    assert_process_exit_ok pid
  ensure
    File.unlink(file) rescue nil
  end

  def test_spawn_redirect_fd_from_file_with_io_object
    file = File.expand_path('../test-input', __FILE__)
    text = 'redirect_fd_from_file_with_io_object'
    File.open(file, 'w') { |fd| fd.write(text) }

    pid = _spawn(%Q{test "$(cat)" = "#{text}"}, STDIN => file)
    assert_process_exit_ok pid
  ensure
    File.unlink(file) rescue nil
  end

  def test_spawn_redirect_fd_to_file_with_symbolic_name_and_flags
    file = File.expand_path('../test-output', __FILE__)
    text = 'redirect_fd_to_file_with_symbolic_name'
    5.times do
        pid = _spawn('echo', text, :out => [file, 'a'])
        assert_process_exit_ok pid
    end
    assert File.exist?(file)
    assert_equal "#{text}\n" * 5, File.read(file)
  ensure
    File.unlink(file) rescue nil
  end

  ##
  # :pgroup => <pgid>

  def test_spawn_inherit_pgroup_from_parent_by_default
    pgrp = Process.getpgrp
    pid = _spawn("ruby", "-e", "exit(Process.getpgrp == #{pgrp} ? 0 : 1)")
    assert_process_exit_ok pid
  end

  def test_spawn_inherit_pgroup_from_parent_when_nil
    pgrp = Process.getpgrp
    pid = _spawn("ruby", "-e", "exit(Process.getpgrp == #{pgrp} ? 0 : 1)", :pgroup => nil)
    assert_process_exit_ok pid
  end

  def test_spawn_new_pgroup_with_true
    pid = _spawn("ruby", "-e", "exit(Process.getpgrp == $$ ? 0 : 1)", :pgroup => true)
    assert_process_exit_ok pid
  end

  def test_spawn_new_pgroup_with_zero
    pid = _spawn("ruby", "-e", "exit(Process.getpgrp == $$ ? 0 : 1)", :pgroup => 0)
    assert_process_exit_ok pid
  end

  def test_spawn_explicit_pgroup
    pgrp = Process.getpgrp
    pid = _spawn("ruby", "-e", "exit(Process.getpgrp == #{pgrp} ? 0 : 1)", :pgroup => pgrp)
    assert_process_exit_ok pid
  end

  ##
  # Exceptions

  def test_spawn_raises_exception_on_unsupported_options
    exception = nil

    assert_raises ArgumentError do
      begin
        _spawn('echo howdy', :out => '/dev/null', :oops => 'blaahh')
      rescue Exception => e
        exception = e
        raise e
      end
    end

    assert_match /oops/, exception.message
  end

  ##
  # Assertion Helpers

  def assert_process_exit_ok(pid)
    assert_process_exit_status pid, 0
  end

  def assert_process_exit_status(pid, status)
    assert pid.to_i > 0, "pid [#{pid}] should be > 0"
    chpid = ::Process.wait(pid)
    assert_equal chpid, pid
    assert_equal status, $?.exitstatus
  end
end

class SpawnTest < Minitest::Test
  include POSIX::Spawn

  def test_spawn_methods_exposed_at_module_level
    assert POSIX::Spawn.respond_to?(:pspawn)
    assert POSIX::Spawn.respond_to?(:_pspawn)
  end

  ##
  # Options Preprocessing

  def test_extract_process_spawn_arguments_with_options
    assert_equal [{}, [['echo', 'echo'], 'hello', 'world'], {:err => :close}],
      extract_process_spawn_arguments('echo', 'hello', 'world', :err => :close)
  end

  def test_extract_process_spawn_arguments_with_options_and_env
    options = {:err => :close}
    env = {'X' => 'Y'}
    assert_equal [env, [['echo', 'echo'], 'hello world'], options],
      extract_process_spawn_arguments(env, 'echo', 'hello world', options)
  end

  def test_extract_process_spawn_arguments_with_shell_command
    assert_equal [{}, [['/bin/sh', '/bin/sh'], '-c', 'echo hello world'], {}],
      extract_process_spawn_arguments('echo hello world')
  end

  def test_extract_process_spawn_arguments_with_special_cmdname_argv_tuple
    assert_equal [{}, [['echo', 'fuuu'], 'hello world'], {}],
      extract_process_spawn_arguments(['echo', 'fuuu'], 'hello world')
  end
end

class PosixSpawnTest < Minitest::Test
  include SpawnImplementationTests
  def _spawn(*argv)
    POSIX::Spawn.pspawn(*argv)
  end
end

class ForkSpawnTest < Minitest::Test
  include SpawnImplementationTests
  def _spawn(*argv)
    POSIX::Spawn.fspawn(*argv)
  end
end

if ::Process::respond_to?(:spawn)
  class NativeSpawnTest < Minitest::Test
    include SpawnImplementationTests
    def _spawn(*argv)
      ::Process.spawn(*argv)
    end
  end
end