Skip to content

Commit 60b58ba

Browse files
committed
test: add #run_command for tests to use instead of backticks
1 parent 185c3f5 commit 60b58ba

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

tests/test_helper.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,59 @@ def windows_platform?
162162
win_platform_regex = /mingw|mswin/
163163
RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex
164164
end
165+
166+
require 'delegate'
167+
168+
# A wrapper around a ProcessExecuter::Status that also includes command output
169+
# @api public
170+
class CommandResult < SimpleDelegator
171+
# Create a new CommandResult
172+
# @example
173+
# status = ProcessExecuter.spawn(*command, timeout:, out:, err:)
174+
# CommandResult.new(status, out_buffer.string, err_buffer.string)
175+
# @param status [ProcessExecuter::Status] The status of the process
176+
# @param out [String] The standard output of the process
177+
# @param err [String] The standard error of the process
178+
def initialize(status, out, err)
179+
super(status)
180+
@out = out
181+
@err = err
182+
end
183+
184+
# @return [String] The stdout output of the process
185+
attr_reader :out
186+
187+
# @return [String] The stderr output of the process
188+
attr_reader :err
189+
end
190+
191+
# Run a command and return the status including stdout and stderr output
192+
#
193+
# @example
194+
# command = %w[git status]
195+
# status = run(command)
196+
# status.success? # => true
197+
# status.exitstatus # => 0
198+
# status.out # => "On branch master\nnothing to commit, working tree clean\n"
199+
# status.err # => ""
200+
#
201+
# @param command [Array<String>] The command to run
202+
# @param timeout [Numeric, nil] Seconds to allow command to run before killing it or nil for no timeout
203+
# @param raise_errors [Boolean] Raise an exception if the command fails
204+
# @param error_message [String] The message to use when raising an exception
205+
#
206+
# @return [CommandResult] The result of running
207+
#
208+
def run_command(*command, timeout: nil, raise_errors: true, error_message: "#{command[0]} failed")
209+
out_buffer = StringIO.new
210+
out = ProcessExecuter::MonitoredPipe.new(out_buffer)
211+
err_buffer = StringIO.new
212+
err = ProcessExecuter::MonitoredPipe.new(err_buffer)
213+
214+
status = ProcessExecuter.spawn(*command, timeout: timeout, out: out, err: err)
215+
216+
raise "#{error_message}: #{err_buffer.string}" if raise_errors && !status.success?
217+
218+
CommandResult.new(status, out_buffer.string, err_buffer.string)
219+
end
165220
end

tests/units/test_branch.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ def setup
5050
end
5151
end
5252

53+
test 'Git::Base#branches when checked out branch is a remote branch' do
54+
in_temp_dir do
55+
Dir.mkdir('remote_git')
56+
Dir.chdir('remote_git') do
57+
run_command 'git', 'init', '--initial-branch=main'
58+
File.write('file1.txt', 'This is file1')
59+
run_command 'git', 'add', 'file1.txt'
60+
run_command 'git', 'commit', '-m', 'Add file1.txt'
61+
end
62+
63+
run_command 'git', 'clone', File.join('remote_git', '.git'), 'local_git'
64+
65+
Dir.chdir('local_git') do
66+
run_command 'git', 'checkout', 'origin/main'
67+
git = Git.open('.')
68+
assert_nothing_raised { git.branches }
69+
end
70+
end
71+
end
72+
5373
# Git::Lib#current_branch_state
5474

5575
test 'Git::Lib#current_branch_state -- empty repository' do

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy