lib/cli/kit/support/test_helper.rb in cli-kit-4.0.0 vs lib/cli/kit/support/test_helper.rb in cli-kit-5.0.0

- old
+ new

@@ -1,5 +1,7 @@ +require 'cli/kit' + module CLI module Kit module Support module TestHelper def setup @@ -8,11 +10,13 @@ end def assert_all_commands_run(should_raise: true) errors = CLI::Kit::System.error_message CLI::Kit::System.reset! - assert(false, errors) if should_raise && !errors.nil? + # this is in minitest, but sorbet doesn't know that. probably we + # could structure this better. + T.unsafe(self).assert(false, errors) if should_raise && !errors.nil? errors end def teardown super @@ -50,70 +54,74 @@ module ::CLI module Kit module System class << self alias_method :original_system, :system - def system(*a, sudo: false, env: {}, **kwargs) - expected_command = expected_command(*a, sudo: sudo, env: env) + def system(cmd, *a, sudo: false, env: {}, stdin: nil, **kwargs) + a.unshift(cmd) + expected_command = expected_command(a, sudo: sudo, env: env) # In the case of an unexpected command, expected_command will be nil return FakeSuccess.new(false) if expected_command.nil? # Otherwise handle the command if expected_command[:allow] - original_system(*a, sudo: sudo, env: env, **kwargs) + T.unsafe(self).original_system(*a, sudo: sudo, env: env, **kwargs) else FakeSuccess.new(expected_command[:success]) end end alias_method :original_capture2, :capture2 - def capture2(*a, sudo: false, env: {}, **kwargs) - expected_command = expected_command(*a, sudo: sudo, env: env) + def capture2(cmd, *a, sudo: false, env: {}, **kwargs) + a.unshift(cmd) + expected_command = expected_command(a, sudo: sudo, env: env) # In the case of an unexpected command, expected_command will be nil return [nil, FakeSuccess.new(false)] if expected_command.nil? # Otherwise handle the command if expected_command[:allow] - original_capture2(*a, sudo: sudo, env: env, **kwargs) + T.unsafe(self).original_capture2(*a, sudo: sudo, env: env, **kwargs) else [ expected_command[:stdout], FakeSuccess.new(expected_command[:success]), ] end end alias_method :original_capture2e, :capture2e - def capture2e(*a, sudo: false, env: {}, **kwargs) - expected_command = expected_command(*a, sudo: sudo, env: env) + def capture2e(cmd, *a, sudo: false, env: {}, **kwargs) + a.unshift(cmd) + expected_command = expected_command(a, sudo: sudo, env: env) # In the case of an unexpected command, expected_command will be nil return [nil, FakeSuccess.new(false)] if expected_command.nil? # Otherwise handle the command if expected_command[:allow] - original_capture2ecapture2e(*a, sudo: sudo, env: env, **kwargs) + T.unsafe(self).original_capture2e(*a, sudo: sudo, env: env, **kwargs) else [ expected_command[:stdout], FakeSuccess.new(expected_command[:success]), ] end end alias_method :original_capture3, :capture3 - def capture3(*a, sudo: false, env: {}, **kwargs) - expected_command = expected_command(*a, sudo: sudo, env: env) + def capture3(cmd, *a, sudo: false, env: {}, **kwargs) + a.unshift(cmd) + expected_command = expected_command(a, sudo: sudo, env: env) # In the case of an unexpected command, expected_command will be nil return [nil, nil, FakeSuccess.new(false)] if expected_command.nil? # Otherwise handle the command if expected_command[:allow] - original_capture3(*a, sudo: sudo, env: env, **kwargs) + T.unsafe(self).original_capture3(*a, sudo: sudo, env: env, **kwargs) else [ expected_command[:stdout], expected_command[:stderr], FakeSuccess.new(expected_command[:success]), @@ -214,15 +222,16 @@ #{errors[:other].map { |cmd, msg| "{{command:#{cmd}}}\n#{msg}" }.join("\n\n")} EOF end return nil if final_error.empty? + "\n" + final_error.join("\n") # Initial new line for formatting reasons end private - def expected_command(*a, sudo: raise, env: raise) + def expected_command(a, sudo: raise, env: raise) expected_cmd = @delegate_open3[a.join(' ')] if expected_cmd.nil? @delegate_open3[a.join(' ')] = { unexpected: true } return nil