Skip to content

Commit

Permalink
Provide default stubs to allow any command/upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrer committed Nov 29, 2023
1 parent a2a1bca commit c6c22ff
Showing 1 changed file with 74 additions and 58 deletions.
132 changes: 74 additions & 58 deletions spec/support/ssh_test_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def stub_upload(destination)
commander.register_upload_stub(stub)
stub
end

def allow_any_command!
commander.allow_any_command!
end

def allow_any_upload!
commander.allow_any_upload!
end
end

module StubHelpers
Expand All @@ -29,6 +37,14 @@ def stub_command(command)
def stub_upload(destination)
SSHTestKit.stub_upload(destination)
end

def allow_any_command!
SSHTestKit.allow_any_command!
end

def allow_any_upload!
SSHTestKit.allow_any_upload!
end
end

module RSpecMatchers
Expand Down Expand Up @@ -90,35 +106,87 @@ class Commander
attr_reader :uploads, :commands

def initialize
@command_stubs = CommandStubRegistry.new
@upload_stubs = UploadStubRegistry.new
@command_stubs = []
@upload_stubs = []
@uploads = []
@commands = []
end

def register_command_stub(stub)
@command_stubs.register(stub)
@command_stubs << stub
end

def register_upload_stub(stub)
@upload_stubs.register(stub)
@upload_stubs << stub
end

def allow_any_command!
@allow_any_command = true
default_command_stub
end

def allow_any_upload!
@allow_any_upload = true
default_upload_stub
end

def default_command_stub
@default_command_stub ||= CommandStub.new(nil)
end

def default_upload_stub
@default_upload_stub ||= UploadStub.new(nil)
end

def execute_command(host, command)
commands << ExecutedCommand.new(host, command)

stub = @command_stubs.find_applicable(command, host)
stub = find_applicable_command_stub(command, host)
stub.track_invokation!(host, command)
stub
end

def upload!(host, file, destination)
uploads << Upload.new(host, file, destination)

stub = @upload_stubs.find_applicable(destination, host)
stub = find_applicable_upload_stub(destination, host)
stub.track_invokation!(host, file, destination)
stub
end

private

def find_applicable_command_stub(command, host)
stub = @command_stubs.find do |stub|
stub.applicable?(host, command)
end

return stub unless stub.nil?

if @allow_any_command
default_command_stub
else
raise("No stub found for #{command.to_command} on #{host}.\n\nRegistered stubs:\n#{formated_registered_command_stubs_list}\n ")
end
end

def formated_registered_command_stubs_list
@command_stubs.all(&:to_s).join("\n")
end

def find_applicable_upload_stub(destination, host)
stub = @upload_stubs.find do |stub|
stub.applicable?(host, destination)
end

return stub unless stub.nil?

if @allow_any_upload
default_upload_stub
else
raise("No stub found for upload to #{destination} on #{host}.\n\nRegistered stubs:\n#{registered_stubs_list}\n ")
end
end
end

class CommandStub
Expand Down Expand Up @@ -229,34 +297,6 @@ def command_with_host_string
end
end

class CommandStubRegistry
def initialize
@stubs = []
end

def register(stub)
@stubs << stub
end

def find_applicable(command, host)
stub = @stubs.find do |stub|
stub.applicable?(host, command)
end

stub || raise("No stub found for #{command.to_command} on #{host}.\n\nRegistered stubs:\n#{registered_stubs_list}\n ")
end

private

def registered_stubs_list
@stubs.map(&:to_s).join("\n")
end

def matches_command?(stub, command)
CommandHelpers.matching?(command, stub.command)
end
end

class ExecutedCommand
def initialize(host, command)
@host = host
Expand All @@ -282,30 +322,6 @@ def matches_command?(command)
end
end

class UploadStubRegistry
def initialize
@stubs = []
end

def register(stub)
@stubs << stub
end

def find_applicable(destination, host)
stub = @stubs.find do |stub|
stub.applicable?(host, destination)
end

stub || raise("No stub found for upload to #{destination} on #{host}.\n\nRegistered stubs:\n#{registered_stubs_list}\n ")
end

private

def registered_stubs_list
@stubs.map(&:to_s).join("\n")
end
end

class UploadStub
attr_reader :host, :invocations, :destination

Expand Down

0 comments on commit c6c22ff

Please sign in to comment.