Class: Train::Transports::Mock::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/train/transports/mock.rb,
lib/train/transports/mock.rb,
lib/train/transports/mock.rb,
lib/train/transports/mock.rb

Defined Under Namespace

Classes: Command, File, OS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = nil) ⇒ Connection

Returns a new instance of Connection.



66
67
68
69
70
# File 'lib/train/transports/mock.rb', line 66

def initialize(conf = nil)
  super(conf)
  @os = OS.new(self, family: 'unknown')
  @commands = {}
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



63
64
65
# File 'lib/train/transports/mock.rb', line 63

def commands
  @commands
end

#filesObject

Returns the value of attribute files.



63
64
65
# File 'lib/train/transports/mock.rb', line 63

def files
  @files
end

#osObject (readonly)

Returns the value of attribute os.



64
65
66
# File 'lib/train/transports/mock.rb', line 64

def os
  @os
end

Instance Method Details

#command_not_found(cmd) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/train/transports/mock.rb', line 84

def command_not_found(cmd)
  if @options[:verbose]
    STDERR.puts('Command not mocked:')
    STDERR.puts('    '+cmd.to_s.split("\n").join("\n    "))
    STDERR.puts('    SHA: ' + Digest::SHA256.hexdigest(cmd.to_s))
  end
  mock_command(cmd)
end

#file(path) ⇒ Object



104
105
106
# File 'lib/train/transports/mock.rb', line 104

def file(path)
  @files[path] ||= file_not_found(path)
end

#file_not_found(path) ⇒ Object



99
100
101
102
# File 'lib/train/transports/mock.rb', line 99

def file_not_found(path)
  STDERR.puts('File not mocked: '+path.to_s) if @options[:verbose]
  File.new(self, path)
end

#mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0) ⇒ Object



80
81
82
# File 'lib/train/transports/mock.rb', line 80

def mock_command(cmd, stdout = nil, stderr = nil, exit_status = 0)
  @commands[cmd] = Command.new(stdout || '', stderr || '', exit_status)
end

#mock_os(value) ⇒ Object



76
77
78
# File 'lib/train/transports/mock.rb', line 76

def mock_os(value)
  @os = OS.new(self, value)
end

#run_command(cmd) ⇒ Object



93
94
95
96
97
# File 'lib/train/transports/mock.rb', line 93

def run_command(cmd)
  @commands[cmd] ||
    @commands[Digest::SHA256.hexdigest cmd.to_s] ||
    command_not_found(cmd)
end

#to_sObject



108
109
110
# File 'lib/train/transports/mock.rb', line 108

def to_s
  'Mock Connection'
end

#uriObject



72
73
74
# File 'lib/train/transports/mock.rb', line 72

def uri
  'mock://'
end