diff --git a/Gemfile b/Gemfile index 074c7b5..7f4f5e9 100644 --- a/Gemfile +++ b/Gemfile @@ -2,5 +2,4 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in ruby_git.gemspec gemspec diff --git a/lib/ruby_git.rb b/lib/ruby_git.rb index 9c6e0e6..1e5317e 100644 --- a/lib/ruby_git.rb +++ b/lib/ruby_git.rb @@ -160,9 +160,7 @@ def self.clone(repository_url, to_path: '') # or git was not found on the path. # def self.binary_version - command = %w[version] - options = { out: StringIO.new, err: StringIO.new } - version_string = RubyGit::CommandLine.run(*command, **options).stdout[/\d+\.\d+(\.\d+)+/] + version_string = RubyGit::CommandLine.run('version').stdout[/\d+\.\d+(\.\d+)+/] version_string.split('.').collect(&:to_i) end end diff --git a/lib/ruby_git/command_line/options.rb b/lib/ruby_git/command_line/options.rb index d24f3b2..0c0b840 100644 --- a/lib/ruby_git/command_line/options.rb +++ b/lib/ruby_git/command_line/options.rb @@ -9,14 +9,12 @@ module CommandLine # # @api public # - class Options < ProcessExecuter::Options::RunOptions + class Options < ProcessExecuter::Options::RunWithCaptureOptions # Alias for brevity OptionDefinition = ProcessExecuter::Options::OptionDefinition private - # :nocov: SimpleCov on JRuby reports the last with the last argument line is not covered - # The options allowed for objects of this class # @return [Array] # @api private @@ -24,25 +22,24 @@ def define_options [ *super, OptionDefinition.new(:normalize_encoding, default: false, validator: method(:validate_normalize_encoding)), - OptionDefinition.new(:chomp, default: false, validator: method(:validate_chomp)), - OptionDefinition.new(:raise_git_errors, default: true, validator: method(:validate_raise_git_errors)) + OptionDefinition.new(:chomp, default: false, validator: method(:validate_chomp)) ].freeze end - # :nocov: - # Validate the raise_git_errors option value - # @return [String, nil] the error message if the value is not valid + # Wrap ProcessExecuter::ArgumentError in a RubyGit::ArgumentError + # @return [void] + # @raise [RubyGit::ArgumentError] if the options are invalid # @api private - def validate_raise_git_errors - return if [true, false].include?(raise_git_errors) - - errors << "raise_git_errors must be true or false but was #{raise_git_errors.inspect}" + def validate_options + super + rescue ProcessExecuter::ArgumentError => e + raise RubyGit::ArgumentError, e.message, cause: e end # Validate the normalize_encoding option value # @return [String, nil] the error message if the value is not valid # @api private - def validate_normalize_encoding + def validate_normalize_encoding(_key, _value) return if [true, false].include?(normalize_encoding) errors << "normalize_encoding must be true or false but was #{normalize_encoding.inspect}" @@ -51,7 +48,7 @@ def validate_normalize_encoding # Validate the chomp option value # @return [String, nil] the error message if the value is not valid # @api private - def validate_chomp + def validate_chomp(_key, _value) return if [true, false].include?(chomp) errors << "chomp must be true or false but was #{chomp.inspect}" diff --git a/lib/ruby_git/command_line/result.rb b/lib/ruby_git/command_line/result.rb index ad8263e..f10a27c 100644 --- a/lib/ruby_git/command_line/result.rb +++ b/lib/ruby_git/command_line/result.rb @@ -7,27 +7,30 @@ module RubyGit module CommandLine # The result of running a git command # - # Adds stdout and stderr processing to the `ProcessExecuter::Result` class. + # Adds stdout and stderr processing to the `ProcessExecuter::ResultWithCapture` class. # # @api public # class Result < SimpleDelegator # @!method initialize(result) # Initialize a new result object + # # @example - # result = ProcessExecuter.run('echo hello') + # result = Git::CommandLine.run_with_capture('git', 'status') # RubyGit::CommandLine::Result.new(result) - # @param [ProcessExecuter::Result] result The result of running the command + # + # @param [ProcessExecuter::ResultWithCapture] result The result of running the command + # # @return [RubyGit::CommandLine::Result] + # # @api public # Return the processed stdout output (or original if it was not processed) # - # This output is only returned if a stdout redirection is a - # `ProcessExecuter::MonitoredPipe`. - # # @example - # result = ProcessExecuter.run('echo hello': out: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello') + # ) # result.stdout #=> "hello\n" # # @return [String, nil] @@ -39,20 +42,25 @@ def stdout # Process the captured stdout output # # @example - # result = ProcessExecuter.run('echo hello', out: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello') + # ) # result.stdout #=> "hello\n" # result.process_stdout { |stdout, _result| stdout.upcase } # result.stdout #=> "HELLO\n" # result.unprocessed_stdout #=> "hello\n" # # @example Chain processing - # result = ProcessExecuter.run('echo hello', out: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello') + # ) # result.stdout #=> "hello\n" + # # Here is the chain processing: # result.process_stdout { |s| s.upcase }.process_stdout { |s| s.reverse } # result.stdout #=> "OLLEH\n" # result.unprocessed_stdout #=> "hello\n" # - # @return [String, nil] + # @return [self] # # @yield [stdout, result] Yields the stdout output and the result object # @yieldparam stdout [String] The output to process @@ -62,16 +70,19 @@ def stdout # @api public # def process_stdout(&block) - return if block.nil? + return self if block.nil? @processed_stdout = block.call(stdout, self) + self end # Returns the original stdout output before it was processed # # @example - # result = ProcessExecuter.run('echo hello', out: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello') + # ) # result.stdout #=> "hello\n" # result.unprocessed_stdout #=> "hello\n" # result.process_stdout { |s| s.upcase } @@ -92,7 +103,9 @@ def unprocessed_stdout # `ProcessExecuter::MonitoredPipe`. # # @example - # result = ProcessExecuter.run('echo hello 1>&2': err: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello >&2') + # ) # result.stderr #=> "hello\n" # # @return [String, nil] @@ -104,20 +117,25 @@ def stderr # Process the captured stderr output # # @example - # result = ProcessExecuter.run('echo hello 1>&2', err: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello >&2') + # ) # result.stderr #=> "hello\n" # result.process_stderr { |stderr, _result| stderr.upcase } # result.stderr #=> "HELLO\n" # result.unprocessed_stderr #=> "hello\n" # # @example Chain processing - # result = ProcessExecuter.run('echo hello 1>&2', err: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello >&2') + # ) # result.stderr #=> "hello\n" + # # Here is the chain processing: # result.process_stderr { |s| s.upcase }.process_stderr { |s| s.reverse } # result.stderr #=> "OLLEH\n" # result.unprocessed_stderr #=> "hello\n" # - # @return [String, nil] + # @return [self] # # @yield [stderr, result] Yields the stderr output and the result object # @yieldparam stderr [String] The output to process @@ -127,16 +145,19 @@ def stderr # @api public # def process_stderr(&block) - return if block.nil? + return self if block.nil? @processed_stderr = block.call(stderr, self) + self end # Returns the original stderr output before it was processed # # @example - # result = ProcessExecuter.run('echo hello 1>&2', err: StringIO.new) + # result = RubyGit::CommandLine::Result.new( + # ProcessExecuter.run_with_capture('echo hello >&2') + # ) # result.stderr #=> "hello\n" # result.unprocessed_stderr #=> "hello\n" # result.process_stderr { |stderr| stderr.upcase } diff --git a/lib/ruby_git/command_line/runner.rb b/lib/ruby_git/command_line/runner.rb index ef426f5..039ee6d 100644 --- a/lib/ruby_git/command_line/runner.rb +++ b/lib/ruby_git/command_line/runner.rb @@ -154,7 +154,7 @@ def initialize(env, binary_path, global_options, logger) # # @return [RubyGit::CommandLine::Result] the result of the command # - # @raise [ArgumentError] if `args` or `options_hash` are not valid + # @raise [RubyGit::ArgumentError] if `args` or `options_hash` are not valid # # @raise [RubyGit::FailedError] if the command returned a non-zero exitstatus # @@ -165,18 +165,27 @@ def initialize(env, binary_path, global_options, logger) # @raise [RubyGit::ProcessIOError] if an exception was raised while collecting subprocess output # def call(*args, **options_hash) - options_hash[:raise_errors] = false options = RubyGit::CommandLine::Options.new(logger: logger, **options_hash) - begin - result = run_with_chdir([env, *build_git_cmd(args)], options) + result = run(*args, options) + process_result(result, options) + end + + private + + # Run the git command with the given arguments and options + # @return [ProcessExecuter::ResultWithCapture] the result of the command + # @api private + def run(*args, options) + # We don't want ProcessExecuter to raise an error if the command fails, but + # we want to preserve the value of the raise_errors option to use in + # process_result. + options.merge(raise_errors: false).then do |options| + run_with_chdir([env, *build_git_cmd(args)], options) rescue ProcessExecuter::ProcessIOError => e raise RubyGit::ProcessIOError.new(e.message), cause: e.exception.cause end - process_result(result) end - private - # Run command with options with special handling for the `chdir` option on JRuby # # JRuby does not support the `chdir` option in `Process.spawn`. Note that this @@ -185,7 +194,7 @@ def call(*args, **options_hash) # @param args [Array] the command to run # @param options [RubyGit::CommandLine::Options] the options to pass to `Process.spawn` # - # @return [ProcessExecuter::Result] the result of the command + # @return [ProcessExecuter::ResultWithCapture] the result of the command # # @api private # @@ -197,7 +206,9 @@ def run_with_chdir(args, options) # rubocop:disable Metrics/MethodLength Dir.chdir(options.chdir) do saved_chdir = options.chdir options.merge!(chdir: :not_set) - run_and_handle_spawn_error(args, options).tap do + begin + run_and_handle_spawn_error(args, options) + ensure options.merge!(chdir: saved_chdir) end end @@ -212,14 +223,14 @@ def run_with_chdir(args, options) # rubocop:disable Metrics/MethodLength # @param args [Array] the command to run # @param options [RubyGit::CommandLine::Options] the options to pass to `Process.spawn` # - # @return [ProcessExecuter::Result] the result of the command + # @return [ProcessExecuter::ResultWithCapture] the result of the command # # @api private # def run_and_handle_spawn_error(args, options) - ProcessExecuter.run_with_options(args, options) + ProcessExecuter.run_with_capture(*args, options) rescue ProcessExecuter::SpawnError => e - raise RubyGit::SpawnError, e.message + raise RubyGit::SpawnError, e.message, cause: e end # Returns true if running on JRuby @@ -230,11 +241,15 @@ def run_and_handle_spawn_error(args, options) def jruby? = RUBY_ENGINE == 'jruby' # Build the git command line from the available sources to send to `Process.spawn` + # + # @raise [RubyGit::ArgumentError] if the args array contains an array + # # @return [Array] + # # @api private # def build_git_cmd(args) - raise ArgumentError, 'The args array can not contain an array' if args.any? { |a| a.is_a?(Array) } + raise RubyGit::ArgumentError, 'The args array can not contain an array' if args.any? { |a| a.is_a?(Array) } [binary_path, *global_options, *args].map(&:to_s) end @@ -250,18 +265,21 @@ def build_git_cmd(args) # @return [RubyGit::CommandLineResult] the result of the command to return to the caller # # @raise [RubyGit::FailedError] if the command failed + # # @raise [RubyGit::SignaledError] if the command was signaled + # # @raise [RubyGit::TimeoutError] if the command times out + # # @raise [RubyGit::ProcessIOError] if an exception was raised while collecting subprocess output # # @api private # - def process_result(result) + def process_result(result, options) RubyGit::CommandLine::Result.new(result).tap do |processed_result| - raise_any_errors(processed_result) if processed_result.options.raise_git_errors + raise_any_errors(processed_result) if options.raise_errors - processed_result.process_stdout { |s, r| process_output(s, r) } - processed_result.process_stderr { |s, r| process_output(s, r) } + processed_result.process_stdout { |output, result| process_output(output, result) } + processed_result.process_stderr { |output, result| process_output(output, result) } end end diff --git a/lib/ruby_git/errors.rb b/lib/ruby_git/errors.rb index 574b95c..8ef5eb7 100644 --- a/lib/ruby_git/errors.rb +++ b/lib/ruby_git/errors.rb @@ -16,6 +16,7 @@ module RubyGit # ```text # StandardError # └─> RubyGit::Error + # ├─> RubyGit::ArgumentError # ├─> RubyGit::CommandLineError # │ ├─> RubyGit::FailedError # │ └─> RubyGit::SignaledError @@ -28,6 +29,7 @@ module RubyGit # | Error Class | Description | # | --- | --- | # | `Error` | This catch-all error serves as the base class for other custom errors raised by the git gem. | + # | `ArgumentError` | Raised when an invalid argument is passed to a method. | # | `CommandLineError` | A subclass of this error is raised when there is a problem executing the git command line. | # | `FailedError` | This error is raised when the git command line exits with a non-zero status code that is not expected by the git gem. | # | `SignaledError` | This error is raised when the git command line is terminated as a result of receiving a signal. This could happen if the process is forcibly terminated or if there is a serious system error. | @@ -66,6 +68,19 @@ class Error < StandardError; end # rubocop:enable Layout/LineLength + # Raised when an invalid argument is passed to a method + # + # @example Raising RubyGit::ArgumentError due to invalid option value + # begin + # RubyGit::CommandLine.run('status', timeout_after: 'not_a_number') + # rescue RubyGit::ArgumentError => e + # e.message #=> 'timeout_after must be nil or a non-negative real number but was "not_a_number"' + # end + # + # @api public + # + class ArgumentError < RubyGit::Error; end + # Raised when a git command fails or exits because of an uncaught signal # # The git command executed, status, stdout, and stderr are available from this diff --git a/ruby_git.gemspec b/ruby_git.gemspec index 1907b98..2e0b487 100644 --- a/ruby_git.gemspec +++ b/ruby_git.gemspec @@ -56,7 +56,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'yardstick', '~> 0.9' end - spec.add_dependency 'process_executer', '~> 3.2' + spec.add_dependency 'process_executer', '~> 4.0' spec.add_dependency 'rchardet', '~> 1.9' spec.metadata['rubygems_mfa_required'] = 'true' diff --git a/spec/lib/ruby_git/command_line/options_spec.rb b/spec/lib/ruby_git/command_line/options_spec.rb index 70a9916..2c1b58b 100644 --- a/spec/lib/ruby_git/command_line/options_spec.rb +++ b/spec/lib/ruby_git/command_line/options_spec.rb @@ -12,22 +12,14 @@ subject { options } it 'should have attributes with default values' do - expect(subject).to have_attributes(raise_git_errors: true, normalize_encoding: false, chomp: false) + expect(subject).to have_attributes(normalize_encoding: false, chomp: false) end context 'when options are passed' do - let(:options_hash) { { raise_git_errors: false, normalize_encoding: true, chomp: true } } + let(:options_hash) { { normalize_encoding: true, chomp: true } } it 'should have the passed attributes' do - expect(subject).to have_attributes(raise_git_errors: false, normalize_encoding: true, chomp: true) - end - end - - context 'when raise_git_errors is not valid' do - let(:options_hash) { { raise_git_errors: 'not a boolean' } } - - it 'should raise an error' do - expect { subject }.to raise_error(ArgumentError, /raise_git_errors/) + expect(subject).to have_attributes(normalize_encoding: true, chomp: true) end end @@ -35,7 +27,7 @@ let(:options_hash) { { normalize_encoding: 'not a boolean' } } it 'should raise an error' do - expect { subject }.to raise_error(ArgumentError, /normalize_encoding/) + expect { subject }.to raise_error(RubyGit::ArgumentError, /normalize_encoding/) end end @@ -43,7 +35,7 @@ let(:options_hash) { { chomp: 'not a boolean' } } it 'should raise an error' do - expect { subject }.to raise_error(ArgumentError, /chomp/) + expect { subject }.to raise_error(RubyGit::ArgumentError, /chomp/) end end end diff --git a/spec/lib/ruby_git/command_line/result_spec.rb b/spec/lib/ruby_git/command_line/result_spec.rb index fd2cfba..6bd2f63 100644 --- a/spec/lib/ruby_git/command_line/result_spec.rb +++ b/spec/lib/ruby_git/command_line/result_spec.rb @@ -2,7 +2,7 @@ RSpec.describe RubyGit::CommandLine::Result do let(:result) { described_class.new(process_executer_result) } - let(:process_executer_result) { ProcessExecuter.run(*command, **options) } + let(:process_executer_result) { ProcessExecuter.run_with_capture(*command, **options) } let(:command) { ruby_command <<~RUBY } STDOUT.puts 'stdout message' STDERR.puts 'stderr message' @@ -60,7 +60,7 @@ context 'when #process_stdout HAS been called' do it 'should return the original stdout' do - result.process_stdout { |s, _r| s.upcase! } + result.process_stdout { |s, _r| s.upcase } expect(result.unprocessed_stdout).to eq("stdout message#{eol}") end end @@ -119,7 +119,7 @@ context 'when #process_stderr HAS been called' do it 'should return the original stderr' do - result.process_stderr { |s, _r| s.upcase! } + result.process_stderr { |s, _r| s.upcase } expect(result.unprocessed_stderr.with_linux_eol).to eq("stderr message\n") end end diff --git a/spec/lib/ruby_git/command_line/runner_spec.rb b/spec/lib/ruby_git/command_line/runner_spec.rb index db1d176..2f7ecce 100644 --- a/spec/lib/ruby_git/command_line/runner_spec.rb +++ b/spec/lib/ruby_git/command_line/runner_spec.rb @@ -45,18 +45,18 @@ logger = Logger.new(nil) expected_command = [env, binary_path, *global_options, *args] - expected_options = { raise_git_errors: false, logger: Logger } + expected_options = { raise_errors: false, logger: Logger } runner = described_class.new(env, binary_path, global_options, logger) expect(ProcessExecuter).to( - receive(:run_with_options).with( - expected_command, an_object_having_attributes(**expected_options) + receive(:run_with_capture).with( + *expected_command, an_object_having_attributes(**expected_options) ).and_call_original ) runner.call( - *args, chomp: true, raise_git_errors: false, out: StringIO.new, err: StringIO.new, logger: + *args, chomp: true, raise_errors: false, out: StringIO.new, err: StringIO.new, logger: ) end @@ -64,8 +64,8 @@ let(:env) { {} } let(:binary_path) { 'ruby' } let(:global_options) { ['bin/command-line-test'] } - let(:options) { { raise_git_errors:, timeout_after:, chomp: } } - let(:raise_git_errors) { true } + let(:options) { { raise_errors:, timeout_after:, chomp: } } + let(:raise_errors) { true } let(:timeout_after) { nil } let(:chomp) { false } @@ -84,14 +84,14 @@ } end - context 'when raise_git_errors is true' do - let(:raise_git_errors) { true } + context 'when raise_errors is true' do + let(:raise_errors) { true } it { is_expected.to have_attributes(expected_result_attributes) } end - context 'when raise_git_errors is false' do - let(:raise_git_errors) { false } + context 'when raise_errors is false' do + let(:raise_errors) { false } it { is_expected.to have_attributes(expected_result_attributes) } end @@ -110,8 +110,8 @@ } end - context 'when raise_git_errors is true' do - let(:raise_git_errors) { true } + context 'when raise_errors is true' do + let(:raise_errors) { true } it 'should raise a RubyGit::FailedError' do expect { subject }.to raise_error(RubyGit::FailedError) do |e| @@ -120,8 +120,8 @@ end end - context 'when raise_git_errors is false' do - let(:raise_git_errors) { false } + context 'when raise_errors is false' do + let(:raise_errors) { false } it 'should return a result indicating the failure' do expect(subject).to have_attributes(expected_result_attributes) @@ -142,8 +142,8 @@ } end - context 'when raise_git_errors is true' do - let(:raise_git_errors) { true } + context 'when raise_errors is true' do + let(:raise_errors) { true } it 'should raise a RubyGit::SignaledError' do expect { subject }.to raise_error(RubyGit::SignaledError) do |e| @@ -152,8 +152,8 @@ end end - context 'when raise_git_errors is false' do - let(:raise_git_errors) { false } + context 'when raise_errors is false' do + let(:raise_errors) { false } it 'should return a result indicating the signal' do expect(subject).to have_attributes(expected_result_attributes) @@ -183,8 +183,8 @@ end # :nocov: - context 'when raise_git_errors is true' do - let(:raise_git_errors) { true } + context 'when raise_errors is true' do + let(:raise_errors) { true } let(:timeout_after) { 0.05 } it 'should raise a RubyGit::SignaledError' do @@ -194,8 +194,8 @@ end end - context 'when raise_git_errors is false' do - let(:raise_git_errors) { false } + context 'when raise_errors is false' do + let(:raise_errors) { false } let(:timeout_after) { 0.05 } it 'should return a result indicating the signal' do @@ -234,7 +234,7 @@ end describe 'encoding normalization of output' do - let(:options) { { out: StringIO.new, err: StringIO.new, normalize_encoding: } } + let(:options) { { normalize_encoding: } } let(:expected_non_normalized_output) do String.new( @@ -243,7 +243,7 @@ "\xCD\xEF \xE8\xF1\xE2\xE1\xED\xE9\xF4\xE1\xF3\n" \ "\xD6\xE5\xE8\xE3\xE9\xE1\xF4 \xE8\xF1\xE2\xE1\xED\xE9\xF4\xE1\xF3 " \ "\xF1\xE5\xF0\xF1\xE9\xEC\xE9q\xE8\xE5\n" - ).force_encoding('ASCII-8BIT') + ).force_encoding(Encoding.default_external) end let(:expected_normalized_output) { <<~OUTPUT } @@ -288,18 +288,18 @@ def write(*_args) end.new end - let(:options) { { raise_git_errors:, out: } } + let(:options) { { raise_errors:, out: } } - context 'when :raise_git_errors is true' do - let(:raise_git_errors) { true } + context 'when :raise_errors is true' do + let(:raise_errors) { true } it 'should raise a RubyGit::ProcessIOError' do expect { subject }.to raise_error(RubyGit::ProcessIOError) end end - context 'when :raise_git_errors is false' do - let(:raise_git_errors) { false } + context 'when :raise_errors is false' do + let(:raise_errors) { false } it 'should raise a RubyGit::ProcessIOError (even if told not to raise errors)' do expect { subject }.to raise_error(RubyGit::ProcessIOError) diff --git a/spec/lib/ruby_git/worktree_add_spec.rb b/spec/lib/ruby_git/worktree_add_spec.rb index 742ccb9..f92ce3f 100644 --- a/spec/lib/ruby_git/worktree_add_spec.rb +++ b/spec/lib/ruby_git/worktree_add_spec.rb @@ -79,7 +79,8 @@ def untracked_entries let(:pathspecs) { [] } let(:options) { { all: 'invalid' } } - it_behaves_like 'it raises an ArgumentError', %(The 'all:' option must be a Boolean value but was "invalid") + it_behaves_like 'it raises a RubyGit::ArgumentError', + %(The 'all:' option must be a Boolean value but was "invalid") end end @@ -103,7 +104,7 @@ def untracked_entries let(:options) { { force: 'invalid' } } it_behaves_like( - 'it raises an ArgumentError', + 'it raises a RubyGit::ArgumentError', %(The 'force:' option must be a Boolean value but was "invalid") ) end @@ -129,7 +130,7 @@ def untracked_entries let(:options) { { update: 'invalid' } } it_behaves_like( - 'it raises an ArgumentError', + 'it raises a RubyGit::ArgumentError', %(The 'update:' option must be a Boolean value but was "invalid") ) end @@ -155,7 +156,7 @@ def untracked_entries let(:options) { { refresh: 'invalid' } } it_behaves_like( - 'it raises an ArgumentError', + 'it raises a RubyGit::ArgumentError', %(The 'refresh:' option must be a Boolean value but was "invalid") ) end diff --git a/spec/lib/ruby_git/worktree_init_spec.rb b/spec/lib/ruby_git/worktree_init_spec.rb index 3d8b238..7138f19 100644 --- a/spec/lib/ruby_git/worktree_init_spec.rb +++ b/spec/lib/ruby_git/worktree_init_spec.rb @@ -112,11 +112,10 @@ context 'when not a string' do let(:initial_branch) { 123 } - it 'should raise an ArgumentError' do - expect { subject }.to( - raise_error(ArgumentError, %(The 'initial_branch:' option must be a String or nil but was 123)) - ) - end + it_behaves_like( + 'it raises a RubyGit::ArgumentError', + %(The 'initial_branch:' option must be a String or nil but was 123) + ) end end end diff --git a/spec/lib/ruby_git/worktree_open_spec.rb b/spec/lib/ruby_git/worktree_open_spec.rb index 7162726..a4190ad 100644 --- a/spec/lib/ruby_git/worktree_open_spec.rb +++ b/spec/lib/ruby_git/worktree_open_spec.rb @@ -12,7 +12,7 @@ let(:worktree_path) { tmpdir } before { FileUtils.rmdir(worktree_path) } it 'should raise ArgumentError' do - expect { subject }.to raise_error(ArgumentError) + expect { subject }.to raise_error(RubyGit::ArgumentError) end end @@ -23,14 +23,14 @@ FileUtils.touch(worktree_path) end it 'should raise ArgumentError' do - expect { subject }.to raise_error(ArgumentError) + expect { subject }.to raise_error(RubyGit::ArgumentError) end end context 'when worktree_path exists but is not a git working tree' do let(:worktree_path) { tmpdir } it 'should raise ArgumentError' do - expect { subject }.to raise_error(ArgumentError, /not a git repository/) + expect { subject }.to raise_error(RubyGit::ArgumentError, /not a git repository/) end end diff --git a/spec/lib/ruby_git_spec.rb b/spec/lib/ruby_git_spec.rb index a805c09..5da82e9 100644 --- a/spec/lib/ruby_git_spec.rb +++ b/spec/lib/ruby_git_spec.rb @@ -79,7 +79,7 @@ let(:git_version_string) { 'git version 10.11.12' } let(:result) { double(RubyGit::CommandLine::Result, stdout: git_version_string) } it 'should return [10, 11, 12]' do - expect(RubyGit::CommandLine).to receive(:run).with('version', Hash).and_return(result) + expect(RubyGit::CommandLine).to receive(:run).with('version').and_return(result) expect(subject).to eq([10, 11, 12]) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c75f847..55d48a9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -53,7 +53,7 @@ def ci_build? = ENV.fetch('GITHUB_ACTIONS', 'false') == 'true' def run(*command) command = command[0] if command.size == 1 && command[0].is_a?(Array) - ProcessExecuter.run(*command, out: StringIO.new, err: StringIO.new) + ProcessExecuter.run_with_capture(*command) end def status_output @@ -195,13 +195,13 @@ def eol = windows? ? "\r\n" : "\n" end end -RSpec.shared_examples 'it raises an ArgumentError' do |message| - it 'should raise an Argument' do +RSpec.shared_examples 'it raises a RubyGit::ArgumentError' do |message| + it 'should raise an RubyGit::ArgumentError' do allow_any_instance_of(described_class).to( receive(:normalize_path) { |_, path| path } ) - expect { subject }.to(raise_error(ArgumentError, message)) + expect { subject }.to(raise_error(RubyGit::ArgumentError, message)) end end