8000 chore: upgrade to ProcessExecuter 4.x · ruby-git/ruby-git@5b00d3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b00d3b

Browse files
committed
chore: upgrade to ProcessExecuter 4.x
1 parent fb93ef1 commit 5b00d3b

File tree

5 files changed

+28
-54
lines changed

5 files changed

+28
-54
lines changed

git.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
2929

3030
s.add_runtime_dependency 'activesupport', '>= 5.0'
3131
s.add_runtime_dependency 'addressable', '~> 2.8'
32-
s.add_runtime_dependency 'process_executer', '~> 1.3'
32+
s.add_runtime_dependency 'process_executer', '~> 4.0'
3333
s.add_runtime_dependency 'rchardet', '~> 1.9'
3434

3535
s.add_development_dependency 'create_github_release', '~> 2.1'

lib/git/command_line.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ def initialize(env, binary_path, global_opts, logger)
192192
def run(*args, out: nil, err: nil, normalize:, chomp:, merge:, chdir: nil, timeout: nil)
193193
git_cmd = build_git_cmd(args)
194194
begin
195-
result = ProcessExecuter.run(env, *git_cmd, out: out, err: err, merge:, chdir: (chdir || :not_set), timeout: timeout, raise_errors: false)
196-
rescue ProcessExecuter::Command::ProcessIOError => e
195+
options = { chdir: (chdir || :not_set), timeout_after: timeout, raise_errors: false }
196+
options[:out] = out unless out.nil?
197+
options[:err] = err unless err.nil?
198+
options[:merge_output] = merge unless merge.nil?
199+
200+
result = ProcessExecuter.run_with_capture(env, *git_cmd, **options)
201+
rescue ProcessExecuter::ProcessIOError => e
197202
raise Git::ProcessIOError.new(e.message), cause: e.exception.cause
198203
end
199204
process_result(result, normalize, chomp, timeout)
@@ -274,14 +279,10 @@ def post_process_all(raw_outputs, normalize, chomp)
274279
# @api private
275280
#
276281
def post_process(raw_output, normalize, chomp)
277-
if raw_output.respond_to?(:string)
278-
output = raw_output.string.dup
279-
output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize
280-
output.chomp! if chomp
281-
output
282-
else
283-
nil
284-
end
282+
output = raw_output.dup
283+
output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize
284+
output.chomp! if chomp
285+
output
285286
end
286287
end
287288
end

lib/git/command_line_result.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ class CommandLineResult
1919
# result = Git::CommandLineResult.new(git_cmd, status, stdout, stderr)
2020
#
2121
# @param git_cmd [Array<String>] the git command that was executed
22-
# @param status [Process::Status] the status of the process
23-
# @param stdout [String] the output of the process
24-
# @param stderr [String] the error output of the process
22+
# @param status [ProcessExecuter::ResultWithCapture] the status of the process
23+
# @param stdout [String] the processed stdout of the process
24+
# @param stderr [String] the processed stderr of the process
2525
#
2626
def initialize(git_cmd, status, stdout, stderr)
2727
@git_cmd = git_cmd
2828
@status = status
2929
@stdout = stdout
3030
@stderr = stderr
31+
32+
# ProcessExecuter::ResultWithCapture changed the timeout? method to timed_out?
33+
# in version 4.x. This is a compatibility layer to maintain the old method name
34+
# for backward compatibility.
35+
#
36+
status.define_singleton_method(:timeout?) { timed_out? }
3137
end
3238

3339
# @attribute [r] git_cmd

tests/test_helper.rb

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -171,31 +171,6 @@ def windows_platform?
171171
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
172172
end
173173

174-
require 'delegate'
175-
176-
# A wrapper around a ProcessExecuter::Status that also includes command output
177-
# @api public
178-
class CommandResult < SimpleDelegator
179-
# Create a new CommandResult
180-
# @example
181-
# status = ProcessExecuter.spawn(*command, timeout:, out:, err:)
182-
# CommandResult.new(status, out_buffer.string, err_buffer.string)
183-
# @param status [ProcessExecuter::Status] The status of the process
184-
# @param out [String] The standard output of the process
185-
# @param err [String] The standard error of the process
186-
def initialize(status, out, err)
187-
super(status)
188-
@out = out
189-
@err = err
190-
end
191-
192-
# @return [String] The stdout output of the process
193-
attr_reader :out
194-
195-
# @return [String] The stderr output of the process
196-
attr_reader :err
197-
end
198-
199174
# Run a command and return the status including stdout and stderr output
200175
#
201176
# @example
@@ -213,17 +188,12 @@ def initialize(status, out, err)
213188
#
214189
# @return [CommandResult] The result of running
215190
#
216-
def run_command(*command, timeout: nil, raise_errors: true, error_message: "#{command[0]} failed")
217-
out_buffer = StringIO.new
218-
out = ProcessExecuter::MonitoredPipe.new(out_buffer)
219-
err_buffer = StringIO.new
220-
err = ProcessExecuter::MonitoredPipe.new(err_buffer)
221-
222-
status = ProcessExecuter.spawn(*command, timeout: timeout, out: out, err: err)
191+
def run_command(*command, raise_errors: true, error_message: "#{command[0]} failed")
192+
result = ProcessExecuter.run_with_capture(*command, raise_errors: false)
223193

224-
raise "#{error_message}: #{err_buffer.string}" if raise_errors && !status.success?
194+
raise "#{error_message}: #{result.stderr}" if raise_errors && !result.success?
225195

226-
CommandResult.new(status, out_buffer.string, err_buffer.string)
196+
result
227197
end
228198
end
229199

tests/units/test_command_line.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def merge
6161
command_line = Git::CommandLine.new(env, binary_path, global_opts, logger)
6262
args = []
6363
error = assert_raise ArgumentError do
64-
command_line.run(*args, out: out_writer, err: err_writer, normalize: normalize, chomp: chomp, merge: merge, timeout: 'not a number')
64+
command_line.run(*args, normalize: normalize, chomp: chomp, timeout_after: 'not a number')
6565
end
6666
end
6767

@@ -97,7 +97,6 @@ def merge
9797
assert_equal([{}, 'ruby', 'bin/command_line_test', '--stdout=stdout output', '--stderr=stderr output'], result.git_cmd)
9898
assert_equal('stdout output', result.stdout.chomp)
9999
assert_equal('stderr output', result.stderr.chomp)
100-
assert(result.status.is_a? ProcessExecuter::Command::Result)
101100
assert_equal(0, result.status.exitstatus)
102101
end
103102

@@ -239,10 +238,8 @@ def write(*args)
239238
command_line = Git::CommandLine.new(env, binary_path, global_opts, logger)
240239
args = ['--stderr=ERROR: fatal error', '--stdout=STARTING PROCESS']
241240
Tempfile.create do |f|
242-
err_writer = f
243-
result = command_line.run(*args, out: out_writer, err: err_writer, normalize: normalize, chomp: chomp, merge: merge)
244-
f.rewind
245-
assert_equal('ERROR: fatal error', f.read.chomp)
241+
result = command_line.run(*args, normalize: normalize, chomp: chomp, merge: merge)
242+
assert_equal('ERROR: fatal error', result.stderr.chomp)
246243
end
247244
end
248245

0 commit comments

Comments
 (0)
0