Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/process_executer/destinations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def self.matching_destination_class(destination)
destination_classes =
ProcessExecuter::Destinations.constants
.map { |const| ProcessExecuter::Destinations.const_get(const) }
.select { |const| const.is_a?(Class) }
.grep(Class)
.reject { |klass| klass == ProcessExecuter::Destinations::DestinationBase }

destination_classes.find { |klass| klass.handles?(destination) }
Expand Down
4 changes: 2 additions & 2 deletions lib/process_executer/options/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def initialize(**options_hash)
#
def allowed_options
@allowed_options ||=
define_options.each_with_object({}) do |option, hash|
hash[option.name] = option
define_options.to_h do |option|
[option.name, option]
end.freeze
end

Expand Down
83 changes: 44 additions & 39 deletions spec/process_executer/monitored_pipe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
COMMAND
Dir.mktmpdir do |dir|
path = File.join(dir, 'output.txt')
f = File.open(path, 'w', 0o644)
fd = f.fileno
monitored_pipe = ProcessExecuter::MonitoredPipe.new(fd)
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close
f.close
expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
File.open(path, 'w', 0o644) do |f|
fd = f.fileno
monitored_pipe = ProcessExecuter::MonitoredPipe.new(fd)
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close

expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
end
expect(File.read(path).gsub("\r\n", "\n")).to eq("stdout output\n")
end
end
Expand Down Expand Up @@ -91,16 +92,15 @@

Dir.mktmpdir do |dir|
path = File.join(dir, 'output.txt')
f = File.open(path, 'w', 0o644)
monitored_pipe = ProcessExecuter::MonitoredPipe.new(f)
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close

expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
expect(f.closed?).to eq(false)
File.open(path, 'w', 0o644) do |f|
monitored_pipe = ProcessExecuter::MonitoredPipe.new(f)
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close

f.close
expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
expect(f.closed?).to eq(false)
end

expect(File.read(path).gsub("\r\n", "\n")).to eq("stdout output\n")
end
Expand Down Expand Up @@ -140,16 +140,18 @@
Dir.mktmpdir do |dir|
filepath = File.join(dir, 'output.txt')

f = File.open(filepath, 'w', 0o600)
f.puts 'initial content'
f.close
File.open(filepath, 'w', 0o600) do |f|
f.puts 'initial content'
f.close

monitored_pipe = ProcessExecuter::MonitoredPipe.new(filepath)
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close
monitored_pipe = ProcessExecuter::MonitoredPipe.new(filepath)
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close

expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
end

expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
unless windows?
file_permissions = File.stat(filepath).mode & 0o7777
expect(file_permissions).to eq(0o600)
Expand Down Expand Up @@ -477,19 +479,21 @@
Dir.mktmpdir do |dir|
destination1 = File.join(dir, 'output1.txt')
filepath2 = File.join(dir, 'output2.txt')
destination2 = File.open(filepath2, 'w', 0o644)
destination3 = StringIO.new
monitored_pipe = ProcessExecuter::MonitoredPipe.new([:tee, destination1, destination2, destination3])

command = ruby_command(<<~COMMAND)
puts 'stdout output'
COMMAND
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close
destination2.close
File.open(filepath2, 'w', 0o644) do |destination2|
monitored_pipe = ProcessExecuter::MonitoredPipe.new([:tee, destination1, destination2, destination3])

command = ruby_command(<<~COMMAND)
puts 'stdout output'
COMMAND
_pid, status = Process.wait2(Process.spawn(*command, out: monitored_pipe))
monitored_pipe.close

expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
end

expect(monitored_pipe.exception).to be_nil
expect(status.exitstatus).to eq(0)
expect(File.read(destination1).gsub("\r\n", "\n")).to eq("stdout output\n")
expect(File.read(filepath2).gsub("\r\n", "\n")).to eq("stdout output\n")
expect(destination3.string.gsub("\r\n", "\n")).to eq("stdout output\n")
Expand Down Expand Up @@ -578,10 +582,11 @@
it 'should write to the file descriptor' do
Dir.mktmpdir do |dir|
path = File.join(dir, 'output.txt')
file = File.open(path, 'w')
pid = Process.spawn('echo hello', out: file.fileno)
Process.wait(pid)
file.close
File.open(path, 'w') do |file|
pid = Process.spawn('echo hello', out: file.fileno)
Process.wait(pid)
end

expect(File.read(path)).to eq("hello\n")
end
end
Expand Down
16 changes: 9 additions & 7 deletions spec/process_executer_run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,11 @@
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
out_buffer = StringIO.new
out_file = File.open('stdout.txt', 'w')
ProcessExecuter.run(*command, out: [:tee, out_buffer, out_file])
out_file.close

File.open('stdout.txt', 'w') do |out_file|
ProcessExecuter.run(*command, out: [:tee, out_buffer, out_file])
end

expect(out_buffer.string.gsub("\r\n", "\n")).to eq("Test output\n")
expect(File.read('stdout.txt').gsub("\r\n", "\n")).to eq("Test output\n")
end
Expand Down Expand Up @@ -619,10 +621,10 @@

Dir.mktmpdir do |dir|
Dir.chdir(dir) do
file = File.open('output.txt', 'w')
options = { out: [:child, 6], err: [:child, 6], 6 => file }
ProcessExecuter.run(*command, **options)
file.close
File.open('output.txt', 'w') do |file|
options = { out: [:child, 6], err: [:child, 6], 6 => file }
ProcessExecuter.run(*command, **options)
end

expect(File.read('output.txt').gsub("\r\n", "\n")).to match(/^stdout output\n/)
expect(File.read('output.txt').gsub("\r\n", "\n")).to match(/^stderr output\n/)
Expand Down